@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/camera_detector.py +107 -107
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "OpenCV",
3
3
  "name": "@camera.ui/camera-ui-opencv",
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "description": "camera.ui opencv plugin",
6
6
  "author": "seydx (https://github.com/seydx/camera.ui)",
7
7
  "main": "./src/main.py",
@@ -91,113 +91,113 @@ class CameraDetector:
91
91
  self.start()
92
92
 
93
93
  async def __detect(self) -> None:
94
- try:
95
- first_frame = None
96
- last_frame_time = 0
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
- now = time.time() * 1000
107
-
108
- detector_model = self.__detector_model()
109
-
110
- if detector_model == "Frame Difference":
111
- area = self.camera_storage.values["frame_difference"]["area"]
112
- reference_frame_frequency = self.camera_storage.values["frame_difference"][
113
- "reference_frame_frequency"
114
- ]
115
-
116
- if first_frame is None or (
117
- reference_frame_frequency
118
- and now - last_frame_time > (reference_frame_frequency * 1000)
119
- ):
120
- first_frame = image_array
121
- last_frame_time = now
122
- continue
123
-
124
- dets = await asyncio.get_event_loop().run_in_executor(
125
- self.executor,
126
- get_detections_fd,
127
- first_frame,
128
- image_array,
129
- area,
130
- )
131
-
132
- elif detector_model == "Background Substraction":
133
- if backSub is None:
134
- backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=18, detectShadows=False)
135
-
136
- threshold = self.camera_storage.values["background_substraction"]["threshold"]
137
- area = self.camera_storage.values["background_substraction"]["area"]
138
-
139
- dets = await asyncio.get_event_loop().run_in_executor(
140
- self.executor,
141
- get_detections_bs,
142
- image_array,
143
- backSub,
144
- threshold,
145
- area,
146
- )
147
-
148
- else: # default
149
- blur = self.camera_storage.values["default"]["blur"]
150
- threshold = self.camera_storage.values["default"]["threshold"]
151
- area = self.camera_storage.values["default"]["area"]
152
- reference_frame_frequency = self.camera_storage.values["default"][
153
- "reference_frame_frequency"
154
- ]
155
-
156
- image_array = cv2.stackBlur(image_array, (blur, blur))
157
-
158
- if first_frame is None or (
159
- reference_frame_frequency
160
- and now - last_frame_time > (reference_frame_frequency * 1000)
161
- ):
162
- first_frame = image_array
163
- last_frame_time = now
164
- continue
165
-
166
- dets = await asyncio.get_event_loop().run_in_executor(
167
- self.executor,
168
- get_detections,
169
- first_frame,
170
- image_array,
171
- threshold,
172
- area,
173
- )
174
-
175
- detections: list[Detection] = []
176
-
177
- for det in dets:
178
- (x1, y1, x2, y2) = det
179
-
180
- detection: Detection = {
181
- "label": "motion",
182
- "confidence": 1,
183
- "boundingBox": (x1, y1, x2, y2),
184
- "inputWidth": frame_image["info"]["width"],
185
- "inputHeight": frame_image["info"]["height"],
186
- "origWidth": frame.metadata["origWidth"],
187
- "origHeight": frame.metadata["origHeight"],
188
- }
189
-
190
- detections.append(detection)
191
-
192
- await self.camera.update_state("motion", {"detections": detections}, frame)
193
-
194
- except asyncio.CancelledError:
195
- pass
196
- except Exception as error:
197
- self.logger.error(self.camera.name, "Error generating frames", error)
198
- finally:
199
- if not self.closed:
200
- self.restart()
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"]