-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.py
297 lines (277 loc) · 11.9 KB
/
interface.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import cv2
import numpy as np
import threading
from PIL import Image
from . import reid, nearby, detect, draw_boxes, get_data
class reidCore:
def __init__(self, *, sample_frames=120, sample_gap=10):
self.__tasks = {}
self.__tasks_len = {}
self.__sample = {}
self.__sample_frames = sample_frames
self.__sample_gap = sample_gap
self.__stop_flag = {}
self.__flag = False
def gen_sample(self, taskname):
if taskname not in self.__tasks_len:
return
l = self.__tasks_len[taskname]
temp = self.__tasks[taskname][l-self.__sample_frames:l:self.__sample_gap]
if len(temp) == 0:
self.__sample[taskname] = []
return
size = list(temp[0].shape[:2])
while size[1] > 640 or size[0] > 480:
size[0] //= 2
size[1] //= 2
for i in range(len(temp)):
temp[i] = cv2.resize(temp[i], (size[1], size[0]))
self.__sample[taskname] = temp
def check_flag(self, taskname):
if self.__flag:
self.__stop_flag.pop(taskname)
return False
if self.__stop_flag[taskname]:
self.__stop_flag.pop(taskname)
return False
return True
def save_video(self, taskname, images, output_path):
print('save video, task: {}, frames: {},path: {}'.format(taskname, len(images), output_path))
fourcc = 'mp4v'
fps = 30
shape = images[0].shape
width = shape[1]
height = shape[0]
videoWriter = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*fourcc), fps, (width, height))
if not videoWriter.isOpened():
print('task {} save videoWriter not open'.format(taskname))
return
with open(output_path+'lock', 'wb'):
pass
cnt = 0
tot = len(images)
print('start save video, task{}'.format(taskname))
for image in images:
if not self.check_flag(taskname):
print('task "{}" stoped'.format(taskname))
videoWriter.release()
return
videoWriter.write(image[:,:,::-1])
cnt += 1
if cnt % 30:
print('video save {}/{}'.format(cnt, tot))
videoWriter.release()
import os
os.remove(output_path+'lock')
print('task {} save end!'.format(taskname))
def __multicam(self, taskname, query_path, video_paths, output_paths):
print('multicam task "{}" thread start running : {} {} {}'.format(
taskname, query_path, video_paths, output_paths
))
for i in range(len(video_paths)):
video_path = video_paths[i]
output_path = output_paths[i]
print('multicam task "{}" solving {}'.format(taskname, video_path))
self.__tasks[taskname] = []
self.__tasks_len[taskname] = 0
it = reid(query_path, video_path)
cnt = 0
for i in it:
if not self.check_flag(taskname):
print('multicam task "{}" stoped'.format(taskname))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
return
if cnt % 30 == 0:
print('processing {} frame'.format(cnt))
self.__tasks[taskname].append(i)
self.__tasks_len[taskname] += 1
cnt += 1
self.gen_sample(taskname)
print('multicam task "{}":{} solved!'.format(taskname, video_path))
self.save_video(taskname, self.__tasks[taskname], output_path)
print('multicam task "{}":{} saved at {}'.format(taskname, video_path, output_path))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
print('multicam task "{}" all end'.format(taskname))
def multicam(self, taskname, query_path, video_paths, output_paths):
if len(video_paths) != len(output_paths):
raise Exception("video number & output number not match {},{}".format(len(video_paths, len(output_paths))))
thread = threading.Thread(target=self.__multicam, args=(taskname, query_path, video_paths, output_paths))
thread.daemon = True
self.__stop_flag[taskname] = False
self.__flag = False
thread.start()
print('multicam task "{}" thread start'.format(taskname))
def __nearperson(self, taskname, query_path, video_path, output_path, nearby_k=1):
print('nearperson task "{}" thread start running : {} {} {}'.format(
taskname, query_path, video_path, output_path
))
print('nearperson task "{}" getting person'.format(taskname))
that_person = nearby(query_path, video_path, nearby_k=nearby_k)
print('nearperson task "{}" got {} person'.format(taskname, len(that_person)))
self.__tasks[taskname] = []
self.__tasks_len[taskname] = 0
it = reid([query_path]+ that_person, video_path)
for i in it:
if not self.check_flag(taskname):
print('nearperson task "{}" stoped'.format(taskname))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
return
self.__tasks[taskname].append(i)
self.__tasks_len[taskname] += 1
self.gen_sample(taskname)
print('nearperson task "{}" solved!'.format(taskname))
self.save_video(taskname, self.__tasks[taskname], output_path)
print('nearperson task "{}" saved at {}'.format(taskname, output_path))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
def nearperson(self, taskname, query_path, video_path, output_path):
thread = threading.Thread(target=self.__nearperson, args=(taskname, query_path, video_path, output_path))
thread.daemon = True
self.__stop_flag[taskname] = False
self.__flag = False
thread.start()
print('nearperson task "{}" thread start'.format(taskname))
def sample(self, taskname, output_path):
print('sample task "{}":{}'.format(taskname, output_path))
self.gen_sample(taskname)
if (taskname not in self.__sample) or len(self.__sample[taskname]) == 0:
return False
gif_list = []
for i in self.__sample[taskname]:
gif_list.append(Image.fromarray(i))
gif_list[0].save(output_path, save_all=True, append_images=gif_list[1:], duration=int(1000/60), loop=0)
return True
def stop(self, taskname=None):
if taskname is None:
self.__flag = True
return
if taskname not in self.__stop_flag:
return
self.__stop_flag[taskname] = True
class carCore:
def __init__(self, *, sample_frames=120, sample_gap=10):
self.__tasks = {}
self.__tasks_len = {}
self.__sample = {}
self.__sample_frames = sample_frames
self.__sample_gap = sample_gap
self.__stop_flag = {}
self.__flag = False
def gen_sample(self, taskname):
if taskname not in self.__tasks_len:
return
l = self.__tasks_len[taskname]
temp = self.__tasks[taskname][l-self.__sample_frames:l:self.__sample_gap]
if len(temp) == 0:
self.__sample[taskname] = []
return
size = list(temp[0].shape[:2])
while size[1] > 640 or size[0] > 480:
size[0] //= 2
size[1] //= 2
for i in range(len(temp)):
temp[i] = cv2.resize(temp[i], (size[1], size[0]))
self.__sample[taskname] = temp
def check_flag(self, taskname):
if self.__flag:
self.__stop_flag.pop(taskname)
return False
if self.__stop_flag[taskname]:
self.__stop_flag.pop(taskname)
return False
return True
def save_video(self, taskname, images, output_path):
print('save video, task: {}, frames: {},path: {}'.format(taskname, len(images), output_path))
fourcc = 'mp4v'
fps = 30
shape = images[0].shape
width = shape[1]
height = shape[0]
videoWriter = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*fourcc), fps, (width, height))
if not videoWriter.isOpened():
print('task {} save videoWriter not open'.format(taskname))
return
with open(output_path+'lock', 'wb'):
pass
cnt = 0
tot = len(images)
print('start save video, task{}'.format(taskname))
for image in images:
if not self.check_flag(taskname):
print('task "{}" stoped'.format(taskname))
videoWriter.release()
return
videoWriter.write(image[:,:,::-1])
cnt += 1
if cnt % 30:
print('video save {}/{}'.format(cnt, tot))
videoWriter.release()
import os
os.remove(output_path+'lock')
print('task {} save end!'.format(taskname))
def __car(self, taskname, query_path, video_paths, output_paths):
print('car task "{}" thread start running : {} {} {}'.format(
taskname, query_path, video_paths, output_paths
))
for i in range(len(video_paths)):
video_path = video_paths[i]
output_path = output_paths[i]
print('car task "{}" solving {}'.format(taskname, video_path))
self.__tasks[taskname] = []
self.__tasks_len[taskname] = 0
cnt = 0
video = get_data(video_path)
video_length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
for frame_num in range(video_length):
ret, image = video.read()
if not ret:
break
image = np.flip(image, 2)
bboxes = detect(image, class_='car')
image = draw_boxes(image, bboxes, copy=False)
if not self.check_flag(taskname):
print('car task "{}" stoped'.format(taskname))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
return
if cnt % 30 == 0:
print('processing {} frame'.format(cnt))
self.__tasks[taskname].append(image)
self.__tasks_len[taskname] += 1
cnt += 1
self.gen_sample(taskname)
print('car task "{}":{} solved!'.format(taskname, video_path))
self.save_video(taskname, self.__tasks[taskname], output_path)
print('car task "{}":{} saved at {}'.format(taskname, video_path, output_path))
self.__tasks_len.pop(taskname)
self.__tasks.pop(taskname)
print('car task "{}" all end'.format(taskname))
def car(self, taskname, query_path, video_paths, output_paths):
if len(video_paths) != len(output_paths):
raise Exception("video number & output number not match {},{}".format(len(video_paths, len(output_paths))))
thread = threading.Thread(target=self.__car, args=(taskname, query_path, video_paths, output_paths))
thread.daemon = True
self.__stop_flag[taskname] = False
self.__flag = False
thread.start()
print('car task "{}" thread start'.format(taskname))
def sample(self, taskname, output_path):
print('sample task "{}":{}'.format(taskname, output_path))
self.gen_sample(taskname)
if (taskname not in self.__sample) or len(self.__sample[taskname]) == 0:
return False
gif_list = []
for i in self.__sample[taskname]:
gif_list.append(Image.fromarray(i))
gif_list[0].save(output_path, save_all=True, append_images=gif_list[1:], duration=int(1000/60), loop=0)
return True
def stop(self, taskname=None):
if taskname is None:
self.__flag = True
return
if taskname not in self.__stop_flag:
return
self.__stop_flag[taskname] = True