Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 1 | import paho.mqtt.client as mqtt |
| 2 | import time |
| 3 | import os |
| 4 | import sys |
| 5 | import threading |
| 6 | import logging as log |
| 7 | from multiprocessing import Process, Queue, Value, Array, Lock |
| 8 | |
| 9 | import roc |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class BaseCamera(object): |
Shad Ansari | 26682be | 2021-10-26 03:52:35 +0000 | [diff] [blame] | 13 | process = {} # background process that reads frames from camera |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 14 | frame = {} # frame queue |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 15 | activity_counter = Value('i', 0) |
| 16 | cameras = Array('i', [0, 0, 0, 0]) |
| 17 | lock = Lock() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 18 | |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame^] | 19 | def __init__(self, device, key, mbrlow, mbrhigh, devicegroup, noroc): |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 20 | self.mqttBroker = "localhost" |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 21 | self.device = device |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 22 | self.key = key |
| 23 | self.mbrlow = mbrlow |
| 24 | self.mbrhigh = mbrhigh |
| 25 | self.devicegroup = devicegroup |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame^] | 26 | self.noroc = noroc |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 27 | |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 28 | """Start the background camera process if it isn't running yet.""" |
| 29 | if BaseCamera.cameras[int(self.device)] == 0: |
| 30 | BaseCamera.cameras[int(self.device)] = 1 |
| 31 | self.last_detected = None |
| 32 | self.timer = None |
| 33 | self.detected = False |
| 34 | BaseCamera.frame[self.device] = Queue(100) |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame^] | 35 | self.set_resolution(self.device, "low") |
Shad Ansari | 26682be | 2021-10-26 03:52:35 +0000 | [diff] [blame] | 36 | # start background frame process |
| 37 | BaseCamera.process[self.device] = Process(target=self._process, args=(self.device)) |
| 38 | BaseCamera.process[self.device].start() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 39 | # wait until frames are available |
Shad Ansari | 60ca8cc | 2021-11-02 18:46:44 +0000 | [diff] [blame] | 40 | _ = self.get_frame() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 41 | |
| 42 | def get_frame(self): |
| 43 | """Return the current camera frame.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 44 | |
Shad Ansari | 60ca8cc | 2021-11-02 18:46:44 +0000 | [diff] [blame] | 45 | # blocks |
| 46 | return BaseCamera.frame[self.device].get(block=True) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 47 | |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 48 | def frames(self): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 49 | """"Generator that returns frames from the camera.""" |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 50 | raise NotImplementedError('Must be implemented by subclasses.') |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 51 | |
Shad Ansari | 26682be | 2021-10-26 03:52:35 +0000 | [diff] [blame] | 52 | def _process(self, device): |
| 53 | """Camera background process.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 54 | frames_iterator = self.frames() |
| 55 | for frame in frames_iterator: |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 56 | BaseCamera.frame[device].put(frame, block=True) |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame] | 57 | |
Shad Ansari | 26682be | 2021-10-26 03:52:35 +0000 | [diff] [blame] | 58 | BaseCamera.process[device] = None |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 59 | |
| 60 | def person_detected(self, num): |
| 61 | self.last_detected = time.time() |
| 62 | if not self.detected: |
| 63 | BaseCamera.lock.acquire() |
| 64 | BaseCamera.activity_counter.value += 1 |
| 65 | BaseCamera.lock.release() |
| 66 | self.set_resolution_high() |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame^] | 67 | if self.noroc is True: |
| 68 | roc.set_mbr(self.key, self.devicegroup, self.mbrhigh) |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 69 | self.detected = True |
| 70 | self.start_timer() |
| 71 | |
| 72 | def no_person_detected(self): |
| 73 | self.detected = False |
| 74 | self.timer = None |
| 75 | BaseCamera.lock.acquire() |
| 76 | BaseCamera.activity_counter.value -=1 |
| 77 | if BaseCamera.activity_counter.value <= 0: |
| 78 | BaseCamera.activity_counter.value = 0 |
| 79 | self.set_resolution_low() |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame^] | 80 | if self.noroc is True: |
| 81 | roc.set_mbr(self.key, self.devicegroup, self.mbrlow) |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 82 | BaseCamera.lock.release() |
| 83 | |
| 84 | |
| 85 | def start_timer(self): |
| 86 | # log.info("Start timer for device {}".format(device)) |
| 87 | self.timer = threading.Timer(10.0, self.timer_expiry) |
| 88 | self.timer.start() |
| 89 | |
| 90 | |
| 91 | def set_resolution_high(self): |
| 92 | for device in range(0, 4): |
| 93 | self.set_resolution(str(device), "high") |
| 94 | |
| 95 | |
| 96 | def set_resolution_low(self): |
| 97 | for device in range(0, 4): |
| 98 | self.set_resolution(str(device), "low") |
| 99 | |
| 100 | |
| 101 | def set_resolution(self, device, level): |
| 102 | log.info("Setting camera {} resolution to {}".format(device, level)) |
| 103 | client = mqtt.Client() |
| 104 | client.connect(self.mqttBroker) |
| 105 | client.publish("camera/" + str(5000 + int(device)), level) |
| 106 | |
| 107 | |
| 108 | def timer_expiry(self): |
| 109 | now = time.time() |
| 110 | diff = now - self.last_detected |
| 111 | log.info("timer_expiry() - now:{}, last_detected:{}".format(now, self.last_detected)) |
| 112 | if diff > 5.0: |
| 113 | self.no_person_detected() |
| 114 | else: |
| 115 | # Restart timer since person detected not too long back |
| 116 | self.start_timer() |