首页 > 其他分享 >ARS展览项目(三)——识别面部得到特征点

ARS展览项目(三)——识别面部得到特征点

时间:2024-09-24 10:15:30浏览次数:1  
标签:ARS temp pose 面部 face 展览 faces include cv

这个表情识别项目的第一步

下面是代码,作用是识别出人脸,并且把人脸标记为68个特征点。
然后把每次识别的一组(68个数据)保存在 .txt文件里,一秒识别n次,那么一秒就生成n个txt

本篇博客的实现方法参照了https://blog.csdn.net/zmdsjtu/article/details/53667929,这个很好用,感谢这位博主。

//#pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")//去除CMD窗口
#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include<string>

using namespace dlib;
using namespace std;
void write_txt(string name1, string content1, bool over_write);
int main()
{

	try
	{

		cv::VideoCapture cap(0);

		//image_window win;

		// Load face detection and pose estimation models.
		frontal_face_detector detector = get_frontal_face_detector();
		shape_predictor pose_model;
		deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
		std::vector<rectangle> faces2;
		// Grab and process frames until the main window is closed by the user.
		int filename = 0;
		//	while (cv::waitKey(30) != 27)

		//	while (cv::waitKey(30) != 27){
		//	cv::VideoCapture cap("1.avi");
		if (!cap.isOpened())
		{
			cerr << "Unable to connect to camera" << endl;
			return 1;
		}

		int frame1 = cap.get(7);
		//	while (cv::waitKey(30) != 27 && --frame1 != 14)

		while (cv::waitKey(30) != 27)
		{
			// Grab a frame
			cv::Mat temp;
			cap >> temp;
			//	temp = cv::imread("1.bmp");
			cv_image<bgr_pixel> cimg(temp);
			// Detect faces 
			std::vector<rectangle> faces = detector(cimg);
			if (!faces.empty())faces2 = faces;
			// Find the pose of each face.
			std::vector<full_object_detection> shapes;
			if (faces.empty()) {

				//		for (unsigned long i = 0; i < faces2.size(); ++i)
				//	shapes.push_back(pose_model(cimg, faces2[i]));
			}
			else {
				for (unsigned long i = 0; i < faces.size(); ++i)
					shapes.push_back(pose_model(cimg, faces[i]));
			}
			if (!shapes.empty()) {
				cv::line(temp, cvPoint(faces[0].left(), faces[0].top()), cvPoint(faces[0].right(), faces[0].top()), cv::Scalar(255, 0, 0));
				cv::line(temp, cvPoint(faces[0].left(), faces[0].top()), cvPoint(faces[0].left(), faces[0].bottom()), cv::Scalar(255, 0, 0));


				filename++;
				float aaa2 = -(faces[0].top() - faces[0].bottom()) / 300.0;
				cout << aaa2 << endl;
				for (int i = 0; i < 68; i++) {
					circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
					putText(temp, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0), 1, 4);
					//--------------这部分是想用来采集训练数据的

					write_txt((to_string(filename) + ".txt"), to_string((shapes[0].part(i).x() - faces[0].left()) / aaa2), 0);
					write_txt((to_string(filename) + ".txt"), to_string((shapes[0].part(i).y() - faces[0].top()) / aaa2), 0);
					//----------------------------------------------------------------
				}
				//		Drawarrow(temp, shapes[0].part(36).x(), shapes[0].part(48).x(), shapes[0].part(30).x(), shapes[0].part(45).x(), shapes[0].part(54).x(), shapes[0].part(36).y(), shapes[0].part(48).y(), shapes[0].part(30).y(), shapes[0].part(45).y(), shapes[0].part(54).y());
			}
			//Display it all on the screen
			//		cv::namedWindow("Dlib特征点", CV_WINDOW_NORMAL);
			//		cv::setWindowProperty("Dlib特征点", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
			imshow("Dlib特征点", temp);

			//	cv::waitKey(-1);
		}
		//}
		cv::destroyAllWindows();
	}

	catch (serialization_error& e)
	{
		cout << "You need dlib's default face landmarking model file to run this example." << endl;
		cout << "You can get it from the following URL: " << endl;
		cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
		cout << endl << e.what() << endl;
	}
	//catch (exception& e)
	//{
	//cout << e.what() << endl;
	//}
}
void write_txt(string name1, string content1, bool over_write) {
	ofstream write_into(name1, over_write ? ios::trunc : ios::app);

	if (write_into.is_open()) {//如果成功的话       
		write_into << content1 << endl;
		write_into.close();
	}
}

标签:ARS,temp,pose,面部,face,展览,faces,include,cv
From: https://www.cnblogs.com/atanchen/p/18428487

相关文章

  • ARS展览项目(二)——环境搭建:opencv、dlib、VS2017
    先说用到的软件和函数库VS2017——我用VS2017社区版来开发,原因是软件免费而且好用,本项目用C++来做opencv——OpenComputerVision是计算机视觉的库,有多种语言的接口,而且函数库也很丰富dlib——Dlib是一个包含机器学习算法的C++开源工具包,提供大量的机器学习/图像处理算法(网......
  • ARS展览项目(七)——C-多线程:Socket-表情识别整合
    说明一下我这边做表情识别和Socket,表情识别要实时,Socket要一直监听表情识别的结果,那么就只好用C++多线程来解决这个“两个功能一直并且同时运行”的问题。否则,如果是单线程的话,只能运行表情识别一段时间,切换发送一段时间,又切换回来,这样没有多线程好。还要说解决的难点写成多......
  • QCustomPlot QCPBars横向柱状图示例
    #include"qcustomplot.h"intmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlotcustomPlot;customPlot.resize(700,500);customPlot.show();QCPAxis*keyAxis=customPlot.yAxis;QCPAxis*valueAxis=......
  • QCustomPlot QCPBars纵向柱状图示例
    #include"qcustomplot.h"intmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlotcustomPlot;customPlot.resize(700,500);customPlot.show();QCPAxis*keyAxis=customPlot.xAxis;QCPAxis*valueAxis=......
  • 帝国CMS提示parse error syntax error的解决方法
    当帝国CMS提示 parseerrorsyntaxerror 时,这意味着PHP解析器遇到了一个语法错误。这种错误通常发生在PHP代码中存在不符合PHP语法规范的地方。以下是一些排查和解决此类问题的方法:1.检查错误的具体位置查看错误信息:错误信息通常会指出哪个文件和哪一行出现了问题。打开错......
  • 我在 Marscode 用了 3 天,转行成为 Python 程序员
    以下是「 豆包MarsCode 体验官」优秀文章,作者不惑_。豆包MarsCode 项目实战Java程序员转行Python学习之路俗话说:工欲善其事,必先利其器。在历史的长河中,新手程序员最大的痛点之一就是搭建开发环境。先就是今天,如果你没有VSCode,甚至也没有其他IDE,那么也没有关系。豆包......
  • 帝国CMS提示parse error syntax error的解决方法
    当帝国CMS提示“Parseerror:syntaxerror”时,这通常意味着PHP解析器遇到了无法理解的代码,最常见的原因是语法错误。以下是一些解决此类问题的方法:常见原因及解决办法检查语法错误:解决办法:仔细检查报错行附近的代码,查找是否有语法错误,如缺少分号、括号不匹配、字符串未闭合......
  • 如何解决"Parse error: syntax error"
    当遇到"Parseerror:syntaxerror"这类错误时,通常表明PHP在解析脚本时遇到了语法错误。这类错误通常比较容易定位,因为错误提示会给出具体的文件名和行号。下面是一些详细的解决步骤:解决方法:查看错误提示:错误提示通常会显示具体的文件名和行号。例如:  Parseerr......
  • 帝国CMS提示parseerrorsyntaxerror如何解决
    当帝国CMS提示“Parseerror:syntaxerror”时,这通常意味着PHP在解析脚本时遇到了语法错误。这种错误可能是由多种原因引起的,以下是一些常见的解决方法:解决方法:检查PHP版本兼容性:如果你正在使用的是较新的PHP版本(比如PHP7及以上),而帝国CMS的某些部分代码可能尚未更新以适应......
  • Python的configparser模块中,ConfigParser和RawConfigParser的区别
    在Python的configparser模块中,ConfigParser()和RawConfigParser()是两个不同的类,用于解析配置文件。它们之间的主要区别在于对配置文件中的值进行处理的方式。一、区别1、ConfigParser()类是configparser模块的旧版本,它提供了一些额外的特性,如对配置文件中的值进行插值替换。......