OpenCV 错误(-215:断言失败)!empty() [重复]

问题描述 投票:0回答:2

我有以下代码:

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 
                                     'haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 
                                    'opencv_haarcascade_eye.xml')

img = cv2.imread('lena.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray)

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)

k = cv2.waitKey(0)

if k == 27:     
    
    cv2.destroyAllWindows()

elif k == ord('s'):

    cv2.imwrite('frame',img)

    cv2.destroyAllWindows()

运行它会产生以下错误:

eyes = eye_cascade.detectMultiScale(roi_gray)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale' [ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

当我删除

eyes = eye_cascade.detectMultiScale(roi_gray)
部分时,代码在最喜欢的检测中工作正常,但在
eyes = eye_cascade.detectMultiScale(roi_gray)
部分中显示错误。

如何解决这个问题?

python opencv image-processing face-detection eye-detection
2个回答
0
投票

错误发生在分类器加载过程中。应该是

eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 
                                    'haarcascade_eye.xml')

而不是

eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 
                                    'opencv_haarcascade_eye.xml')

至少,这个解决方案显示在here,并且我用OpenCV(4.5.1)测试了它


-1
投票

根据这个话题关于

roi_gray
解释

for (x,y,w,h) in faces:
    roi_gray=gray[y:y+h,x:x+w]    #This particular code will return the cropped face from the image.
    roi_color = img[y:y+h, x:x+w] #This particular code will return the details of the image that u will recive after getting the co-ordinates of the image.

您确定图像中有一张脸吗?

roy_gray
从图像中返回裁剪后的脸部,但如果没有,我认为您可能会收到错误。

© www.soinside.com 2019 - 2024. All rights reserved.