OpenCV车辆检测

时间:2019-11-21 17:38:29

标签: python opencv image-processing

我正在尝试实施一个程序来识别视频/图片中的车辆。主要要求是性能(几乎是实时的),因此我放弃了ImageAI和使用Keras-VGG16进行额外训练的模型。

OpenCV为我提供了所需的性能,但是却远远不能接受:虽然有时效果很好,但在下一帧失败了。

我正在考虑可能的选择: A)用我自己的图像重新训练模型 b)使用一些图像预处理 ...?

非常感谢您的帮助或线索。

我的代码:

import cv2
import os
car_cascade = cv2.CascadeClassifier('cars3.xml')

#input_path = ...
files = os.listdir(input_path)

for f in files:
    input_file = input_path + '/' + f;
    print(input_file)
    img = cv2.imread(input_file, 1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    cars = car_cascade.detectMultiScale(gray, 1.1, 1)
    for (x, y, w, h) in cars:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 2)
        ncars = ncars + 1
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

示例:

Example of good and bad result

1 个答案:

答案 0 :(得分:0)

您是否尝试过dnn对象检测模块?

https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

您可以使用几种可用的模型架构来权衡速度和准确性。

就测量速度而言,尝试执行多个推断并在以后的推断中测量速度,通常第一个速度较慢,因为它正在加载模型。

以下是在视频上运行的一些示例代码:

https://github.com/opencv/opencv/blob/master/samples/dnn/object_detection.py

如果硬件支持,则可以设置后端以进一步提高速度。我看到的最好的性能是来自今年GSOC的nvidia后端:

https://github.com/opencv/opencv/pull/14827