CV2 人脸动态捕捉

CV2 人脸动态捕捉

源代码

安装:pip install openCV-python

import cv2
import numpy as np
 
cv2.namedWindow("Face_Detect") #定义一个窗口
cap=cv2.VideoCapture(0) #捕获摄像头图像
success,frame=cap.read() #读入第一帧
path = 'C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect'
classifier=cv2.CascadeClassifier("./haarcascade_frontalface_alt.xml")
classifier.load(path+'\haarcascade_frontalface_default.xml')
 
while success:#如果读入帧正常
    size=frame.shape[:2]
    image=np.zeros(size,dtype=np.float16)
    image=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.equalizeHist(image,image)
    divisor=8
    h,w=size
    minSize=(int(w/divisor),int(h/divisor)) #像素一定是整数,或者用w//divisor

    faceRects=classifier.detectMultiScale(image,1.2,2,cv2.CASCADE_SCALE_IMAGE,minSize)
    #人脸识别

    if len(faceRects)> 0:
        for faceRect in faceRects:
            x,y,w,h=faceRect
            cv2.circle(frame,(x+w//2,y+h//2),min(w//2,h//2),(255,0,0),2) #圆形轮廓
            cv2.circle(frame,(x+w//4,y+2*h//5),min(w//8,h//8),(0,255,0),2) #左眼轮廓
            cv2.circle(frame,(x+3*w//4,y+2*h//5),min(w//8,h//8),(0,255,0),2)#右眼轮廓
            cv2.circle(frame,(x+w//2,y+2*h//3),min(w//8,h//8),(0,255,0),2) #鼻子轮廓
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255),2)   #矩形轮廓

    cv2.imshow("Face_Detect",frame)
    #显示轮廓
    success,frame=cap.read()#如正常则读入下一帧
    key = cv2.waitKey(200)
    c=chr(int(key)&255)
    if c in ['q','Q',chr(27)]:#如果键入‘q'退出循环
        print('exit')
        break#退出循环
 
 #循环结束则清零
cap.release()
cv2.destroyAllWindows()

注意:

** windows系统平台上运行,可能会出现以下错误:**
error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function ‘cv::CascadeClassifier::detectMultiScale’
** 解决办法:**
在C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect目录下将haarcascade_frontalface_default.xml文件添加到该目录下

你可能感兴趣的:(人工智能,人脸识别,python)