forked from kly1997/head_shoulder-detection-by-yolov3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_box.py
386 lines (337 loc) · 14.7 KB
/
video_box.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import time
import sys
import numpy
from timeit import default_timer as timer
import PyQt5.sip
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from cv2 import *
from yolo1 import YOLO
from PyQt5.QtWidgets import *
from PIL import Image
class VideoBox(QWidget):
VIDEO_TYPE_OFFLINE = 0
VIDEO_TYPE_REAL_TIME = 1
STATUS_INIT = 0
STATUS_PLAYING = 1
STATUS_PAUSE = 2
video_url = ""
def __init__(self, video_url="", video_type=VIDEO_TYPE_OFFLINE, auto_play=False):
self.yolo = YOLO()
QWidget.__init__(self)
self.video_url = video_url
self.video_type = video_type # 0: offline 1: realTime
self.auto_play = auto_play
self.status = self.STATUS_INIT # 0: init 1:playing 2: pause
# 组件展示
self.pictureLabel = QLabel()
init_image = QPixmap("cat.jpeg").scaled(720, 480)
self.pictureLabel.setPixmap(init_image)
# self.playButton = QPushButton()
# self.playButton.clicked.connect(self.switch_video)
self.playButton = QPushButton('save', self)
self.openButton = QPushButton('webCam', self)
self.transButton = QPushButton('trans', self)
self.exitButton = QPushButton('exit', self)
self.playButton.clicked.connect(self.switch_video)
self.openButton.clicked.connect(self.openSlot)
self.transButton.clicked.connect(self.transSlot)
self.exitButton.clicked.connect(self.closeSlot)
layout = QGridLayout(self)
layout.addWidget(self.playButton, 4, 1, 1, 1)
layout.addWidget(self.openButton, 4, 2, 1, 1)
layout.addWidget(self.transButton, 4, 3, 1, 1)
layout.addWidget(self.exitButton, 4, 4, 1, 1)
layout.addWidget(self.pictureLabel, 0, 1, 3, 4)
#control_box = QHBoxLayout()
# control_box.setContentsMargins(0, 0, 0, 0)
# control_box.addWidget(self.playButton)
# layout = QVBoxLayout()
#layout.addWidget(self.pictureLabel, 0, 1, 3, 4)
#layout.addLayout(control_box)
self.setLayout(layout)
# timer 设置
self.timer = VideoTimer()
self.timer.timeSignal.signal[str].connect(self.show_video_images)
# video 初始设置
self.playCapture = VideoCapture()
if self.video_url != "":
self.set_timer_fps()
if self.auto_play:
self.switch_video()
# self.videoWriter = VideoWriter('*.mp4', VideoWriter_fourcc('M', 'J', 'P', 'G'), self.fps, size)
def reset(self):
self.timer.stop()
self.playCapture.release()
self.status = VideoBox.STATUS_INIT
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def set_timer_fps(self):
self.playCapture.open(self.video_url)
fps = self.playCapture.get(CAP_PROP_FPS)
self.timer.set_fps(fps)
self.playCapture.release()
def set_video(self, url, video_type=VIDEO_TYPE_OFFLINE, auto_play=False):
self.reset()
self.video_url = url
self.video_type = video_type
self.auto_play = auto_play
self.set_timer_fps()
if self.auto_play:
self.switch_video()
def play(self):
if self.video_url == "" or self.video_url is None:
return
if not self.playCapture.isOpened():
self.playCapture.open(self.video_url)
self.timer.start()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
self.status = VideoBox.STATUS_PLAYING
def stop(self):
if self.video_url == "" or self.video_url is None:
return
if self.playCapture.isOpened():
self.timer.stop()
if self.video_type is VideoBox.VIDEO_TYPE_REAL_TIME:
self.playCapture.release()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.status = VideoBox.STATUS_PAUSE
def re_play(self):
if self.video_url == "" or self.video_url is None:
return
self.playCapture.release()
self.playCapture.open(self.video_url)
self.timer.start()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
self.status = VideoBox.STATUS_PLAYING
def openSlot(self):
value, ok = QInputDialog.getText(self, "web_cam", "请输相机地址:", QLineEdit.Normal,
"http://admin:[email protected]:8081")
if value == '':
vid = cv2.VideoCapture(0)
else:
vid = cv2.VideoCapture(value)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
accum_time = 0
curr_fps = 0
fps = "FPS: ??"
prev_time = timer()
while True:
return_value, frame = vid.read()
image = Image.fromarray(frame)
image = self.yolo.detect_image(image, 1)
# result = cv2.cvtColor(numpy.asarray(image), cv2.COLOR_RGB2BGR)
result = numpy.asarray(image)
curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.50, color=(255, 0, 0), thickness=1)
# result = cv2.resize(result, (500, 500), interpolation=cv2.INTER_CUBIC)
# height, width = result.shape[:2]
# if result.ndim == 3:
# rgb = cvtColor(result, COLOR_BGR2RGB)
# elif result.ndim == 2:
# rgb = cvtColor(result, COLOR_GRAY2BGR)
# temp_image = QImage(rgb.flatten(), width, height, QImage.Format_RGB888)
# temp_pixmap = QPixmap.fromImage(temp_image)
# # temp_pixmap = cv2.resize(temp_pixmap, (500, 500), interpolation=cv2.INTER_CUBIC)
# self.pictureLabel.setPixmap(temp_pixmap)
# result = cv2.resize(result, (1, 1), interpolation=cv2.INTER_CUBIC)
cv2.imshow("result", result)
# cv2.imshow("result", result)
# if isOutput:
# out.write(result)
# print('ok')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
self.yolo.close_session()
def transSlot(self):
fileName, tmp = QFileDialog.getOpenFileName(self, 'Open Video', 'video', '*')
if fileName is '':
return
if fileName.split('.')[-1] == ('jpg'or 'png' or 'jpeg'):
image = Image.open(fileName)
frame = self.yolo.detect_image(image, 1)
frame = cv2.cvtColor(numpy.asarray(frame), cv2.COLOR_RGB2BGR)
# self.yolo.close_session()
height, width = frame.shape[:2]
if frame.ndim == 3:
rgb = cvtColor(frame, COLOR_BGR2RGB)
elif frame.ndim == 2:
rgb = cvtColor(frame, COLOR_GRAY2BGR)
temp_image = []
temp_image = QImage(rgb.flatten(), width, height, QImage.Format_RGB888)
temp_pixmap = QPixmap.fromImage(temp_image)
self.pictureLabel.setPixmap(temp_pixmap)
else:
print(fileName)
vid = cv2.VideoCapture(fileName)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
# video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC))
# video_FourCC = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# video_fps = vid.get(cv2.CAP_PROP_FPS)
# video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
# int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# isOutput = True if output_path != "" else False
# if isOutput:
# print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size))
# print("!!! value:", output_path, video_FourCC, video_fps, video_size)
# out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) fin_time = timer()
accum_time = 0
curr_fps = 0
fps = "FPS: ??"
prev_time = timer()
while True:
s_time = timer()
return_value, frame = vid.read()
image = Image.fromarray(frame)
result = self.yolo.detect_image(image, 1)
s1_time = timer()
# result = cv2.cvtColor(numpy.asarray(image), cv2.COLOR_RGB2BGR)
#result = numpy.asarray(image)
curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.50, color=(255, 0, 0), thickness=1)
#result = cv2.resize(result, (500, 500), interpolation=cv2.INTER_CUBIC)
# height, width = result.shape[:2]
# if result.ndim == 3:
# rgb = cvtColor(result, COLOR_BGR2RGB)
# elif result.ndim == 2:
# rgb = cvtColor(result, COLOR_GRAY2BGR)
# temp_image = QImage(rgb.flatten(), width, height, QImage.Format_RGB888)
# temp_pixmap = QPixmap.fromImage(temp_image)
# # temp_pixmap = cv2.resize(temp_pixmap, (500, 500), interpolation=cv2.INTER_CUBIC)
# self.pictureLabel.setPixmap(temp_pixmap)
# result = cv2.resize(result, (1, 1), interpolation=cv2.INTER_CUBIC)
cv2.imshow("result", result)
s2_time = timer()
# cv2.imshow("result", result)
# if isOutput:
# out.write(result)
# print('ok')
print(s2_time - s1_time)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
self.yolo.close_session()
def closeSlot(self):
exit()
def show_video_images(self):
if self.playCapture.isOpened():
success, frame = self.playCapture.read()
if success:
height, width = frame.shape[:2]
if frame.ndim == 3:
rgb = cvtColor(frame, COLOR_BGR2RGB)
elif frame.ndim == 2:
rgb = cvtColor(frame, COLOR_GRAY2BGR)
temp_image = QImage(rgb.flatten(), width, height, QImage.Format_RGB888)
temp_pixmap = QPixmap.fromImage(temp_image)
self.pictureLabel.setPixmap(temp_pixmap)
else:
print("read failed, no frame data")
success, frame = self.playCapture.read()
if not success and self.video_type is VideoBox.VIDEO_TYPE_OFFLINE:
print("play finished") # 判断本地文件播放完毕
self.reset()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaStop))
return
else:
print("open file or capturing device error, init again")
self.reset()
def switch_video(self):
fileName, tmp = QFileDialog.getOpenFileName(self, 'Open Video', 'video', '*')
if fileName is '':
return
print(fileName)
dir = QFileDialog.getExistingDirectory(self,
"选取文件夹",
"C:/") # 起始路径
output_path =dir+'/new_'+fileName.split('/')[-1]
print(output_path)
vid = cv2.VideoCapture(fileName)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
# video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC))
video_FourCC = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video_fps = vid.get(cv2.CAP_PROP_FPS)
video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
isOutput = True if output_path != "" else False
if isOutput:
print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size))
print("!!! value:", output_path, video_FourCC, video_fps, video_size)
out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size)
accum_time = 0
curr_fps = 0
fps = "FPS: ??"
prev_time = timer()
while True:
return_value, frame = vid.read()
image = Image.fromarray(frame)
result = self.yolo.detect_image(image, 1)
# result = np.asarray(image)
curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.50, color=(255, 0, 0), thickness=2)
# cv2.namedWindow("result", cv2.WINDOW_NORMAL)
cv2.imshow("result", result)
if isOutput:
out.write(result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
self.yolo.close_session()
class Communicate(QObject):
signal = pyqtSignal(str)
class VideoTimer(QThread):
def __init__(self, frequent=20):
QThread.__init__(self)
self.stopped = False
self.frequent = frequent
self.timeSignal = Communicate()
self.mutex = QMutex()
def run(self):
with QMutexLocker(self.mutex):
self.stopped = False
while True:
if self.stopped:
return
self.timeSignal.signal.emit("1")
time.sleep(1 / self.frequent)
def stop(self):
with QMutexLocker(self.mutex):
self.stopped = True
def is_stopped(self):
with QMutexLocker(self.mutex):
return self.stopped
def set_fps(self, fps):
self.frequent = fps
if __name__ == "__main__":
mapp = QApplication(sys.argv)
mw = VideoBox()
#mw.set_video("F:\DEEPL\yolo\play\pyqt5-opencv-video-master//resource//video.mp4", VideoBox.VIDEO_TYPE_OFFLINE, False)
mw.show()
sys.exit(mapp.exec_())