人脸识别引擎SeetaFaceEngine中Identification模块用于比较两幅人脸图像的相似度,以下是测试代码:

int test_recognize()
{
	const std::string path_images{ "E:/GitCode/Face_Test/testdata/recognization/" };
	seeta::FaceDetection detector("E:/GitCode/Face_Test/src/SeetaFaceEngine/FaceDetection/model/seeta_fd_frontal_v1.0.bin");
	seeta::FaceAlignment alignment("E:/GitCode/Face_Test/src/SeetaFaceEngine/FaceAlignment/model/seeta_fa_v1.1.bin");
	seeta::FaceIdentification face_recognizer("E:/GitCode/Face_Test/src/SeetaFaceEngine/FaceIdentification/model/seeta_fr_v1.0.bin");

	detector.SetMinFaceSize(20);
	detector.SetMaxFaceSize(200);
	detector.SetScoreThresh(2.f);
	detector.SetImagePyramidScaleFactor(0.8f);
	detector.SetWindowStep(4, 4);

	std::vector<std::vector<seeta::FacialLandmark>> landmards;

	// detect and alignment
	for (int i = 0; i < 20; i++) {
		std::string image = path_images + std::to_string(i) + ".jpg";
		//fprintf(stderr, "start process image: %s\n", image.c_str());

		cv::Mat src_ = cv::imread(image, 1);
		if (src_.empty()) {
			fprintf(stderr, "read image error: %s\n", image.c_str());
			continue;
		}

		cv::Mat src;
		cv::cvtColor(src_, src, CV_BGR2GRAY);

		seeta::ImageData img_data;
		img_data.data = src.data;
		img_data.width = src.cols;
		img_data.height = src.rows;
		img_data.num_channels = 1;

		std::vector<seeta::FaceInfo> faces = detector.Detect(img_data);
		if (faces.size() == 0) {
			fprintf(stderr, "%s don't detect face\n", image.c_str());
			continue;
		}

		// Detect 5 facial landmarks: two eye centers, nose tip and two mouth corners
		std::vector<seeta::FacialLandmark> landmard(5);
		alignment.PointDetectLandmarks(img_data, faces[0], &landmard[0]);

		landmards.push_back(landmard);

		cv::rectangle(src_, cv::Rect(faces[0].bbox.x, faces[0].bbox.y,
			faces[0].bbox.width, faces[0].bbox.height), cv::Scalar(0, 255, 0), 2);

		for (auto point : landmard) {
			cv::circle(src_, cv::Point(point.x, point.y), 2, cv::Scalar(0, 0, 255), 2);
		}

		std::string save_result = path_images + "_" + std::to_string(i) + ".jpg";
		cv::imwrite(save_result, src_);
	}

	int width = 200;
	int height = 200;
	cv::Mat dst(height * 5, width * 4, CV_8UC3);
	for (int i = 0; i < 20; i++) {
		std::string input_image = path_images + "_" + std::to_string(i) + ".jpg";
		cv::Mat src = cv::imread(input_image, 1);
		if (src.empty()) {
			fprintf(stderr, "read image error: %s\n", input_image.c_str());
			return -1;
		}

		cv::resize(src, src, cv::Size(width, height), 0, 0, 4);
		int x = (i * width) % (width * 4);
		int y = (i / 4) * height;
		cv::Mat part = dst(cv::Rect(x, y, width, height));
		src.copyTo(part);
	}
	std::string output_image = path_images + "result_alignment.png";
	cv::imwrite(output_image, dst);

	// crop image
	for (int i = 0; i < 20; i++) {
		std::string image = path_images + std::to_string(i) + ".jpg";
		//fprintf(stderr, "start process image: %s\n", image.c_str());

		cv::Mat src_img = cv::imread(image, 1);
		if (src_img.data == nullptr) {
			fprintf(stderr, "Load image error: %s\n", image.c_str());
			return -1;
		}

		if (face_recognizer.crop_channels() != src_img.channels()) {
			fprintf(stderr, "channels dismatch: %d, %d\n", face_recognizer.crop_channels(), src_img.channels());
			return -1;
		}

		// ImageData store data of an image without memory alignment.
		seeta::ImageData src_img_data(src_img.cols, src_img.rows, src_img.channels());
		src_img_data.data = src_img.data;

		// Create a image to store crop face.
		cv::Mat dst_img(face_recognizer.crop_height(), face_recognizer.crop_width(), CV_8UC(face_recognizer.crop_channels()));
		seeta::ImageData dst_img_data(dst_img.cols, dst_img.rows, dst_img.channels());
		dst_img_data.data = dst_img.data;
		// Crop Face
		face_recognizer.CropFace(src_img_data, &landmards[i][0], dst_img_data);

		std::string save_image_name = path_images + "crop_" + std::to_string(i) + ".jpg";
		cv::imwrite(save_image_name, dst_img);
	}

	dst = cv::Mat(height * 5, width * 4, CV_8UC3);
	for (int i = 0; i < 20; i++) {
		std::string input_image = path_images + "crop_" + std::to_string(i) + ".jpg";
		cv::Mat src_img = cv::imread(input_image, 1);
		if (src_img.empty()) {
			fprintf(stderr, "read image error: %s\n", input_image.c_str());
			return -1;
		}

		cv::resize(src_img, src_img, cv::Size(width, height), 0, 0, 4);
		int x = (i * width) % (width * 4);
		int y = (i / 4) * height;
		cv::Mat part = dst(cv::Rect(x, y, width, height));
		src_img.copyTo(part);
	}
	output_image = path_images + "result_crop.png";
	cv::imwrite(output_image, dst);

	// extract feature
	int feat_size = face_recognizer.feature_size();
	if (feat_size != 2048) {
		fprintf(stderr, "feature size mismatch: %d\n", feat_size);
		return -1;
	}

	float* feat_sdk = new float[feat_size * 20];

	for (int i = 0; i < 20; i++) {
		std::string input_image = path_images + "crop_" + std::to_string(i) + ".jpg";
		cv::Mat src_img = cv::imread(input_image, 1);
		if (src_img.empty()) {
			fprintf(stderr, "read image error: %s\n", input_image.c_str());
			return -1;
		}

		cv::resize(src_img, src_img, cv::Size(face_recognizer.crop_height(), face_recognizer.crop_width()));

		// ImageData store data of an image without memory alignment.
		seeta::ImageData src_img_data(src_img.cols, src_img.rows, src_img.channels());
		src_img_data.data = src_img.data;

		// Extract feature
		face_recognizer.ExtractFeature(src_img_data, feat_sdk + i * feat_size);
	}

	float* feat1 = feat_sdk;
	// varify(recognize)
	for (int i = 1; i < 20; i++) {
		std::string image = std::to_string(i) + ".jpg";
		float* feat_other = feat_sdk + i * feat_size;

		// Caculate similarity
		float sim = face_recognizer.CalcSimilarity(feat1, feat_other);
		fprintf(stdout, "0.jpg -- %s similarity: %f\n", image.c_str(), sim);
	}

	delete[] feat_sdk;

	return 0;
}

从网上找了20张图像,前19张为周星驰,最后一张为汤唯,用于测试此模块,测试结果如下:

detect/alignment结果如下:

crop结果如下:

取上图中最左上图为标准图,与其它19幅图作验证,测试结果如下:

GitHubhttps://github.com/fengbingchun/Face_Test

人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码的更多相关文章

  1. 人脸识别引擎SeetaFaceEngine中Alignment模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Alignment模块用于检测人脸关键点,包括5个点,两个眼的中心.鼻尖.两个嘴角,以下是测试代码: int test_alignment() { std: ...

  2. 人脸识别引擎SeetaFaceEngine中Detection模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Detection模块用于人脸检测,以下是测试代码: int test_detection() { std::vector<std::string&g ...

  3. 人脸识别引擎SeetaFaceEngine简介及在windows7 vs2013下的编译

    SeetaFaceEngine是开源的C++人脸识别引擎,无需第三方库,它是由中科院计算所山世光老师团队研发.它的License是BSD-2. SeetaFaceEngine库包括三个模块:人脸检测( ...

  4. 【计算机视觉】SeetaFace Engine开源C++人脸识别引擎

    SeetaFace Engine是一个开源的C++人脸识别引擎,它可以在不依赖第三方的条件下载CPU上运行.他包含三个关键部分,即:SeetaFace Detection,SeetaFace Alig ...

  5. Android打开相机进行人脸识别,使用虹软人脸识别引擎

    上一张效果图,渣画质,能看就好 功能说明: 人脸识别使用的是虹软的FreeSDK,包含人脸追踪,人脸检测,人脸识别,年龄.性别检测功能,其中本demo只使用了FT和FR(人脸追踪和人脸识别),封装了开 ...

  6. 教你如何认识人脸识别开发套件中的双目摄像、3D结构光摄像头、单目摄像头的区别及详细讲解

    深圳市宁远电子提供的人脸识别模组可支持双目摄像头和3D结构光摄像头,在客户咨询中经常有被问到双目的为什么会比单目的成本高,区别在哪里,他们的适用于哪些场景呢?在此,深圳市宁远电子技术工程师就为大家详细 ...

  7. 关于人脸识别引擎FaceRecognitionDotNet的实例

    根据我上篇文章的分享,我提到了FaceRecognitionDotNet,它是python语言开发的一个项目face_recognition移植.结果真是有喜有忧,喜的是很多去关注了,进行了下载,我看 ...

  8. 人脸识别引擎SeetaFace编译 ubuntu

    00.SeetaFace简介 SeetaFace Engine is an open source C++ face recognition engine, which can run on CPU ...

  9. .NET的关于人脸识别引擎分享(C#)

    https://www.cnblogs.com/RainbowInTheSky/p/10247921.html

随机推荐

  1. python中执行shell命令

    查看输出结果 import os output = os.popen('cat 6018_gap_5_predict/solusion2/solusion2_0-1.txt | wc -l') pri ...

  2. fastjson反序列化TemplatesImpl

    环境参考第一个链接,直接用IDEA打开 编译EvilObject.java成EvilObject.class 先看poc,其中NASTY_CLASS为TemplatesImpl类,evilCode是E ...

  3. LeetCode刷题(数据库)---- 超过5名学生的课

    题:请列出所有超过或等于5名学生的课. 有一个courses 表 ,有: student (学生) 和 class (课程). 例如,表: +---------+------------+ | stu ...

  4. PAT——1028. 人口普查

    某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月 ...

  5. Selenium自动化测试之基本控件使用

    Selenium自动化测试之基本控件使用 1.输入框input: 在Html中样式: <input id="username" type="text"&g ...

  6. Knowledge-Reserve

    Knowledge-Reserve ComputerOperatingSystem 编译 静态库&动态库(Linux) 静态链接&动态链接 内存 内联函数&宏 Static&a ...

  7. Java并发编程(七)终结线程

    线程的状态 一个线程会有如下五个状态 1.新建:线程在被创建时会暂时处于这种状态,此时系统为线程分配资源并对其进行初始化 2.就绪:此时线程已经可以运行,只是系统没有为其分配CPU时间. 3.运行:系 ...

  8. 【js】 Uncaught RangeError: Invalid string length

    今天项目比较催的比较着急,浏览器总是崩溃,后来报了一个Uncaught RangeError: Invalid string length(字符串长度无效) 的错误. 在ajax请求后得到的json数 ...

  9. XML第一次简单入门(Lab分析)

    In this tutorial you will create a well-formed and verified XML file. Consider the XML document belo ...

  10. Centos7前后台运行jar包

    方式一: java -jar lf-test-1.0-SNAPSHOT.jar 前台运行,当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出. 方式二: java -ja ...