-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar_Counting.py
38 lines (27 loc) · 1.03 KB
/
Car_Counting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
import numpy as np
vid = cv2.VideoCapture("12_Car_Counting/traffic.avi")
backsubtractor=cv2.createBackgroundSubtractorMOG2()
i = 0
while True:
ret,frame = vid.read()
if ret:
fgmask = backsubtractor.apply(frame)
cv2.line(frame,(50,0),(50,300),(0,255,0,2))
cv2.line(frame,(70,0),(70,300),(0,255,0,2))
contours,hierarchy = cv2.findContours(fgmask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
try: hierarchy = hierarchy[0]
except:hierarchy=[]
for contour,hier in zip(contours,hierarchy):
(x,y,w,h)=cv2.boundingRect(contour)
if w>40 and h>40:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
if x>50 and x<70:
i+=1
cv2.putText(frame,"car"+str(i),(90,100),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),2,cv2.LINE_AA)
##cv2.imshow("Car Counting",fgmask)
cv2.imshow("Car Counting",frame)
if cv2.waitKey(10) & 0xFF==ord("q"):
break
vid.release()
cv2.destroyAllWindows()