【问题标题】:Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale在opencv python中使用级联分类器进行人脸检测
【发布时间】:2023-04-08 00:49:01
【问题描述】:

我正在使用内置的级联分类器进行人脸检测。
代码是这样的(OpenCV Python Tutorials):

import numpy as np
import cv2

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

img = cv2.imread('ammma.jpg')
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 = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+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)
cv2.waitKey(0)
cv2.destroyAllWindows()

但是当我运行代码时出现以下错误:

C:\Python27\python.exe C:/Users/DELL/Downloads/Amma/code/fd.py

OpenCV 错误: cv::CascadeClassifier::detectMultiScale 中的断言失败 (!empty()),文件 C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp,行1634
回溯(最近一次通话最后):
文件“C:/Users/DELL/Downloads/Amma/code/fd.py”,第 10 行,在
faces = face_cascade.detectMultiScale(img, 1.3, 5)
cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp:1634: 错误: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

【问题讨论】:

  • 应该是cv2.rectangle(...) 而不是cv2.Rectangle(...)

标签:
python-2.7
opencv
computer-vision
face-detection