|
@@ -0,0 +1,123 @@
|
|
1
|
+'''
|
|
2
|
+OpenCV을 이용한 영상캡쳐 테스트
|
|
3
|
+'''
|
|
4
|
+import cv2
|
|
5
|
+import sys
|
|
6
|
+from PyQt5 import QtCore
|
|
7
|
+from PyQt5 import QtWidgets
|
|
8
|
+from PyQt5 import QtGui
|
|
9
|
+
|
|
10
|
+class ShowVideo(QtCore.QObject):
|
|
11
|
+
|
|
12
|
+ flag = 0
|
|
13
|
+
|
|
14
|
+ camera = cv2.VideoCapture(0)
|
|
15
|
+
|
|
16
|
+ ret, image = camera.read()
|
|
17
|
+ height, width = image.shape[:2]
|
|
18
|
+
|
|
19
|
+ VideoSignal1 = QtCore.pyqtSignal(QtGui.QImage)
|
|
20
|
+ VideoSignal2 = QtCore.pyqtSignal(QtGui.QImage)
|
|
21
|
+
|
|
22
|
+ def __init__(self, parent=None):
|
|
23
|
+ super(ShowVideo, self).__init__(parent)
|
|
24
|
+
|
|
25
|
+ @QtCore.pyqtSlot()
|
|
26
|
+ def startVideo(self):
|
|
27
|
+ global image
|
|
28
|
+
|
|
29
|
+ run_video = True
|
|
30
|
+ while run_video:
|
|
31
|
+ ret, image = self.camera.read()
|
|
32
|
+ color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
33
|
+
|
|
34
|
+ qt_image1 = QtGui.QImage(color_swapped_image.data,
|
|
35
|
+ self.width,
|
|
36
|
+ self.height,
|
|
37
|
+ color_swapped_image.strides[0],
|
|
38
|
+ QtGui.QImage.Format_RGB888)
|
|
39
|
+ self.VideoSignal1.emit(qt_image1)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+ if self.flag:
|
|
43
|
+ img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
44
|
+ img_canny = cv2.Canny(img_gray, 50, 100)
|
|
45
|
+
|
|
46
|
+ qt_image2 = QtGui.QImage(img_canny.data,
|
|
47
|
+ self.width,
|
|
48
|
+ self.height,
|
|
49
|
+ img_canny.strides[0],
|
|
50
|
+ QtGui.QImage.Format_Grayscale8)
|
|
51
|
+
|
|
52
|
+ self.VideoSignal2.emit(qt_image2)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ loop = QtCore.QEventLoop()
|
|
56
|
+ QtCore.QTimer.singleShot(25, loop.quit) #25 ms
|
|
57
|
+ loop.exec_()
|
|
58
|
+
|
|
59
|
+ @QtCore.pyqtSlot()
|
|
60
|
+ def canny(self):
|
|
61
|
+ self.flag = 1 - self.flag
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+class ImageViewer(QtWidgets.QWidget):
|
|
65
|
+ def __init__(self, parent=None):
|
|
66
|
+ super(ImageViewer, self).__init__(parent)
|
|
67
|
+ self.image = QtGui.QImage()
|
|
68
|
+ self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
|
|
69
|
+
|
|
70
|
+ def paintEvent(self, event):
|
|
71
|
+ painter = QtGui.QPainter(self)
|
|
72
|
+ painter.drawImage(0, 0, self.image)
|
|
73
|
+ self.image = QtGui.QImage()
|
|
74
|
+
|
|
75
|
+ def initUI(self):
|
|
76
|
+ self.setWindowTitle('Test')
|
|
77
|
+
|
|
78
|
+ @QtCore.pyqtSlot(QtGui.QImage)
|
|
79
|
+ def setImage(self, image):
|
|
80
|
+ if image.isNull():
|
|
81
|
+ print("Viewer Dropped frame!")
|
|
82
|
+
|
|
83
|
+ self.image = image
|
|
84
|
+ if image.size() != self.size():
|
|
85
|
+ self.setFixedSize(image.size())
|
|
86
|
+ self.update()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+if __name__ == '__main__':
|
|
90
|
+ app = QtWidgets.QApplication(sys.argv)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+ thread = QtCore.QThread()
|
|
94
|
+ thread.start()
|
|
95
|
+ vid = ShowVideo()
|
|
96
|
+ vid.moveToThread(thread)
|
|
97
|
+
|
|
98
|
+ image_viewer1 = ImageViewer()
|
|
99
|
+ image_viewer2 = ImageViewer()
|
|
100
|
+
|
|
101
|
+ vid.VideoSignal1.connect(image_viewer1.setImage)
|
|
102
|
+ vid.VideoSignal2.connect(image_viewer2.setImage)
|
|
103
|
+
|
|
104
|
+ push_button1 = QtWidgets.QPushButton('Start')
|
|
105
|
+ push_button2 = QtWidgets.QPushButton('Canny')
|
|
106
|
+ push_button1.clicked.connect(vid.startVideo)
|
|
107
|
+ push_button2.clicked.connect(vid.canny)
|
|
108
|
+
|
|
109
|
+ vertical_layout = QtWidgets.QVBoxLayout()
|
|
110
|
+ horizontal_layout = QtWidgets.QHBoxLayout()
|
|
111
|
+ horizontal_layout.addWidget(image_viewer1)
|
|
112
|
+ horizontal_layout.addWidget(image_viewer2)
|
|
113
|
+ vertical_layout.addLayout(horizontal_layout)
|
|
114
|
+ vertical_layout.addWidget(push_button1)
|
|
115
|
+ vertical_layout.addWidget(push_button2)
|
|
116
|
+
|
|
117
|
+ layout_widget = QtWidgets.QWidget()
|
|
118
|
+ layout_widget.setLayout(vertical_layout)
|
|
119
|
+
|
|
120
|
+ main_window = QtWidgets.QMainWindow()
|
|
121
|
+ main_window.setCentralWidget(layout_widget)
|
|
122
|
+ main_window.show()
|
|
123
|
+ sys.exit(app.exec_())
|