SeetaFace使用(问题)

    由于毕业设计功能需要对人脸进行特征点定位,这两天查看了许多开源的人脸识别库,特地尝试了一下的有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就非常之快了,问题也就莫名其妙解决了。在这过程中,我查了许多的资料,并没有任何博主提出过这个点,所以我个人还是觉得有必要记录下来,希望能帮到和我一样,第一次尝试使用第三方动态链接库的人。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值