Initial commit for web interface

Change-Id: I133eaf37221a050eb3c87e245b86ae54c610d446
diff --git a/person_detection/app.py b/person_detection/app.py
new file mode 100644
index 0000000..1b1fbde
--- /dev/null
+++ b/person_detection/app.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+from importlib import import_module
+import os
+from flask import Flask, render_template, Response
+from argparse import ArgumentParser, SUPPRESS
+
+# import camera driver
+if os.environ.get('CAMERA'):
+    Camera = import_module('camera_' + os.environ['CAMERA']).Camera
+else:
+    #from camera import Camera
+    from person_detection import Camera
+
+# Raspberry Pi camera module (requires picamera package)
+# from camera_pi import Camera
+
+app = Flask(__name__)
+
+
+@app.route('/')
+def index():
+    """Video streaming home page."""
+    return render_template('index.html')
+
+
+def gen(camera):
+    """Video streaming generator function."""
+    print("Video streaming generator function.")
+    while True:
+        frame = camera.get_frame()
+        yield (b'--frame\r\n'
+               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
+
+
+@app.route('/video_feed')
+def video_feed():
+    """Video streaming route. Put this in the src attribute of an img tag."""
+    print("video_feed()", args)
+    camera = Camera(args)
+    print("Camera: ", camera)
+    return Response(gen(camera),
+                    mimetype='multipart/x-mixed-replace; boundary=frame')
+
+def build_argparser():
+    parser = ArgumentParser(add_help=False)
+    args = parser.add_argument_group('Options')
+    args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
+    args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.",
+                      required=True, type=str)
+    args.add_argument("-i", "--input",
+                      help="Required. Path to video file or image. 'cam' for capturing video stream from camera",
+                      required=True, type=str)
+    args.add_argument("-l", "--cpu_extension",
+                      help="Optional. Required for CPU custom layers. Absolute path to a shared library with the "
+                           "kernels implementations.", type=str, default=None)
+    args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None)
+    args.add_argument("-d", "--device",
+                      help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is "
+                           "acceptable. The demo will look for a suitable plugin for device specified. "
+                           "Default value is CPU", default="CPU", type=str)
+    args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str)
+    args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
+                      default=0.5, type=float)
+    args.add_argument("-ns", help='No show output', action='store_true')
+
+    return parser
+
+if __name__ == '__main__':
+    args = build_argparser().parse_args()
+    app.run(host='0.0.0.0', threaded=True)
diff --git a/person_detection/base_camera.py b/person_detection/base_camera.py
new file mode 100644
index 0000000..96c148a
--- /dev/null
+++ b/person_detection/base_camera.py
@@ -0,0 +1,101 @@
+import time
+import threading
+try:
+    from greenlet import getcurrent as get_ident
+except ImportError:
+    try:
+        from thread import get_ident
+    except ImportError:
+        from _thread import get_ident
+
+
+class CameraEvent(object):
+    """An Event-like class that signals all active clients when a new frame is
+    available.
+    """
+    def __init__(self):
+        self.events = {}
+
+    def wait(self):
+        """Invoked from each client's thread to wait for the next frame."""
+        ident = get_ident()
+        if ident not in self.events:
+            # this is a new client
+            # add an entry for it in the self.events dict
+            # each entry has two elements, a threading.Event() and a timestamp
+            self.events[ident] = [threading.Event(), time.time()]
+        return self.events[ident][0].wait()
+
+    def set(self):
+        """Invoked by the camera thread when a new frame is available."""
+        now = time.time()
+        remove = None
+        for ident, event in self.events.items():
+            if not event[0].isSet():
+                # if this client's event is not set, then set it
+                # also update the last set timestamp to now
+                event[0].set()
+                event[1] = now
+            else:
+                # if the client's event is already set, it means the client
+                # did not process a previous frame
+                # if the event stays set for more than 5 seconds, then assume
+                # the client is gone and remove it
+                if now - event[1] > 5:
+                    remove = ident
+        if remove:
+            del self.events[remove]
+
+    def clear(self):
+        """Invoked from each client's thread after a frame was processed."""
+        self.events[get_ident()][0].clear()
+
+
+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()
+
+    def __init__(self):
+        """Start the background camera thread if it isn't running yet."""
+        if BaseCamera.thread is None:
+            BaseCamera.last_access = time.time()
+
+            # start background frame thread
+            BaseCamera.thread = threading.Thread(target=self._thread)
+            BaseCamera.thread.start()
+
+            # wait until frames are available
+            while self.get_frame() is None:
+                time.sleep(0)
+
+    def get_frame(self):
+        """Return the current camera frame."""
+        BaseCamera.last_access = time.time()
+
+        # wait for a signal from the camera thread
+        BaseCamera.event.wait()
+        BaseCamera.event.clear()
+
+        return BaseCamera.frame
+
+    def frames():
+        """"Generator that returns frames from the camera."""
+        raise RuntimeError('Must be implemented by subclasses.')
+
+    def _thread(self):
+        """Camera background thread."""
+        frames_iterator = self.frames()
+        for frame in frames_iterator:
+            BaseCamera.frame = frame
+            BaseCamera.event.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:
+                frames_iterator.close()
+                print('Stopping camera thread due to inactivity.')
+                break
+        BaseCamera.thread = None
diff --git a/person_detection/person_detection.py b/person_detection/person_detection.py
index 702f9f8..6f56a6f 100644
--- a/person_detection/person_detection.py
+++ b/person_detection/person_detection.py
@@ -13,6 +13,7 @@
 from argparse import ArgumentParser, SUPPRESS
 from imutils import build_montages
 from openvino.inference_engine import IECore
+from base_camera import BaseCamera
 
 
 def build_argparser():
@@ -40,152 +41,164 @@
     return parser
 
 
-def main():
-    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
-    args = build_argparser().parse_args()
-    model_xml = args.model
-    model_bin = os.path.splitext(model_xml)[0] + ".bin"
+class Camera(BaseCamera):
 
-    # Read IR
-    log.info("Reading IR...")
-    net = IECore().read_network(model=model_xml, weights=model_bin)
+    def __init__(self, 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"
 
-    assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
-    assert len(net.outputs) == 1, "Demo supports only single output topologies"
-    input_blob = next(iter(net.inputs))
-    out_blob = next(iter(net.outputs))
+        # Read IR
+        log.info("Reading IR...")
+        net = IECore().read_network(model=model_xml, weights=model_bin)
 
-    log.info("Loading IR to the plugin...")
-    exec_net = IECore().load_network(network=net, device_name=args.device, num_requests=2)
-    # Read and pre-process input image
-    n, c, h, w = net.inputs[input_blob].shape
-    del net
-    if args.input == 'cam':
-        input_stream = 0
-    elif args.input == 'gstreamer':
-        # gst rtp sink
-        input_stream = 'udpsrc port=5000 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'
-    else:
-        input_stream = args.input
-        assert os.path.isfile(args.input), "Specified input file doesn't exist"
+        assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
+        assert len(net.outputs) == 1, "Demo supports only single output topologies"
+        self.input_blob = next(iter(net.inputs))
+        self.out_blob = next(iter(net.outputs))
 
-    if input_stream == 'gstreamer':
-        cap = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
-    else:
-        cap = cv2.VideoCapture(input_stream)
-
-    if args.labels:
-        with open(args.labels, 'r') as f:
-            labels_map = [x.strip() for x in f]
-    else:
-        labels_map = None
-
-    cur_request_id = 0
-    next_request_id = 1
-
-    log.info("Starting inference in async mode...")
-    log.info("To switch between sync and async modes press Tab button")
-    log.info("To stop the demo execution press Esc button")
-
-    # Async doesn't work if True
-    # Request issues = Runtime Error: [REQUEST BUSY]
-    is_async_mode = False
-    #is_async_mode = True
-    render_time = 0
-    ret, frame = cap.read()
-
-    frameList = []
-
-    print("To close the application, press 'CTRL+C' or any key with focus on the output window")
-
-    while True:
-        if is_async_mode:
-            ret, next_frame = cap.read()
+        log.info("Loading IR to the plugin...")
+        self.exec_net = IECore().load_network(network=net, device_name=args.device, num_requests=2)
+        # Read and pre-process input image
+        self.n, self.c, self.h, self.w = net.inputs[self.input_blob].shape
+        del net
+        if args.input == 'cam':
+            input_stream = 0
+        elif args.input == 'gstreamer':
+            # gst rtp sink
+            input_stream = 'udpsrc port=5000 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'
         else:
-            ret, frame = cap.read()
-        if not ret:
-            break
-        initial_w = cap.get(3)
-        initial_h = cap.get(4)
+            input_stream = args.input
+            assert os.path.isfile(args.input), "Specified input file doesn't exist"
 
-        # Main sync point:
-        # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
-        # in the regular mode we start the CURRENT request and immediately wait for it's completion
-        inf_start = time.time()
-        if is_async_mode:
-            if ret:
-                in_frame = cv2.resize(next_frame, (w, h))
-                in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
-                in_frame = in_frame.reshape((n, c, h, w))
-                exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
+        if input_stream == 'gstreamer':
+            self.cap = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
         else:
-            if ret:
-                in_frame = cv2.resize(frame, (w, h))
-                in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
-                in_frame = in_frame.reshape((n, c, h, w))
-                exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
+            self.cap = cv2.VideoCapture(input_stream)
 
-        if exec_net.requests[cur_request_id].wait(-1) == 0:
-            inf_end = time.time()
-            det_time = inf_end - inf_start
+        if args.labels:
+            with open(args.labels, 'r') as f:
+                self.labels_map = [x.strip() for x in f]
+        else:
+            self.labels_map = None
 
-            # Parse detection results of the current request
-            res = exec_net.requests[cur_request_id].outputs[out_blob]
+        self.args = args
 
-            for obj in res[0][0]:
-                # Draw only objects when probability more than specified threshold
-                if obj[2] > args.prob_threshold:
-                    xmin = int(obj[3] * initial_w)
-                    ymin = int(obj[4] * initial_h)
-                    xmax = int(obj[5] * initial_w)
-                    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))
-                    cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
-                    det_label = labels_map[class_id] if labels_map else str(class_id)
-                    cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
-                                cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
-                    print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin,
-                          'xmax:', xmax, 'ymax:', ymax)
+        super(Camera, self).__init__()
 
-            # Draw performance stats
-            inf_time_message = "Inference time: Not applicable for async mode" if 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 is_async_mode:
-                async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id)
+    def __del__(self):
+        self.cap.release()
+        cv2.destroyAllWindows()
+
+    def frames(self):
+        cur_request_id = 0
+        next_request_id = 1
+
+        log.info("Starting inference in async mode...")
+        log.info("To switch between sync and async modes press Tab button")
+        log.info("To stop the demo execution press Esc button")
+
+        # Async doesn't work if True
+        # Request issues = Runtime Error: [REQUEST BUSY]
+        self.is_async_mode = False
+        #is_async_mode = True
+        render_time = 0
+        ret, frame = self.cap.read()
+
+        frameList = []
+
+        print("To close the application, press 'CTRL+C' or any key with focus on the output window")
+
+        while True:
+            if self.is_async_mode:
+                ret, next_frame = self.cap.read()
             else:
-                async_mode_message = "Async mode is off. Processing request {}".format(cur_request_id)
+                ret, frame = self.cap.read()
+            if not ret:
+                break
+            initial_w = self.cap.get(3)
+            initial_h = self.cap.get(4)
 
-            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)
+            # Main sync point:
+            # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
+            # in the regular mode we start the CURRENT request and immediately wait for it's completion
+            inf_start = time.time()
+            if self.is_async_mode:
+                if ret:
+                    in_frame = cv2.resize(next_frame, (self.w, self.h))
+                    in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
+                    in_frame = in_frame.reshape((self.n, self.c, self.h, self.w))
+                    self.exec_net.start_async(request_id=next_request_id, inputs={self.input_blob: in_frame})
+            else:
+                if ret:
+                    in_frame = cv2.resize(frame, (self.w, self.h))
+                    in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
+                    in_frame = in_frame.reshape((self.n, self.c, self.h, self.w))
+                    self.exec_net.start_async(request_id=cur_request_id, inputs={self.input_blob: in_frame})
 
-        render_start = time.time()
+            if self.exec_net.requests[cur_request_id].wait(-1) == 0:
+                inf_end = time.time()
+                det_time = inf_end - inf_start
 
-        if not args.ns:
-            if ret:
-                cv2.imshow("Detection results", frame)
-            render_end = time.time()
-            render_time = render_end - render_start
+                # Parse detection results of the current request
+                res = self.exec_net.requests[cur_request_id].outputs[self.out_blob]
 
-        if is_async_mode:
-            cur_request_id, next_request_id = next_request_id, cur_request_id
+                for obj in res[0][0]:
+                    # Draw only objects when probability more than specified threshold
+                    if obj[2] > self.args.prob_threshold:
+                        xmin = int(obj[3] * initial_w)
+                        ymin = int(obj[4] * initial_h)
+                        xmax = int(obj[5] * initial_w)
+                        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))
+                        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),
+                                    cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
+                        print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin,
+                              'xmax:', xmax, 'ymax:', ymax)
 
-            frame = next_frame
-        key = cv2.waitKey(1)
-        if key == 27:
-            break
-        if 9 == key:
-            is_async_mode = not is_async_mode
-            log.info("Switched to {} mode".format("async" if is_async_mode else "sync"))
+                # 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)
 
-    cap.release()
-    cv2.destroyAllWindows()
+                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)
+
+            render_start = time.time()
+
+            yield cv2.imencode('.jpg', frame)[1].tobytes()
+
+            if not self.args.ns:
+                if ret:
+                    cv2.imshow("Detection results", frame)
+                render_end = time.time()
+                render_time = render_end - render_start
+
+            if self.is_async_mode:
+                cur_request_id, next_request_id = next_request_id, cur_request_id
+
+                frame = next_frame
+            key = cv2.waitKey(1)
+            if key == 27:
+                break
+            if 9 == key:
+                self.is_async_mode = not self.is_async_mode
+                log.info("Switched to {} mode".format("async" if self.is_async_mode else "sync"))
 
 
 if __name__ == '__main__':
-    sys.exit(main() or 0)
+    args = build_argparser().parse_args()
+    camera = Camera(args)
+    camera.frames()
+    del camera
diff --git a/person_detection/templates/index.html b/person_detection/templates/index.html
new file mode 100644
index 0000000..26ab1e8
--- /dev/null
+++ b/person_detection/templates/index.html
@@ -0,0 +1,9 @@
+<html>
+  <head>
+    <title>Person Detection - Aether Edge Application</title>
+  </head>
+  <body>
+    <h1>Person Detection - Aether Edge Application</h1>
+    <img src="{{ url_for('video_feed') }}">
+  </body>
+</html>