OpenCV实现人脸检测功能
本文实例为大家分享了OpenCV实现人脸检测功能的具体代码,供大家参考,具体内容如下
1、HAAR级联检测
#include#include usingnamespacecv; #include #include usingnamespacestd; intmain(intartc,char**argv){ face_detect_haar(); waitKey(0); return0; } voidface_detect_haar(){ CascadeClassifierfaceDetector; std::stringhaar_data_file="./models/haarcascades/haarcascade_frontalface_alt_tree.xml"; faceDetector.load(haar_data_file); vector faces; //VideoCapturecapture(0); VideoCapturecapture("./video/test.mp4"); Matframe,gray; intcount=0; while(capture.read(frame)){ int64start=getTickCount(); if(frame.empty()) { break; } //水平镜像调整 //flip(frame,frame,1); imshow("input",frame); if(frame.channels()==4) cvtColor(frame,frame,COLOR_BGRA2BGR); cvtColor(frame,gray,COLOR_BGR2GRAY); equalizeHist(gray,gray); faceDetector.detectMultiScale(gray,faces,1.2,1,0,Size(30,30),Size(400,400)); for(size_tt=0;t =0)break; } printf("totalface:%d\n",count); }
2、 DNN人脸检测
#include#include usingnamespacecv; usingnamespacecv::dnn; #include #include usingnamespacestd; constsize_tinWidth=300; constsize_tinHeight=300; constdoubleinScaleFactor=1.0; constScalarmeanVal(104.0,177.0,123.0); constfloatconfidenceThreshold=0.7; voidface_detect_dnn(); voidmtcnn_demo(); intmain(intargc,char**argv) { face_detect_dnn(); waitKey(0); return0; } voidface_detect_dnn(){ //这里采用tensorflow模型 std::stringmodelBinary="./models/dnn/face_detector/opencv_face_detector_uint8.pb"; std::stringmodelDesc="./models/dnn/face_detector/opencv_face_detector.pbtxt"; //初始化网络 dnn::Netnet=readNetFromTensorflow(modelBinary,modelDesc); net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableTarget(DNN_TARGET_CPU); if(net.empty()) { printf("Loadmodelsfail...\n"); return; } //打开摄像头 //VideoCapturecapture(0); VideoCapturecapture("./video/test.mp4"); if(!capture.isOpened()){ printf("Don'tfindvideo...\n"); return; } Matframe; intcount=0; while(capture.read(frame)){ int64start=getTickCount(); if(frame.empty()) { break; } //水平镜像调整 //flip(frame,frame,1); imshow("input",frame); if(frame.channels()==4) cvtColor(frame,frame,COLOR_BGRA2BGR); //输入数据调整 MatinputBlob=blobFromImage(frame,inScaleFactor, Size(inWidth,inHeight),meanVal,false,false); net.setInput(inputBlob,"data"); //人脸检测 Matdetection=net.forward("detection_out"); vector layersTimings; doublefreq=getTickFrequency()/1000; doubletime=net.getPerfProfile(layersTimings)/freq; MatdetectionMat(detection.size[2],detection.size[3],CV_32F,detection.ptr ()); ostringstreamss; for(inti=0;i (i,2); if(confidence>confidenceThreshold) { count++; intxLeftBottom=static_cast (detectionMat.at (i,3)*frame.cols); intyLeftBottom=static_cast (detectionMat.at (i,4)*frame.rows); intxRightTop=static_cast (detectionMat.at (i,5)*frame.cols); intyRightTop=static_cast (detectionMat.at (i,6)*frame.rows); Rectobject((int)xLeftBottom,(int)yLeftBottom, (int)(xRightTop-xLeftBottom), (int)(yRightTop-yLeftBottom)); rectangle(frame,object,Scalar(0,255,0)); ss< =0)break; } printf("totalface:%d\n",count); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。