@camera.ui/camera-ui-opencv 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/camera_detector.py +107 -107
package/package.json
CHANGED
package/src/camera_detector.py
CHANGED
|
@@ -91,113 +91,113 @@ class CameraDetector:
|
|
|
91
91
|
self.start()
|
|
92
92
|
|
|
93
93
|
async def __detect(self) -> None:
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
backSub
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
94
|
+
while not self.closed:
|
|
95
|
+
try:
|
|
96
|
+
first_frame = None
|
|
97
|
+
last_frame_time = 0
|
|
98
|
+
backSub: Optional[cv2.BackgroundSubtractorMOG2] = None
|
|
99
|
+
|
|
100
|
+
async for frame in self.camera.get_frames():
|
|
101
|
+
if self.closed:
|
|
102
|
+
break
|
|
103
|
+
|
|
104
|
+
frame_image = await frame.to_image({"format": {"to": "gray"}})
|
|
105
|
+
image_array = np.array(frame_image["image"])
|
|
106
|
+
|
|
107
|
+
now = time.time() * 1000
|
|
108
|
+
|
|
109
|
+
detector_model = self.__detector_model()
|
|
110
|
+
|
|
111
|
+
if detector_model == "Frame Difference":
|
|
112
|
+
area = self.camera_storage.values["frame_difference"]["area"]
|
|
113
|
+
reference_frame_frequency = self.camera_storage.values["frame_difference"][
|
|
114
|
+
"reference_frame_frequency"
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
if first_frame is None or (
|
|
118
|
+
reference_frame_frequency
|
|
119
|
+
and now - last_frame_time > (reference_frame_frequency * 1000)
|
|
120
|
+
):
|
|
121
|
+
first_frame = image_array
|
|
122
|
+
last_frame_time = now
|
|
123
|
+
continue
|
|
124
|
+
|
|
125
|
+
dets = await asyncio.get_event_loop().run_in_executor(
|
|
126
|
+
self.executor,
|
|
127
|
+
get_detections_fd,
|
|
128
|
+
first_frame,
|
|
129
|
+
image_array,
|
|
130
|
+
area,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
elif detector_model == "Background Substraction":
|
|
134
|
+
if backSub is None:
|
|
135
|
+
backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=18, detectShadows=False)
|
|
136
|
+
|
|
137
|
+
threshold = self.camera_storage.values["background_substraction"]["threshold"]
|
|
138
|
+
area = self.camera_storage.values["background_substraction"]["area"]
|
|
139
|
+
|
|
140
|
+
dets = await asyncio.get_event_loop().run_in_executor(
|
|
141
|
+
self.executor,
|
|
142
|
+
get_detections_bs,
|
|
143
|
+
image_array,
|
|
144
|
+
backSub,
|
|
145
|
+
threshold,
|
|
146
|
+
area,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
else: # default
|
|
150
|
+
blur = self.camera_storage.values["default"]["blur"]
|
|
151
|
+
threshold = self.camera_storage.values["default"]["threshold"]
|
|
152
|
+
area = self.camera_storage.values["default"]["area"]
|
|
153
|
+
reference_frame_frequency = self.camera_storage.values["default"][
|
|
154
|
+
"reference_frame_frequency"
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
image_array = cv2.stackBlur(image_array, (blur, blur))
|
|
158
|
+
|
|
159
|
+
if first_frame is None or (
|
|
160
|
+
reference_frame_frequency
|
|
161
|
+
and now - last_frame_time > (reference_frame_frequency * 1000)
|
|
162
|
+
):
|
|
163
|
+
first_frame = image_array
|
|
164
|
+
last_frame_time = now
|
|
165
|
+
continue
|
|
166
|
+
|
|
167
|
+
dets = await asyncio.get_event_loop().run_in_executor(
|
|
168
|
+
self.executor,
|
|
169
|
+
get_detections,
|
|
170
|
+
first_frame,
|
|
171
|
+
image_array,
|
|
172
|
+
threshold,
|
|
173
|
+
area,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
detections: list[Detection] = []
|
|
177
|
+
|
|
178
|
+
for det in dets:
|
|
179
|
+
(x1, y1, x2, y2) = det
|
|
180
|
+
|
|
181
|
+
detection: Detection = {
|
|
182
|
+
"label": "motion",
|
|
183
|
+
"confidence": 1,
|
|
184
|
+
"boundingBox": (x1, y1, x2, y2),
|
|
185
|
+
"inputWidth": frame_image["info"]["width"],
|
|
186
|
+
"inputHeight": frame_image["info"]["height"],
|
|
187
|
+
"origWidth": frame.metadata["origWidth"],
|
|
188
|
+
"origHeight": frame.metadata["origHeight"],
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
detections.append(detection)
|
|
192
|
+
|
|
193
|
+
await self.camera.update_state("motion", {"detections": detections}, frame)
|
|
194
|
+
except asyncio.CancelledError:
|
|
195
|
+
pass
|
|
196
|
+
except Exception as error:
|
|
197
|
+
self.logger.error(self.camera.name, "Error generating frames", error)
|
|
198
|
+
self.logger.log(self.camera.name, "Restarting motion detection in 5 seconds...")
|
|
199
|
+
await asyncio.sleep(5)
|
|
200
|
+
continue
|
|
201
201
|
|
|
202
202
|
def __detector_model(self) -> Literal["Frame Difference", "Background Substraction", "Default"]:
|
|
203
203
|
return self.camera_storage.values["motion_detector"]
|