本文实例为大家分享了基于OpenCV实现视频的人脸检测具体代码,供大家参考,具体内容如下
前提条件
1.摄像头
2.已安装Python和OpenCV3
代码
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
|
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
# 打开视频捕获设备
video_capture = cv2.VideoCapture( 0 )
while True :
if not video_capture.isOpened():
print ( 'Unable to load camera.' )
sleep( 5 )
pass
# 读视频帧
ret, frame = video_capture.read()
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 调用分类器进行检测
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.1 ,
minNeighbors = 5 ,
minSize = ( 30 , 30 ),
#flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# 画矩形框
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), ( 0 , 255 , 0 ), 2 )
# 显示视频
cv2.imshow( 'Video' , frame)
if cv2.waitKey( 1 ) & 0xFF = = ord ( 'q' ):
break
# 关闭摄像头设备
video_capture.release()
# 关闭所有窗口
cv2.destroyAllWindows()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/chenhaifeng2016/article/details/72878377