简体   繁体   中英

i am getting an error in my face recognition code while excuting it in pycharm, have also tried executing it on terminal

My face recognition code is getting and error. The error is as follows:

Traceback (most recent call last):
  File "C:/Users/Harsh/Desktop/python codes/main.py", line 19, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xeqjxthj\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xeqjxthj\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

the code is as follows:

import cv2


face_cascade = \
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_frontalface_alt.xml')

eye_cascade = \
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    if ret is True:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    else:
            continue

    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+h]
        roi_color = img[y:y+h, x:x+h]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew), (ex+ew, ey+eh), (0, 255, 0), 2)
    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xFf

    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

That error means that your xml file could not be found.

You got some typos here:

face_cascade =
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_frontalface_alt.xml')

eye_cascade =
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_eye.xml')

Notice the slash (/) at the beginning of the path. Correct that and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM