-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.py
74 lines (61 loc) · 2.25 KB
/
speed.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
74
import cv2
import time
cascade_src = 'dataset/cars1.xml'
video_src = 'dataset/3.MP4'
def Speed_Cal(time):
#Here i converted m to Km and second to hour then divison to reach Speed in this form (KM/H)
#the 9.144 is distance of free space between two lines # found in https://news.osu.edu/slow-down----those-lines-on-the-road-are-longer-than-you-think/
#i know that the 9.144 is an standard and my video may not be that but its like guess and its need Field research
try:
Speed = (9.144*3600)/(time*1000)
return Speed
except ZeroDivisionError:
print (5)
#car num
i = 1
start_time = time.time()
#video ....
cap = cv2.VideoCapture(video_src)
car_cascade = cv2.CascadeClassifier(cascade_src)
while True:
ret, img = cap.read()
height, width, x = img.shape
#line a
ax1=0
ay=int(3*height/5)
ax2=width
#line b
bx1=0
by=ay+100
bx2=width
if (type(img) == type(None)):
break
#bluring to have exacter detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.1, 2)
#line a #i know road has got
cv2.line(img,(ax1,ay),(ax2,ay),(255,0,0),2)
#line b
cv2.line(img,(bx1,by),(bx2,by),(255,0,0),2)
for (x,y,w,h) in cars:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.circle(img,(int((x+x+w)/2),int((y+y+h)/2)),1,(0,255,0),-1)
while int(ay) == int((y+y+h)/2):
start_time = time.time()
break
while int(ay) <= int((y+y+h)/2):
if int(by) <= int((y+y+h)/2)&int(by+10) >= int((y+y+h)/2):
cv2.line(img,(bx1,by),(bx2,by),(0,255,0),2)
Speed = Speed_Cal(time.time() - start_time)
print("Car Number "+str(i)+" Speed: "+str(Speed))
i = i + 1
cv2.putText(img, "Speed: "+str(Speed)+"KM/H", (x,y-15), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0),3);
break
else :
cv2.putText(img, "Calculating", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),3)
break
cv2.imshow('video', img)
if cv2.waitKey(33) == 27:
break
cap.release()
cv2.destroyAllWindows()