-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv2_cam_pyqt.py
166 lines (138 loc) · 5.63 KB
/
cv2_cam_pyqt.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
"""
@update: 2021.05.14
"""
import cv2
import sys, time, os
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from Ui_main import Ui_MainWindow
class Camera(QtCore.QThread):
rawdata = QtCore.pyqtSignal(np.ndarray)
def __init__(self, parent=None):
super().__init__(parent)
self.cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
if self.cam is None or not self.cam.isOpened():
self.connect = False
self.running = False
else:
self.connect = True
self.running = False
def run(self):
while self.running and self.connect:
ret, img = self.cam.read()
if ret:
self.rawdata.emit(img)
else:
print("Warning!!!")
self.connect = False
def open(self):
if self.connect:
self.running = True
def stop(self):
if self.connect:
self.running = False
def close(self):
if self.connect:
self.running = False
time.sleep(1)
self.cam.release()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.viewData.setScaledContents(True)
self.view_x = self.view.horizontalScrollBar()
self.view_y = self.view.verticalScrollBar()
self.view.installEventFilter(self)
self.last_move_x = 0
self.last_move_y = 0
self.frame_num = 0
self.ProcessCam = Camera()
if self.ProcessCam.connect:
self.debugBar('Connection!!!')
self.ProcessCam.rawdata.connect(self.getRaw)
else:
self.debugBar('Disconnection!!!')
self.camBtn_open.clicked.connect(self.openCam)
self.camBtn_stop.clicked.connect(self.stopCam)
def getRaw(self, data):
self.showData(data)
def openCam(self):
if self.ProcessCam.connect:
self.ProcessCam.open()
self.ProcessCam.start()
self.camBtn_open.setEnabled(False)
self.camBtn_stop.setEnabled(True)
self.viewCbo_roi.setEnabled(True)
def stopCam(self):
if self.ProcessCam.connect:
self.ProcessCam.stop()
self.camBtn_open.setEnabled(True)
self.camBtn_stop.setEnabled(False)
self.viewCbo_roi.setEnabled(False)
def showData(self, img):
self.Ny, self.Nx, _ = img.shape
# 反轉顏色
img_new = np.zeros_like(img)
img_new[...,0] = img[...,2]
img_new[...,1] = img[...,1]
img_new[...,2] = img[...,0]
img = img_new
# qimg = QtGui.QImage(img[:,:,0].copy().data, self.Nx, self.Ny, QtGui.QImage.Format_Indexed8)
qimg = QtGui.QImage(img.data, self.Nx, self.Ny, QtGui.QImage.Format_RGB888)
self.viewData.setScaledContents(True)
self.viewData.setPixmap(QtGui.QPixmap.fromImage(qimg))
if self.viewCbo_roi.currentIndex() == 0: roi_rate = 0.5
elif self.viewCbo_roi.currentIndex() == 1: roi_rate = 0.75
elif self.viewCbo_roi.currentIndex() == 2: roi_rate = 1
elif self.viewCbo_roi.currentIndex() == 3: roi_rate = 1.25
elif self.viewCbo_roi.currentIndex() == 4: roi_rate = 1.5
else: pass
self.viewForm.setMinimumSize(self.Nx*roi_rate, self.Ny*roi_rate)
self.viewForm.setMaximumSize(self.Nx*roi_rate, self.Ny*roi_rate)
self.viewData.setMinimumSize(self.Nx*roi_rate, self.Ny*roi_rate)
self.viewData.setMaximumSize(self.Nx*roi_rate, self.Ny*roi_rate)
if self.frame_num == 0:
self.time_start = time.time()
if self.frame_num >= 0:
self.frame_num += 1
self.t_total = time.time() - self.time_start
if self.frame_num % 100 == 0:
self.frame_rate = float(self.frame_num) / self.t_total
self.debugBar('FPS: %0.3f frames/sec' % self.frame_rate)
def eventFilter(self, source, event):
if source == self.view:
if event.type() == QtCore.QEvent.MouseMove:
if self.last_move_x == 0 or self.last_move_y == 0:
self.last_move_x = event.pos().x()
self.last_move_y = event.pos().y()
distance_x = self.last_move_x - event.pos().x()
distance_y = self.last_move_y - event.pos().y()
self.view_x.setValue(self.view_x.value() + distance_x)
self.view_y.setValue(self.view_y.value() + distance_y)
self.last_move_x = event.pos().x()
self.last_move_y = event.pos().y()
elif event.type() == QtCore.QEvent.MouseButtonRelease:
self.last_move_x = 0
self.last_move_y = 0
return QtWidgets.QWidget.eventFilter(self, source, event)
def closeEvent(self, event):
if self.ProcessCam.running:
self.ProcessCam.close()
self.ProcessCam.terminate()
QtWidgets.QApplication.closeAllWindows()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q:
if self.ProcessCam.running:
self.ProcessCam.close()
time.sleep(1)
self.ProcessCam.terminate()
QtWidgets.QApplication.closeAllWindows()
def debugBar(self, msg):
self.statusBar.showMessage(str(msg), 5000)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())