复制代码
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <opencv2/objdetect/objdetect.hpp> using namespace cv; using namespace std; #include "stdio.h" void detectAndDraw(Mat & img,CascadeClassifier & cascade, CascadeClassifier & nestedCascde, double scale,bool tryflip,int _iPicNum) { int i = 0; double t = 0; vector<Rect> faces,faces2; const static Scalar colors [] = { CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), CV_RGB(255,128,0), CV_RGB(255,2555,0), CV_RGB(255,0,0), CV_RGB(255,0,255) }; Mat gray,smallImg(cvRound(img.rows/scale),cvRound(img.cols/scale),CV_8UC1); cvtColor(img,gray,CV_BGR2GRAY); resize(gray,smallImg,smallImg.size(),0,0,INTER_LINEAR); equalizeHist(smallImg,smallImg); //t = (double)cvGetTickCount(); int iEnable = 0; cascade.detectMultiScale(smallImg,faces,1.1,2,0 | CV_HAAR_SCALE_IMAGE,Size(10,10)); for(vector<Rect>::const_iterator r = faces.begin();r != faces.end();r++,i++) { Mat smallImgROL; vector<Rect> nestedObjects; Point center; Scalar color = colors[i%8]; int radius; double aspect_ratio = (double)r->width/r->height; if(0.75 < aspect_ratio && aspect_ratio < 1.3) { iEnable = 1; center.x = cvRound((r->x + r->width * 0.5) * scale); center.y = cvRound ((r->y + r->height * 0.5) * scale); radius = cvRound((r->width + r->height) * 0.25 * scale); Rect rect1(center.x - radius,center.y - radius,radius * 2,radius * 2); Mat roi1; img(rect1).copyTo(roi1); char cbuf[12] = {0}; sprintf(cbuf,"ceshijieguo/%d_%d.jpg",_iPicNum,i); imwrite(cbuf,roi1); // circle(img,center,radius,color,3,8,0); //rectangle(img,center.x ,center.y,color ,3,8,0); rectangle(img,cvPoint(cvRound(r->x * scale),cvRound(r->y * scale)), cvPoint(cvRound((r->x + r->width -1 )*scale),cvRound((r->y + r->height - 1)* scale)), color ,3,8,0); } else { rectangle(img,cvPoint(cvRound(r->x * scale),cvRound(r->y * scale)), cvPoint(cvRound((r->x + r->width -1 )*scale),cvRound((r->y + r->height - 1)* scale)), color ,3,8,0); //视频画框 // for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++) // rectangle(img, cvPoint(r->x, r->y), cvPoint(r->x + r->width - 1, r->y + r->height - 1), Scalar(255, 0, 255), 3, 8, 0); //文字标注 // putText(imgForShow, p.first, Point(BBox.x, BBox.y), FONT_HERSHEY_PLAIN, 2, Scalar(255, 0, 0)); } if( iEnable) { char cbuf[18] = {0}; sprintf(cbuf,"ceshijieguo/%d.jpg",_iPicNum); imwrite(cbuf,img); imshow("video",img); } } imshow("video",img); cvWaitKey(1); } int main() { Mat frame; Mat edges; CascadeClassifier cascade,nestedCascade; bool stop = false; cascade.load("haarcascade_frontalface_alt.xml"); nestedCascade.load("haarcascade_eye.xml"); VideoCapture capture; capture.open("ceshi.mkv"); double rate = capture.get(CV_CAP_PROP_FPS); int i = 0; while(capture.read(frame)) { detectAndDraw(frame,cascade,nestedCascade,4,0,i+ 1); i++; } return 0; }
最后
以上就是感动香氛最近收集整理的关于opencv检测视频中的人脸,并标记画框,同时输出检测结果到文件夹的全部内容,更多相关opencv检测视频中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复