在Raspberry PI中使用Tkinter或guizero将视频源(OpenCV + picamera)输出到GUI?

时间:2019-03-10 12:17:21

标签: python opencv tkinter raspberry-pi

我正在关注本教程 https://solarianprogrammer.com/2018/04/21/python-opencv-show-video-tkinter-window/ 并发现它仅适用于USB相机。

我有在OpenCV脚本上使用picamera的这段代码。

class VideoStream:
    def __init__(self, resolution=(640,480),framerate=30):
        from picamera.array import PiRGBArray
        from picamera import PiCamera
        self.camera = PiCamera()
        self.camera.resolution = resolution
        self.camera.framerate = framerate
        self.rawCapture = PiRGBArray(self.camera,size=resolution)
        self.stream = self.camera.capture_continuous(
            self.rawCapture, format = "bgr", use_video_port = True)

        self.frame = []
        self.stopped = False

    def start(self):
        Thread(target=self.update,args=()).start()
        return self

    def update(self):
        for f in self.stream:
            self.frame = f.array
            self.rawCapture.truncate(0)
            if self.stopped:
                self.stream.close()
                self.rawCapture.close()
                self.camera.close()

    def read(self):
         return self.frame

    def stop(self):
         self.stopped = True

如果我使用的是OpenCV的GUI(cv2.imshow),则此方法有效,但是我想在其他GUI上显示输出,因为我想在其中添加一些文本和按钮。

0 个答案:

没有答案