
OpenCV에서 캡처한 영상을 pyQt5 윈도우에 보여주기OpenCV/Qt2023. 10. 13. 22:36
Table of Contents
반응형
OpenCV에서 캡처한 영상을 pyQt5로 작성된 GUI에서 보여주는 방법을 다룹니다.
깃허브에 있는 코드를 수정하여 사용했습니다.
2019. 1. 14 최초작성
처음 실행하면 버튼 2개만 보입니다.
start 버튼을 클릭하면 웹캠 영상이 보입니다.
Canny 버튼을 클릭하면 오른쪽에 캐니 영상을 보여줍니다.
# 출처 - https://github.com/ddd4117/GUI/blob/master/src/camera_test.py # 수정 - webnautes import cv2 import sys from PyQt5 import QtCore from PyQt5 import QtWidgets from PyQt5 import QtGui class ShowVideo(QtCore.QObject): flag = 0 camera = cv2.VideoCapture(0) ret, image = camera.read() height, width = image.shape[:2] VideoSignal1 = QtCore.pyqtSignal(QtGui.QImage) VideoSignal2 = QtCore.pyqtSignal(QtGui.QImage) def __init__(self, parent=None): super(ShowVideo, self).__init__(parent) @QtCore.pyqtSlot() def startVideo(self): global image run_video = True while run_video: ret, image = self.camera.read() color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) qt_image1 = QtGui.QImage(color_swapped_image.data, self.width, self.height, color_swapped_image.strides[0], QtGui.QImage.Format_RGB888) self.VideoSignal1.emit(qt_image1) if self.flag: img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) img_canny = cv2.Canny(img_gray, 50, 100) qt_image2 = QtGui.QImage(img_canny.data, self.width, self.height, img_canny.strides[0], QtGui.QImage.Format_Grayscale8) self.VideoSignal2.emit(qt_image2) loop = QtCore.QEventLoop() QtCore.QTimer.singleShot(25, loop.quit) #25 ms loop.exec_() @QtCore.pyqtSlot() def canny(self): self.flag = 1 - self.flag class ImageViewer(QtWidgets.QWidget): def __init__(self, parent=None): super(ImageViewer, self).__init__(parent) self.image = QtGui.QImage() self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent) def paintEvent(self, event): painter = QtGui.QPainter(self) painter.drawImage(0, 0, self.image) self.image = QtGui.QImage() def initUI(self): self.setWindowTitle('Test') @QtCore.pyqtSlot(QtGui.QImage) def setImage(self, image): if image.isNull(): print("Viewer Dropped frame!") self.image = image if image.size() != self.size(): self.setFixedSize(image.size()) self.update() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) thread = QtCore.QThread() thread.start() vid = ShowVideo() vid.moveToThread(thread) image_viewer1 = ImageViewer() image_viewer2 = ImageViewer() vid.VideoSignal1.connect(image_viewer1.setImage) vid.VideoSignal2.connect(image_viewer2.setImage) push_button1 = QtWidgets.QPushButton('Start') push_button2 = QtWidgets.QPushButton('Canny') push_button1.clicked.connect(vid.startVideo) push_button2.clicked.connect(vid.canny) vertical_layout = QtWidgets.QVBoxLayout() horizontal_layout = QtWidgets.QHBoxLayout() horizontal_layout.addWidget(image_viewer1) horizontal_layout.addWidget(image_viewer2) vertical_layout.addLayout(horizontal_layout) vertical_layout.addWidget(push_button1) vertical_layout.addWidget(push_button2) layout_widget = QtWidgets.QWidget() layout_widget.setLayout(vertical_layout) main_window = QtWidgets.QMainWindow() main_window.setCentralWidget(layout_widget) main_window.show() sys.exit(app.exec_()) |
최초 작성 2019. 1. 24
반응형
'OpenCV > Qt' 카테고리의 다른 글
OpenCV에서 캡처한 webcam 영상을 QT에서 보여주기 (0) | 2023.10.13 |
---|---|
OpenCV에서 로드한 image 파일을 QT에서 보여주고 간단한 영상처리 (0) | 2023.10.13 |