| from multiprocessing import Process, Queue |
| |
| |
| class BaseCamera(object): |
| process = {} # background process that reads frames from camera |
| frame = {} # frame queue |
| |
| def __init__(self, device=None, idle=False): |
| """Start the background camera process if it isn't running yet.""" |
| self.device = device |
| if self.device not in BaseCamera.process: |
| BaseCamera.process[self.device] = None |
| if BaseCamera.process[self.device] is None: |
| |
| self.frame[device] = Queue(100) |
| |
| # start background frame process |
| BaseCamera.process[self.device] = Process(target=self._process, args=(self.device)) |
| BaseCamera.process[self.device].start() |
| |
| # wait until frames are available |
| _ = self.get_frame() |
| |
| def get_frame(self): |
| """Return the current camera frame.""" |
| |
| # blocks |
| return BaseCamera.frame[self.device].get(block=True) |
| |
| def frames(self): |
| """"Generator that returns frames from the camera.""" |
| raise NotImplementedError('Must be implemented by subclasses.') |
| |
| def _process(self, device): |
| """Camera background process.""" |
| frames_iterator = self.frames() |
| for frame in frames_iterator: |
| BaseCamera.frame[device].put(frame, block=True) |
| |
| BaseCamera.process[device] = None |