-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowpage.py
172 lines (144 loc) · 5.61 KB
/
showpage.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
import time
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from cv2 import *
print(type(str("project_video.mp4")))
class VideoBox(QWidget):
# QWidget = "project_video.mp4"
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):
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("../assets/images/no_video.jpeg").scaled(self.width(), self.height())
self.pictureLabel.setPixmap(init_image)
self.playButton = QPushButton()
self.playButton.setEnabled(True)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.switch_video)
control_box = QHBoxLayout()
control_box.setContentsMargins(0, 0, 0, 0)
control_box.addWidget(self.playButton)
layout = QVBoxLayout()
layout.addWidget(self.pictureLabel)
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.playCapture.open(self.video_url)
fps = self.playCapture.get(CAP_PROP_FPS)
self.timer.set_fps(fps)
self.playCapture.release()
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 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):
if self.video_url == "" or self.video_url is None:
return
if self.status is VideoBox.STATUS_INIT:
self.playCapture.open(self.video_url)
self.timer.start()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
elif self.status is VideoBox.STATUS_PLAYING:
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))
elif self.status is VideoBox.STATUS_PAUSE:
if self.video_type is VideoBox.VIDEO_TYPE_REAL_TIME:
self.playCapture.open(self.video_url)
self.timer.start()
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
self.status = (VideoBox.STATUS_PLAYING,
VideoBox.STATUS_PAUSE,
VideoBox.STATUS_PLAYING)[self.status]
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__":
app = QApplication(sys.argv)
# app = QtWidgets.QApplication(sys.argv)
# QWidget = "project_video.mp4"
box = VideoBox(QWidget)
box.show()
sys.exit(app.exec_())
# import sys
# # from PyQt5 import QtWidgets
# # import PyQt5
# from PyQt5 import QtCore, QtGui, QtWidgets
#
# def main():
# app = QtWidgets.QApplication(sys.argv)
# window = QtWidgets.QMainWindow()
# button = QtWidgets.QPushButton("Hello, PyQt!")
# window.setCentralWidget(button)
# window.show()
# app.exec_()
#
# if __name__ == '__main__':
# main()