opencv 多线程问题“QBasicTimer只能用于以QThread启动的线程”

j5fpnvbx  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(375)

我正在用Python 3.4开发一个程序,我使用OpenCV来检测一个实时视频中的符号,我还使用PyQt5来开发GUI。这是我的代码:

# coding=utf-8
import kivy
kivy.require("1.9.2")

from PyQt5 import QtCore, QtWidgets, uic
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import cv2
import time
import RPi.GPIO as GPIO
import sys
import threading

qtCreatorFile = "mainwindow.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow, threading.Thread):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.stackedWidget.setCurrentIndex(0)
        print("Index0")
        case=0
        self.iniciar_button.clicked.connect(self.teste,case)

    def teste(self, case):

        if case==0:
            self.stackedWidget.setCurrentIndex(1)
            t= threading.Thread(target=self.processo)
            t.start()
            print("case0")
        if case==1:
            print("case1")
            self.stackedWidget.setCurrentIndex(2)
            i=0
            while i==0:
                if self.iniciar2.clicked():
                    self.stackedWidget.setCurrentIndex(1)
                    d= threading.Thread(target=self.processo)
                    d.start()
                    i=1

##    @asyncio.coroutine    
    def processo(self):
        global garrafasA
        global garrafasR
        f=1
        while f==0:
            GPIO.setwarnings(False)
            GPIO.setmode(GPIO.BOARD)
            entradaIR = 11
            GPIO.setup(entradaIR,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
            entrada = GPIO.input(entradaIR)
            if entrada==1:
                f=1

         (do something more here but nothing important)

        while flag1 == 1 and saida == 1 and x == 1: # Activar tapete de ACEITAÇÃO
            print ("Aceitacao")
            garrafasA += 1
            time.sleep(1.5)
            saida = GPIO.input(saidaIR)
            pwm1.start(DC)
            GPIO.output(motor1A,GPIO.HIGH)
            GPIO.output(motor1B,GPIO.LOW)
            GPIO.output(motor1E,GPIO.HIGH)
            saida = GPIO.input(saidaIR)
            self.nGarrafasA.display(garrafasA)
            if saida == 0:
                break

        entrada = GPIO.input(entradaIR)
        while flag2 == 1 and entrada == 0: # Inverter tapete de ACEITAÇÃO
            print ("inverter")
            if flag3 == 0:
                garrafasR += 1
                flag3 = 1
            time.sleep(1.5)
            pwm1.start(DC)
            GPIO.output(motor1A,GPIO.LOW)
            GPIO.output(motor1B,GPIO.HIGH)
            GPIO.output(motor1E,GPIO.HIGH)
            entrada = GPIO.input(entradaIR)
            self.nGarrafasR.display(garrafasR)

        entrada = GPIO.input(entradaIR)
        while flag2 == 1 and entrada == 1: # Parar tapete de entrada na devolução              
            print ("Entrada",entrada)
            print ("Parar tapete")
            GPIO.output(motor1E,GPIO.LOW)
            pwm1.stop()
            flag2=0
            case=1
            self.teste(case)
            return

if __name__ == "__main__":
    garrafasA = 0
    garrafasR = 0
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我创建了一个新线程,以便能够运行GUI,同时在函数“processso”上进行符号检测,但一旦此函数结束,它将返回到函数“teste”上的“case1”以等待按钮“iniciar2.这里是他打印“case1”并将页面更改为索引2后的问题我得到消息“QBasicTimer只能用于以QThread”启动的线程。
有人知道我做错了什么吗?有人可以帮助我吗?我处理这个错误一段时间前,我不知道是什么问题。
先谢谢你。

yk9xbfzb

yk9xbfzb1#

QBasicTimer.stop失败,可能是尝试从其他线程停止
QBasicTimer.start定时器不能从其他线程启动

# I don't know why that message is occured....
# There is List Control to print Log Message In Main UI class

# ===================================
# Thread Class
# ===================================
class CThreadEvents(QThread):
    def __init__(self, pParent=None):
        super().__init__()
        self.m_parent = pParent

    def run(self):
        strTemp = ""
        v = 0

        while self.running:
            v = v + 1
            self.m_parent.setLogMessage(str(v))
            time.sleep(0.01)
# ===================================
# Main UI Class
# ===================================
class MainWindow(QMainWindow, form_class):
    def pushButton_LaunchEvent(self):
        self.t1 = CThreadEvents(self)
        self.t1.start()

    def setLogMessage(tempMessage):
        self.listWidgetLog.addItem(QListWidgetItem(tempMessage))

相关问题