python+opencv人脸检测
2018-03-11 本文已影响23人
少寨主的互联网洞察
环境配置:
请参考下面我的文章:
这里需要使用opencv自带的haarcascade_frontalface_alt2.xml
python代码如下:
import cv2
import sys
cascPath="./haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture=cv2.VideoCapture(0)
while(video_capture.isOpened()):
ret,frame=video_capture.read()
if ret==True:
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=3,minSize=(30, 30),flags=0)
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()
特别强调:需要注意缩进的问题!