Two cameras work

Change-Id: Ibd6f0454d5d67c57c4a7735b79a854c0d3a9c59f
diff --git a/person_detection/app.py b/person_detection/app.py
index 5e3a5c7..c188163 100644
--- a/person_detection/app.py
+++ b/person_detection/app.py
@@ -34,7 +34,7 @@
 def video_feed(device):
     """Video streaming route. Put this in the src attribute of an img tag."""
     global args
-    camera = Camera(int(device), args)
+    camera = Camera(device, args)
     return Response(gen(camera),
                     mimetype='multipart/x-mixed-replace; boundary=frame')
 
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
diff --git a/person_detection/person_detection.py b/person_detection/person_detection.py
index 32d2bc2..cf8b4f7 100644
--- a/person_detection/person_detection.py
+++ b/person_detection/person_detection.py
@@ -43,7 +43,7 @@
 
 class Camera(BaseCamera):
 
-    def __init__(self, port, args):
+    def __init__(self, device, args):
         log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
         model_xml = args.model
         model_bin = os.path.splitext(model_xml)[0] + ".bin"
@@ -66,8 +66,9 @@
             self.input_stream = 0
         elif args.input == 'gstreamer':
             # gst rtp sink
-            self.input_stream = 'udpsrc port=' + port + 'caps = " application/x-rtp, encoding-name=JPEG,payload=26" ! rtpjpegdepay ! decodebin ! videoconvert ! appsink'
+            self.input_stream = 'udpsrc port=500' + device + ' caps = " application/x-rtp, encoding-name=JPEG,payload=26" ! rtpjpegdepay ! decodebin ! videoconvert ! appsink'
             #input_stream = 'udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink'
+            print("input_stream:", self.input_stream)
         else:
             self.input_stream = args.input
             assert os.path.isfile(args.input), "Specified input file doesn't exist"
@@ -78,10 +79,9 @@
         else:
             self.labels_map = None
 
-        self.port = port
         self.args = args
 
-        super(Camera, self).__init__()
+        super(Camera, self).__init__(device)
 
     def __del__(self):
         self.cap.release()
@@ -151,7 +151,7 @@
                         ymax = int(obj[6] * initial_h)
                         class_id = int(obj[1])
                         # Draw box and label\class_id
-                        color = (min(class_id * 12.5, 255), min(class_id * 7, 255), min(class_id * 5, 255))
+                        color = (min(class_id * 12.5, 255),min(class_id * 7, 255), min(class_id * 5, 255))
                         cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
                         det_label = self.labels_map[class_id] if self.labels_map else str(class_id)
                         cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
@@ -159,19 +159,8 @@
                         # print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin,
                         #      'xmax:', xmax, 'ymax:', ymax)
 
-                # Draw performance stats
-                inf_time_message = "Inference time: Not applicable for async mode" if self.is_async_mode else \
-                    "Inference time: {:.3f} ms".format(det_time * 1000)
-                render_time_message = "OpenCV rendering time: {:.3f} ms".format(render_time * 1000)
-                if self.is_async_mode:
-                    async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id)
-                else:
-                    async_mode_message = "Async mode is off. Processing request {}".format(cur_request_id)
-
-                cv2.putText(frame, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
-                cv2.putText(frame, render_time_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
-                cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5,
-                            (10, 10, 200), 1)
+                cv2.putText(frame, self.device, (10, int(initial_h - 20)),
+                        cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
 
             render_start = time.time()