Raspberry Pi : OpenCV Car Counter
Raspberry Pi OpenCV Car Counter is a project to count vehicles or moving objects based on video
feed from the road. Red dots will be used to count the cars, Green line will be used to as the
trajectory and will be the counter, while the Blue writings will be the counter.
We will use OpenCV and Imutils and here are the codings :
$ sudo apt-get install python-opencv
$ sudo pip install imutils
Python car-counter.py
import
cv2
backsub = cv2.BackgroundSubtractorMOG() #background subtraction to isolate moving
cars
capture = cv2.VideoCapture("/home/pi/Downloads/video.mp4")
counter = 0
x = 0
y = 0
sen = 0
minArea=1
lineCount = 50
if capture.isOpened():
ret, frame = capture.read()
else:
ret = False
while ret:
ret, frame = capture.read()
if ret==False:
break
fgmask = backsub.apply(frame, None, 0.01)
erode=cv2.erode(fgmask,None,iterations=3) #erosion to erase unwanted small
contours
moments=cv2.moments(erode,True) #moments method applied
area=moments['m00']
if moments['m00'] >=minArea:
x=int(moments['m10']/moments['m00'])
y=int (moments['m01']/moments['m00'])
if x<lineCount:
sen=sen<<1
else:
sen=(sen<<1)|1
sen=sen&0x03
if sen==1:
counter=counter+1
cv2.circle(frame,(x,y),5,(0,0,255),-1) #center, radius, colour, -1=fill
cv2.line(frame,(lineCount,0),(lineCount,200),(0,255,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,'counter='+str(counter), (10,30),font,1, (255, 0, 0), 2)
#cv2.putText(frame,'x='+str(x)+' y='+str(y), (10,60),font,1, (255, 0, 0), 2)
cv2.imshow("counter", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
http://shahrulnizam.com/raspberry-pi-lesson-opencv-car-counter/
Raspberry Pi Traffic Analyzer
Hardwares Needed :
Raspberry Pi 2 Model B ARM v7 with 1 GB RAM
5 V 2 A switching Power Supply with MicroUSB cable
Logitech HD Pro C920
Monoprice flexible mini tripod
WiPi for internet connectivity
https://www.mathworks.com/matlabcentral/fileexchange/52456-analyzing-traffic-using-a-webcam-
a-raspberry-pi-and-thingspeak
other references :
https://www.instructables.com/id/Image-Recognition-With-TensorFlow-on-Raspberry-Pi/
https://www.youtube.com/watch?v=npZ-8Nj1YwY
https://towardsdatascience.com/real-time-car-pedestrian-lane-detection-using-tensorflow-object-
detection-api-and-an-ios-fbb1b7cbb157
https://towardsdatascience.com/track-vehicles-and-people-using-yolov3-and-tensorflow-
4f3d0e5b1b5f
https://www.pyimagesearch.com/2019/12/02/opencv-vehicle-detection-tracking-and-speed-
estimation/
http://shahrulnizam.com/raspberry-pi-lesson-opencv-car-counter/
https://www.mathworks.com/matlabcentral/fileexchange/52456-analyzing-traffic-using-a-webcam-
a-raspberry-pi-and-thingspeak
https://www.mathworks.com/videos/programming-raspberry-pi-with-simulink-87003.html?
type=shadow