错误:函数“ detectMultiScale”中的!empty() [英] ERROR: !empty() in function 'detectMultiScale'

查看:489
本文介绍了错误:函数“ detectMultiScale”中的!empty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下小代码:

from google.colab import drive
from IPython.display import display
import PIL
from PIL import Image, ImageDraw
import kraken
from kraken import pageseg
import cv2 as cv

img = Image.open("/content/drive/My Drive/images/dropfire.jpg")

face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_frontalface_default.xml")
eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_eye.xml")

file_name = "/content/drive/My Drive/images/dropfire.jpg"
img = cv.imread(file_name)
pil_img = Image.open(file_name)
cv_img = pil_img.convert('L')
cv_img = cv.imread(file_name)

faces = face_cascade.detectMultiScale(cv_img)

当我运行最后一个单元格时( code> faces ),它引发:

When I run the last cell (with faces), it raises:

error                                     Traceback (most recent call last)
<ipython-input-23-2bd7582f8a20> in <module>()
----> 1 faces = face_cascade.detectMultiScale(cv_img)

error: OpenCV(4.1.2) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

中,直到该单元格一切正常。根据StackOverflow 答案,我添加了 cv.data.haarcascades + cv.CascadeClassifier 的括号中。尽管人们说这对他们有用,但对我却不起作用...

Until this cell everything works fine. According to a StackOverflow answer, I added cv.data.haarcascades + in the brackets of cv.CascadeClassifier. Although people said this worked for them, it doesn't for me somehow...

任何帮助都是值得的。

推荐答案

imho,小代码您已经有太多的噪音(不必要的代码行)。
我认为这会使您更难以理解发生了什么。

imho, that "little code" of yours has too much noise (unnecessary lines of code) already. I think it makes for you even harder to understand what's going on.

注释掉/删除除以下内容以外的所有行:

Comment out/delete all the lines except these:

    import cv2 as cv
    
    face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
    eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_eye.xml")

    file_name = "/content/drive/My Drive/images/dropfire.jpg"
    img = cv.imread(file_name)  # this reads the image already
    
    cv_img = cv.imread(file_name) # this reads same image once more, not sure if intended
    faces = face_cascade.detectMultiScale(cv_img)  

如果您的图片 dropfire确实存在于该路径上,这应该没有错误,但不会显示任何人脸检测结果。您可以在末尾添加以下行以查看结果:

If your image "dropfire" really exists at that path, this should work without error, but it won't show any results of face detection. You can add these lines at the end to see the result:

    for (x, y, w, h) in faces:
        cv.rectangle(cv_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
    cv.imshow("Nice face", cv_img)
    cv.waitKey(0)

cv.data.haarcascades已经具有所有这些cv2 xml文件的路径,因此您只需使用文件名。

cv.data.haarcascades already has path to all those cv2 xml files, so you only need to use file names.

这篇关于错误:函数“ detectMultiScale”中的!empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆