Ask Your Question
0

how to solve (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

asked 2020-09-24 10:01:27 -0600

updated 2020-09-24 10:37:42 -0600

berak gravatar image

please help me i got this error.


error                                     Traceback (most recent call last)
<ipython-input-27-8f798bcb3e24> in <module>
     19 
     20     rerect_size = cv2.resize(im, (im.shape[1] // rect_size, im.shape[0] // rect_size))
---> 21     faces = haarcascade.detectMultiScale(rerect_size)
     22     for f in faces:
     23         (x, y, w, h) = [v * rect_size for v in f]

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-9d_dfo3_\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

this is my code

import cv2
import numpy as np
from tensorflow.keras.models import load_model
model=load_model("E:\Data Science\Mask detector\model2-008.model")

results={0:'without mask',1:'mask'}
GR_dict={0:(0,0,255),1:(0,255,0)}

rect_size = 4
cap = cv2.VideoCapture(0) 


haarcascade = cv2.CascadeClassifier('/home/user_name/.local/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')

while True:
    (rval, im) = cap.read()
    im=cv2.flip(im,1,1) 


    rerect_size = cv2.resize(im, (im.shape[1] // rect_size, im.shape[0] // rect_size))
    faces = haarcascade.detectMultiScale(rerect_size)
    for f in faces:
        (x, y, w, h) = [v * rect_size for v in f] 

        face_img = im[y:y+h, x:x+w]
        rerect_sized=cv2.resize(face_img,(150,150))
        normalized=rerect_sized/255.0
        reshaped=np.reshape(normalized,(1,150,150,3))
        reshaped = np.vstack([reshaped])
        result=model.predict(reshaped)


        label=np.argmax(result,axis=1)[0]

        cv2.rectangle(im,(x,y),(x+w,y+h),GR_dict[label],2)
        cv2.rectangle(im,(x,y-40),(x+w,y),GR_dict[label],-1)
        cv2.putText(im, results[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)

    cv2.imshow('LIVE',   im)
    key = cv2.waitKey(10)

    if key == 27: 
        break

cap.release()

cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-09-24 10:44:53 -0600

berak gravatar image

your program is running on windows, but you try to use a linux path to the cascade xml file, so it was not loaded.

try to replace the

/home/user_name/.local/lib/python3.6/

part with the path to the python version you really use here. (and ofc. check if the file is there!)

you'll get this error, until you get the path to the haarcascade_frontalface_default.xml right.

edit flag offensive delete link more

Comments

thanks, solved it

tortilla gravatar imagetortilla ( 2020-09-25 06:54:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-24 10:01:27 -0600

Seen: 12,357 times

Last updated: Sep 24 '20