Skip to content
Advertisement

error: (-215:Assertion failed) !empty() in function CV2 Error

I am making a program in python that just draws a rectangle around a car. I am currently stuck on getting the coordinates of the car, here is the code:

#################################################
import cv2
#################################################
car_data = cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml")
img = cv2.imread("car_front.jpeg")
#################################################
img_but_bnw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

car_coordinates = car_data.detectMultiScale(img_but_bnw)
print(car_coordinates)

#################################################
cv2.imshow("Detect Everything", img_but_bnw)
cv2.waitKey()
print("Code Completed")
#################################################

I am running in on an error with the function “cv2.detectMultiScale”. error:

File "e:Python2Body_Detection.py", line 11, in <module>
    car_coordinates = car_data.detectMultiScale(img_but_bnw)
cv2.error: OpenCV(4.6.0) D:aopencv-pythonopencv-pythonopencvmodulesobjdetectsrccascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

I have tried googling, it says to use cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml") instead of cv2.CascadeClassifier("cars.xml"). It didn’t work :(, Any help would be appreciated.

Advertisement

Answer

The file cars.xml is not part of the opencv library although you may find tutorials in the internet that use this filename. The folder addressed by cv2.data.haarcascades includes xml examples for things like eye and face detection (current content see https://github.com/opencv/opencv/tree/master/data).

You can search for an existing cars.xml example by other authors and copy it to your project folder. Then just use "cars.xml" without cv2.data.haarcascades.

E.g. I found this project Vehicle Detection with Haar Cascades that includes a file cars.xml working fine with your code above.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement