在python中使用opencv进行人脸检测

时间:2018-07-04 19:42:58

标签: python-3.x opencv

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_fontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for(x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gay[y:y+h, x:x+w]
        roi_color = img[y:y+h, xx+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2)

        cv2.imshow('img',img)
        k=cv2.waitKey(30)
        if k==27:
            break

cap.release()
cv2.destroyAllWindows()

我是python和opencv的新手。我正在使用级联进行面部检测和眼睛检测。每当我运行上面的脚本时,都会出现以下错误:

***Traceback (most recent call last):
  File "C:/Users/Sagoe Gad/Documents/Python/Udemy_tut/app/Computer Vision/cascade/newScript.py", line 11, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale***

请提供任何帮助。您的回答将会产生影响。

0 个答案:

没有答案