Home > Back-end >  i got this error : (-215:Assertion failed) !empty() in function 'cv: :CascadeClassifier: :detec
i got this error : (-215:Assertion failed) !empty() in function 'cv: :CascadeClassifier: :detec

Time:05-18

I am trying to create with my partrners a semaphore detector, but on my partner's pc it gives the following problem:

exception has ocurred: error x 
OpenCV(4.5.5) D:\a\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv: :CascadeClassifier: :detectMultiScale'
File "C:\Users\marce\OneDrive\Escritorio\P.E.D.R.O\Detector de Objetos y Coloes\version_final_detector.py", line 17, in <module> toy=semaforoClassif.detectMultiScale(frameHSV,scaleFactor=5,minNeighbors=400,minSize=(70,78))

the complete code is:

import cv2
import numpy as np
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)

verdeBajo = np.array([40, 170, 170],np.uint8)
verdeAlto = np.array([85, 255, 255],np.uint8)
redBajo1 = np.array([0,170,170],np.uint8)
redAlto1 = np.array([10,255,255],np.uint8)
redBajo2 = np.array([173,170,170],np.uint8)
redAlto2 = np.array([179,255,255],np.uint8)
semaforoClassif = cv2.CascadeClassifier('cascade.xml')
while True:
    ret,frame = cap.read()
    if ret==True:   
        frameHSV = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        toy = semaforoClassif.detectMultiScale(frameHSV,
        scaleFactor = 5,
        minNeighbors = 400,
        minSize=(70,78))
        for (x,y,w,h) in toy:
            cv2.rectangle(frame, (x,y),(x w,y h),(255,0,0),2)
            cv2.putText(frame,'Semaforo',(x,y-10),2,0.7,(255,0,0),2,cv2.LINE_AA)
            rectangulo=cv2.rectangle(frame, (x,y),(x w,y h),(255,0,0),2)
            frameHSV2 = cv2.cvtColor(rectangulo,cv2.COLOR_BGR2HSV)
            maskRed1 = cv2.inRange(frameHSV2,redBajo1,redAlto1)
            maskRed2 = cv2.inRange(frameHSV2,redBajo2,redAlto2)
            mask = cv2.inRange(frameHSV2,verdeBajo,verdeAlto)
            contornos,hierachy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            contornosr1,hierarchy2=cv2.findContours(maskRed1,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            contornosr2,hierarchy3=cv2.findContours(maskRed2,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            cv2.drawContours(rectangulo, contornosr1, -1, (0,0,255), 0)
            cv2.drawContours(rectangulo, contornosr2, -1, (0,0,255), 0)
            cv2.drawContours(rectangulo, contornos, -1, (0,255,0), 0)   
        cv2.imshow('maskGreen',mask)
        cv2.imshow('frame',frame)
        cv2.imshow('maskRed1', maskRed1)
        cv2.imshow('maskRed2', maskRed2)
        if cv2.waitKey(1) & 0xFF == ord('c'):
            break 
        
cap.release()
cv2.destroyAllWindows()

CodePudding user response:

I suspect that you are not loading your cascade.xml file correctly. What is your folder structure? Maybe try using the complete path of this file 'cascade.xml'. If the script is executing in the same folder as this file is, then it should be fine. If the file 'cascade.xml' isn't present on the same folder, then it must be it.

CodePudding user response:

If cascade.xml is a file installed with cv2 then you may need to add path to folder with .xml

cv2 has cv2.data.haarcascades with path to this folder and you may have to use

cv2.CascadeClassifier( os.path.join(cv2.data.haarcascades, 'cascade.xml') )

You can also use it to check if you have this file in this folder

for filename in os.listdir( cv2.data.haarcascades ):
    print(filename)

And if this is your private file .xml in folder with code then you may have to use /full/path/to/cascade.xml because code may run in different folder (in different Current Working Directory which you can see with os.getcwd()) and it can search file in wrong place.

OR you can create path to folder with code and later add it to cascade.xml

BASE = os.path.dirname(os.path.abspath(__file__))

cv2.CascadeClassifier( os.path.join(BASE, 'cascade.xml') )
  • Related