python+opencv人脸检测

环境配置:
请参考下面我的文章:

https://www.jianshu.com/p/86a32db2488b

这里需要使用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()

特别强调:需要注意缩进的问题!

效果演示
python+opencv人脸检测_第1张图片
Inked捕获_LI.jpg

你可能感兴趣的:(python+opencv人脸检测)