【发布时间】:2020-11-27 07:09:02
【问题描述】:
所以基本上我是在 python 中使用 openCV 编写一个程序,它可以检测汽车,然后将图片转换为灰度图像。但是,当我运行代码时,它会显示错误。
这是错误
:第 19 行,在 汽车 = 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'
这是我的代码:
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()
【问题讨论】:
-
使用文件的完整路径。