Linked Questions

Popular Questions

Event throttling in RxPY

Asked by At

I read video frames from OpenCV video capture and would like to drop some to keep things up-to-date in case of slow processing.

How could I ensure that frames are not become accumulated in memory using the following code?

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))

where:

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

and:

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

and:

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))

where:

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

Related Questions