Created
November 29, 2021 08:45
-
-
Save zdenop/a94e7ed7fae9e8d6a579390d32e1f565 to your computer and use it in GitHub Desktop.
Read barcode with QT5 camera without cv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import io | |
| import sys | |
| import beepy | |
| from PIL import Image | |
| from PyQt5 import QtCore, QtMultimedia, QtGui | |
| from PyQt5.QtMultimedia import * | |
| from PyQt5.QtMultimediaWidgets import * | |
| from PyQt5.QtWidgets import * | |
| from pyzbar import pyzbar | |
| class MainWindow(QMainWindow): | |
| def __init__(self): | |
| super().__init__() | |
| self.available_cameras = QCameraInfo.availableCameras() | |
| if not self.available_cameras: | |
| print("No camera found.") | |
| sys.exit() | |
| self.known_codes = set() | |
| self.camera = QCamera(self.available_cameras[0]) | |
| self.camera.setCaptureMode(QCamera.CaptureViewfinder) | |
| self.camera.imageProcessing().setWhiteBalanceMode(QCameraImageProcessing.WhiteBalanceAuto) | |
| self.current_camera_name = self.available_cameras[0].description() | |
| self.capture = QCameraImageCapture(self.camera) | |
| self.capture.setCaptureDestination(QCameraImageCapture.CaptureToBuffer) | |
| self.probe = QtMultimedia.QVideoProbe(self) | |
| self.probe.videoFrameProbed.connect(self.processFrame) | |
| self.probe.setSource(self.camera) | |
| exposure = self.camera.exposure() | |
| exposure.setExposureMode(QCameraExposure.ExposureAuto) | |
| exposure.setExposureCompensation(0) | |
| exposure.setMeteringMode(QCameraExposure.MeteringSpot) | |
| self.status = QStatusBar() | |
| self.setStatusBar(self.status) | |
| self.status.showMessage(f"Using {self.current_camera_name}") | |
| self.viewfinder = QCameraViewfinder() | |
| self.viewfinder.show() | |
| self.setCentralWidget(self.viewfinder) | |
| self.camera.setViewfinder(self.viewfinder) | |
| toolbar = QToolBar() | |
| self.addToolBar(toolbar) | |
| start_action = QAction( | |
| self.style().standardIcon(QStyle.SP_MediaPlay), "Start camera", self | |
| ) | |
| start_action.triggered.connect(self.startCamera) | |
| toolbar.addAction(start_action) | |
| stop_action = QAction( | |
| self.style().standardIcon(QStyle.SP_MediaStop), "Stop camera", self | |
| ) | |
| stop_action.triggered.connect(self.stopCamera) | |
| toolbar.addAction(stop_action) | |
| self.setGeometry(100, 100, 800, 600) | |
| self.show() | |
| def startCamera(self): | |
| self.camera.start() | |
| def stopCamera(self): | |
| self.camera.stop() | |
| def processFrame(self, frame): | |
| QApplication.processEvents() | |
| if frame.isValid(): | |
| cloneFrame = QVideoFrame(frame) | |
| cloneFrame.map(QAbstractVideoBuffer.MapMode.ReadOnly) | |
| buffer = QtCore.QBuffer() | |
| buffer.open(QtCore.QBuffer.ReadWrite) | |
| frame.image().save(buffer, "JPEG") | |
| pil_im = Image.open(io.BytesIO(buffer.data())) | |
| barcodes = pyzbar.decode(pil_im) | |
| for barcode in barcodes: | |
| data = barcode.data.decode("utf-8") | |
| if data not in self.known_codes: | |
| self.known_codes.add(data) | |
| message = f"Found Type : {barcode.type} Barcode : {data}" | |
| self.status.showMessage(message) | |
| print(message) | |
| beepy.beep() | |
| cloneFrame.unmap() | |
| def closeEvent(self, event): | |
| if self.probe.isActive(): | |
| self.probe.videoFrameProbed.disconnect(self.processFrame) | |
| self.probe.deleteLater() | |
| self.camera.stop() | |
| event.accept() | |
| if __name__ == "__main__": | |
| app = QApplication(sys.argv) | |
| app.setStyle(QStyleFactory.create("fusion")) | |
| mainWindow = MainWindow() | |
| mainWindow.show() | |
| sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment