Two cameras work
Change-Id: Ibd6f0454d5d67c57c4a7735b79a854c0d3a9c59f
diff --git a/person_detection/base_camera.py b/person_detection/base_camera.py
index a479166..88a1e0e 100644
--- a/person_detection/base_camera.py
+++ b/person_detection/base_camera.py
@@ -52,20 +52,23 @@
class BaseCamera(object):
- thread = None # background thread that reads frames from camera
- frame = None # current frame is stored here by background thread
- last_access = 0 # time of last client access to the camera
- event = CameraEvent()
- port = 0 # default starting port offset
+ thread = {} # background thread that reads frames from camera
+ frame = {} # current frame is stored here by background thread
+ last_access = {} # time of last client access to the camera
+ event = {}
- def __init__(self):
+ def __init__(self, device=None):
"""Start the background camera thread if it isn't running yet."""
- if BaseCamera.thread is None:
- BaseCamera.last_access = time.time()
+ self.device = device
+ BaseCamera.event[self.device] = CameraEvent()
+ if self.device not in BaseCamera.thread:
+ BaseCamera.thread[self.device] = None
+ if BaseCamera.thread[self.device] is None:
+ BaseCamera.last_access[self.device] = time.time()
# start background frame thread
- BaseCamera.thread = threading.Thread(target=self._thread)
- BaseCamera.thread.start()
+ BaseCamera.thread[self.device] = threading.Thread(target=self._thread, args=(self.device))
+ BaseCamera.thread[self.device].start()
# wait until frames are available
while self.get_frame() is None:
@@ -73,30 +76,30 @@
def get_frame(self):
"""Return the current camera frame."""
- BaseCamera.last_access = time.time()
+ BaseCamera.last_access[self.device] = time.time()
# wait for a signal from the camera thread
- BaseCamera.event.wait()
- BaseCamera.event.clear()
+ BaseCamera.event[self.device].wait()
+ BaseCamera.event[self.device].clear()
- return BaseCamera.frame
+ return BaseCamera.frame[self.device]
def frames(self):
""""Generator that returns frames from the camera."""
raise NotImplementedError('Must be implemented by subclasses.')
- def _thread(self):
+ def _thread(self, device):
"""Camera background thread."""
frames_iterator = self.frames()
for frame in frames_iterator:
- BaseCamera.frame = frame
- BaseCamera.event.set() # send signal to clients
+ BaseCamera.frame[device] = frame
+ BaseCamera.event[device].set() # send signal to clients
time.sleep(0)
# if there hasn't been any clients asking for frames in
# the last 10 seconds then stop the thread
- if time.time() - BaseCamera.last_access > 10:
+ if time.time() - BaseCamera.last_access[device] > 10:
frames_iterator.close()
print('Stopping camera thread due to inactivity.')
break
- BaseCamera.thread = None
+ BaseCamera.thread[device] = None