VS+OPENCV+手机摄像头实现人脸和人眼跟踪
IP摄像头
首先用手机下载一个IP摄像头
百度网盘:https://pan.baidu.com/s/1U6wm4O_7W_L41jZ7Vj75Ow
提取码:4d1o
app如下图
1、实现方式
①手机和电脑同在一个局域网
②手机提供热点给电脑
③不同wifi 改变IP即可
2、打开IP摄像头
注意此时,会提示一个用户名和密码,请记住!!!
3、这里有个局域网的IP,就是我们要VS连接的地址
4、代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; CascadeClassifier face_dector,right_dector,left_dector; string xmlfilepath = "D:/softwire/opencv_files/self_opencv/install/etc/haarcascades/haarcascade_frontalface_alt.xml"; string xmlfilepath_right_eye = "D:/softwire/opencv_files/self_opencv/install/etc/haarcascades/haarcascade_eye.xml"; string xmlfilepath_left_eye = "D:/softwire/opencv_files/self_opencv/install/etc/haarcascades/haarcascade_eye.xml"; int main() { //VideoCapture capture(0); //flip 可以把window摄像头拍摄左右调度 /* String filepath = "D:/VS2019/files/video/faceandeye.mp4"; if (!capture.open(filepath)) { cout << "cannot open file"; return -1; }*/ VideoCapture capture; capture.open("http://admin:[email protected]:8081"); //用户名+密码+IP地址 if(!(face_dector.load(xmlfilepath)&&right_dector.load(xmlfilepath_right_eye)&& left_dector.load(xmlfilepath_left_eye))) { cout << "cannot open xml file"; return -1; } |
5、HAAR级联分类器实现人脸跟踪和人眼捕捉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | Mat frame,gray; char c; while (capture.read(frame)) { // imshow("Input Video", frame); flip(frame, frame, 1); cvtColor(frame, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray); vector<Rect> rect; face_dector.detectMultiScale(gray, rect, 1.1, 3, 0, Size(20, 20)); for (size_t t = 0; t < rect.size(); t++) { rectangle(frame, rect[t], Scalar(0, 0, 255)); Rect right_eye_rect; Rect left_eye_rect; int offsety = rect[t].height / 4; int offsetx = rect[t].width / 8; int eyeheight = rect[t].height/2 - offsety; int eyewidth = rect[t].width/2 - offsetx; left_eye_rect.x = rect[t].x + offsetx; left_eye_rect.y = rect[t].y + offsety; left_eye_rect.height = eyeheight; left_eye_rect.width = eyewidth; Mat left_eye = gray(left_eye_rect); vector<Rect> left_ROI; left_dector.detectMultiScale(left_eye, left_ROI, 1.1, 3, 0, Size(20, 20)); //参考xml 给出的最小尺寸 for (size_t i = 0; i < left_ROI.size(); i++) { Rect left_rect; left_rect.x = rect[t].x + left_ROI[i].x+offsetx; left_rect.y = rect[t].y + left_ROI[i].y+offsety; left_rect.width = left_ROI[i].width; left_rect.height = left_ROI[i].height; rectangle(frame, left_rect, Scalar(0, 255, 255)); } right_eye_rect.x = rect[t].x + rect[t].width / 2; right_eye_rect.y = rect[t].y + offsety; right_eye_rect.height = eyeheight; right_eye_rect.width = eyewidth; Mat right_eye = gray(right_eye_rect); vector<Rect> right_ROI; right_dector.detectMultiScale(right_eye, right_ROI, 1.1, 3, 0, Size(20, 20)); //参考xml 给出的最小尺寸 for (size_t i = 0; i < right_ROI.size(); i++) { Rect right_rect; right_rect.x = rect[t].x + rect[t].width / 2+right_ROI[i].x; right_rect.y = rect[t].y + right_ROI[i].y + offsety; right_rect.width = right_ROI[i].width; right_rect.height = right_ROI[i].height; rectangle(frame, right_rect, Scalar(255, 255, 255)); } } imshow("Output Video", frame); c = waitKey(50); if (c == 27) break; } capture.release(); cout << "Hello World!\n"; } |
6、效果展示
说明
①环境 VS2019+OPENCV420+HUAWEI手机
② xml是官方提供的,准确率没那么高
③眼睛捕捉的没那么准,因为SIze()设置的大了,这里只为了说明IP摄像头的使用,不做修改
④人眼捕捉,有点人体生态学的意味,具体不解释。