-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
228 lines (176 loc) · 6.01 KB
/
query.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import pymysql
import time
import sys
from os import listdir
import cv2
import threading
import numpy as np
start = time.time()
FRAMEMAX = 800
myDirectory = "/Users/Neo/Documents/Programming/Python3/Image/"
class thread1(threading.Thread):
def __init__(self,filename):
super(thread1,self).__init__()
self.filename=filename
def run(self):
playVideo(self.filename)
class thread2(threading.Thread):
def __init__(self,filename):
super(thread2,self).__init__()
self.filename=filename
def run(self):
videoAnasis(self.filename)
def readfile(myDirectory):
allFileName = listdir(myDirectory)
allFileName.sort()
return allFileName[len(allFileName)-2]
def writeintodb(data):
MWE = data[0]
MEW = data[1]
CNS = data[2]
CSN = data[3]
time = data[4]
try:
conn = pymysql.connect(host="172.17.108.132",port=3306,user='root',passwd='526840',db='smarttraffic')
cur = conn.cursor()
cur.execute("INSERT INTO traffic(time,Magnolia_WE,Magnolia_EW,College_NS,College_SN) VALUES (%s,%s,%s,%s,%s)"
,(time,MWE,MEW,CNS,CSN))
conn.commit()
cur.close()
conn.close()
return True
except:
print("Unexpected error:", sys.exc_info()[0])
return False
def playVideo(filename):
frameNum = 0
cap = cv2.VideoCapture('./Image/'+filename)
fgbg = cv2.createBackgroundSubtractorMOG2()
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('output.avi',fourcc,20.0,(1280,720))
while (1):
frameNum += 1
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
rawdata = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR) # gray values
# out.write(frame)
cv2.imshow('frame',fgmask)
k = cv2.waitKey(1) & 0xff
if k == 1:
break
if frameNum > FRAMEMAX:
break
cap.release()
cv2.destroyAllWindows()
def videoAnasis(filename, filenamePrefix):
cap = cv2.VideoCapture('./Image/'+filename)
VIDEO_WIDTH = 1280
VIDEO_HEIGHT = 720
NUMOFPIXELS = VIDEO_WIDTH * VIDEO_HEIGHT
fgbg = cv2.createBackgroundSubtractorMOG2()
frameNum = 0
EMag_E_totalWhite = 0 # car in E Magnolia Ave, going eastboundf
EMag_E_pixel_count = 0.1 # count totally how many pixels in this area
EMag_W_totalWhite = 0
EMag_W_pixel_count = 0.1
SCol_S_totalWhite = 0 # car in S College Str, going southbound
SCol_S_pixel_count = 0.1
SCol_N_totalWhite = 0
SCol_N_pixel_count = 0.1
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
while(1):
frameNum += 1
ret, frame = cap.read()
if frameNum%50==0:
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
rawdata = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR) # gray values
# for i in range(VIDEO_HEIGHT):
# for j in range(VIDEO_WIDTH):
# tmp = 0
# if rawdata[i][j][0] > 120:
# tmp = 1
#
# whiteNum[i + j * VIDEO_WIDTH] = tmp
#totalWhite[frameNum] = sum(whiteNum.values())
#print(totalWhite)
step1=5
step2=5
# print(time.time()-start)
for i in range(0,VIDEO_HEIGHT,step1):
for j in range(0,VIDEO_WIDTH,step2):
tmp = 0
if rawdata[i][j][0] > 120:
tmp = 1
if i - 0.63 * j >= 328.63: # j is x, i is y
SCol_N_pixel_count += 1
SCol_N_totalWhite += tmp
elif i + 0.88 * j >= 1476.08:
# if i<2000:
EMag_E_pixel_count += 1
EMag_E_totalWhite += tmp
#elif i + 0.56 * j >= 942.28:
elif i + 0.56 * j >= 1142.28:
EMag_W_pixel_count += 1
EMag_W_totalWhite += tmp
else:
SCol_S_pixel_count += 1
SCol_S_totalWhite += tmp
# cv2.imshow('frame',rawdata)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
if frameNum > FRAMEMAX:
break
cap.release()
cv2.destroyAllWindows()
ratio_Emag_E=float(EMag_E_totalWhite)/EMag_E_pixel_count
ratio_Emag_W=float(EMag_W_totalWhite)/EMag_W_pixel_count
ratio_SCOl_N=float(SCol_N_totalWhite)/SCol_N_pixel_count
ratio_SCOl_S=float(SCol_S_totalWhite)/SCol_S_pixel_count
condition=[0,0,0,0,int(filenamePrefix)]
traffic=[ratio_Emag_E,ratio_Emag_W,ratio_SCOl_S,ratio_SCOl_N]
threshold=[0.05,0.1,0.3,0.5]
for i in range(4):
if traffic[i]<=threshold[0]:
condition[i]=1
elif traffic[i]<=threshold[1]:
condition[i]=2
elif traffic[i]<=threshold[2]:
condition[i]=3
elif traffic[i]<=threshold[3]:
condition[i]=4
else:
condition[i]=5
# print(time.time()-start)
#
# print(EMag_E_totalWhite)
# print(EMag_W_totalWhite)
# print(SCol_N_totalWhite)
# print(SCol_S_totalWhite)
# print('--------------------')
# print(EMag_E_pixel_count)
# print(EMag_W_pixel_count)
# print(SCol_N_pixel_count)
# print(SCol_S_pixel_count)
# print('----------------------')
print(ratio_Emag_E)
print(ratio_Emag_W)
print(ratio_SCOl_N)
print(ratio_SCOl_S)
#
# print(frameNum)
# print(time.time()-start)
return condition
# thread1(file).start()
# thread2(file).start()
while(1):
file = readfile(myDirectory)
newfilePrefix = file[0:10]
newfile = newfilePrefix + "-1" +".mp4"
# playVideo(file)
road_condition = videoAnasis(file,newfilePrefix)
writeintodb(road_condition)
print(road_condition)