├── README.md ├── __pycache__ └── ui_main_window.cpython-35.pyc ├── main_window.py ├── ui_main_window.py └── ui_main_window.ui /README.md: -------------------------------------------------------------------------------- 1 | # pyqt5_camera_viewer 2 | In this example, we demonstrate how to create simple camera viewer using Opencv3 and PyQt5. 3 | 4 | If you need to change ui_main_window.ui with QtDesigner, don't forget to regenerate ui_main_window.py file using this command: 5 | ```{r, engine='sh', count_lines} 6 | pyuic5 main_window.ui -o main_window.py 7 | ``` 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /__pycache__/ui_main_window.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berrouba-med-amine/simple-python-camera-viewer-opencv3-PyQt5/7ee7b40b12c5562bf97839d32dcbd9aff9e12a66/__pycache__/ui_main_window.cpython-35.pyc -------------------------------------------------------------------------------- /main_window.py: -------------------------------------------------------------------------------- 1 | """ 2 | In this example, we demonstrate how to create simple camera viewer using Opencv3 and PyQt5 3 | 4 | Author: Berrouba.A 5 | Last edited: 21 Feb 2018 6 | """ 7 | 8 | # import system module 9 | import sys 10 | 11 | # import some PyQt5 modules 12 | from PyQt5.QtWidgets import QApplication 13 | from PyQt5.QtWidgets import QWidget 14 | from PyQt5.QtGui import QImage 15 | from PyQt5.QtGui import QPixmap 16 | from PyQt5.QtCore import QTimer 17 | 18 | # import Opencv module 19 | import cv2 20 | 21 | from ui_main_window import * 22 | 23 | class MainWindow(QWidget): 24 | # class constructor 25 | def __init__(self): 26 | # call QWidget constructor 27 | super().__init__() 28 | self.ui = Ui_Form() 29 | self.ui.setupUi(self) 30 | 31 | # create a timer 32 | self.timer = QTimer() 33 | # set timer timeout callback function 34 | self.timer.timeout.connect(self.viewCam) 35 | # set control_bt callback clicked function 36 | self.ui.control_bt.clicked.connect(self.controlTimer) 37 | 38 | # view camera 39 | def viewCam(self): 40 | # read image in BGR format 41 | ret, image = self.cap.read() 42 | # convert image to RGB format 43 | image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 44 | # get image infos 45 | height, width, channel = image.shape 46 | step = channel * width 47 | # create QImage from image 48 | qImg = QImage(image.data, width, height, step, QImage.Format_RGB888) 49 | # show image in img_label 50 | self.ui.image_label.setPixmap(QPixmap.fromImage(qImg)) 51 | 52 | # start/stop timer 53 | def controlTimer(self): 54 | # if timer is stopped 55 | if not self.timer.isActive(): 56 | # create video capture 57 | self.cap = cv2.VideoCapture(0) 58 | # start timer 59 | self.timer.start(20) 60 | # update control_bt text 61 | self.ui.control_bt.setText("Stop") 62 | # if timer is started 63 | else: 64 | # stop timer 65 | self.timer.stop() 66 | # release video capture 67 | self.cap.release() 68 | # update control_bt text 69 | self.ui.control_bt.setText("Start") 70 | 71 | 72 | if __name__ == '__main__': 73 | app = QApplication(sys.argv) 74 | 75 | # create and show mainWindow 76 | mainWindow = MainWindow() 77 | mainWindow.show() 78 | 79 | sys.exit(app.exec_()) -------------------------------------------------------------------------------- /ui_main_window.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file 'main_window.ui' 4 | # 5 | # Created by: PyQt5 UI code generator 5.5.1 6 | # 7 | # WARNING! All changes made in this file will be lost! 8 | 9 | from PyQt5 import QtCore, QtGui, QtWidgets 10 | 11 | class Ui_Form(object): 12 | def setupUi(self, Form): 13 | Form.setObjectName("Form") 14 | Form.resize(525, 386) 15 | self.horizontalLayout = QtWidgets.QHBoxLayout(Form) 16 | self.horizontalLayout.setObjectName("horizontalLayout") 17 | self.verticalLayout = QtWidgets.QVBoxLayout() 18 | self.verticalLayout.setObjectName("verticalLayout") 19 | self.image_label = QtWidgets.QLabel(Form) 20 | self.image_label.setObjectName("image_label") 21 | self.verticalLayout.addWidget(self.image_label) 22 | self.control_bt = QtWidgets.QPushButton(Form) 23 | self.control_bt.setObjectName("control_bt") 24 | self.verticalLayout.addWidget(self.control_bt) 25 | self.horizontalLayout.addLayout(self.verticalLayout) 26 | 27 | self.retranslateUi(Form) 28 | QtCore.QMetaObject.connectSlotsByName(Form) 29 | 30 | def retranslateUi(self, Form): 31 | _translate = QtCore.QCoreApplication.translate 32 | Form.setWindowTitle(_translate("Form", "Cam view")) 33 | self.image_label.setText(_translate("Form", "TextLabel")) 34 | self.control_bt.setText(_translate("Form", "Start")) 35 | -------------------------------------------------------------------------------- /ui_main_window.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Form 4 | 5 | 6 | 7 | 0 8 | 0 9 | 525 10 | 386 11 | 12 | 13 | 14 | Cam view 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | TextLabel 23 | 24 | 25 | 26 | 27 | 28 | 29 | Start 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | --------------------------------------------------------------------------------