0
votes

so basically I am writing a program in python using openCV that can detect cars and then turns the picture into a gray-scale image. However, it shows an error when I run the code.

This is the error

: line 19, in cars = car_tracker.detectMultiScale(black_n_white) cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Here is my code:

import cv2

#These are our images
img_file ='car.jpg'

#This will be the result of the algorithm after it is trained
classifier_file = 'car_detector.xml'

#create opencv image
img = cv2.imread(img_file)

#create a car classifier
car_tracker = cv2.CascadeClassifier(classifier_file)

#convert it to black and white first cuz it makes it 3x faster for the algorithm
black_n_white = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#detect cars
cars = car_tracker.detectMultiScale(black_n_white)

#display the image with car spotted
cv2.imshow('car detector', black_n_white)

#this makes it wait 
cv2.waitKey()
1
Use full paths for your files. - stateMachine

1 Answers

0
votes

The error is thrown since OpenCV cannot find your xml file, probably you wrote the wrong path. A good way to check whether your file was loaded properly is to first initialize the classifier (clf = cv2.CascadeClassifier()) and then check if clf.load(filename) returns True.