【问题标题】:Error:(-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'错误:(-215:断言失败)!函数'cv :: CascadeClassifier :: detectMultiScale'中的empty()
【发布时间】:2021-06-30 16:46:16
【问题描述】:

我正在尝试使用 python 的“Open-CV”进行人脸检测。该程序向我抛出了一个我作为 python 初学者无法理解的错误。 程序错误:

Traceback (most recent call last):
  File "c:\Users\(User's name)\Documents\Python\Face-dection\Facedectection.py", line 6, in <module>
    gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 

代码:

import cv2

face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

img = cv2.imread("photo.jpg")
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_cascade.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5)

for x, y, w, h in faces:
    img=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

resized=cv2.resize(img,(int(img.shape[1]/3)),(int(img.shape[0]/3)))

cv2.imshow("Deteced-face",resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

注意:照片和.xml文件放在同一个文件夹中

【问题讨论】:

    标签: python-3.x face-detection opencv-python face


    【解决方案1】:

    您需要在引用.xml 文件之前添加cv2.data.haarcascades

    #[...]
    
    face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change
    
    #[...]
    

    你的代码还有一个问题,看看这段代码:

    resized=cv2.resize(img,(int(img.shape[1]/3)),(int(img.shape[0]/3)))
    

    您应该传递一个图像和它的新宽度和高度(以元组格式)作为参数 但是您将图像以及宽度和高度作为单独的整数而不是在 元组格式。

    它应该是这样的:

    resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 
    '''
    Notice I have removed an extra bracket after
    cv2.resize(img,(int(img.shape[1]/3)) -> cv2.resize(img,(int(img.shape[1]/3)...
    and also I have removed an extra bracket before
    (int(img.shape[0]/3))) -> int(img.shape[0]/3)))
    '''
    

    这应该可行:

    import cv2
    
    face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change
    
    img = cv2.imread("photo.jpg")
    gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    faces=face_cascade.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5)
    
    for x, y, w, h in faces:
        img=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
    
    resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 
    
    cv2.imshow("Deteced-face",resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 收到错误:来自您的代码的Traceback (most recent call last): File "c:\Users\ketan\Documents\Python\Face-dection\Facedectection.py", line 3, in &lt;module&gt; face_cascade=cv2.CascadeClassifier(cv2.data.haarcascade + "haarcascade_frontalface_default.xml") AttributeError: module 'cv2.data' has no attribute 'haarcascade'
    • 感谢@Ketan 指出我的错误,我已修复它,请查看更新后的答案并告诉我这是否适合您
    • 您好,在您更新代码后,我收到错误消息:error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
    • 不知道你怎么了,但我的代码运行良好
    • 只要确保文件扩展名与代码中的相同(.jpg
    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 2020-07-17
    • 2021-02-26
    • 2021-11-02
    • 2019-06-04
    • 2022-06-22
    • 2021-11-18
    • 2022-10-15
    相关资源
    最近更新 更多