SeetaFace使用(问题)

本文对比了libfacedetection和seetaface人脸识别库在人脸检测速度上的表现,并详细介绍了seetaface在Visual Studio 2017和opencv 4.0环境下的人脸对齐实现代码。

    由于毕业设计功能需要对人脸进行特征点定位,这两天查看了许多开源的人脸识别库,特地尝试了一下的有ShiqiYu老师所发布的libfacedetection和山世光老师团队所发布的seetaface人脸识别库。其中ShiqiYu老师的libfacedetection这两天才正式开源,所以颇有兴趣得去使用了,在Debug模式下成功运行了该算法,和seetaface比较来看,速度占有比较明显的优势,但是Release版本下运行时始终有链接报错,还需要好好得找一下问题,且提供的测试代码为人脸检测部分,而seetaface则是明确的有人脸检测、对齐、识别三大模块,所以目前还是偏向于使用seetaface。当然libfacedetection也很棒,所以依旧贴上github地址:https://github.com/ShiqiYu/libfacedetection

    在对seetaface的初次尝试使用进行介绍前,首先感谢一下开发者的开源分享精神,完完全全是造福了我们这些小菜鸟啊。在这之前,我几乎没有使用过动态链接库的形式,所以在尝试的过程中确实还是碰到了不少问题,好在seetaface的使用资料众多,最终花了2个晚上还是顺利得运行了起来。首先贴github地址:https://github.com/seetaface/SeetaFaceEngine,由于具体的使用方法是在是有太多的Blog介绍了,所以在这里我只贴上我Visual Studio2017 + opencv4.0的情况下测试该算法的测试代码:

#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>

#include <opencv.hpp>
#include <opencv2\opencv.hpp>

#include "face_detection.h"
#include "face_alignment.h"
using namespace std;
using namespace cv;

#ifdef _WIN32
//std::string DATA_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceAlignment/data/";
std::string DATA_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceDetection/data/";
std::string MODEL_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceAlignment/model/";
#else
//std::string DATA_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceAlignment/data/";
std::string DATA_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceDetection/data/";
std::string MODEL_DIR = "F:/ThirdPartyLib/SeetaFaceEngine-master/FaceAlignment/model/";
#endif

int main(int argc, char** argv)
{
	clock_t start, finish;
	// Initialize face detection model
	seeta::FaceDetection detector("F:/ThirdPartyLib/SeetaFaceEngine-master/FaceDetection/model/seeta_fd_frontal_v1.0.bin");
	detector.SetMinFaceSize(40);
	detector.SetScoreThresh(2.f);
	detector.SetImagePyramidScaleFactor(0.8f);
	detector.SetWindowStep(4, 4);
	cout << "Face detection model loaded." << endl;

	// Initialize face alignment model 
	seeta::FaceAlignment point_detector((MODEL_DIR + "seeta_fa_v1.1.bin").c_str());
	cout << "Face alignment model loaded." << endl;

	Mat img_color = imread(DATA_DIR + "0_1_1.jpg", IMREAD_UNCHANGED);
	Mat img_gray;
	if (img_color.channels() != 1) {
		cvtColor(img_color, img_gray, COLOR_BGR2GRAY);
	}
	else {
		img_gray = img_color;
	}
	seeta::ImageData image_data;
	image_data.data = img_gray.data;
	image_data.width = img_gray.cols;
	image_data.height = img_gray.rows;
	image_data.num_channels = 1;
	int pts_num = 5;

	cout << "Start to detect faces..." << endl;
	start = clock();
	// Detect faces
	std::vector<seeta::FaceInfo> faces = detector.Detect(image_data);
	int32_t face_num = static_cast<int32_t>(faces.size());

	if (face_num == 0)
	{
		cout << "Error: no face detected." << endl;
		return 0;
	}
	finish = clock();
	cout << "detect time :" << finish - start << "/" << CLOCKS_PER_SEC << "(s)" << endl;

	// Detect 5 facial landmarks
	cout << "Start to align..." << endl;
	seeta::FacialLandmark points[5];
	point_detector.PointDetectLandmarks(image_data, faces[0], points);

	// Visualize the results
	rectangle(img_color, Point(faces[0].bbox.x, faces[0].bbox.y), Point(faces[0].bbox.x + faces[0].bbox.width - 1, faces[0].bbox.y + faces[0].bbox.height - 1), CV_RGB(255, 0, 0));
	for (int i = 0; i < pts_num; i++)
	{
		circle(img_color, Point(points[i].x, points[i].y), 2, CV_RGB(0, 255, 0), FILLED);
		cout << "point[" << i << "] = (" << points[i].x << ", " << points[i].y << ");" << endl;
	}

	imshow("result", img_color);
	waitKey(0);
	imwrite("F:/Iris/facePics/programPic/result.jpg", img_color);
	system("pause");
	return 0;
}

注意使用的时候,需要根据具体情况修改其中的地址变量。

    最后,介绍一下,我碰到的问题,总体来说,对于seetaface的使用过程,意外得非常顺利,除了一开始地址手误,导致图片读取失败而报错以外,几乎没有任何其他报错。但是非常不顺利的事,在进行facedetection的过程中,我发现detection非常之慢,大概花了30s左右的时间,这完全是不可能的啊。就是在这期间,我去尝试了ShiqiYu老师的算法,发现虽然时间小于30s,但是也非常慢,确定了这是个绕不开的问题。不过也正是在尝试libfacedetection使用的过程中,我注意到,说明文档里有提到最好采用Release运行,然后,发现,Release运行seetaface就非常之快了,问题也就莫名其妙解决了。在这过程中,我查了许多的资料,并没有任何博主提出过这个点,所以我个人还是觉得有必要记录下来,希望能帮到和我一样,第一次尝试使用第三方动态链接库的人。

在C++中使用SeetaFace6Open进行人脸识别的学习和开发,可以按照以下步骤进行: ### 准备工作 1. **环境搭建**: - 安装必要的依赖库,如OpenCV。 - 下载并安装SeetaFace6Open库,确保可以从GitHub或其他官方渠道获取最新的源代码。 2. **模型文件准备**: - 获取所需的模型文件,例如`face_detector.csta`等,并确保它们位于程序能够访问的路径中。 ### 示例代码 以下是一个简单的示例,展示如何在C++中使用SeetaFace6Open进行人脸检测: ```cpp #include <seeta/FaceDetector.h> #include <opencv2/opencv.hpp> // 创建人脸检测器 seeta::FaceDetector* new_fd() { seeta::ModelSetting setting; setting.append("face_detector.csta"); return new seeta::FaceDetector(setting); } int main_image() { // 设置模型 seeta::ModelSetting setting; setting.set_device(SEETA_DEVICE_CPU); setting.append("/home/kier/CLionProjects/FaceBox/torch-faceboxes/model.json"); // 创建人脸检测器 seeta::FaceDetector FD(setting); // 用OpenCV读取图片 auto img = cv::imread("hu.ge.jpg"); std::cout << "Got image: [" << img.cols << ", " << img.rows << ", " << img.channels() << "]" << std::endl; // OpenCV数据转换为SeetaImageData SeetaImageData simg; simg.height = img.rows; simg.width = img.cols; simg.channels = img.channels(); simg.data = img.data; // 面部检测 auto faces = FD.detect(simg); std::cout << faces.size << std::endl; // OpenCV绘制框到图像上 for (int i = 0; i < faces.size; ++i) { auto &face = faces.data[i]; auto &pos = face.pos; cv::rectangle(img, cv::Rect(pos.x, pos.y, pos.width, pos.height), CV_RGB(0, 128, 128), 3); } // OpenCV显示 cv::imshow("FaceBoxes", img); auto key = cv::waitKey(); return 0; } ``` ### 学习资源 - **官方文档**:查阅SeetaFace6Open的官方文档,了解API的具体使用方法。 - **社区支持**:参与相关的开发者社区和技术论坛,与其他开发者交流经验和解决问题。 - **实践项目**:通过实际项目应用所学知识,不断迭代和完善自己的技能[^3]。 ### 开发技巧 - **性能优化**:根据应用场景的不同,可以选择不同的设备(CPU/GPU)来提高处理速度。 - **错误处理**:在实际开发过程中,注意异常处理和日志记录,以便于调试和维护。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值