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
/
Copy pathmultiTread.py
191 lines (163 loc) · 5.7 KB
/
multiTread.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
import matplotlib
from threading import *
from time import sleep
import numpy as np
matplotlib.use("TKAgg")
print(matplotlib.get_backend())
from matplotlib import pyplot as plt
import cv2
def fun(x):
pass
#It convert the time into frame number#
def calcFrame(x,y):
frame_time=int((x*60+y)*30)
return frame_time
refIm = cv2.imread('refFrame.jpg')
temp=refIm.copy()
temp1=temp.copy()
for i in range(232):
for j in range(302):
temp[i][j]=0
temp1[i][j]=1
#to create window frame
st0=np.hstack((temp,temp1,temp))
st1=np.hstack((temp1,temp,temp1))
st2=np.hstack((temp,temp1,temp))
#cv2.imshow('fWin',fWin)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
(h, w) = st0.shape[:2]
print(h,w)
class lane1(Thread):
lane1_start_time = calcFrame(1, 60)
lane1_end_time = calcFrame(2, 35)
vid1 = cv2.VideoCapture('latestData.mp4')
vid1.set(1, lane1_start_time)
def run(self):
while vid1.get(1) <= (lane1_end_time+1000):
print("lane1")
ret1, frame1 = vid1.read()
global st0
st0[:232,302:604]=frame1
cv2.imshow("frame1",st0)
cv2.waitKey(0)
cv2.destroyAllWindows()
class lane2(Thread):
lane2_start_time = calcFrame(2, 52)
lane2_end_time = calcFrame(3, 22)
vid2 = cv2.VideoCapture('latestData.mp4')
vid2.set(1, lane2_start_time)
def run(self):
while vid2.get(1) <= (lane2_end_time + 1000):
print("lane2")
ret2, frame2 = vid2.read()
st1[:232,0:302]=frame2
#cv2.imshow("frame2", frame2)
#cv2.waitKey(1)
class lane3(Thread):
lane3_start_time = calcFrame(6, 56)
lane3_end_time = calcFrame(7, 26)
vid3 = cv2.VideoCapture('latestData.mp4')
vid3.set(1, lane3_start_time)
def run(self):
while vid3.get(1) <= (lane3_end_time + 1000):
print("lane3")
ret3, frame3 = vid3.read()
st1[:232,605:906]=frame3
#cv2.imshow("frame3", frame3)
#cv2.waitKey(1)
class lane4(Thread):
lane4_start_time = calcFrame(12, 22)
lane4_end_time = calcFrame(12, 52)
vid4 = cv2.VideoCapture('latestData.mp4')
vid4.set(1, lane4_start_time)
def run(self):
while vid4.get(1) <= (lane4_end_time + 1000):
print("lane4")
ret4, frame4 = vid4.read()
st2[:232,303:604]=frame4
#cv2.imshow("frame4", frame4)
#cv2.waitKey(1)
cv2.namedWindow("window")
cv2.createTrackbar("kernel", "window", 3, 1000, fun)
cv2.createTrackbar("threshold", "window", 53, 1000, fun)
cv2.createTrackbar("dilate", "window", 15, 1000, fun)
if __name__ == "__main__":
refIm = cv2.imread('refFrame.jpg')
refIm2 = cv2.cvtColor(refIm, cv2.COLOR_BGR2GRAY)
#Time for the lane1#
lane1_start_time=calcFrame(1,55)
lane1_end_time=calcFrame(2,30)
vid1 = cv2.VideoCapture('latestData.mp4')
vid1.set(1,lane1_start_time)
# Time for the lane2#
lane2_start_time = calcFrame(2,52)
lane2_end_time = calcFrame(3,22)
vid2= cv2.VideoCapture('latestData.mp4')
vid2.set(1,lane2_start_time)
# Time for the lane3#
lane3_start_time = calcFrame(6,56)
lane3_end_time = calcFrame(7,26)
vid3= cv2.VideoCapture('latestData.mp4')
vid3.set(1,lane3_start_time)
# Time for the lane4#
lane4_start_time = calcFrame(12,22)
lane4_end_time = calcFrame(12,52)
vid4= cv2.VideoCapture('latestData.mp4')
vid4.set(1,lane4_start_time)
l1=lane1()
l2 = lane2()
l3 = lane3()
l4=lane4()
l1.start()
l2.start()
l3.start()
l4.start()
#cv2.waitKey(1)
#plt.show()
fWin=np.vstack((st0,st1,st2))
cv2.imshow('fWin',fWin)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
while (vid.isOpened()):
ret, frame = vid.read()
(h, w) = frame.shape[:2]
# print(h,w)
vidClone = frame.copy()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
y = type(gray)
# print(y)
bg = refIm2.copy().astype('float')
diff = cv2.absdiff(bg.astype('uint8'), gray)
thresh = cv2.getTrackbarPos("threshold", "window")
thresholded = cv2.threshold(diff, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('Input', gray)
cv2.imshow('Original', frame)
cv2.imshow('Dif', thresholded)
k = 3
if k % 2 == 0:
k = k + 1
kernel = np.ones((k, k), "uint8")
opening = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, kernel)
cv2.imshow('opening', opening)
dilate = cv2.getTrackbarPos("dilate", "window")
dilated = cv2.dilate(opening, None, iterations=dilate)
cv2.imshow("dilated", dilated)
contour, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in range(len(contour)):
z = cv2.drawContours(vidClone, contour, i, (0, 255, 0))
M = cv2.moments(contour[i])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
area = cv2.contourArea(contour[i])
cv2.putText(vidClone, str(area), (cx - 10, cy - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("video clone", vidClone)
keypress = cv2.waitKey(30) & 0xFF
# if the user pressed "q", then stop looping
if keypress == ord('q'):
break
# workbook.close()'''
#vid.release
#cv2.destroyAllWindows()