Python "self.running=Bool" 的线程问题

Python threading problem for "self.running=Bool"

我想为立体视觉创建相机程序。因此我认为左右相机的程序。因为我想单独为我的相机工作。否则程序会变慢并且相机会提供低 fps。无论如何...

当我尝试真实状态时;程序真正启动但是当我按下停止按钮时程序停止没有问题但是当我按下开始按钮时不会再次启动。

或者我试过None状态;程序启动没有问题,当我按下开始按钮时,凸轮 window 出现,但当我这次按下停止按钮时没有停止。

from threading import Thread
import time
import cv2
import keyboard

class Left_Frame(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.running = False # True or False or None i tried...

    def run(self):
        cap = cv2.VideoCapture(0)
        while self.running:
            success, img = cap.read()
            cv2.imshow("qwe", img)
            tecla = cv2.waitKey(1) & 0xFf

    def stop(self):
        self.running = False

    def ss(self):
        self.running=True

left=Left_Frame()

if not left.is_alive():
    left.start()

while True:
    if keyboard.is_pressed('f'):
        left.stop()
        time.sleep(1)

    if keyboard.is_pressed('s'):
        left.ss()
        time.sleep(1)

这是解决方案。

from threading import Thread
import time
import urllib.request
import cv2
import keyboard


class left_Frame(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.running = True

    def run(self):
        cap = cv2.VideoCapture("http://192.168.137.109/stream")
        state=True
        while self.running:
            # print(state)

            if state==True:
                success, img = cap.read()
                cv2.imshow("left", img)
                tecla = cv2.waitKey(1) & 0xFf

            if keyboard.is_pressed('f'):
                state = False
                cap.release()
                cv2.destroyAllWindows()

            if keyboard.is_pressed('s'):
                if not state==True:
                    cap = cv2.VideoCapture("http://192.168.137.109/stream")
                    state = True

left=left_Frame()
left.start()