[AI] PyQT5 Webcam 사진 촬영

Bora Kwon·2022년 8월 1일
0
import cv2
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
import time, datetime

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()


class ShowVideo(QtCore.QObject):
    flag = 1
    camera = cv2.VideoCapture(0)

    ret, image = camera.read()
    height, width = image.shape[:2]

    VideoSignal1 = QtCore.pyqtSignal(QtGui.QImage)
    VideoSignal2 = QtCore.pyqtSignal(QtGui.QImage)
    VideoSignal3 = 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_image = QtGui.QImage(color_swapped_image.data,
                                    self.width,
                                    self.height,
                                    color_swapped_image.strides[0],
                                    QtGui.QImage.Format_RGB888)

            self.VideoSignal1.emit(qt_image)
            self.VideoSignal2.emit(qt_image)
            self.VideoSignal3.emit(qt_image)

            loop = QtCore.QEventLoop()
            QtCore.QTimer.singleShot(25, loop.quit) #25 ms
            loop.exec_()

    @QtCore.pyqtSlot()
    def stopVideo(self):
        ret, image = self.camera.read()


    @QtCore.pyqtSlot()
    def savePicture(self):
        # 이미지 저장하는 함수
        ret, img = self.camera.read()
        if ret:
            now = datetime.datetime.now()
            date = now.strftime('%Y%m%d')
            filename = './data/{}_{}.png'.format(date, self.flag)
            self.flag = self.flag + 1
            cv2.imwrite(filename, img)
            # self.image_queue.put_nowait(filename)



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    thread = QtCore.QThread()
    thread.start()
    vid = ShowVideo()
    vid.moveToThread(thread)

    image_viewer1 = ImageViewer()
    image_viewer2 = ImageViewer()
    image_viewer3 = ImageViewer()

    vid.VideoSignal1.connect(image_viewer1.setImage)
    vid.VideoSignal2.connect(image_viewer2.setImage)
    vid.VideoSignal3.connect(image_viewer3.setImage)

    push_button1 = QtWidgets.QPushButton('시작')
    push_button2 = QtWidgets.QPushButton('캡쳐')
    push_button3 = QtWidgets.QPushButton('저장')
    push_button4 = QtWidgets.QPushButton('취소')
    push_button1.clicked.connect(vid.startVideo)
    push_button2.clicked.connect(vid.stopVideo)
    push_button3.clicked.connect(vid.savePicture)
    push_button4.clicked.connect(vid.startVideo)
    # push_button2.clicked.connect(vid.canny)

    layout = QtWidgets.QVBoxLayout()
    box01 = QtWidgets.QHBoxLayout()
    box02 = QtWidgets.QHBoxLayout()

    # vertical_layout = QtWidgets.QVBoxLayout()
    # horizontal_layout = QtWidgets.QHBoxLayout()
    box01.addWidget(image_viewer1)
    box01.addWidget(image_viewer2)
    box02.addWidget(image_viewer3)
    # vertical_layout.addLayout(horizontal_layout)
    box02.addWidget(push_button1)
    box02.addWidget(push_button2)
    box02.addWidget(push_button3)
    box02.addWidget(push_button4)
    layout.addLayout(box01)
    layout.addLayout(box02)
    layout_widget = QtWidgets.QWidget()
    layout_widget.setLayout(layout)

    main_window = QtWidgets.QMainWindow()
    main_window.setCentralWidget(layout_widget)
    main_window.show()
    sys.exit(app.exec_())
profile
Software Developer

0개의 댓글