带有树莓派硬件的 PyQt5 GUI

问题描述

我正在编写使用 basler 相机进行对象测试的代码,并为其创建了一个基于 PyQt5 的 GUI。我的 GUI 工作正常,但现在我必须为其集成外部硬件。为了。前任。我必须从 GUI 中选择和对象,然后我将按下外部开关,聚焦灯将打开捕获图像并将其与给定的宽度条件进行比较并在 GUI 上显示输出,即选择了哪个作业以及它是否匹配。

当我按下开关时,我的 GUI 冻结并且我没有在 GUI 屏幕上看到结果。 如果我做错了什么,请帮助我

import cv2
import numpy as np
import time
from pypylon import pylon
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel,QComboBox
from RPi import GPIO as GPIO
GPIO.setwarnings(False)

class Receipe(QMainWindow):
    
    def __init__(self):
        super().__init__()
        combo = QComboBox(self)
        combo.addItem("A")
        combo.addItem("B")
        combo.addItem("C")
        combo.move(50,50)
        self.qlabel = QLabel(self)
        self.qlabel.move(50,16)
        combo.activated[str].connect(self.onChanged)
        self.status = QLabel(self)
        self.status.move(50,86)
        self.setGeometry(50,50,320,200)
        self.setWindowTitle("Receipe Selection")
        self.show()
        
        
        
        self.light_switch = 16
        self.relay_light = 8
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.light_switch,GPIO.IN)
        GPIO.setup(self.relay_light,GPIO.OUT)
        GPIO.output(self.relay_light,False)

    def onChanged(self,text):
        self.qlabel.setText("Type"+" "+text+" "+"Job Is Selected")
        self.qlabel.adjustSize()
        
        #mention coondition here
        img = Camera().frame
        print ("Capture Frame")
        while True:
            if GPIO.input(self.light_switch) == 0:
                print ("Switch Pressed")
                time.sleep(1)
                GPIO.output(self.relay_light,True)
                time.sleep(1)
                process = ImgProcess()
                print ("Process Image")
                GPIO.output(self.relay_light,False)
                result = process.process(text,img)
                print(result)              
                if result == True:
                    print("in result")
                    self.status.setText("Ok")
                else:
                    self.status.setText("Not Ok")


class Camera:
    def __init__(self):
        self.serial_no = "23252513"
        self.ExposureTime = 4000
        self.device_id = None
        devices = [i.GetSerialNumber() for i in pylon.TlFactory.GetInstance().EnumerateDevices()]
        if self.serial_no in devices:
                self.device_id = "23252513"
        else:
            print('Camera not found')

        info = pylon.DeviceInfo()
        info.SetSerialNumber(self.serial_no)
        self.camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice(info))
        # self.camera = pylon.InstantCamera(pylon.TlFactory.CreateDevice(self.device_id))
        self.camera.Open()
        self.camera.ExposureAuto.SetValue('Off')
        self.camera.ExposureTimeRaw.SetValue(self.ExposureTime)
        self.camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)


    @property
    def frame(self):
        if self.device_id is not None:
            converter = pylon.ImageFormatConverter()
            converter.OutputPixelFormat = pylon.PixelType_BGR8packed
            converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

            while self.camera.IsGrabbing():
                try:
                    try:
                        grabResult = self.camera.RetrieveResult(5000,pylon.TimeoutHandling_ThrowException)
                    except pylon.TimeoutHandling_ThrowException as e:
                        print(e)
                    except Exception as e:
                        print(e)

                    if grabResult.GrabSucceeded():
                        frame = converter.Convert(grabResult).GetArray()
                        return frame
                    else:
                        print('Camera not found')
                    grabResult.Release()
                except Exception as e:
                    print(e)
            else:
                print('Camera not found')
            self.camera.StopGrabbing()

    def close(self):
        self.camera.Close()

class ImgProcess:
    def __init__(self):
        self.data = None
        self.relay_ok = 10
        self.relay_faulty = 12
        GPIO.setup(self.relay_ok,GPIO.OUT)
        GPIO.setup(self.relay_faulty,GPIO.OUT)
        GPIO.output(self.relay_ok,False)
        GPIO.output(self.relay_faulty,False)
        
    def process(self,txt,img):
        self.data = txt
        result = ""
        status = True
        oimg = img
        gimg = cv2.cvtColor(oimg,cv2.COLOR_BGR2GRAY)
        bw_img = cv2.inRange(gimg,130)
        kernel = np.ones((2,2),np.uint8)
        erode_img = cv2.dilate(bw_img,kernel=kernel,iterations=1)
        cntr,hierarchy = cv2.findContours(erode_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        cntr = sorted(cntr,key=cv2.contourArea)
        job_cntr = cntr[-1]
        x,y,w,h = cv2.boundingRect(job_cntr)
        cv2.rectangle(oimg,(x,y),(x + w,y + h),(0,255),2)
        # print(x,h)

        if w < 700:
            result = "A"
            print("TYPE-A Job Is Detected")
        elif (w > 800) and (w < 900):
            result = "B"
            print("TYPE-B Job Is Detected")
        elif (w > 900) and (w < 1000):
            result = "C"
            print("TYPE-C Job Is Detected")
        else:
            print("INVALID JOB")

        if result == self.data:
            GPIO.output(self.relay_ok,True)
            time.sleep(1)
            GPIO.output(self.relay_ok,False)
            print("Ok")
            
        else:
            GPIO.output(self.relay_faulty,True)
            time.sleep(1)
            GPIO.output(self.relay_faulty,False)
            print("Faulty")
            status = False
        
        return status


if __name__ == '__main__':
    app = QApplication(sys.argv)
    recp = Receipe()
    sys.exit(app.exec_())

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)