首页 > 解决方案 > harcascadePath = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'

问题描述

我正在尝试使用 haarcascade 进行面部和眼睛检测。运行时抛出 harcascadePath = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' 错误。错误不是针对脸,而是针对眼睛。

import cv2
import numpy as np

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


cap=cv2.VideoCapture(r"C:\Users\intel\Downloads\face-demographics-walking-and-pause.mp4")

while True:
    ret,img=cap.read()
    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),3)
        roi_gray=gray[y:y+h,x:x+w]
        roi_color=img[y:y+h,x:x+w]
        eyes=eyes_cascade.detectMultiscale(roi_gray, 1.3, 5)
        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(30) and 0xff
    if k==27:
        break

cap.release()
cv2.destroyAllWindows()

错误是

AttributeError: 'cv2.CascadeClassifier' object has no attribute 'detectMultiscale'
PS C:\Users\intel\Documents\code\python>

标签: pythonopencvcomputer-visionattributeerrorface-recognition

解决方案


您的代码的错误应该在第 13 行。“detectMultiscale”您必须将第一个字母“scale”写成大写。

检测多尺度

我希望它有帮助!


推荐阅读