繁体   English   中英

如何在 Python 中将 Function 连接到 PyQT5 GUI

[英]How to Connect Function to PyQT5 GUI in Python

import sys
from os import path

import cv2
import numpy as np

from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui

import pytesseract
from PIL import Image
from pytesseract import image_to_string
from gtts import gTTS
import os


pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"


tessdata_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'



class RecordVideo(QtCore.QObject):
    image_data = QtCore.pyqtSignal(np.ndarray)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.camera = cv2.VideoCapture(0)

        self.timer = QtCore.QBasicTimer()

    def start_recording(self):
        self.timer.start(0, self)

    
    def timerEvent(self, event):
        if (event.timerId() != self.timer.timerId()):
            return

        read, data = self.camera.read()
        if read:
            self.image_data.emit(data)
    def framesave(self):
        
        read, data = self.camera.read()
        if read:
            cv2.imwrite('a.png',data)
            img=Image.fromarray(data)
            img.load()
            
            text=pytesseract.image_to_string(img, lang='spa', config=tessdata_dir_config)
        


class FaceDetectionWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.image = QtGui.QImage()
        self._red = (0, 0, 255)
        self._width = 2
        self._min_size = (30, 30)


    def image_data_slot(self, image_data):


        
        self.image = self.get_qimage(image_data)
        if self.image.size() != self.size():
            self.setFixedSize(self.image.size())

        self.update()
    
        
        
    def get_qimage(self, image: np.ndarray):
        height, width, colors = image.shape
        bytesPerLine = 3 * width
        QImage = QtGui.QImage

        image = QImage(image.data,
                       width,
                       height,
                       bytesPerLine,
                       QImage.Format_RGB888)

        image = image.rgbSwapped()
        return image

def static_ROI(self, cropped:np.ndarray):
    # height, width = image.shape[:2]
    #
    # top_left_x = int(width / 3)
    # top_left_y = int((height / 2) + (height / 4))
    # bottom_right_x = int((width / 3) * 2)
    # bottom_right_y = int((height / 2) - (height / 4))
    #
    # cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), 255, 3)
    #
    # image = image[bottom_right_y:top_left_y, top_left_x:bottom_right_x]

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawImage(0, 0, self.image)
        self.image = QtGui.QImage()


class MainWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.face_detection_widget = FaceDetectionWidget()

        # TODO: set video port
        self.record_video = RecordVideo()

        image_data_slot = self.face_detection_widget.image_data_slot
        self.record_video.image_data.connect(image_data_slot)

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(self.face_detection_widget)
        self.run_button = QtWidgets.QPushButton('Start')
        layout.addWidget(self.run_button)

        self.run_button.clicked.connect(self.record_video.start_recording)

        self.screenshot = QtWidgets.QPushButton('Snap Shot')
        layout.addWidget(self.screenshot)

        self.screenshot.clicked.connect(self.record_video.framesave)
        self.setLayout(layout)


    
def main():
    app = QtWidgets.QApplication(sys.argv)

    main_window = QtWidgets.QMainWindow()
    main_widget = MainWidget()
    main_window.setCentralWidget(main_widget)
    main_window.show()

    sys.exit(app.exec_())


if __name__ == '__main__':

    main()

以上是在实时相机视图上进行字符识别的代码。 我想添加 static ROI 以限制感兴趣的区域,下面的代码执行此操作:

height, width = frame.shape[:2]

            # Define ROI Box Dimensions
            top_left_x = int(width / 3)
            top_left_y = int((height / 2) + (height / 4))
            bottom_right_x = int((width / 3) * 2)
            bottom_right_y = int((height / 2) - (height / 4))

            # Draw rectangular window for our region of interest
            cv2.rectangle(frame, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), 255, 3)

            # Crop window of observation we defined above
            cropped = frame[bottom_right_y:top_left_y, top_left_x:bottom_right_x]

如何为下面的 function 建立 GUI 连接?

我尝试的是在 FaceDetectionWidget 类下调用这个 function 但它没有用。 我想要的是:

具有 ROI 的 LiveView

我想知道我应该在哪个 class 创建这个 function 以及如何调用它。

我会让它更简单,并在小部件的paintEvent 中绘制 ROI。

class FaceDetectionWidget(QtWidgets.QWidget):
    drawROI = True
    # ...
    def setDrawROI(self, draw):
        self.drawROI = draw
        self.update()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawImage(0, 0, self.image)
        if not self.drawROI:
            return
        width = self.image.width()
        height = self.image.height()
        painter.setPen(QtGui.QPen(QtCore.Qt.red, 2))
        painter.drawRect(width / 3, height / 4, width / 3, height / 2)

然后,您可以使用setDrawROI(True/False)启用/禁用 ROI。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM