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