This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
forked from arjunc246/Traffic-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalamFinal2.py
214 lines (184 loc) · 6.8 KB
/
finalamFinal2.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import cv2
import numpy as np
from sklearn.externals import joblib
from threading import *
import time
refIm = cv2.imread('refFrame.jpg')
refIm2 = cv2.cvtColor(refIm, cv2.COLOR_BGR2GRAY)
roi = np.ones(refIm2.shape, "uint8")
#setting roi
cv2.rectangle(roi, (62, 60), (242, 180), 255, -1)
bg = refIm2.copy()
bg=cv2.bitwise_and(bg,roi)
model = joblib.load("model.cpickle")
TIME=0
class timer(Thread):
#canvas=
def run(self):
canvas=np.zeros((232,302),"uint8")
for i in range(TIME):
cv2.putText(canvas,str(TIME-i),(int(canvas.shape[0]/2),int(canvas.shape[1]/2)),cv2.FONT_HERSHEY_SIMPLEX,1.0,(255,0,255))
time.sleep(1)
canvas=np.zeros((232,302),"uint8")
def calcFrame(x, y):
frame_time = int((x * 60 + y) * 35)
return frame_time
def process(frame):
vidClone=frame.copy()
global roi
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray=cv2.bitwise_and(gray,roi)
diff = cv2.absdiff(bg.astype('uint8'), gray)
# threshold logic#
thresh = 53
thresholded = cv2.threshold(diff, thresh, 255, cv2.THRESH_BINARY)[1]
# Opening logic#
k = 3
kernel = np.ones((k, k), "uint8")
opening = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, kernel)
#cv2.imshow('opening', opening)
# dilation logic#
dilate = 15
dilated = cv2.dilate(opening, None, iterations=dilate)
# change to _,contour,_ for latest version#
_,contour, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Finding the area of each contour#
for i in range(len(contour)):
M = cv2.moments(contour[i])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
area = cv2.contourArea(contour[i])
if area>=3700:
z = cv2.drawContours(vidClone, contour, i, (0, 255, 0),3)
#cv2.putText(vidClone, str(area), (cx - 10, cy - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 3)
time=model.predict(area)
global TIME
TIME=int(time)
cv2.imshow("vidClone",vidClone)
return vidClone
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
return
if __name__ == "__main__":
vid1 = cv2.VideoCapture('latestData.mp4')
vid2 = cv2.VideoCapture('latestData.mp4')
vid3 = cv2.VideoCapture('latestData.mp4')
vid4 = cv2.VideoCapture('latestData.mp4')
#global refIm
temp=refIm.copy()
temp1=temp.copy()
for i in range(232):
for j in range(302):
temp[i][j]=0
temp1[i][j]=255
timer=temp.copy()
# setting the video frame#
lane1_start_time = calcFrame(1, 60)
lane1_end_time = calcFrame(2, 36)
vid1.set(1, lane1_start_time)
_,frame1=vid1.read()
lane2_start_time = calcFrame(2, 52)
lane2_end_time = calcFrame(3, 22)
vid2.set(1, lane2_start_time)
_,frame2=vid1.read()
lane3_start_time = calcFrame(6, 56)
lane3_end_time = calcFrame(7, 26)
vid3.set(1, lane3_start_time)
_,frame3=vid1.read()
lane4_start_time = calcFrame(12, 22)
lane4_end_time = calcFrame(12, 52)
vid4.set(1, lane4_start_time)
_,frame4=vid1.read()
#display window
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,timer,frame2))
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
#lane1
# reading the reference image#
while vid1.get(1) <= (lane1_end_time):
ret1, frame1 = vid1.read()
frame1=process(frame1)
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,timer,frame2))
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
#if vid1.get(1)==calcFrame(2,20):
# t=thread()
cv2.imshow("frame",fWin)
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
#lane2
while vid2.get(1) <= (lane2_end_time):
ret1, frame2 = vid2.read()
frame2=process(frame2)
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,timer,frame2))
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
cv2.imshow("frame",fWin)
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
#lane3
while vid3.get(1) <= (lane3_end_time) and TIME:
ret1, frame3 = vid3.read()
frame3=process(frame3)
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,timer,frame2))
#for i in range(TIME):
#cv2.putText(timer,str(TIME-i),(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255))
#time.sleep(1)
#timer=temp.copy()
#cv2.imshow('st1',st1)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
x,y=400,400
#for i in range(TIME):
# print(TIME-i)
cv2.putText(fWin,str(TIME),(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255))
# time.sleep(1)
cv2.imshow("frame",fWin)
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
#lane4
while vid4.get(1) <= (lane4_end_time):
ret1, frame4 = vid4.read()
frame4=process(frame4)
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,timer,frame2))
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
cv2.imshow("frame",fWin)
keypress = cv2.waitKey(1) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
cv2.destroyAllWindows()
'''
vid1.set(1, lane2_start_time)
_,frame2=vid1.read()
vid1.set(1, lane3_start_time)
_,frame3=vid1.read()
vid1.set(1, lane4_start_time)
_,frame4=vid1.read()
st0=np.hstack((temp,frame1,temp))
st1=np.hstack((frame4,temp,frame2))
st2=np.hstack((temp,frame3,temp))
fWin=np.vstack((st0,st1,st2))
cv2.imshow('fWin',fWin)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow("video clone", vidClone)
cv2.imshow('fWin',fWin)
vid.release
cv2.destroyAllWindows()
'''