【问题标题】:detectMultiScale function cannot be found in cv2?cv2 中找不到detectMultiScale 函数?
【发布时间】:2021-04-10 21:25:00
【问题描述】:

第一次发帖。

我目前正在尝试在 M1 macbook air 上使用虚拟工作室在 python 中使用 opencv2,但由于某种原因我不能使用 detectMultiScale 函数?我已完全卸载所有内容并重新安装所有内容均无济于事。 error: (-215) !empty() in function detectMultiScale

例如: grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

face_coordinates = train_face_data.detectMultiScale(grayscaled_img)

了解我

    face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
cv2.error: OpenCV(4.5.1) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-39p1qqfs/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

另外,我对使用 python 完全陌生,所以我有一个快速的问题是我是否需要创建一个虚拟环境,激活它,然后在那里 pip 下载 opencv2?我尝试将 cv2 下载到我的 python 文件的终端中,但我无法在那里导入 cv2。我在想可能是因为文件路径不同?但我不知道如何将 pip 安装程序用于某个文件路径。我只能在 venv 中安装 cv2 后才能导入 cv2。

请指教,谢谢!

在问这个问题之前我已经试过了:

import cv2

#trained face data import
trained_face_data = cv2.CascadeClassifier('frontfacedetector.xml')

#import my face
img = cv2.imread('Aaron_Prof_Pic.jpg')

#change to grayscale
grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#detecting faces
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)

cv2.imshow('why isnt this working', grayscaled_img)
cv2.waitKey()

print("Code completed")

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    这意味着haarcascade_frontalface_default.xml 与代码不在同一目录中,或者它已损坏。

    如果它在同一目录中,请尝试使用

    trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_default.xml")
    

    最终的代码应该有点像:

    import cv2
    trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_default.xml")
    img = cv2.imread('Aaron_Prof_Pic.jpg')
    imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = trained_face_data.detectMultiScale(imgGray)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0,255,0), 2) 
    cv2.imshow("Face detector", img)
    key_press = cv2.waitKey(1)
    if key_press == 32: #The ascii code for spacebar
        quit()#quits the programme when spacebar is hit
    

    【讨论】:

    • 对不起,我打错了,我实际上有 .在真实的代码中。问题是 detectMultiScale 没有被检测为函数
    • 这意味着 haarcascade 文件不在同一个文件夹中,或者它已损坏
    • 你从cv2的官方github仓库下载了.xml吗?如果是,请查看我的答案
    • 尝试在您的 IDE 上运行最终代码(我在回答中提到过),看看是否可行
    • 成功了!你做了什么?对不起,我不明白您的代码与我的代码之间的概念,我想学习以便提高我的技能。谢谢!
    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 2021-08-08
    • 2019-06-04
    • 2020-12-04
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多