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