-2

I am trying to do face and eye recognition with rectangles for eyes and face using OpenCv Python. I read some of the questions and I tried the answers below the questions on similiar topics but still getting the error. The code that i've tried activates the webcam and in a second it stops working.

Here the error message:

error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Here my code that i have tried:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye_default.xml')


while True:
    ret, frame = cap.read()
    
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
        roi_gray = gray[y:y+w, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_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), 5)
            
        
        
    cv2.imshow('frame', frame)
    
    if cv2.waitKey(1) == ord('q'):
        break
    
cap.release()

cv2.destroyAllWindows()

Thank you.

Edit: I noticed that once I run the code the webcam has been activated and if there is no face in front of the camera it stays active without any error but once I show my face it stops working.

Arda Kandemir
  • 49
  • 1
  • 1
  • 7
  • 1
    [error: (-215) !empty() in function detectMultiScale](https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale) maybe this old discussion can help? seems like the XML path might be the problem – ozerodb Apr 13 '22 at 16:42

1 Answers1

0

Hope this will work !

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")


while True:
    ret, frame = cap.read()
    
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
        roi_gray = gray[y:y+w, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_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), 5)
            
        
        
    cv2.imshow('frame', frame)
    
    if cv2.waitKey(1) == ord('q'):
        break
    
cap.release()

cv2.destroyAllWindows()
Parthiban Marimuthu
  • 665
  • 1
  • 8
  • 15
  • Thank you for your quick response. I tried your suggestion and the webcam light is on but the frame window doesn't appear and the error code remains the same. – Arda Kandemir Apr 13 '22 at 11:37
  • @ArdaKandemir hope you are referring the xml files from the correct path or make sure they are placed within the same folder as the python script – Jeru Luke Apr 13 '22 at 14:49
  • My bad, I thought it needs to be stay in haarcascade folder. I appreciate it thank you sir. – Arda Kandemir Apr 15 '22 at 10:45