Resolution control via MQTT
Change-Id: I3a98f19fac8a34098703c7bdf60e1e17f644a635
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 12d5c52..b3d5835 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -17,6 +17,9 @@
RUN apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
+# TODO - is this needed?
+RUN pip3 install paho-mqtt
+
WORKDIR /var/person-detection-app
COPY . .
diff --git a/person_detection/mqtt.py b/person_detection/mqtt.py
new file mode 100644
index 0000000..89bed3f
--- /dev/null
+++ b/person_detection/mqtt.py
@@ -0,0 +1,53 @@
+import paho.mqtt.client as mqtt
+import time
+import os
+import sys
+import threading
+import logging as log
+
+mqttBroker ="localhost"
+
+resolution = {}
+timer = {}
+timestamp = {}
+
+def init(device):
+ set_resolution(device, "low")
+
+
+def person_detected(device, num):
+ timestamp[device] = time.time()
+ if device in resolution and resolution[device] == "high":
+ return
+ set_resolution(device, "high")
+
+
+def start_timer(device):
+ # log.info("Start timer for device {}".format(device))
+ timer[device] = threading.Timer(10.0, timer_expiry, device)
+ timer[device].start()
+
+
+def remove_timer(device):
+ del timer[device]
+
+
+def set_resolution(device, level):
+ log.info("Setting camera {} resolution to {}".format(device, level))
+ resolution[device] = level
+ if level == "high" and device not in timer:
+ start_timer(device)
+ client = mqtt.Client()
+ client.connect(mqttBroker)
+ client.publish("camera/" + str(5000 + int(device)), level)
+
+
+def timer_expiry(device):
+ now = time.time()
+ diff = now - timestamp[device]
+ # log.info("timer_expiry() - now:{}, timestamp:{}".format(now, timestamp[device]))
+ if diff > 5.0:
+ set_resolution(device, "low")
+ remove_timer(device)
+ else:
+ start_timer(device)
diff --git a/person_detection/person_detection.py b/person_detection/person_detection.py
index aa54601..15c4e82 100644
--- a/person_detection/person_detection.py
+++ b/person_detection/person_detection.py
@@ -17,6 +17,8 @@
from base_camera import BaseCamera
+import mqtt
+
Shape = namedtuple('Shape', ['n','c','h','w'])
class Camera(BaseCamera):
@@ -36,6 +38,8 @@
self.device = device
+ mqtt.init(device)
+
super(Camera, self).__init__(device, args.idle)
def __del__(self):
@@ -145,6 +149,9 @@
cv2.putText(frame, "camera: {}".format(self.device), (10, int(initial_h - 20)),
cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
+ if obj_count > 0:
+ mqtt.person_detected(self.device, obj_count)
+
yield cv2.imencode('.jpg', frame)[1].tobytes()
if self.is_async_mode:
diff --git a/requirements.txt b/requirements.txt
index c91b2ad..4258352 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -7,6 +7,7 @@
Jinja2==3.0.1
MarkupSafe==2.0.1
numpy==1.19.5
+paho-mqtt==1.6.1
typing-extensions==3.10.0.2
Werkzeug==2.0.1
zipp==3.6.0