Resolution control via MQTT
Change-Id: I3a98f19fac8a34098703c7bdf60e1e17f644a635
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: