打开简历和Tkinter

时间:2019-06-25 20:21:55

标签: python python-3.x opencv tkinter

我正在尝试将视频帧加载到tkinter标签。我尝试运行以下方法。我的网络摄像头打开,但标签上没有加载送纸。我应该使用时间间隔限制吗?

    self.cap = cv2.VideoCapture(0)
    self.updateCamera()


def updateCamera(self):
    # Get a frame from the video source

    ret, frame = self.cap.read()
    frame = cv2.resize(frame, (800,600))
    frame = PIL.Image.fromarray(frame)
    frame = PIL.ImageTk.PhotoImage(frame)
    self.camraLabel.configure(image=frame)
    print("Here")
    self.camraLabel.after(1000,self.updateCamera)

如果我注释掉self.camraLabel.after(1000,self.updateCamera),则标签上会出现静止图像。想不到我做错了什么。

1 个答案:

答案 0 :(得分:0)

您创建了一个永无止境的递归函数。这很不好,您能看到函数将永远不会退出吗?它还会阻止标签更新,因此您永远不会看到图像。相反,您应该使用基于时间的循环,例如以下内容:

    import time
    import cv2

    # store current time
    curr_time = time.time()
    # get camera 
    self.cap = cv2.VideoCapture(0)

    def updateCamera(self):
        # Get a frame from the video source
        ret, frame = self.cap.read()
        frame = cv2.resize(frame, (800,600))
        frame = PIL.Image.fromarray(frame)
        frame = PIL.ImageTk.PhotoImage(frame)
        self.camraLabel.configure(image=frame)
        print("Here")

    # loop forever
    while True:
            # check if the frame needs to be updated
            if time.time()-curr_time > 1:
                    # if more then a second has passed,
                    # get a new frame and update curr_time
                    self.updateCamera()
                    curr_time = time.time()

            # do other stuf

免责声明:未经测试的代码