【问题标题】:'cv2.CascadeClassifier' object has no attribute 'detectMultiscale''cv2.CascadeClassifier' 对象没有属性 'detectMultiscale'
【发布时间】:2020-03-01 13:36:53
【问题描述】:

我只是想使用 haarcascade 检测面部和眼睛。但这是我收到的错误。

Traceback (most recent call last):   File "L:/Project/1", line 10, in <module>
    faces=face_cascade.detectMultiscale(gray,1.3,5) AttributeError: 'cv2.CascadeClassifier' object has no attribute

'detectMultiscale'

这是我在网上找到的代码

import numpy as np
import cv2

face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade=cv2.CascadeClassifier('haarcascade_eye.xml')

img=cv2.imread('download.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

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

【问题讨论】:

  • 使用detectMultiScale,大写S,而不是detectMultiscale。

标签: python


【解决方案1】:

正如@roganjosh 在问题的 cmets 中提到的,这里的问题是方法名称拼写不正确。正确的函数调用是:

face_cascade.detectMultiScale(...)

不同之处在于比例尺中的“S”是大写的。

【讨论】:

    【解决方案2】:

    一个类似的代码对我来说适用于两个 XML 文件的绝对路径。示例如下。

    1.从 [https://github.com/opencv/opencv/tree/master/data/haarcascades]

    下载 XML 文件

    2。提及 XML 文件的绝对路径(下面的示例)

    face_cascade = cv2.CascadeClassifier('C:\Python\haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('C:\Python\haarcascade_eye.xml')
    

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2016-01-21
      • 2018-02-20
      • 2014-05-08
      • 2020-06-04
      • 2019-08-14
      相关资源
      最近更新 更多