ROC integration
Change-Id: Ica1d8b10e9c0318d89361ff85206aabfa5f58e7f
diff --git a/person_detection/base_camera.py b/person_detection/base_camera.py
index 28ee57d..d7a7d29 100644
--- a/person_detection/base_camera.py
+++ b/person_detection/base_camera.py
@@ -1,23 +1,39 @@
-from multiprocessing import Process, Queue
+import paho.mqtt.client as mqtt
+import time
+import os
+import sys
+import threading
+import logging as log
+from multiprocessing import Process, Queue, Value, Array, Lock
+
+import roc
class BaseCamera(object):
process = {} # background process that reads frames from camera
frame = {} # frame queue
+ activity_counter = Value('i', 0)
+ cameras = Array('i', [0, 0, 0, 0])
+ lock = Lock()
- def __init__(self, device=None, idle=False):
- """Start the background camera process if it isn't running yet."""
+ def __init__(self, device, key, mbrlow, mbrhigh, devicegroup):
+ self.mqttBroker = "localhost"
self.device = device
- if self.device not in BaseCamera.process:
- BaseCamera.process[self.device] = None
- if BaseCamera.process[self.device] is None:
+ self.key = key
+ self.mbrlow = mbrlow
+ self.mbrhigh = mbrhigh
+ self.devicegroup = devicegroup
- self.frame[device] = Queue(100)
-
+ """Start the background camera process if it isn't running yet."""
+ if BaseCamera.cameras[int(self.device)] == 0:
+ BaseCamera.cameras[int(self.device)] = 1
+ self.last_detected = None
+ self.timer = None
+ self.detected = False
+ BaseCamera.frame[self.device] = Queue(100)
# start background frame process
BaseCamera.process[self.device] = Process(target=self._process, args=(self.device))
BaseCamera.process[self.device].start()
-
# wait until frames are available
_ = self.get_frame()
@@ -38,3 +54,59 @@
BaseCamera.frame[device].put(frame, block=True)
BaseCamera.process[device] = None
+
+ def person_detected(self, num):
+ self.last_detected = time.time()
+ if not self.detected:
+ BaseCamera.lock.acquire()
+ BaseCamera.activity_counter.value += 1
+ BaseCamera.lock.release()
+ self.set_resolution_high()
+ roc.set_mbr(self.key, self.devicegroup, self.mbrhigh)
+ self.detected = True
+ self.start_timer()
+
+ def no_person_detected(self):
+ self.detected = False
+ self.timer = None
+ BaseCamera.lock.acquire()
+ BaseCamera.activity_counter.value -=1
+ if BaseCamera.activity_counter.value <= 0:
+ BaseCamera.activity_counter.value = 0
+ self.set_resolution_low()
+ roc.set_mbr(self.key, self.devicegroup, self.mbrlow)
+ BaseCamera.lock.release()
+
+
+ def start_timer(self):
+ # log.info("Start timer for device {}".format(device))
+ self.timer = threading.Timer(10.0, self.timer_expiry)
+ self.timer.start()
+
+
+ def set_resolution_high(self):
+ for device in range(0, 4):
+ self.set_resolution(str(device), "high")
+
+
+ def set_resolution_low(self):
+ for device in range(0, 4):
+ self.set_resolution(str(device), "low")
+
+
+ def set_resolution(self, device, level):
+ log.info("Setting camera {} resolution to {}".format(device, level))
+ client = mqtt.Client()
+ client.connect(self.mqttBroker)
+ client.publish("camera/" + str(5000 + int(device)), level)
+
+
+ def timer_expiry(self):
+ now = time.time()
+ diff = now - self.last_detected
+ log.info("timer_expiry() - now:{}, last_detected:{}".format(now, self.last_detected))
+ if diff > 5.0:
+ self.no_person_detected()
+ else:
+ # Restart timer since person detected not too long back
+ self.start_timer()