【问题标题】:Why do I get a error in openCV ::CascadeClassifier::detectMultiScale?为什么我在 openCV ::CascadeClassifier::detectMultiScale 中出现错误?
【发布时间】: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()

【问题讨论】:

  • 使用文件的完整路径。

标签: python opencv


【解决方案1】:

由于 OpenCV 找不到您的 xml 文件而引发错误,可能是您写入了错误的路径。检查文件是否正确加载的一个好方法是首先初始化分类器 (clf = cv2.CascadeClassifier()),然后检查 clf.load(filename) 是否返回 True。

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 2022-06-22
    • 2021-11-02
    • 2021-04-15
    • 2016-09-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多