1

I have been doing the traffic vehicle counter using Python and OpenCV. My current algorithm is counting the number of vehicles per frame. It results in the same vehicle is counting more than once per each frame. Instead, i want the Unique vehicle count in a video. Count each car only once. What technique i have to use to achieve this.

import cv2
print(cv2.__version__)

cascade_src = 'cars.xml'
video_src = 'dataset/video2.avi'
#video_src = 'dataset/video2.avi'

cap = cv2.VideoCapture(video_src)
car_cascade = cv2.CascadeClassifier(cascade_src)

while True:
    ret, img = cap.read()
    if (type(img) == type(None)):
        break

    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)      

    cv2.imshow('video', img)
    print "Found "+str(len(cars))+" car(s)"
    b=str(len(cars))
    a= float(b)
    if a>=5:
        print ("more traffic")
    else:
        print ("no traffic")    
    if cv2.waitKey(33) == 27:
        break

cv2.destroyAllWindows()
Ramineni Ravi Teja
  • 3,568
  • 26
  • 37
  • Use background subtraction [See this] (https://stackoverflow.com/questions/36254452/counting-cars-opencv-python-issue) – Pygirl Jan 16 '18 at 16:13

1 Answers1

0

1) Apply Background subtraction

2) Apply moments function to each frame to get the centroid of the moving cars

3) Define a region of pixel values(x,y) .When the centroid of moving car crosses this range ,increment the counter by one

Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • refer to this https://medium.com/machine-learning-world/tutorial-making-road-traffic-counting-app-based-on-computer-vision-and-opencv-166937911660 – Pygirl Jan 16 '18 at 17:15