0

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()
furas
  • 134,197
  • 12
  • 106
  • 148
altherian
  • 1
  • 1
  • 1
    Try to use absolute path in cv2.CascadeClassifier('cascade.xml') or make sure that cascade.xml is present/available in your working directory. – Micka May 17 '22 at 18:26
  • i dont know if it makes a difference, but on my tests i use the incorporate camera of my notebook, but my partner use an external one – altherian May 17 '22 at 19:17
  • always put full error message (starting at word "Traceback") in question (not in comments, not it title) as text (not screenshot, not link to external portal). There are other useful information. – furas May 17 '22 at 20:51

2 Answers2

0

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.

Gabriel Pellegrino
  • 1,042
  • 1
  • 8
  • 17
  • the folder structure used by my partner is: c:\Users\OneDrive\Escritorio\P.E.D.R.O\Detector de Objetos y Colores (that's where he put them). I use this one: C:\Users\Ivan\Desktop\Cosas Iván\Python Project – altherian May 17 '22 at 19:10
  • Inside your folder `C:\Users\Ivan\Desktop\Cosas Iván\Python Project` you have your python script and the cascade.xml file? What about your friend? Does he have a cascade.xml file inside the folder `C:\Users\OneDrive\Escritorio\P.E.D.R.O\Detector de Objetos y Colores`? – Gabriel Pellegrino May 17 '22 at 21:05
  • There were 2 reasons why it didn't work. the number 0 in "cap=(cv2.VideoCapture(0,cv2.CAP_DSHOW)" indicated which port number the camera was connected and the other problem was that both files were not in the same directory. – altherian May 21 '22 at 00:08
0

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') )
furas
  • 134,197
  • 12
  • 106
  • 148
  • There were 2 reasons why it didn't work. the number 0 in: "cap=(cv2.VideoCapture(0,cv2.CAP_DSHOW)" indicated which port number the camera was connected and the other problem was that both files were not in the same directory. – altherian May 21 '22 at 00:09