简体   繁体   English

我找不到 detectMultiScale() 错误的解决方案

[英]I can't find the solution to detectMultiScale() error

I am using Windows7 and PyCharm 2019.3 and I've tried everything I knew but I'm unable to rectify this error.我使用的是 Windows7 和 PyCharm 2019.3,我已经尝试了我所知道的一切,但我无法纠正这个错误。 Kindly Help.请帮助。

Error:错误:

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1658: error: (-215:Assertion failed):empty() in function 'cv::CascadeClassifier::detectMultiScale' cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1658: 错误: (-215:Assertion failed): empty() in function 'cv ::CascadeClassifier::detectMultiScale'

Code:代码:


import numpy as np
import cv2
import os
from matplotlib import pyplot as plt

img = r'C:\Users\Vinay\Desktop\picture.jpg'
cascade_path = os.path.dirname(os.path.abspath(__file__))
face_cascade = cv2.CascadeClassifier(cascade_path+r'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cascade_path+r'haarcascade_eye.xml')

img = cv2.imread(img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Picture", gray)
cv2.waitKey(500)

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()

For anyone who comes across this question, I'd like to help you since I did find the solution to this.对于遇到这个问题的任何人,我都想帮助你,因为我确实找到了解决方案。 I just copied the required.xml files in the same directory I was working.我只是将 required.xml 文件复制到我正在工作的同一目录中。 This simple step cleared the error.这个简单的步骤清除了错误。 The reason behind it is that the interpreter was having trouble finding the classifiers even after specifying the path.其背后的原因是解释器即使在指定路径后也无法找到分类器。 Also, you can try dropping the cascade_path line and directly mention the path in the face_cascade line.此外,您可以尝试删除 cascade_path 行并直接在 face_cascade 行中提及路径。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM