Repository navigation
Expand file tree
/
Copy pathzones.py
More file actions
131 lines (109 loc) · 4.41 KB
/
Copy pathzones.py
File metadata and controls
131 lines (109 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import logging
import cv2
from const import TOPIC_FRAME_SCAN_POSTPROC
from lib.data_stream import DataStream
from lib.helpers import Filter, calculate_absolute_coords, report_labels
from lib.mqtt.binary_sensor import MQTTBinarySensor
class Zone:
def __init__(self, zone, camera_resolution, config, mqtt_queue):
self._logger = logging.getLogger(__name__ + "." + config.camera.name_slug)
if getattr(config.camera.logging, "level", None):
self._logger.setLevel(config.camera.logging.level)
self._coordinates = zone["coordinates"]
self._camera_resolution = camera_resolution
self._config = config
self._mqtt_queue = mqtt_queue
self._name = zone["name"]
self._objects_in_zone = []
self._labels_in_zone = []
self._reported_label_count = {}
self._object_filters = {}
self._trigger_recorder = False
zone_labels = (
zone["labels"] if zone["labels"] else config.object_detection.labels
)
for object_filter in zone_labels:
self._object_filters[object_filter.label] = Filter(object_filter)
self._mqtt_devices = {}
if self._mqtt_queue:
self._mqtt_devices["zone"] = MQTTBinarySensor(
config, mqtt_queue, zone["name"]
)
for label in zone_labels:
self._mqtt_devices[label.label] = MQTTBinarySensor(
config, mqtt_queue, f"{zone['name']} {label.label}"
)
self._post_processor_topic = (
f"{config.camera.name_slug}/{TOPIC_FRAME_SCAN_POSTPROC}",
)
def filter_zone(self, frame):
objects_in_zone = []
labels_in_zone = []
self._trigger_recorder = False
for obj in frame.objects:
if self._object_filters.get(obj.label) and self._object_filters[
obj.label
].filter_object(obj):
x1, _, x2, y2 = calculate_absolute_coords(
(obj.rel_x1, obj.rel_y1, obj.rel_x2, obj.rel_y2,),
self._camera_resolution,
)
middle = ((x2 - x1) / 2) + x1
if cv2.pointPolygonTest(self.coordinates, (middle, y2), False) >= 0:
obj.relevant = True
objects_in_zone.append(obj)
if obj.label not in labels_in_zone:
labels_in_zone.append(obj.label)
if self._object_filters[obj.label].triggers_recording:
self._trigger_recorder = True
if self._object_filters[obj.label].post_processor:
DataStream.publish_data(
(
f"{self._post_processor_topic}/"
f"{self._object_filters[obj.label].post_processor}"
),
{
"camera_config": self._config,
"frame": frame,
"object": obj,
"zone": self._name,
},
)
self.objects_in_zone = objects_in_zone
self.labels_in_zone = labels_in_zone
def on_connect(self, client):
for device in self._mqtt_devices.values():
device.on_connect(client)
@property
def coordinates(self):
return self._coordinates
@property
def objects_in_zone(self):
return self._objects_in_zone
@objects_in_zone.setter
def objects_in_zone(self, objects):
if objects == self._objects_in_zone:
return
self._objects_in_zone = objects
if self._mqtt_queue:
attributes = {}
attributes["objects"] = [obj.formatted for obj in objects]
self._mqtt_devices["zone"].publish(bool(objects), attributes)
@property
def labels_in_zone(self):
return self._objects_in_zone
@labels_in_zone.setter
def labels_in_zone(self, labels):
self._labels_in_zone, self._reported_label_count = report_labels(
labels,
self._labels_in_zone,
self._reported_label_count,
self._mqtt_queue,
self._mqtt_devices,
)
@property
def trigger_recorder(self):
return self._trigger_recorder
@property
def name(self):
return self._name