forked from gayensouvik1/traffic-congestion-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcongestion3.py
75 lines (45 loc) · 1.35 KB
/
congestion3.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cv2
import time
print(cv2.__version__)
cascade_src = 'cars.xml'
car_cascade = cv2.CascadeClassifier(cascade_src)
import glob
video_list = glob.glob("dataset/video2.avi")
count_car = 0
res = 0
def mark_cars(cars,img):
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
def show_result(img):
cv2.imshow('video', img)
global res
res = res+1
src = "result1/"+str(res)+".jpg"
import scipy.misc
scipy.misc.imsave(src, img)
# img.save("result/"+str(res)+".jpg")
def congested(count_car):
if count_car<8:
return "Low"
elif count_car<16:
return "Medium"
else:
return "High"
def detect(video_src):
cap = cv2.VideoCapture(video_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)
mark_cars(cars,img)
count_car = len(cars)
cv2.putText(img,str(count_car)+"->"+congested(count_car), (20,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255),2)
show_result(img)
if cv2.waitKey(33) == 27:
break
cv2.destroyAllWindows()
for my_video_src in video_list:
detect(my_video_src)
time.sleep(5)