简体   繁体   English

有条件的开启 CV 汽车检测

[英]Open CV Car Detection with conditions

I'm attempting to create a program in which my code analyses a video from my security camera and locates the cars that have been spotted.我正在尝试创建一个程序,在该程序中我的代码分析来自我的安全摄像头的视频并定位被发现的汽车。 I've figured out how to find the cars and draw red rectangles around them, but I'd like to add a condition that only draws boxes if there are more than 5 cars detected.我已经弄清楚如何找到汽车并在它们周围绘制红色矩形,但我想添加一个条件,如果检测到超过 5 辆汽车,则仅绘制框。 However, I am unable to do so due to the presence of arrays.但是,由于数组的存在,我无法这样做。 How would I go about fixing this code?我将如何修复此代码?


import cv2

classifier_file = 'cars.xml'

car_tracker = cv2.CascadeClassifier(classifier_file)

video = cv2.VideoCapture("footage.mp4")


while True:
    (read_successful, frame) = video.read()

    if read_successful:
        grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    else:
        break
    
    cars = car_tracker.detectMultiScale(grayscaled_frame)

    if cars > 4:
        for (x, y, w, h) in cars:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)


    cv2.imshow('Car Detector', frame)
    cv2.waitKey(0)

By the way, I am using an anaconda environment along with python 3.8.8顺便说一句,我正在使用 anaconda 环境和 python 3.8.8

Here is one example using a different technique, in this case you can use cvlib it has a class object_detection to detect multiple classes.这是使用不同技术的一个示例,在这种情况下,您可以使用 cvlib 它有一个类object_detection来检测多个类。 This uses the yolov4 weights, you will be capable of detect any car in the road or in this case from one webcam, only make sure that the car are ahead the camera not in 2d position.这使用 yolov4 权重,您将能够检测道路上的任何汽车,或者在这种情况下通过一个网络摄像头,只需确保汽车在摄像头前面而不是在 2d 位置。

import cv2
import matplotlib.pyplot as plt
# pip install cvlib
import cvlib as cv
from cvlib.object_detection import draw_bbox


im = cv2.imread('cars3.jpg')

#cv2.imshow("cars", im)
#cv2.waitKey(0)

bbox, label, conf = cv.detect_common_objects(im) 
#yolov4 weights will be downloaded. Check: C:\Users\USER_NAME\.cvlib\object_detection\yolo
# if the download was not successful delete yolo folder and try again, it will be downloaded again
# after download the weights keep running the script it will say 0 cars, then close Anaconda spyder anda reopen it and try again.

#get bbox
output_image_with_bbox = draw_bbox(im, bbox, label, conf)


number_cars = label.count('car')
if number_cars > 5:
    print('Number of cars in the image is: '+ str(number_cars))
    plt.imshow(output_image_with_bbox)
    plt.show()
else:
    print('Number of cars in the image is: '+ str(number_cars))
    plt.imshow(im)
    plt.show()

output:输出:

在此处输入图片说明

Greetings.问候。

You have an array of detections.您有一系列检测。

All you need is to call len() on the array to get the number of elements.您只需要在数组上调用len()即可获取元素数。 This is a built-in function of Python.这是 Python 的内置函数。

cars = car_tracker.detectMultiScale(grayscaled_frame)

if len(cars) >= 5:
    for (x, y, w, h) in cars:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

No need for any other libraries.不需要任何其他库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM