0

使用 opencv 4.1.2 在 Pycharm (python 3.6) 上尝试标准人脸检测算法,但反复出现此错误:

faces = face_cascade.detectMultiScale(gray, 1.3, 5) cv2.error: OpenCV(4.1.2) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty () 在函数'detectMultiScale

代码:

import cv2

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

img = cv2.imread('face.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    img = 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()

也尝试提供 .xml 文件的完整位置,但错误仍然存​​在。试过:

import cv2

face_cascade = cv2.CascadeClassifier('home/PycharmProjects/helloworld/venv/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')

test=face_cascade.load('home/PycharmProjects/helloworld/venv/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')

print(test)
print(face_cascade)

返回:假

4

1 回答 1

0

提供 xml 的绝对路径。例如:

/home/path/to/.xml
于 2020-01-31T14:08:25.543 回答