zoukankan      html  css  js  c++  java
  • Realsense 人脸识别

    一.代码声明

         下面的代码是博主参考了Intel realsense官方SDK和官方例程后写的一段较为简单的代码,实现了简单的多人脸实时检测及跟踪功能。官方的人脸检测例程功能较多,但代码量很大,阅读起来也较困难,所以博主写了一段较为精简的人脸检测代码。

    二.具体代码

    1. #include <pxcsensemanager.h>    
    2. #include <pxcsession.h>    
    3. #include "util_render.h"    
    4. #include <iostream>  
    5. #include <string>  
    6. #include <stdio.h>    
    7. #include <opencv2opencv.hpp>    
    8. #include <windows.h>  
    9.   
    10. #define WIDTH 640    
    11. #define HEIGHT 480    
    12.   
    13. using namespace cv;  
    14. using namespace std;  
    15.   
    16. void DrawRectangle(Mat &img, Rect box)  
    17. {  
    18.     rectangle(img, box, Scalar(255, 255, 0),2);  
    19. }  
    20.   
    21. int main(int argc, char** argv)  
    22. {  
    23.     PXCSenseManager *psm = 0;  
    24.     psm = PXCSenseManager::CreateInstance();  
    25.     if (!psm)  
    26.     {  
    27.         wprintf_s(L"Unabel to create the PXCSenseManager ");  
    28.         return 1;  
    29.     }  
    30.   
    31.     //使能人脸跟踪  
    32.     psm->EnableFace();  
    33.   
    34.     // 初始化管道  
    35.     psm->Init();  
    36.   
    37.     //得到一个人脸模块的实例  
    38.     PXCFaceModule *faceModule = psm->QueryFace();  
    39.   
    40.     if (faceModule == NULL)  
    41.     {  
    42.         wprintf_s(L"Unabel to query FaceModule ");  
    43.         return 3;  
    44.     }  
    45.   
    46.     //创建一个人脸追踪模块动态配置的实例  
    47.     PXCFaceConfiguration *cfg = faceModule->CreateActiveConfiguration();  
    48.   
    49.     if (cfg == NULL)  
    50.     {  
    51.         wprintf_s(L"Unabel to create FaceConfiguration ");  
    52.         return 4;  
    53.     }  
    54.   
    55.     cfg->detection.isEnabled = TRUE; //这句也可注释掉,不影响检测结果  
    56.   
    57.     //使能所有警告  
    58.     cfg->EnableAllAlerts();  
    59.   
    60.     //将任何参数的改变反馈给faceModule  
    61.     cfg->ApplyChanges();  
    62.   
    63.     //创建人脸数据的实例  
    64.     PXCFaceData *facedata = faceModule->CreateOutput();  
    65.   
    66.     PXCImage *colorIm;  
    67.     PXCImage::ImageData color_data;  
    68.     PXCImage::ImageInfo color_info;  
    69.   
    70.     while (psm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)  
    71.   
    72.     {  
    73.         if (psm->AcquireFrame(true) < PXC_STATUS_NO_ERROR) break;  
    74.   
    75.         //获取最新的人脸追踪配置参数  
    76.         facedata->Update();  
    77.   
    78.         PXCCapture::Sample *sample = psm->QuerySample();  
    79.         colorIm = sample->color;  
    80.   
    81.         if (colorIm->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_RGB24, &color_data) < PXC_STATUS_NO_ERROR)  
    82.             wprintf_s(L"未正常获取彩色图 ");  
    83.   
    84.         color_info = sample->color->QueryInfo();  
    85.   
    86.         Mat color(Size(color_info.width, color_info.height), CV_8UC3, (void*)color_data.planes[0], color_data.pitches[0] / sizeof(uchar));  
    87.           
    88.         //取出检测到的人脸数目  
    89.         pxcI32 nfaces = facedata->QueryNumberOfDetectedFaces();  
    90.   
    91.         //对视野内每一张人脸追踪处理  
    92.         for (pxcI32 i = 0; i < nfaces; i++) {  
    93.             //按序号获取一个人脸的数据实例  
    94.             PXCFaceData::Face *trackedface = facedata->QueryFaceByIndex(i);  
    95.             PXCFaceData::DetectionData *detectiondata = trackedface->QueryDetection();  
    96.   
    97.             if (detectiondata == NULL)  
    98.             {  
    99.                 wprintf_s(L"Unabel to get detection data ");  
    100.                 return 5;  
    101.             }  
    102.   
    103.             //将当前人脸的位置数据存在rect中  
    104.             PXCRectI32 rect;  
    105.             detectiondata->QueryBoundingRect(&rect);  
    106.   
    107.             //PXCRectI32到opencv中Rect类的转化  
    108.             Rect cvrect = Rect(rect.x, rect.y, rect.w, rect.h);  
    109.             DrawRectangle(color, cvrect);  
    110.   
    111.             //给当前人脸加上识别序号的文字  
    112.             stringstream ss;  
    113.             ss << i;  
    114.             string id = ss.str();  
    115.             id = "ID:" + id;  
    116.             putText(color,id, Point(rect.x+rect.w/2, rect.y-rect.h/20), CV_FONT_HERSHEY_COMPLEX, 0.4, Scalar(255, 255, 0));  
    117.         }  
    118.                           
    119.         colorIm->ReleaseAccess(&color_data);  
    120.   
    121.         stringstream ss;  
    122.         ss << nfaces;  
    123.         string  num_faces= ss.str();  
    124.         num_faces =num_faces + " faces in the field of view.";  
    125.         putText(color, num_faces, Point(color.rows/20,color.cols/40), CV_FONT_HERSHEY_COMPLEX, 0.5, Scalar(153, 51, 250));  
    126.   
    127.         psm->ReleaseFrame();  
    128.         imshow("face_detection", color);  
    129.         waitKey(1);  
    130.     }  
    131.     facedata->Release();  
    132.     cfg->Release();  
    133.     psm->Close();  
    134.     psm->Release();  
    135. }  
    #include <pxcsensemanager.h>  
    #include <pxcsession.h>  
    #include "util_render.h"  
    #include <iostream>
    #include <string>
    #include <stdio.h>  
    #include <opencv2opencv.hpp>  
    #include <windows.h>
    
    #define WIDTH 640  
    #define HEIGHT 480  
    
    using namespace cv;
    using namespace std;
    
    void DrawRectangle(Mat &img, Rect box)
    {
    	rectangle(img, box, Scalar(255, 255, 0),2);
    }
    
    int main(int argc, char** argv)
    {
    	PXCSenseManager *psm = 0;
    	psm = PXCSenseManager::CreateInstance();
    	if (!psm)
    	{
    		wprintf_s(L"Unabel to create the PXCSenseManager
    ");
    		return 1;
    	}
    
    	//使能人脸跟踪
    	psm->EnableFace();
    
    	// 初始化管道
    	psm->Init();
    
    	//得到一个人脸模块的实例
    	PXCFaceModule *faceModule = psm->QueryFace();
    
    	if (faceModule == NULL)
    	{
    		wprintf_s(L"Unabel to query FaceModule
    ");
    		return 3;
    	}
    
    	//创建一个人脸追踪模块动态配置的实例
    	PXCFaceConfiguration *cfg = faceModule->CreateActiveConfiguration();
    
    	if (cfg == NULL)
    	{
    		wprintf_s(L"Unabel to create FaceConfiguration
    ");
    		return 4;
    	}
    
    	cfg->detection.isEnabled = TRUE; //这句也可注释掉,不影响检测结果
    
    	//使能所有警告
    	cfg->EnableAllAlerts();
    
    	//将任何参数的改变反馈给faceModule
    	cfg->ApplyChanges();
    
    	//创建人脸数据的实例
    	PXCFaceData *facedata = faceModule->CreateOutput();
    
    	PXCImage *colorIm;
    	PXCImage::ImageData color_data;
    	PXCImage::ImageInfo color_info;
    
    	while (psm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
    
    	{
    		if (psm->AcquireFrame(true) < PXC_STATUS_NO_ERROR) break;
    
    		//获取最新的人脸追踪配置参数
    		facedata->Update();
    
    		PXCCapture::Sample *sample = psm->QuerySample();
    		colorIm = sample->color;
    
    		if (colorIm->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_RGB24, &color_data) < PXC_STATUS_NO_ERROR)
    			wprintf_s(L"未正常获取彩色图
    ");
    
    		color_info = sample->color->QueryInfo();
    
    		Mat color(Size(color_info.width, color_info.height), CV_8UC3, (void*)color_data.planes[0], color_data.pitches[0] / sizeof(uchar));
    		
    		//取出检测到的人脸数目
    		pxcI32 nfaces = facedata->QueryNumberOfDetectedFaces();
    
    		//对视野内每一张人脸追踪处理
    		for (pxcI32 i = 0; i < nfaces; i++) {
    			//按序号获取一个人脸的数据实例
    			PXCFaceData::Face *trackedface = facedata->QueryFaceByIndex(i);
    			PXCFaceData::DetectionData *detectiondata = trackedface->QueryDetection();
    
    			if (detectiondata == NULL)
    			{
    				wprintf_s(L"Unabel to get detection data
    ");
    				return 5;
    			}
    
    			//将当前人脸的位置数据存在rect中
    			PXCRectI32 rect;
    			detectiondata->QueryBoundingRect(&rect);
    
    			//PXCRectI32到opencv中Rect类的转化
    			Rect cvrect = Rect(rect.x, rect.y, rect.w, rect.h);
    			DrawRectangle(color, cvrect);
    
                //给当前人脸加上识别序号的文字
    			stringstream ss;
                ss << i;
                string id = ss.str();
    			id = "ID:" + id;
    			putText(color,id, Point(rect.x+rect.w/2, rect.y-rect.h/20), CV_FONT_HERSHEY_COMPLEX, 0.4, Scalar(255, 255, 0));
    		}
    	    				
    		colorIm->ReleaseAccess(&color_data);
    
    		stringstream ss;
    		ss << nfaces;
    		string  num_faces= ss.str();
    		num_faces =num_faces + " faces in the field of view.";
    		putText(color, num_faces, Point(color.rows/20,color.cols/40), CV_FONT_HERSHEY_COMPLEX, 0.5, Scalar(153, 51, 250));
    
    		psm->ReleaseFrame();
    		imshow("face_detection", color);
    		waitKey(1);
    	}
    	facedata->Release();
    	cfg->Release();
    	psm->Close();
    	psm->Release();
    }
    

    运行结果和丑照:

    三.心得体会

    说实话,研究realsense的sdk是略痛苦的,因为可参考的资料实在有限,除了官方sdk就只有一个开发者论坛了。当你上网搜索kinect的相关资料时,可以发现大批大神的研究例程和心得。反过来看realsense,新闻很多,都是说它怎么怎么厉害,但是并没有多少对开发者有参考价值的资料。realsense毫无疑问比kinect强悍,体积又小,但因为发布时间较短,所以大部分人都还在研究这玩意怎么用。

    就像上面这个程序,博主在看完sdk关于人脸检测的介绍之后,按照它的框架去码代码,码的时候自己都有点迷糊每一步的意思。最后程序闪退,得不到任何结果,人脸都没有,还检测什么。无奈之下只有看example了,天哪噜,代码量实在大,还有一堆写窗口和按钮的,只能慢慢从里面探索出对自己有用的东西。

    看久了也就能看出一些门道了,比如ApplyChanges到底有什么用, 原来FaceConfiguration这个接口一旦创建了,就和FaceModule独立了,使用ApplyChanges是为了在模块配置参数发生变化时能通过update来将最新的配置参数传递给FaceModule。然而一开始,我并有理解这些,所以代码里连update都没有,结果只能是gg。

    甚至在刚开始的时候,我的enableface还有配置实例的创建代码都是写在while获取每一帧里面的,后面才理解了人脸模块的使能以及人脸追踪的配置应该在获取图片帧之前完成,然后获取一帧图片之后,更新人脸配置参数,对该帧图片检测人脸,对每一个检测到的人脸进行处理。

    总之,路漫漫,博主继续研究去了。

  • 相关阅读:
    Cannot complete this action,please try again. Correlation ID :bd640a9d-4c19-doff-2fe0-6ce1104b59ae
    我的工作流为什么启动不了
    关于IE下复选框的样式问题
    使用SharePoint rest语法返回已上传的SharePoint Excel 文件
    Running the complier from the Command Line
    关于inline
    来到坡国
    在oracle Virtual Box 虚拟机中搭建hadoop1.2.1完全分布式环境(转自康哥的博客)
    hadoop调试
    linux相关指令学习
  • 原文地址:https://www.cnblogs.com/safezone/p/5636904.html
Copyright © 2011-2022 走看看