0

I've been trying to run a program that detects when you blink and gives a message in the console in response to a blink. It has run successfully on a different laptop before but has been causing errors on my current laptop. I'm not sure what the errors mean. I've tried looking at similar posts but I haven't been able to figure it out yet. I'm a bit new to this area. Could you help me out? Following are the errors, the versions of the programs and the code. Thank you!

Errors: Traceback (most recent call last): File "C:...\PycharmProjects\PersonalProjects\main.py", line 19, in faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(200, 200)) cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

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

Versions Python 3.9, Pycharm 2021.2, Opencv-python 4.5.3.56, numpy 1.21.2,

Code:

import numpy as np
import cv2

#initializiing Face and eye cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')

#Variable store execution state
first_read = True
cap = cv2.VideoCapture(0)
ret ,img = cap.read()
while(ret):
    ret, img = cap.read()
    #Coverting recorded image to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #Applying filter to remove impurities
    gray = cv2.bilateralFilter(gray, 5, 1, 1)
    #Detecting the face for region of image to be fed to eye classifier
    faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(200, 200))
    if(len(faces) > 0):
        for (x, y, w, h) in faces:
            img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0),2)
            #rol_face is face fed to eye classifier
            rol_face = gray[y:y+h, x:x+w]
            rol_face_clr = img[y:y+h, x:x+w]
            eyes = eye_cascade.detectMultiScale(rol_face, 1.3, 5, minSize=(50, 50))
            if(len(eyes) >= 2):
                #Check if program is running for detection
                if(first_read):
                    cv2.putText(img,"Eye detected, press s to begin", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0,255,0), 2)
                else:
                    cv2.putText(img, "Eyes open!", (70, 70), cv2.FONT_HERSHEY_PLAIN, 2, (255,255,255), 2)
            else:
                if(first_read):
                    #to ensure if eyes are present before starting
                    cv2.putText(img, "No eyes detected", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 2)
                else:
                    #This will print on console and restart algorithm
                    print("Blink detected -- do you want to continue; press y")
                    cv2.waitKey(3000)
                    first_read = True
    else:
        cv2.putText(img, "No face detected", (100, 100), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2)
    cv2.imshow('img', img)
    a = cv2.waitKey(1)
    if(a==ord('q')):
        break
    elif(a==ord('s') and first_read):
        #This will start the detection
        first_read = False
cap.release()
cv2.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Iris S.
  • 11
  • 2
  • Does this answer your question? [Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale](https://stackoverflow.com/questions/30857908/face-detection-throws-error-empty-in-function-cvcascadeclassifierdetectm) – Christoph Rackwitz Jul 25 '22 at 02:14
  • your cascade was not loaded. check path – berak Jul 25 '22 at 07:24

0 Answers0