SoFunction
Updated on 2024-12-16

pyqt5 example of using cv2 to display images, camera

As shown below:

#! /usr/bin/python3
# coding = utf-8
# from PyQt5 import QtGui,QtCore,Qt
import sys
from  import Qt,pyqtSignal,QSize,QRect,QMetaObject, QCoreApplication, pyqtSlot,QPropertyAnimation,QThread
from  import QIcon, QFont, QPixmap, QPainter, QImage
from  import QMainWindow, QApplication

import cv2
from  import SIGNAL, time
from qtpy importQtCore


class mycsms(QMainWindow):
    def __init__(self):
        super(mycsms, self).__init__()
        (self)
        = QImage()
        = (0)
        = Timer("updatePlay()")
        (, SIGNAL("updatePlay()"), )

    # Read the camera
    def showCamer(self):
        if ():
            ret, frame= ()
        else:
            ret = False
        # Read and write disk mode
        # ("",frame)
        #("")

        height, width, bytesPerComponent= 
        bytesPerLine = bytesPerComponent* width
        # Transform color space order
        (frame, cv2.COLOR_BGR2RGB,frame)
        # to QImage objects
        = QImage(, width, height, bytesPerLine, QImage.Format_RGB888)
        (())

if __name__ == "__main__":
    app = QApplication()
    myshow = mycsms()
    ()
    ()
    (app.exec_())

# Thread class:
class Timer():

    def __init__(self, signal="updateTime()", parent=None):
        super(Timer, self).__init__(parent)
        = False
        = signal
        = ()

    def run(self):
        with ():
            = False
        while True:
            if :
                return
            (())
            #40 milliseconds to send a signal
            (0.04)

    def stop(self):
        with ():
            = True

    def isStoped(self):
        with ():
            return 

Above this pyqt5 using cv2 display image,camera example is all I share with you, I hope to give you a reference, and I hope you support me more.