RxPY中的事件限制

时间:2019-02-03 06:24:01

标签: python-3.x opencv rx-py

我从OpenCV视频捕获中读取了视频帧,并希望丢弃一些帧以保持最新状态,以免处理速度慢。

如何使用以下代码确保不会在内存中累积帧?

class ImageSource(Subject):
    def arlo(cameraId: int, login: str, passwd: str):
        return SyncSource(ArloSource(cameraId, login, passwd))

    def file(path: str):
        return SyncSource(FileSource(path))

其中:

class SyncSource(ImageSource):
    def __init__(self, source: ImageSource):
        super().__init__()
        source.observe_on(EventLoopScheduler()) \
            .subscribe(self)

和:

class FileSource(CaptureSource):
    def __init__(self, path: str):
        super().__init__(VideoCapture(path))

和:

class ArloSource(CaptureSource):
    def __init__(self, cameraId: int, login: str, passwd: str):        
        arlo = Arlo(login, passwd)
        basestations = arlo.GetDevices('basestation')
        cameras = arlo.GetDevices('camera')
        streamUrl = arlo.StartStream(basestations[0], cameras[cameraId])
        super().__init__(VideoCapture(streamUrl))

其中:

class CaptureSource(ImageSource):
    def __init__(self, capture: VideoCapture):
        super().__init__()
        self.thread = Thread(target = self.read, args = [capture])
        self.thread.start()

    def read(self, capture: VideoCapture):
        while(True):
            ok, frame = capture.read()
            if(ok):
                self.on_next(Image(frame))
            else:
                self.on_completed()
                break

0 个答案:

没有答案