@camera.ui/camera-ui-opencv 0.0.20 → 0.0.22

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/bundle.zip ADDED
Binary file
package/package.json CHANGED
@@ -1,28 +1,29 @@
1
1
  {
2
2
  "displayName": "OpenCV",
3
3
  "name": "@camera.ui/camera-ui-opencv",
4
- "version": "0.0.20",
4
+ "version": "0.0.22",
5
5
  "description": "camera.ui opencv plugin",
6
6
  "author": "seydx (https://github.com/seydx/camera.ui)",
7
- "main": "./src/main.py",
8
- "type": "module",
7
+ "main": "./dist/main.py",
8
+ "type": "commonjs",
9
9
  "scripts": {
10
+ "bundle": "npm run format && npm run lint && cui bundle",
10
11
  "format": "ruff format",
11
12
  "install-updates": "npm i --save",
12
- "lint": "ruff check --fix",
13
- "update": "updates --update ./",
14
- "prepublishOnly": "npm i --package-lock-only && npm run format && npm run lint"
15
- },
16
- "dependencies": {},
17
- "devDependencies": {
18
- "updates": "^16.4.0"
13
+ "lint": "ruff check",
14
+ "lint:fix": "ruff check --fix",
15
+ "prepublishOnly": "node -e \"if(!process.env.SAFE_PUBLISH){console.error('Error: Please use @camera.ui/cli to publish the plugin:\\n npm run publish:alpha\\n npm run publish:beta\\n npm run publish:latest\\n');process.exit(1)}\"",
16
+ "publish:alpha": "cui publish --alpha",
17
+ "publish:beta": "cui publish --beta",
18
+ "publish:latest": "cui publish --latest",
19
+ "update": "updates --update ./"
19
20
  },
20
21
  "bugs": {
21
22
  "url": "https://github.com/seydx/camera.ui/issues"
22
23
  },
23
24
  "engines": {
24
- "camera.ui": ">=0.0.34-alpha.1",
25
- "node": ">=20.17.0"
25
+ "camera.ui": ">=0.0.34-alpha.2",
26
+ "node": ">=20.18.0"
26
27
  },
27
28
  "homepage": "https://github.com/seydx/camera.ui#readme",
28
29
  "keywords": [
@@ -39,6 +40,10 @@
39
40
  "camera.ui": {
40
41
  "pythonVersion": "3.11",
41
42
  "extension": "motionDetection",
42
- "dependencies": []
43
+ "dependencies": [],
44
+ "options": {
45
+ "extendedMotionDetection": true
46
+ },
47
+ "bundled": true
43
48
  }
44
- }
49
+ }
package/CHANGELOG.md DELETED
@@ -1,8 +0,0 @@
1
- All notable changes to this project will be documented in this file.
2
-
3
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
-
6
- ## [X.X.X] - ???
7
-
8
- - Initial Release
package/CONTRIBUTING.md DELETED
@@ -1 +0,0 @@
1
- # Contributing
package/LICENSE.md DELETED
@@ -1,22 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023-2024 seydx <dev@seydx.com>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md DELETED
@@ -1 +0,0 @@
1
- # @camera.ui/camera-ui-opencv
@@ -1,3 +0,0 @@
1
- {
2
- "schema": {}
3
- }
package/logo.png DELETED
Binary file
package/requirements.txt DELETED
@@ -1,3 +0,0 @@
1
- numpy==1.26.4
2
- opencv-python-headless==4.10.0.84
3
- camera-ui-python-types==0.1.93
@@ -1,413 +0,0 @@
1
- import asyncio
2
- from concurrent.futures import ThreadPoolExecutor
3
- from typing import Any, Literal, Optional
4
-
5
- import cv2
6
- import numpy as np
7
- from camera_ui_python_types import (
8
- CameraDevice,
9
- CameraStorage,
10
- Detection,
11
- LoggerService,
12
- PluginAPI,
13
- )
14
- from detector_defaults import (
15
- available_models,
16
- default_area,
17
- default_area_bs,
18
- default_area_fd,
19
- default_blur,
20
- default_dilt,
21
- default_learning_rate,
22
- default_model,
23
- default_threshold,
24
- default_threshold_bs,
25
- )
26
- from opencv_utils import get_detections, get_detections_bs, get_detections_fd
27
- from plugin_typings import CameraStorageValues
28
-
29
- background_tasks = set[asyncio.Task[Any]]()
30
-
31
-
32
- class CameraDetector:
33
- def __init__(self, api: PluginAPI, camera: CameraDevice, logger: LoggerService) -> None:
34
- super().__init__()
35
-
36
- self.api = api
37
- self.camera = camera
38
- self.logger = logger
39
-
40
- self.started = False
41
- self.closed = False
42
- self.frame_generation_task: Optional[asyncio.Task[Any]] = None
43
- self.executor = None
44
- self.previous_frame: Optional[np.ndarray[Any, Any]] = None
45
-
46
- self.camera_storage: CameraStorage[CameraStorageValues] = self.__create_camera_storage()
47
- self.camera.on_connected.subscribe(lambda connected: self.start() if connected else self.close())
48
-
49
- def start(self) -> None:
50
- if not self.started:
51
- self.started = True
52
- self.closed = False
53
-
54
- self.executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="OpenCV")
55
-
56
- self.logger.log(
57
- f"Starting motion detection for camera {self.camera.name} ({self.__detector_model()})..."
58
- )
59
-
60
- self.frame_generation_task = asyncio.create_task(
61
- self.__detect(), name=f"MotionDetection-{self.camera.name}"
62
- )
63
-
64
- background_tasks.add(self.frame_generation_task)
65
- self.frame_generation_task.add_done_callback(
66
- lambda _: background_tasks.remove(self.frame_generation_task)
67
- if self.frame_generation_task in background_tasks
68
- else None
69
- )
70
-
71
- def close(self) -> None:
72
- if self.started and not self.closed:
73
- self.started = False
74
- self.closed = True
75
-
76
- self.logger.log(f"Stopping motion detection for camera {self.camera.name}...")
77
-
78
- if self.frame_generation_task is not None:
79
- self.frame_generation_task.cancel()
80
- self.frame_generation_task = None
81
-
82
- if self.executor is not None:
83
- self.executor.shutdown(wait=False)
84
- self.executor = None
85
-
86
- def restart(self) -> None:
87
- self.logger.log(f"Restarting motion detection for camera {self.camera.name}...")
88
-
89
- self.close()
90
-
91
- if self.camera.connected:
92
- self.start()
93
-
94
- async def __detect(self) -> None:
95
- while not self.closed:
96
- try:
97
- backSub: Optional[cv2.BackgroundSubtractorMOG2] = None
98
-
99
- async for frame in self.camera.get_frames():
100
- if self.closed:
101
- break
102
-
103
- frame_image = await frame.to_image({"format": {"to": "gray"}})
104
- image_array = np.array(frame_image["image"])
105
-
106
- detector_model = self.__detector_model()
107
-
108
- if detector_model == "Frame Difference":
109
- area = self.camera_storage.values["frame_difference"]["area"]
110
-
111
- if self.previous_frame is None:
112
- self.previous_frame = image_array
113
- continue
114
-
115
- dets = await asyncio.get_event_loop().run_in_executor(
116
- self.executor,
117
- get_detections_fd,
118
- self.previous_frame,
119
- image_array,
120
- area,
121
- )
122
-
123
- self.previous_frame = image_array
124
-
125
- elif detector_model == "Background Substraction":
126
- if backSub is None:
127
- backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=18, detectShadows=False)
128
-
129
- threshold = self.camera_storage.values["background_substraction"]["threshold"]
130
- area = self.camera_storage.values["background_substraction"]["area"]
131
- learning_rate = self.camera_storage.values["background_substraction"]["learning_rate"]
132
-
133
- dets = await asyncio.get_event_loop().run_in_executor(
134
- self.executor,
135
- get_detections_bs,
136
- image_array,
137
- backSub,
138
- threshold,
139
- area,
140
- learning_rate,
141
- )
142
-
143
- else: # default
144
- blur = self.camera_storage.values["default"]["blur"]
145
- threshold = self.camera_storage.values["default"]["threshold"]
146
- area = self.camera_storage.values["default"]["area"]
147
- image_array = cv2.stackBlur(image_array, (blur, blur))
148
-
149
- if self.previous_frame is None:
150
- self.previous_frame = image_array
151
- continue
152
-
153
- dets = await asyncio.get_event_loop().run_in_executor(
154
- self.executor,
155
- get_detections,
156
- self.previous_frame,
157
- image_array,
158
- threshold,
159
- area,
160
- )
161
-
162
- self.previous_frame = image_array
163
-
164
- detections: list[Detection] = []
165
-
166
- for det in dets:
167
- (x1, y1, x2, y2) = det
168
-
169
- detection: Detection = {
170
- "label": "motion",
171
- "confidence": 1,
172
- "boundingBox": (x1, y1, x2, y2),
173
- "inputWidth": frame_image["info"]["width"],
174
- "inputHeight": frame_image["info"]["height"],
175
- "origWidth": frame.metadata["origWidth"],
176
- "origHeight": frame.metadata["origHeight"],
177
- }
178
-
179
- detections.append(detection)
180
-
181
- await self.camera.update_state("motion", {"detections": detections}, frame)
182
- except asyncio.CancelledError:
183
- break
184
- except Exception as error:
185
- self.logger.error(self.camera.name, "Error generating frames", error)
186
- self.logger.log(self.camera.name, "Restarting motion detection in 5 seconds...")
187
- await asyncio.sleep(5)
188
- continue
189
-
190
- def __detector_model(self) -> Literal["Frame Difference", "Background Substraction", "Default"]:
191
- return self.camera_storage.values["motion_detector"]
192
-
193
- def __create_camera_storage(self) -> CameraStorage[CameraStorageValues]:
194
- camera_storage: CameraStorage[CameraStorageValues] = (
195
- self.api.storage_controller.create_camera_storage(
196
- self,
197
- self.camera.id,
198
- {
199
- "motion_detector": {
200
- "type": "string",
201
- "key": "motion_detector",
202
- "title": "Motion Detector",
203
- "description": "Select the motion detection model to use",
204
- "group": "Manage",
205
- "enum": available_models,
206
- "store": True,
207
- "defaultValue": default_model,
208
- "required": True,
209
- "onSet": self.__change_model,
210
- },
211
- "default": {
212
- "type": "object",
213
- "key": "default",
214
- "title": "Default",
215
- "description": "Settings for the default model",
216
- "group": "Default",
217
- "opened": True,
218
- "hidden": True,
219
- "properties": {
220
- "area": {
221
- "type": "number",
222
- "key": "area",
223
- "title": "Area",
224
- "description": "Minimum size of detected motion (pixels)",
225
- "store": True,
226
- "defaultValue": default_area,
227
- "minimum": 10,
228
- "maximum": 1000,
229
- "step": 1,
230
- "required": True,
231
- "onSet": lambda old, new: self.logger.log(
232
- self.camera.name, f"Default model motion area changed from {old} to {new}"
233
- ),
234
- },
235
- "threshold": {
236
- "type": "number",
237
- "key": "threshold",
238
- "title": "Threshold",
239
- "description": "Sensitivity of motion detection (higher = less sensitive)",
240
- "store": True,
241
- "defaultValue": default_threshold,
242
- "minimum": 1,
243
- "maximum": 255,
244
- "step": 1,
245
- "required": True,
246
- "onSet": lambda old, new: self.logger.log(
247
- self.camera.name, f"Default model threshold changed from {old} to {new}"
248
- ),
249
- },
250
- "blur": {
251
- "type": "number",
252
- "key": "blur",
253
- "title": "Blur",
254
- "description": "Gaussian blur radius to reduce noise",
255
- "store": True,
256
- "defaultValue": default_blur,
257
- "minimum": 1,
258
- "maximum": 21,
259
- "step": 1,
260
- "required": True,
261
- "onSet": lambda old, new: self.logger.log(
262
- self.camera.name, f"Default model blur radius changed from {old} to {new}"
263
- ),
264
- },
265
- "dilation": {
266
- "type": "number",
267
- "key": "dilation",
268
- "title": "Dilation",
269
- "description": "Expansion of detected motion areas",
270
- "store": True,
271
- "defaultValue": default_dilt,
272
- "minimum": 1,
273
- "maximum": 21,
274
- "step": 1,
275
- "required": True,
276
- "onSet": lambda old, new: self.logger.log(
277
- self.camera.name,
278
- f"Default model dilation size changed from {old} to {new}",
279
- ),
280
- },
281
- },
282
- },
283
- "background_substraction": {
284
- "type": "object",
285
- "key": "background_substraction",
286
- "title": "Background Substraction",
287
- "description": "Settings for the background substraction model",
288
- "group": "Background Substraction",
289
- "opened": True,
290
- "hidden": True,
291
- "properties": {
292
- "area": {
293
- "type": "number",
294
- "key": "area",
295
- "title": "Area",
296
- "description": "Minimum size of detected motion (pixels)",
297
- "store": True,
298
- "defaultValue": default_area_bs,
299
- "minimum": 10,
300
- "maximum": 1000,
301
- "step": 1,
302
- "required": True,
303
- "onSet": lambda old, new: self.logger.log(
304
- self.camera.name,
305
- f"Background substraction model area changed from {old} to {new}",
306
- ),
307
- },
308
- "threshold": {
309
- "type": "number",
310
- "key": "threshold",
311
- "title": "Threshold",
312
- "description": "Sensitivity of motion detection (higher = less sensitive)",
313
- "store": True,
314
- "defaultValue": default_threshold_bs,
315
- "minimum": 1,
316
- "maximum": 255,
317
- "step": 1,
318
- "required": True,
319
- "onSet": lambda old, new: self.logger.log(
320
- self.camera.name,
321
- f"Background substraction model threshold changed from {old} to {new}",
322
- ),
323
- },
324
- "learning_rate": {
325
- "type": "number",
326
- "key": "threshold",
327
- "title": "Learning Rate",
328
- "description": "Speed of background model adaptation (0-1, -1 for auto)",
329
- "store": True,
330
- "defaultValue": default_learning_rate,
331
- "minimum": -1,
332
- "maximum": 1,
333
- "step": 0.01,
334
- "required": True,
335
- "onSet": lambda old, new: self.logger.log(
336
- self.camera.name,
337
- f"Background substraction model threshold changed from {old} to {new}",
338
- ),
339
- },
340
- },
341
- },
342
- "frame_difference": {
343
- "type": "object",
344
- "key": "frame_difference",
345
- "title": "Frame Difference",
346
- "description": "Settings for the frame difference model",
347
- "group": "Frame Difference",
348
- "opened": True,
349
- "hidden": True,
350
- "properties": {
351
- "area": {
352
- "type": "number",
353
- "key": "area",
354
- "title": "Area",
355
- "description": "Minimum size of detected motion (pixels)",
356
- "store": True,
357
- "defaultValue": default_area_fd,
358
- "minimum": 10,
359
- "maximum": 1000,
360
- "step": 1,
361
- "required": True,
362
- "onSet": lambda old, new: self.logger.log(
363
- self.camera.name,
364
- f"Frame difference model area changed from {old} to {new}",
365
- ),
366
- }
367
- },
368
- },
369
- },
370
- )
371
- )
372
-
373
- current_model = camera_storage.values["motion_detector"]
374
- current_model_schema = None
375
-
376
- if current_model == "Default":
377
- current_model_schema = camera_storage.getSchema("default")
378
- elif current_model == "Background Substraction":
379
- current_model_schema = camera_storage.getSchema("background_substraction")
380
- elif current_model == "Frame Difference":
381
- current_model_schema = camera_storage.getSchema("frame_difference")
382
-
383
- if current_model_schema:
384
- current_model_schema["hidden"] = False
385
-
386
- return camera_storage
387
-
388
- def __change_model(self, old_model: str, new_model: str) -> None:
389
- self.logger.log(self.camera.name, f"Motion detection model changed from {old_model} to {new_model}")
390
-
391
- old_schema = None
392
- new_schema = None
393
- self.previous_frame = None
394
-
395
- if old_model == "Default":
396
- old_schema = self.camera_storage.getSchema("default")
397
- elif old_model == "Background Substraction":
398
- old_schema = self.camera_storage.getSchema("background_substraction")
399
- elif old_model == "Frame Difference":
400
- old_schema = self.camera_storage.getSchema("frame_difference")
401
-
402
- if new_model == "Default":
403
- new_schema = self.camera_storage.getSchema("default")
404
- elif new_model == "Background Substraction":
405
- new_schema = self.camera_storage.getSchema("background_substraction")
406
- elif new_model == "Frame Difference":
407
- new_schema = self.camera_storage.getSchema("frame_difference")
408
-
409
- if old_schema:
410
- old_schema["hidden"] = True
411
-
412
- if new_schema:
413
- new_schema["hidden"] = False
@@ -1,16 +0,0 @@
1
- default_area = 350
2
- default_area_fd = 350
3
- default_area_bs = 350
4
-
5
- default_threshold = 50
6
- default_threshold_bs = 50
7
-
8
- default_blur = 11
9
- default_dilt = 15
10
-
11
- default_learning_rate = -1
12
-
13
- available_models = ["Frame Difference", "Background Substraction", "Default"]
14
-
15
-
16
- default_model = "Background Substraction"
package/src/main.py DELETED
@@ -1,340 +0,0 @@
1
- import asyncio
2
- import tempfile
3
- from concurrent.futures import ThreadPoolExecutor
4
- from typing import Any, Optional, Union
5
-
6
- import cv2
7
- import numpy as np
8
- from camera_detector import CameraDetector
9
- from camera_ui_python_types import (
10
- CameraDevice,
11
- CameraExtension,
12
- FormSubmitResponse,
13
- LoggerService,
14
- MotionDetectionPlugin,
15
- MotionDetectionPluginResponse,
16
- PluginAPI,
17
- RootSchema,
18
- )
19
- from detector_defaults import (
20
- default_area,
21
- default_area_bs,
22
- default_area_fd,
23
- default_blur,
24
- default_dilt,
25
- default_learning_rate,
26
- default_model,
27
- default_threshold,
28
- default_threshold_bs,
29
- )
30
- from opencv_utils import get_detections, get_detections_bs, get_detections_fd, merge_overlapping_boxes
31
-
32
-
33
- class OpenCV(MotionDetectionPlugin):
34
- def __init__(self, logger: LoggerService, api: PluginAPI):
35
- self.api = api
36
- self.logger = logger
37
- self.cameras: dict[str, CameraDevice] = {}
38
- self.detectors: dict[str, CameraDetector] = {}
39
-
40
- self.api.on("finishLaunching", self.start)
41
- self.api.on("shutdown", self.stop)
42
-
43
- self.api.device_manager.on("cameraSelected", self.camera_selected)
44
- self.api.device_manager.on("cameraDeselected", self.camera_deselected)
45
-
46
- async def camera_selected(self, camera: CameraDevice, extension: CameraExtension) -> None:
47
- self.logger.log(f"New camera added ({extension}): {camera.name}")
48
- self.cameras[camera.id] = camera
49
- self.create_detector(camera)
50
-
51
- def camera_deselected(self, id: str, extension: CameraExtension) -> None:
52
- camera = self.cameras.get(id)
53
-
54
- if camera:
55
- self.logger.log(f"Camera removed ({extension}): {camera.name}")
56
- self.remove_detector(self.cameras[id])
57
- del self.cameras[id]
58
-
59
- def start(self) -> None:
60
- self.logger.log("Plugin Started")
61
-
62
- for camera_device in self.cameras.values():
63
- self.create_detector(camera_device)
64
-
65
- def stop(self) -> None:
66
- self.logger.log("Plugin Stopped")
67
-
68
- detectors_copy = list(self.detectors.values())
69
-
70
- for detector in detectors_copy:
71
- self.remove_detector(detector.camera)
72
-
73
- async def onFormSubmit(self, action_id: str, payload: Any) -> Union[FormSubmitResponse, None]:
74
- print("Form submitted", action_id, payload)
75
-
76
- def configureCameras(self, cameras: list[CameraDevice]) -> None:
77
- for camera in cameras:
78
- self.cameras[camera.id] = camera
79
-
80
- def create_detector(self, camera: CameraDevice) -> None:
81
- detector = CameraDetector(self.api, camera, self.logger)
82
- self.detectors[camera.id] = detector
83
-
84
- def remove_detector(self, camera: CameraDevice) -> None:
85
- detector = self.detectors[camera.id]
86
-
87
- if detector:
88
- detector.close()
89
- del self.detectors[camera.id]
90
-
91
- def interfaceSchema(self) -> RootSchema:
92
- root_schema: RootSchema = {
93
- "schema": {
94
- "motion_detector": {
95
- "type": "string",
96
- "key": "motion_detector",
97
- "title": "Motion Detector",
98
- "description": "Select the motion detection model to use",
99
- "enum": ["Frame Difference", "Background Substraction", "Default"],
100
- "store": False,
101
- "defaultValue": default_model,
102
- "required": True,
103
- },
104
- "default": {
105
- "type": "object",
106
- "key": "default",
107
- "title": "Default",
108
- "description": "Settings for the default model",
109
- "group": "Default",
110
- "opened": True,
111
- "hidden": True,
112
- "properties": {
113
- "area": {
114
- "type": "number",
115
- "key": "area",
116
- "title": "Area",
117
- "description": "Minimum size of detected motion (pixels)",
118
- "store": False,
119
- "defaultValue": default_area,
120
- "minimum": 10,
121
- "maximum": 1000,
122
- "step": 1,
123
- "required": True,
124
- },
125
- "threshold": {
126
- "type": "number",
127
- "key": "threshold",
128
- "title": "Threshold",
129
- "description": "Sensitivity of motion detection (higher = less sensitive)",
130
- "store": False,
131
- "defaultValue": default_threshold,
132
- "minimum": 1,
133
- "maximum": 255,
134
- "step": 1,
135
- "required": True,
136
- },
137
- "blur": {
138
- "type": "number",
139
- "key": "blur",
140
- "title": "Blur",
141
- "description": "Gaussian blur radius to reduce noise",
142
- "store": False,
143
- "defaultValue": default_blur,
144
- "minimum": 1,
145
- "maximum": 21,
146
- "step": 1,
147
- "required": True,
148
- },
149
- "dilation": {
150
- "type": "number",
151
- "key": "dilation",
152
- "title": "Dilation",
153
- "description": "Expansion of detected motion areas",
154
- "store": False,
155
- "defaultValue": default_dilt,
156
- "minimum": 1,
157
- "maximum": 21,
158
- "step": 1,
159
- "required": True,
160
- },
161
- },
162
- },
163
- "background_substraction": {
164
- "type": "object",
165
- "key": "background_substraction",
166
- "title": "Background Substraction",
167
- "description": "Settings for the background substraction model",
168
- "group": "Background Substraction",
169
- "opened": True,
170
- "hidden": True,
171
- "properties": {
172
- "area": {
173
- "type": "number",
174
- "key": "area",
175
- "title": "Area",
176
- "description": "Minimum size of detected motion (pixels)",
177
- "store": False,
178
- "defaultValue": default_area_bs,
179
- "minimum": 10,
180
- "maximum": 1000,
181
- "step": 1,
182
- "required": True,
183
- },
184
- "threshold": {
185
- "type": "number",
186
- "key": "threshold",
187
- "title": "Threshold",
188
- "description": "Sensitivity of motion detection (higher = less sensitive)",
189
- "store": False,
190
- "defaultValue": default_threshold_bs,
191
- "minimum": 1,
192
- "maximum": 255,
193
- "step": 1,
194
- "required": True,
195
- },
196
- "learning_rate": {
197
- "type": "number",
198
- "key": "threshold",
199
- "title": "Learning Rate",
200
- "description": "Speed of background model adaptation (0-1, -1 for auto)",
201
- "store": False,
202
- "defaultValue": default_learning_rate,
203
- "minimum": -1,
204
- "maximum": 1,
205
- "step": 0.01,
206
- "required": True,
207
- },
208
- },
209
- },
210
- "frame_difference": {
211
- "type": "object",
212
- "key": "frame_difference",
213
- "title": "Frame Difference",
214
- "description": "Settings for the frame difference model",
215
- "group": "Frame Difference",
216
- "opened": True,
217
- "hidden": True,
218
- "properties": {
219
- "area": {
220
- "type": "number",
221
- "key": "area",
222
- "title": "Area",
223
- "description": "Minimum size of detected motion (pixels)",
224
- "store": False,
225
- "defaultValue": default_area_fd,
226
- "minimum": 10,
227
- "maximum": 1000,
228
- "step": 1,
229
- "required": True,
230
- }
231
- },
232
- },
233
- }
234
- }
235
-
236
- return root_schema
237
-
238
- async def detectMotion(self, video_path: str, config: dict[str, Any]) -> MotionDetectionPluginResponse:
239
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
240
- output_file = temp_file.name
241
-
242
- cap = cv2.VideoCapture(video_path)
243
- executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="OpenCV")
244
-
245
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
246
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
247
- fps = int(cap.get(cv2.CAP_PROP_FPS))
248
-
249
- fourcc = cv2.VideoWriter.fourcc("a", "v", "c", "1")
250
- out = cv2.VideoWriter(output_file, fourcc, fps, (width, height))
251
-
252
- previous_frame: Optional[np.ndarray[Any, Any]] = None
253
- backSub = None
254
-
255
- detector_model = config.get("motion_detector", "Default")
256
-
257
- while cap.isOpened():
258
- ret, frame = cap.read()
259
- if not ret:
260
- break
261
-
262
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
263
-
264
- if detector_model == "Frame Difference":
265
- area = config.get("frame_difference", {}).get("area", default_area_fd)
266
-
267
- if previous_frame is None:
268
- previous_frame = gray
269
- continue
270
-
271
- dets = await asyncio.get_event_loop().run_in_executor(
272
- executor,
273
- get_detections_fd,
274
- previous_frame,
275
- gray,
276
- area,
277
- )
278
-
279
- previous_frame = gray
280
-
281
- elif detector_model == "Background Substraction":
282
- if backSub is None:
283
- backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=18, detectShadows=False)
284
-
285
- threshold = config.get("background_substraction", {}).get("threshold", default_threshold_bs)
286
- area = config.get("background_substraction", {}).get("area", default_area_bs)
287
-
288
- dets = await asyncio.get_event_loop().run_in_executor(
289
- executor,
290
- get_detections_bs,
291
- gray,
292
- backSub,
293
- threshold,
294
- area,
295
- )
296
-
297
- else: # default
298
- blur = config.get("default", {}).get("blur", default_blur)
299
- threshold = config.get("default", {}).get("threshold", default_threshold)
300
- area = config.get("default", {}).get("area", default_area)
301
- gray = cv2.stackBlur(gray, (blur, blur))
302
-
303
- if previous_frame is None:
304
- previous_frame = gray
305
- continue
306
-
307
- dets = await asyncio.get_event_loop().run_in_executor(
308
- executor,
309
- get_detections,
310
- previous_frame,
311
- gray,
312
- threshold,
313
- area,
314
- )
315
-
316
- previous_frame = gray
317
-
318
- merged_boxes = merge_overlapping_boxes(dets)
319
-
320
- for det in merged_boxes:
321
- (x1, y1, x2, y2) = det
322
-
323
- pt1: cv2.typing.Point = (int(x1), int(y1))
324
- pt2: cv2.typing.Point = (int(x2), int(y2))
325
-
326
- cv2.rectangle(frame, pt1, pt2, (0, 255, 0), 2)
327
-
328
- out.write(frame)
329
-
330
- cap.release()
331
- out.release()
332
- executor.shutdown()
333
-
334
- return {
335
- "filePath": output_file,
336
- }
337
-
338
-
339
- def __main__():
340
- return OpenCV
@@ -1,136 +0,0 @@
1
- from typing import Any, Union, cast
2
-
3
- import cv2
4
- import numpy as np
5
-
6
- DEFAULT_AREA_THRESHOLD = 500
7
- DEFAULT_THRESHOLD = 50
8
- DEFAULT_MASK_KERNEL = np.array((9, 9), dtype=np.uint8)
9
- DEFAULT_LEARNING_RATE = 0.08
10
-
11
-
12
- def merge_overlapping_boxes(
13
- boxes: list[tuple[float, float, float, float]], overlap_threshold: float = 0.1
14
- ) -> list[tuple[float, float, float, float]]:
15
- if not boxes:
16
- return []
17
-
18
- boxes_array = np.array(boxes)
19
- merged_boxes: list[tuple[float, float, float, float]] = []
20
-
21
- while len(boxes_array) > 0:
22
- box = boxes_array[0]
23
- boxes_array: np.ndarray[Any, Any] = boxes_array[1:]
24
-
25
- x1, y1, w1, h1 = box
26
- x2 = x1 + w1
27
- y2 = y1 + h1
28
-
29
- indices_to_remove: list[int] = []
30
- for i, (x1o, y1o, w1o, h1o) in enumerate(boxes_array):
31
- x2o = x1o + w1o
32
- y2o = y1o + h1o
33
-
34
- xx1 = max(x1, x1o)
35
- yy1 = max(y1, y1o)
36
- xx2 = min(x2, x2o)
37
- yy2 = min(y2, y2o)
38
-
39
- w_overlap = max(0, xx2 - xx1)
40
- h_overlap = max(0, yy2 - yy1)
41
- overlap_area = w_overlap * h_overlap
42
- area1 = w1 * h1
43
- area2 = w1o * h1o
44
-
45
- if area1 == 0 or area2 == 0:
46
- continue
47
- overlap_ratio = overlap_area / min(area1, area2)
48
-
49
- if overlap_ratio > overlap_threshold:
50
- x1 = min(x1, x1o)
51
- y1 = min(y1, y1o)
52
- x2 = max(x2, x2o)
53
- y2 = max(y2, y2o)
54
- indices_to_remove.append(i)
55
-
56
- boxes_array = np.delete(boxes_array, indices_to_remove, axis=0)
57
- merged_boxes.append((int(x1), int(y1), int(x2 - x1), int(y2 - y1)))
58
-
59
- return merged_boxes
60
-
61
-
62
- def get_mask(
63
- frame1: np.ndarray[Any, Any],
64
- frame2: np.ndarray[Any, Any],
65
- kernel: np.ndarray[Any, Any] = DEFAULT_MASK_KERNEL,
66
- ) -> cv2.typing.MatLike:
67
- frame_diff = cv2.subtract(frame2, frame1)
68
- frame_diff = cv2.medianBlur(frame_diff, 3)
69
-
70
- mask = cv2.adaptiveThreshold(
71
- frame_diff, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3
72
- )
73
- mask = cv2.medianBlur(mask, 3)
74
- mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
75
-
76
- return mask
77
-
78
-
79
- def get_motion_mask(
80
- fg_mask: cv2.typing.MatLike,
81
- threshold: int = DEFAULT_THRESHOLD,
82
- ) -> cv2.typing.MatLike:
83
- thresh = cv2.threshold(fg_mask, threshold, 255, cv2.THRESH_BINARY)[1]
84
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
85
-
86
- motion_mask = cv2.medianBlur(thresh, 3)
87
- motion_mask = cv2.morphologyEx(motion_mask, cv2.MORPH_OPEN, kernel, iterations=1)
88
- motion_mask = cv2.morphologyEx(motion_mask, cv2.MORPH_CLOSE, kernel, iterations=1)
89
-
90
- return motion_mask
91
-
92
-
93
- def get_contour_detections(
94
- mask: cv2.typing.MatLike, area_threshold: int = DEFAULT_AREA_THRESHOLD
95
- ) -> list[tuple[float, float, float, float]]:
96
- cnts, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
97
- detections: list[tuple[float, float, float, float]] = []
98
- for cnt in cnts:
99
- x, y, w, h = cv2.boundingRect(cnt)
100
- area = w * h
101
- if area > area_threshold:
102
- detections.append((x, y, x + w, y + h))
103
- return detections
104
-
105
-
106
- def get_detections(
107
- blurred_frame1: cv2.typing.MatLike,
108
- blurred_frame2: cv2.typing.MatLike,
109
- threshold: int = DEFAULT_THRESHOLD,
110
- area_threshold: int = DEFAULT_AREA_THRESHOLD,
111
- ) -> list[tuple[float, float, float, float]]:
112
- delta_frame = cv2.absdiff(blurred_frame1, blurred_frame2)
113
- thresh_frame = cv2.threshold(delta_frame, threshold, 255, cv2.THRESH_BINARY)[1]
114
- thresh_frame = cast(cv2.typing.MatLike, cv2.dilate(thresh_frame, None, iterations=2)) # type: ignore
115
- return get_contour_detections(thresh_frame, area_threshold)
116
-
117
-
118
- def get_detections_fd(
119
- frame1: np.ndarray[Any, Any],
120
- frame2: np.ndarray[Any, Any],
121
- area_threshold: int = DEFAULT_AREA_THRESHOLD,
122
- ) -> list[tuple[float, float, float, float]]:
123
- mask = get_mask(frame1, frame2, DEFAULT_MASK_KERNEL)
124
- return get_contour_detections(mask, area_threshold)
125
-
126
-
127
- def get_detections_bs(
128
- frame: np.ndarray[Any, Any],
129
- backSub: Union[cv2.BackgroundSubtractorMOG2, cv2.BackgroundSubtractorKNN],
130
- threshold: int = DEFAULT_THRESHOLD,
131
- area_threshold: int = DEFAULT_AREA_THRESHOLD,
132
- learning_rate: float = DEFAULT_LEARNING_RATE,
133
- ) -> list[tuple[float, float, float, float]]:
134
- fg_mask = backSub.apply(frame, learningRate=learning_rate)
135
- motion_mask = get_motion_mask(fg_mask, threshold=threshold)
136
- return get_contour_detections(motion_mask, area_threshold)
@@ -1,29 +0,0 @@
1
- from typing import Literal, TypedDict
2
-
3
- Detectors = Literal["Frame Difference", "Background Substraction", "Default"]
4
-
5
-
6
- class DefaultModelValues(TypedDict):
7
- area: int
8
- threshold: int
9
- blur: int
10
- dilation: int
11
- # reference_frame_frequency: int
12
-
13
-
14
- class BackgroundSubstractionValues(TypedDict):
15
- area: int
16
- threshold: int
17
- learning_rate: float
18
-
19
-
20
- class FrameDifferenceValues(TypedDict):
21
- area: int
22
- # reference_frame_frequency: int
23
-
24
-
25
- class CameraStorageValues(TypedDict):
26
- motion_detector: Detectors
27
- default: DefaultModelValues
28
- background_substraction: BackgroundSubstractionValues
29
- frame_difference: FrameDifferenceValues