DVR-Scan-Headless 1.8__py3-none-any.whl

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 (45) hide show
  1. dvr_scan/LICENSE +47 -0
  2. dvr_scan/__init__.py +55 -0
  3. dvr_scan/__main__.py +80 -0
  4. dvr_scan/app/__init__.py +10 -0
  5. dvr_scan/app/__main__.py +218 -0
  6. dvr_scan/app/about_window.py +137 -0
  7. dvr_scan/app/application.py +1700 -0
  8. dvr_scan/app/common.py +31 -0
  9. dvr_scan/app/region_editor.py +1416 -0
  10. dvr_scan/app/scan_window.py +258 -0
  11. dvr_scan/app/widgets.py +213 -0
  12. dvr_scan/cli.py +511 -0
  13. dvr_scan/config.py +607 -0
  14. dvr_scan/controller.py +215 -0
  15. dvr_scan/detector.py +124 -0
  16. dvr_scan/docs/404.html +1 -0
  17. dvr_scan/docs/assets/javascripts/bundle.88dd0f4e.min.js +15 -0
  18. dvr_scan/docs/assets/stylesheets/main.6f8fc17f.min.css +1 -0
  19. dvr_scan/docs/assets/stylesheets/palette.06af60db.min.css +1 -0
  20. dvr_scan/docs/changelog.html +1 -0
  21. dvr_scan/docs/docs.html +73 -0
  22. dvr_scan/docs/faq.html +13 -0
  23. dvr_scan/docs/guide.html +1 -0
  24. dvr_scan/docs/index.html +5 -0
  25. dvr_scan/docs/resources.html +30 -0
  26. dvr_scan/docs/style.min.css +1 -0
  27. dvr_scan/dvr-scan-logo.png +0 -0
  28. dvr_scan/dvr-scan.ico +0 -0
  29. dvr_scan/dvr-scan.png +0 -0
  30. dvr_scan/opencv_loader.py +37 -0
  31. dvr_scan/overlays.py +251 -0
  32. dvr_scan/platform.py +236 -0
  33. dvr_scan/region.py +65 -0
  34. dvr_scan/scanner.py +1303 -0
  35. dvr_scan/shared/__init__.py +287 -0
  36. dvr_scan/shared/cli.py +291 -0
  37. dvr_scan/shared/settings.py +72 -0
  38. dvr_scan/subtractor.py +163 -0
  39. dvr_scan/video_joiner.py +188 -0
  40. dvr_scan_headless-1.8.dist-info/METADATA +119 -0
  41. dvr_scan_headless-1.8.dist-info/RECORD +45 -0
  42. dvr_scan_headless-1.8.dist-info/WHEEL +5 -0
  43. dvr_scan_headless-1.8.dist-info/entry_points.txt +2 -0
  44. dvr_scan_headless-1.8.dist-info/licenses/LICENSE +47 -0
  45. dvr_scan_headless-1.8.dist-info/top_level.txt +1 -0
dvr_scan/scanner.py ADDED
@@ -0,0 +1,1303 @@
1
+ #
2
+ # DVR-Scan: Video Motion Event Detection & Extraction Tool
3
+ # --------------------------------------------------------------
4
+ # [ Site: https://www.dvr-scan.com/ ]
5
+ # [ Repo: https://github.com/Breakthrough/DVR-Scan ]
6
+ #
7
+ # Copyright (C) 2016 Brandon Castellano <http://www.bcastell.com>.
8
+ # DVR-Scan is licensed under the BSD 2-Clause License; see the included
9
+ # LICENSE file, or visit one of the above pages for details.
10
+ #
11
+ """``dvr_scan.scanner`` Module
12
+
13
+ Contains the motion scanning engine (`MotionScanner`) for DVR-Scan.
14
+ """
15
+
16
+ import logging
17
+ import queue
18
+ import subprocess
19
+ import sys
20
+ import threading
21
+ import typing as ty
22
+ from dataclasses import dataclass
23
+ from enum import Enum
24
+ from pathlib import Path
25
+
26
+ import cv2
27
+ import numpy as np
28
+ from scenedetect import FrameTimecode
29
+ from scenedetect.platform import FakeTqdmObject
30
+ from tqdm import tqdm
31
+
32
+ from dvr_scan.detector import MotionDetector
33
+ from dvr_scan.overlays import BoundingBoxOverlay, TextOverlay
34
+ from dvr_scan.platform import (
35
+ HAS_PILLOW,
36
+ HAS_TKINTER,
37
+ get_min_screen_bounds,
38
+ is_ffmpeg_available,
39
+ )
40
+ from dvr_scan.region import Point, Size, bound_point, load_regions
41
+ from dvr_scan.subtractor import SubtractorCNT, SubtractorCudaMOG2, SubtractorMOG2
42
+ from dvr_scan.video_joiner import VideoJoiner
43
+
44
+ if HAS_TKINTER and HAS_PILLOW:
45
+ from dvr_scan.app.region_editor import RegionEditor
46
+
47
+ logger = logging.getLogger("dvr_scan")
48
+
49
+ DEFAULT_VIDEOWRITER_CODEC = "XVID"
50
+ """Default codec to use with OpenCV VideoWriter."""
51
+
52
+ MAX_DECODE_QUEUE_SIZE: int = 4
53
+ """Maximum size of the queue of frames waiting to be processed after decoding."""
54
+
55
+ MAX_ENCODE_QUEUE_SIZE: int = 4
56
+ """Maximum size of the queue of encode events waiting to be processed."""
57
+
58
+ DEFAULT_FFMPEG_INPUT_ARGS = "-v error"
59
+ """Default arguments to add before input when invoking ffmpeg."""
60
+
61
+ DEFAULT_FFMPEG_OUTPUT_ARGS = (
62
+ "-map 0:v:0 -map 0:a? -map 0:s? -c:v libx264 -preset veryfast -crf 22 -c:a aac"
63
+ )
64
+ """Default arguments passed to ffmpeg when using OutputMode.FFMPEG."""
65
+
66
+ COPY_MODE_OUTPUT_ARGS = "-map 0:v:0 -map 0:a? -map 0:s? -c:v copy -c:a copy"
67
+ """Default arguments passed to ffmpeg when using OutputMode.COPY."""
68
+
69
+ # TODO(#89): Add ability to set output name template.
70
+ OUTPUT_FILE_TEMPLATE = "{VIDEO_NAME}.DSME_{EVENT_NUMBER}.{EXTENSION}"
71
+ """Template to use for generating output files."""
72
+
73
+ PROGRESS_BAR_DESCRIPTION = "Detected: %d | Progress"
74
+ """Template to use for progress bar."""
75
+
76
+
77
+ class DetectorType(Enum):
78
+ """Type of motion detector to use (see dvr_scan.detector for implementations)."""
79
+
80
+ MOG2 = SubtractorMOG2
81
+ CNT = SubtractorCNT
82
+ MOG2_CUDA = SubtractorCudaMOG2
83
+
84
+
85
+ class OutputMode(Enum):
86
+ """Mode to export each motion event using."""
87
+
88
+ SCAN_ONLY = 1
89
+ """Don't generate any output files."""
90
+ OPENCV = 2
91
+ """Output using OpenCV VideoWriter."""
92
+ COPY = 3
93
+ """Output using ffmpeg in codec-copy mode."""
94
+ FFMPEG = 4
95
+ """Output using ffmpeg."""
96
+
97
+ def __str__(self):
98
+ if self == OutputMode.SCAN_ONLY:
99
+ return "SCAN_ONLY"
100
+ if self == OutputMode.OPENCV:
101
+ return "SCAN_ONLY"
102
+ if self == OutputMode.COPY:
103
+ return "SCAN_ONLY"
104
+ if self == OutputMode.FFMPEG:
105
+ return "SCAN_ONLY"
106
+
107
+
108
+ @dataclass
109
+ class DecodeEvent:
110
+ """Event generated by decode thread on each video frame."""
111
+
112
+ frame_bgr: np.ndarray
113
+ timecode: FrameTimecode
114
+
115
+
116
+ @dataclass
117
+ class EncodeFrameEvent:
118
+ """Event generated by main thread for each frame in a motion event."""
119
+
120
+ frame_bgr: np.ndarray
121
+ timecode: FrameTimecode
122
+ score: float
123
+ bounding_box: ty.Tuple[int, int, int, int]
124
+ end_of_event: bool = False
125
+
126
+
127
+ @dataclass
128
+ class MotionMaskEvent:
129
+ """Event generated by main thread containing the motion mask for each decoded frame."""
130
+
131
+ motion_mask: np.ndarray
132
+ timecode: FrameTimecode
133
+ score: float
134
+ bounding_box: ty.Optional[ty.Tuple[int, int, int, int]]
135
+
136
+
137
+ @dataclass
138
+ class MotionEvent:
139
+ """Contiguous sequence of frames where motion was detected."""
140
+
141
+ start: FrameTimecode
142
+ end: FrameTimecode
143
+
144
+
145
+ @dataclass
146
+ class DetectionResult:
147
+ """Motion events detected from scanning `num_frames` consecutive frames."""
148
+
149
+ event_list: ty.List[MotionEvent]
150
+ num_frames: int
151
+
152
+
153
+ def _scale_kernel_size(kernel_size: int, downscale_factor: int):
154
+ if kernel_size in (0, 1):
155
+ return kernel_size
156
+ corrected_size = round(kernel_size / float(downscale_factor))
157
+ if corrected_size % 2 == 0:
158
+ corrected_size -= 1
159
+ return max(corrected_size, 3)
160
+
161
+
162
+ # TODO(#75): See if this needs to be tweaked for the CNT algorithm.
163
+ def _recommended_kernel_size(frame_width: int, downscale_factor: int) -> int:
164
+ corrected_width = round(frame_width / float(downscale_factor))
165
+ return 7 if corrected_width >= 1920 else 5 if corrected_width >= 1280 else 3
166
+
167
+
168
+ def _extract_event_ffmpeg(
169
+ input_path: Path,
170
+ output_path: Path,
171
+ start_time: FrameTimecode,
172
+ end_time: FrameTimecode,
173
+ ffmpeg_input_args: str,
174
+ ffmpeg_out_args: str,
175
+ log_args: bool = False,
176
+ ) -> bool:
177
+ args: ty.List[str] = [
178
+ "ffmpeg",
179
+ "-y",
180
+ "-nostdin",
181
+ *ffmpeg_input_args.split(" "),
182
+ "-ss",
183
+ start_time.get_timecode(),
184
+ "-i",
185
+ str(input_path),
186
+ "-t",
187
+ (end_time - start_time).get_timecode(),
188
+ *ffmpeg_out_args.split(" "),
189
+ str(output_path),
190
+ ]
191
+ if log_args or logger.getEffectiveLevel() == logging.DEBUG:
192
+ logger.info("%s", " ".join(args))
193
+ # Invoke the command and capture the output (exception is raised on non-zero return code).
194
+ output: str = subprocess.check_output(
195
+ args=args,
196
+ text=True,
197
+ stderr=subprocess.STDOUT,
198
+ )
199
+ # Log any output we get from the ffmpeg process.
200
+ if output:
201
+ verbosity = logging.INFO
202
+ # Upgrade verbosity depending on specified ffmpeg verbosity (if any).
203
+ if any(["-v %s" % v in ffmpeg_input_args for v in ["panic", "fatal", "error"]]):
204
+ verbosity = logging.ERROR
205
+ elif "-v warning" in ffmpeg_input_args:
206
+ verbosity = logging.WARNING
207
+ logger.log(
208
+ verbosity,
209
+ "ffmpeg output:\n\n%s",
210
+ output,
211
+ )
212
+
213
+
214
+ # TODO: Pogram options should be separated out from those variables required for a motion scan.
215
+ class MotionScanner:
216
+ """MotionScanner represents the DVR-Scan program state, including the current settings for
217
+ various options, coordinating overall scanning logic (via the scan() method)."""
218
+
219
+ def __init__(
220
+ self,
221
+ input_videos: ty.List[Path],
222
+ input_mode: str = "opencv",
223
+ frame_skip: int = 0,
224
+ show_progress: bool = False,
225
+ debug_mode: bool = False,
226
+ ):
227
+ self._show_progress: bool = show_progress
228
+ self._debug_mode: bool = debug_mode
229
+
230
+ # Scan state and options they come from:
231
+
232
+ # Output Parameters (set_output)
233
+ self._comp_file: ty.Optional[Path] = None # -o/--output
234
+ self._mask_file: ty.Optional[Path] = None # -mo/--mask-output
235
+ self._fourcc: ty.Any = None # opencv-codec
236
+ self._output_mode: OutputMode = None # -m/--output-mode / -so/--scan-only
237
+ self._ffmpeg_input_args: ty.Optional[str] = None # input args for OutputMode.FFMPEG/COPY
238
+ self._ffmpeg_output_args: ty.Optional[str] = None # output args for OutputMode.FFMPEG
239
+ self._output_dir: ty.Optional[Path] = None # -d/--directory
240
+ # TODO: Replace uses of self._output_dir with
241
+ # a helper function called "get_output_path".
242
+
243
+ # Overlay Parameters (set_overlays)
244
+ self._timecode_overlay = None # -tc/--time-code, None or TextOverlay
245
+ self._metrics_overlay = None # -fm/--frame-metrics, None or TextOverlay
246
+ self._bounding_box = None # -bb/--bounding-box, None or BoundingBoxOverlay
247
+
248
+ # Motion Detection Parameters (set_detection_params)
249
+ self._subtractor_type = DetectorType.MOG2 # -b/--bg-subtractor
250
+ self._threshold = 0.15 # -t/--threshold
251
+ self._variance_threshold = 16.0 # variance-threshold
252
+ self._kernel_size = None # -k/--kernel-size
253
+ self._downscale_factor = 1 # -df/--downscale-factor
254
+ self._learning_rate = -1 # learning-rate
255
+ self._max_threshold = 255.0 # max-threshold
256
+ self._max_area = 1.0 # max-area
257
+ self._max_width = 1.0 # max-width
258
+ self._max_height = 1.0 # max-height
259
+
260
+ # Motion Event Parameters (set_event_params)
261
+ self._min_event_len = None # -l/--min-event-length
262
+ self._pre_event_len = None # -tb/--time-before-event
263
+ self._post_event_len = None # -tp/--time-post-event
264
+ self._use_pts = None # --use_pts
265
+
266
+ # Region Parameters (set_region)
267
+ self._region_editor = False # -w/--region-window
268
+ self._regions: ty.List[ty.List[Point]] = [] # -a/--add-region, -w/--region-window
269
+ self._load_region: ty.Optional[Path] = None # -R/--load-region
270
+ self._save_region: ty.Optional[Path] = None # -s/--save-region
271
+ self._max_roi_size_deprecated = None
272
+ self._show_roi_window_deprecated = False
273
+ self._roi_deprecated = None
274
+
275
+ # Input Video Parameters (set_video_time)
276
+ self._input: VideoJoiner = VideoJoiner(input_videos, backend=input_mode) # -i/--input
277
+ self._frame_skip: int = frame_skip # -fs/--frame-skip
278
+ self._start_time: FrameTimecode = None # -st/--start-time
279
+ self._end_time: FrameTimecode = None # -et/--end-time
280
+
281
+ # Internal Variables
282
+ self._stop: threading.Event = threading.Event()
283
+ self._decode_thread_exception = None
284
+ self._encode_thread_exception = None
285
+ self._video_writer: ty.Optional[cv2.VideoWriter] = None
286
+ self._mask_size: ty.Tuple[int, int] = None
287
+ self._mask_writer: ty.Optional[cv2.VideoWriter] = None
288
+ self._num_events: int = 0
289
+
290
+ # Thumbnail production (set_thumbnail_params)
291
+ self._thumbnails = None
292
+ self._highscore = 0
293
+ self._highframe = None
294
+
295
+ # Callbacks for UI integration
296
+ self._scan_started = None
297
+ self._processed_frame = None
298
+
299
+ # Make sure we initialize defaults now that we loaded the input videos.
300
+ self.set_detection_params()
301
+ self.set_event_params()
302
+ self.set_thumbnail_params()
303
+ self.set_video_time()
304
+
305
+ @property
306
+ def framerate(self) -> float:
307
+ """Get the framerate of the currently loaded video(s)."""
308
+ return self._input.framerate
309
+
310
+ def set_output(
311
+ self,
312
+ output_dir: ty.Optional[Path] = None,
313
+ comp_file: ty.Optional[Path] = None,
314
+ mask_file: ty.Optional[Path] = None,
315
+ output_mode: ty.Union[OutputMode, str] = OutputMode.SCAN_ONLY,
316
+ opencv_fourcc: str = DEFAULT_VIDEOWRITER_CODEC,
317
+ ffmpeg_input_args: str = DEFAULT_FFMPEG_INPUT_ARGS,
318
+ ffmpeg_output_args: str = DEFAULT_FFMPEG_OUTPUT_ARGS,
319
+ ):
320
+ """Sets the path and encoder codec to use when exporting videos.
321
+
322
+ Arguments:
323
+ output_dir: If set, folder where output files will be written to.
324
+ If empty string, output will be the current working directory.
325
+ comp_file: If set, represents path to single video that all motion events will
326
+ be written to. Otherwise each motion event will be saved as a separate file
327
+ based on the (first) input video filename.
328
+ output_mode: The mode to use when generating/encoding the output file. Certain
329
+ features may not be compatible with all ouptut modes.
330
+ opencv_fourcc: The four-letter identifier of the encoder/video
331
+ codec to use when output_mode is OutputMode.OPENCV. Possible values are:
332
+ XVID, MP4V, MP42, H264.
333
+ ffmpeg_input_args: Arguments to pass to ffmpeg before the input video. Only used
334
+ when output_mode is OutputMode.FFMPEG or OutputMode.COPY.
335
+ ffmpeg_output_args: Arguments to pass to ffmpeg for the output video. Only used when
336
+ output_mode is OutputMode.FFMPEG.
337
+
338
+ Raises:
339
+ ValueError:
340
+ - codec is not four characters
341
+ - comp_file is set but output_mode is not OutputMode.OPENCV
342
+ - output_dir is set but either comp_file or mask_file are absolute paths
343
+ - multiple input videos and output_mode is not OutputMode.OPENCV
344
+ - output_mode is OutputMode.FFMPEG or OutputMode.COPY but ffmpeg is not available
345
+ KeyError:
346
+ - output_mode does not exist in OutputMode
347
+ """
348
+ if output_dir:
349
+ if comp_file and comp_file.is_absolute():
350
+ raise ValueError("output file cannot be absolute path if output directory is set!")
351
+ if mask_file and mask_file.is_absolute():
352
+ raise ValueError("mask file cannot be absolute path if output directory is set!")
353
+ if len(opencv_fourcc) != 4:
354
+ raise ValueError("codec must be exactly FOUR (4) characters")
355
+ if not isinstance(output_mode, OutputMode):
356
+ output_mode = OutputMode[output_mode.upper().replace("-", "_")]
357
+ if len(self._input.paths) > 1 and output_mode not in (
358
+ OutputMode.SCAN_ONLY,
359
+ OutputMode.OPENCV,
360
+ ):
361
+ raise ValueError(
362
+ "input concatenation is only supported in `scan-only` or `opencv` mode."
363
+ )
364
+ if comp_file is not None and output_mode != OutputMode.OPENCV:
365
+ raise ValueError("output to single file is only supported with mode `opencv`")
366
+ if output_mode in (OutputMode.FFMPEG, OutputMode.COPY) and not is_ffmpeg_available():
367
+ raise ValueError("ffmpeg is required to use output mode FFMPEG/COPY")
368
+ self._comp_file = comp_file
369
+ self._mask_file = mask_file
370
+ self._output_mode = output_mode
371
+ self._fourcc = cv2.VideoWriter_fourcc(*opencv_fourcc.upper())
372
+ self._ffmpeg_input_args = ffmpeg_input_args
373
+ self._ffmpeg_output_args = ffmpeg_output_args
374
+ # If an output directory is defined, ensure it exists, and if not, try to create it.
375
+ if output_dir:
376
+ output_dir.mkdir(parents=True, exist_ok=True)
377
+ self._output_dir = output_dir
378
+
379
+ def set_overlays(
380
+ self,
381
+ timecode_overlay: ty.Optional[TextOverlay] = None,
382
+ metrics_overlay: ty.Optional[TextOverlay] = None,
383
+ bounding_box: ty.Optional[BoundingBoxOverlay] = None,
384
+ ):
385
+ """Sets options to use if/when drawing overlays on the resulting frames.
386
+
387
+ Arguments:
388
+ draw_timecode: If True, draw a timecode (presentation time) on each frame.
389
+ bounding_box_smoothing: Value to use for temporal smoothing (in time) for drawing a
390
+ bounding box containing all detected motion in each frame. If None, no box will
391
+ be drawn. If <= 1, smoothing will be disabled.
392
+ """
393
+ self._timecode_overlay = timecode_overlay
394
+ self._metrics_overlay = metrics_overlay
395
+ self._bounding_box = bounding_box
396
+
397
+ def set_detection_params(
398
+ self,
399
+ detector_type: DetectorType = DetectorType.MOG2,
400
+ threshold: float = 0.15,
401
+ max_threshold: float = 255.0,
402
+ max_area: float = 1.0,
403
+ max_width: float = 1.0,
404
+ max_height: float = 1.0,
405
+ variance_threshold: float = 16.0,
406
+ kernel_size: int = -1,
407
+ downscale_factor: int = 1,
408
+ learning_rate: float = -1,
409
+ ):
410
+ """Set detection parameters."""
411
+ self._threshold = threshold
412
+ self._max_threshold = max_threshold
413
+ self._max_area = max_area
414
+ self._max_width = max_width
415
+ self._max_height = max_height
416
+ self._subtractor_type = detector_type
417
+ if downscale_factor < 0:
418
+ raise ValueError("Downscale factor must be positive.")
419
+ self._downscale_factor = max(downscale_factor, 1)
420
+ assert kernel_size == -1 or kernel_size == 0 or kernel_size >= 3
421
+ self._kernel_size = kernel_size
422
+ self._variance_threshold = variance_threshold
423
+ # TODO: Also allow ability to customize history size, as this is another factor that
424
+ # influences how quickly the background model is updated. When calculated automatically,
425
+ # OpenCV sets learning rate as:
426
+ #
427
+ # learning_rate = 1.0 / min(num_frames, history_length)
428
+ #
429
+ # We should also investigate how this works for CNT and other subtractors.
430
+ self._learning_rate = learning_rate
431
+
432
+ def set_regions(
433
+ self,
434
+ region_editor: bool = False,
435
+ regions: ty.Optional[ty.List[ty.List[Point]]] = None,
436
+ load_region: ty.Optional[Path] = None,
437
+ save_region: ty.Optional[Path] = None,
438
+ roi_deprecated: ty.Optional[ty.List[int]] = None,
439
+ ):
440
+ """Set options for limiting detection regions."""
441
+ self._region_editor = region_editor
442
+ self._regions = regions if regions else []
443
+ self._load_region = load_region
444
+ self._save_region = save_region
445
+
446
+ # Handle deprecated ROI option.
447
+ # TODO(v2.0): Remove.
448
+ if roi_deprecated is not None:
449
+ if roi_deprecated:
450
+ if not all(isinstance(i, int) for i in roi_deprecated):
451
+ raise TypeError("Error: Non-integral type found in specified roi.")
452
+ if any(x < 0 for x in roi_deprecated):
453
+ raise ValueError("Error: Negative value found in roi.")
454
+ if len(roi_deprecated) == 2:
455
+ self._max_roi_size_deprecated = roi_deprecated
456
+ self._show_roi_window_deprecated = True
457
+ elif len(roi_deprecated) == 4:
458
+ self._roi_deprecated = roi_deprecated
459
+ self._show_roi_window_deprecated = False
460
+ else:
461
+ raise ValueError("Error: Expected either 2 or 4 elements in roi.")
462
+ # -roi with no arguments.
463
+ else:
464
+ self._show_roi_window_deprecated = True
465
+
466
+ def set_event_params(
467
+ self,
468
+ min_event_len: ty.Union[int, float, str] = "0.1s",
469
+ time_pre_event: ty.Union[int, float, str] = "1.5s",
470
+ time_post_event: ty.Union[int, float, str] = "2s",
471
+ use_pts: bool = False,
472
+ ):
473
+ """Set motion event parameters."""
474
+ assert self._input.framerate is not None
475
+ self._min_event_len = FrameTimecode(min_event_len, self._input.framerate)
476
+ self._pre_event_len = FrameTimecode(time_pre_event, self._input.framerate)
477
+ self._post_event_len = FrameTimecode(time_post_event, self._input.framerate)
478
+ self._use_pts = use_pts
479
+
480
+ def set_thumbnail_params(self, thumbnails: str = None):
481
+ self._thumbnails = thumbnails
482
+
483
+ def set_video_time(
484
+ self,
485
+ start_time: ty.Optional[ty.Union[int, float, str]] = None,
486
+ end_time: ty.Optional[ty.Union[int, float, str]] = None,
487
+ duration: ty.Optional[ty.Union[int, float, str]] = None,
488
+ ):
489
+ """Set a sub-set of the video in time for processing."""
490
+ assert self._input.framerate is not None
491
+ if start_time is not None:
492
+ self._start_time = FrameTimecode(start_time, self._input.framerate)
493
+ if duration is not None:
494
+ duration = FrameTimecode(duration, self._input.framerate)
495
+ if self._start_time is not None:
496
+ self._end_time = self._start_time + duration.frame_num
497
+ else:
498
+ self._end_time = duration
499
+ elif end_time is not None:
500
+ self._end_time = FrameTimecode(end_time, self._input.framerate)
501
+
502
+ def _handle_regions(self) -> bool:
503
+ # TODO(v2.0): Remove deprecated ROI selection handlers.
504
+ if (self._show_roi_window_deprecated) and (
505
+ self._load_region or self._regions or self._region_editor
506
+ ):
507
+ raise ValueError("Use -r/--region-editor instead of -roi.")
508
+ if not self._select_roi_deprecated():
509
+ return
510
+
511
+ if self._load_region:
512
+ if not self._load_region.exists():
513
+ logger.error(f"File does not exist: {self._load_region}")
514
+ raise ValueError(
515
+ "Could not find specified region file. Ensure the specified path is valid "
516
+ "and the file exists."
517
+ )
518
+ try:
519
+ logger.info(f"Loading regions from file: {self._load_region}")
520
+ region_editor = load_regions(self._load_region)
521
+ except ValueError as ex:
522
+ reason = " ".join(str(arg) for arg in ex.args)
523
+ if not reason:
524
+ reason = "Could not parse region file!"
525
+ logger.error(f"Error loading region from {self._load_region}: {reason}")
526
+ else:
527
+ logger.info(
528
+ "Loaded %d region%s:\n%s",
529
+ len(region_editor),
530
+ "s" if len(region_editor) > 1 else "",
531
+ "\n".join(f"[{i}] = {points}" for i, points in enumerate(region_editor)),
532
+ )
533
+ self._regions += region_editor
534
+ if self._regions:
535
+ self._regions = [
536
+ [bound_point(point, Size(*self._input.resolution)) for point in shape]
537
+ for shape in self._regions
538
+ ]
539
+ if self._region_editor:
540
+ if not HAS_TKINTER:
541
+ logger.error(
542
+ "Error: Region editor requires Tcl/Tk support to run. Try installing "
543
+ "the python3-tk package (sudo apt install python3-tk)."
544
+ )
545
+ raise SystemExit(1)
546
+ if not HAS_PILLOW:
547
+ logger.error(
548
+ "Error: Region editor requires pillow to run. Try installing "
549
+ "the pillow package (pip install pillow)."
550
+ )
551
+ raise SystemExit(1)
552
+
553
+ logger.info("Selecting area of interest:")
554
+ # TODO(#239): We should process this frame (right now it gets skipped).
555
+ frame_for_crop = self._input.read()
556
+ scale_factor = 1
557
+ screen_bounds = get_min_screen_bounds()
558
+ if screen_bounds is not None:
559
+ max_w, max_h = screen_bounds[1], screen_bounds[0]
560
+ frame_w, frame_h = frame_for_crop.shape[1], frame_for_crop.shape[0]
561
+ if (max_h > 0 and frame_h > max_h) or (max_w > 0 and frame_w > max_w):
562
+ logger.debug("Max window size: %d x %d", max_w, max_h)
563
+ # Downscale the image if it's too large for the screen.
564
+ factor_h = frame_h / float(max_h) if max_h > 0 and frame_h > max_h else 1
565
+ factor_w = frame_w / float(max_w) if max_w > 0 and frame_w > max_w else 1
566
+ scale_factor = round(max(factor_h, factor_w))
567
+ region_editor = RegionEditor(
568
+ frame=frame_for_crop,
569
+ initial_shapes=self._regions,
570
+ initial_scale=scale_factor,
571
+ debug_mode=self._debug_mode,
572
+ video_path=self._input.paths[0],
573
+ save_path=self._save_region,
574
+ )
575
+ if not region_editor.run():
576
+ return False
577
+ self._regions = list(region_editor.shapes)
578
+ elif self._save_region:
579
+ region_editor = (
580
+ self._regions
581
+ if self._regions
582
+ else [
583
+ [
584
+ Point(0, 0),
585
+ Point(self._input.resolution[0] - 1, 0),
586
+ Point(self._input.resolution[0] - 1, self._input.resolution[1] - 1),
587
+ Point(0, self._input.resolution[1] - 1),
588
+ ]
589
+ ]
590
+ )
591
+ path = self._save_region
592
+ if self._output_dir:
593
+ path = self._output_dir / path
594
+ with open(path, "w") as region_file:
595
+ for shape in self._regions:
596
+ region_file.write(" ".join(f"{x} {y}" for x, y in shape))
597
+ region_file.write("\n")
598
+ logger.info(f"Saved region data to: {path}")
599
+ if self._regions:
600
+ logger.info(
601
+ f"Limiting detection to {len(self._regions)} "
602
+ f"region{'s' if len(self._regions) > 1 else ''}."
603
+ )
604
+ else:
605
+ logger.debug("no regions selected")
606
+ return True
607
+
608
+ @property
609
+ def frames_remaining(self) -> int:
610
+ num_frames = self._input.total_frames
611
+ # Correct for end time.
612
+ if self._end_time and self._end_time.frame_num < num_frames:
613
+ num_frames = self._end_time.frame_num
614
+ # Correct for current seek position.
615
+ if self._start_time:
616
+ num_frames = max(0, num_frames - self._start_time.frame_num)
617
+ return num_frames
618
+
619
+ def _create_progress_bar(self) -> tqdm:
620
+ return tqdm(
621
+ total=self.frames_remaining,
622
+ unit=" frames",
623
+ desc=PROGRESS_BAR_DESCRIPTION % 0,
624
+ dynamic_ncols=True,
625
+ )
626
+
627
+ def stop(self):
628
+ """Stop the current scan call. Thread-safe."""
629
+ self._stop.set()
630
+ logger.debug("Stop event set.")
631
+
632
+ def is_stopped(self):
633
+ """Check if the current scan call was stopped, or `False` if one wasn't run. Thread-safe."""
634
+ return self._stop.is_set()
635
+
636
+ def set_callbacks(
637
+ self, scan_started: ty.Callable[[int], None], processed_frame: ty.Callable[[int], None]
638
+ ):
639
+ self._scan_started = scan_started
640
+ self._processed_frame = processed_frame
641
+
642
+ def scan(self) -> ty.Optional[DetectionResult]:
643
+ """Performs motion analysis on the MotionScanner's input video(s)."""
644
+ self._stop.clear()
645
+ buffered_frames: ty.List[np.ndarray] = []
646
+ event_window: ty.List[float] = []
647
+ event_list: ty.List[MotionEvent] = []
648
+ num_frames_post_event = 0
649
+ event_start = None
650
+
651
+ in_motion_event = False
652
+ frames_processed = 0
653
+
654
+ # Seek to starting position if required.
655
+ if self._start_time is not None:
656
+ self._input.seek(self._start_time)
657
+
658
+ if not self._use_pts:
659
+ start_frame = self._input.position.frame_num
660
+ else:
661
+ start_frame_ms = self._input.position_ms
662
+
663
+ # Show ROI selection window if required.
664
+ if not self._handle_regions():
665
+ logger.info("Exiting...")
666
+ return None
667
+
668
+ if self._kernel_size == -1:
669
+ # Calculate size of noise reduction kernel. Even if an ROI is set, the auto factor is
670
+ # set based on the original video's input resolution.
671
+ kernel_size = _recommended_kernel_size(
672
+ self._input.resolution[0], self._downscale_factor
673
+ )
674
+ else:
675
+ kernel_size = _scale_kernel_size(self._kernel_size, self._downscale_factor)
676
+
677
+ test_width_height_area = (
678
+ self._max_area < 1.0 or self._max_width < 1.0 or self._max_height < 1.0
679
+ )
680
+
681
+ # Create background subtractor and motion detector.
682
+ # TODO: Figure out how to avoid logging unused parameters or emit a warning. For example,
683
+ # the `variance_threshold` parameter is ignored by the `CNT` subtractor.
684
+ detector = MotionDetector(
685
+ subtractor=self._subtractor_type.value(
686
+ variance_threshold=self._variance_threshold,
687
+ kernel_size=kernel_size,
688
+ learning_rate=self._learning_rate,
689
+ ),
690
+ frame_size=self._input.resolution,
691
+ downscale=self._downscale_factor,
692
+ regions=self._regions,
693
+ )
694
+
695
+ logger.info(
696
+ "Using subtractor %s with kernel_size = %s%s, "
697
+ "variance_threshold = %s and learning_rate = %s",
698
+ self._subtractor_type.name,
699
+ str(kernel_size) if kernel_size else "off",
700
+ " (auto)" if self._kernel_size == -1 else "",
701
+ str(self._variance_threshold) if self._variance_threshold != 16.0 else "auto",
702
+ str(self._learning_rate) if self._learning_rate != -1 else "auto",
703
+ )
704
+
705
+ # Correct event length parameters to account frame skip.
706
+ post_event_len: int = self._post_event_len.frame_num // (self._frame_skip + 1)
707
+ pre_event_len: int = self._pre_event_len.frame_num // (self._frame_skip + 1)
708
+ min_event_len: int = max(self._min_event_len.frame_num // (self._frame_skip + 1), 1)
709
+
710
+ # Calculations below rely on min_event_len always being >= 1 (cannot be zero)
711
+ assert min_event_len >= 1, "min_event_len must be at least 1 frame"
712
+
713
+ # Ensure that we include the exact amount of time specified in `-tb`/`--time-before` when
714
+ # shifting the event start time. Instead of using `-l`/`--min-event-len` directly, we
715
+ # need to compensate for rounding errors when we corrected it for frame skip. This is
716
+ # important as this affects the number of frames we consider for the actual motion event.
717
+ if not self._use_pts:
718
+ start_event_shift: int = self._pre_event_len.frame_num + min_event_len * (
719
+ self._frame_skip + 1
720
+ )
721
+ else:
722
+ start_event_shift_ms: float = (
723
+ self._pre_event_len.get_seconds() + self._min_event_len.get_seconds()
724
+ ) * 1000
725
+
726
+ # Length of buffer we require in memory to keep track of all frames required for -l and -tb.
727
+ buff_len = pre_event_len + min_event_len
728
+ event_end = self._input.position
729
+ if not self._use_pts:
730
+ last_frame_above_threshold = 0
731
+ else:
732
+ last_frame_above_threshold_ms = 0
733
+
734
+ if self._bounding_box:
735
+ self._bounding_box.set_corrections(
736
+ downscale_factor=self._downscale_factor,
737
+ shift=(detector.area[0].x, detector.area[0].y),
738
+ frame_skip=self._frame_skip,
739
+ )
740
+
741
+ # Don't use the first result from the background subtractor.
742
+ processed_first_frame = False
743
+
744
+ # Motion event scanning/detection loop. Need to avoid CLI output/logging until end of the
745
+ # main scanning loop below, otherwise it will interrupt the progress bar.
746
+ logger.info(
747
+ "Scanning %s for motion events...",
748
+ "%d input videos" % len(self._input.paths)
749
+ if len(self._input.paths) > 1
750
+ else "input video",
751
+ )
752
+ logger.debug(
753
+ f"input mode = {self._input._backend.BACKEND_NAME}, output mode = {self._output_mode}"
754
+ )
755
+
756
+ progress_bar = FakeTqdmObject() if not self._show_progress else self._create_progress_bar()
757
+ num_frames_to_process = self.frames_remaining
758
+
759
+ decode_queue = queue.Queue(MAX_DECODE_QUEUE_SIZE)
760
+ decode_thread = threading.Thread(
761
+ target=MotionScanner._decode_thread, args=(self, decode_queue), daemon=True
762
+ )
763
+ decode_thread.start()
764
+
765
+ encode_thread = None
766
+ if self._output_mode != OutputMode.SCAN_ONLY or self._mask_file is not None:
767
+ encode_queue = queue.Queue(MAX_ENCODE_QUEUE_SIZE)
768
+ encode_thread = threading.Thread(
769
+ target=MotionScanner._encode_thread,
770
+ args=(self, encode_queue),
771
+ daemon=True,
772
+ )
773
+ encode_thread.start()
774
+
775
+ if self._scan_started:
776
+ self._scan_started(num_frames=num_frames_to_process)
777
+
778
+ # TODO: The main scanning loop should be refactored into a state machine. The main loop
779
+ # # has grown far too complicated and is difficult to maintain.
780
+ while not self._stop.is_set():
781
+ if self._processed_frame:
782
+ num_events = len(event_list)
783
+ if in_motion_event:
784
+ num_events += 1
785
+ self._processed_frame(progress_bar=progress_bar, num_events=num_events)
786
+ # Keep polling decode queue until it's empty (signaled via None).
787
+ frame: ty.Optional[DecodeEvent] = decode_queue.get()
788
+ if frame is None:
789
+ break
790
+ assert frame.frame_bgr is not None
791
+ pts = frame.timecode.get_seconds() * 1000
792
+ frame_size = (frame.frame_bgr.shape[1], frame.frame_bgr.shape[0])
793
+ if frame_size != self._input.resolution:
794
+ time = frame.timecode
795
+ video_res = self._input.resolution
796
+ logger.warn(
797
+ f"WARNING: Frame {time.frame_num} [{time.get_timecode()}] has unexpected size: "
798
+ f"{frame_size[0]}x{frame_size[1]}, expected {video_res[0]}x{video_res[1]}"
799
+ )
800
+ result = detector.update(frame.frame_bgr)
801
+ frame_score = result.score
802
+
803
+ if test_width_height_area:
804
+ box_width = cv2.boundingRect(result.subtracted)[2] * self._downscale_factor
805
+ box_height = cv2.boundingRect(result.subtracted)[3] * self._downscale_factor
806
+ width_fraction = box_width / self._input.resolution[0]
807
+ height_fraction = box_height / self._input.resolution[1]
808
+ area_fraction = width_fraction * height_fraction
809
+
810
+ # TODO: The rejection filter can be disabled by providing values > 255.0, but we should
811
+ # provide a better method of disabling it. It might also be useful to allow users to
812
+ # specify the amount of consecutive frames the filter can be active for.
813
+ if frame_score >= self._max_threshold:
814
+ frame_score = 0
815
+ if test_width_height_area and (
816
+ area_fraction > self._max_area
817
+ or width_fraction > self._max_width
818
+ or height_fraction > self._max_height
819
+ ):
820
+ frame_score = 0
821
+ above_threshold = frame_score >= self._threshold
822
+
823
+ if above_threshold and frame_score > self._highscore:
824
+ self._highscore = frame_score
825
+ self._highframe = frame.frame_bgr
826
+
827
+ event_window.append(frame_score)
828
+ # The first frame fed to the detector can sometimes produce unreliable results due
829
+ # to it not having any previous information to compare against.
830
+ if not processed_first_frame:
831
+ above_threshold = False
832
+ processed_first_frame = True
833
+ event_window = event_window[-min_event_len:]
834
+
835
+ bounding_box = None
836
+ # TODO: Only call clear() when we exit the current motion event.
837
+ # TODO: Include frames below the threshold for smoothing, or push a sentinel
838
+ # value to update() to compensate the amount of smoothing accordingly.
839
+ if self._bounding_box:
840
+ bounding_box = (
841
+ self._bounding_box.update(result.subtracted)
842
+ if above_threshold
843
+ else self._bounding_box.clear()
844
+ )
845
+
846
+ if self._mask_file and not self._stop.is_set():
847
+ encode_queue.put(
848
+ MotionMaskEvent(
849
+ motion_mask=np.ma.filled(result.masked, fill_value=63),
850
+ timecode=frame.timecode,
851
+ score=frame_score,
852
+ bounding_box=bounding_box,
853
+ )
854
+ )
855
+
856
+ # Last frame was part of a motion event, or still within the post-event window.
857
+ if in_motion_event:
858
+ # If this frame still has motion, reset the post-event window.
859
+ if above_threshold:
860
+ num_frames_post_event = 0
861
+ if not self._use_pts:
862
+ last_frame_above_threshold = frame.timecode.frame_num
863
+ else:
864
+ last_frame_above_threshold_ms = pts
865
+ # Otherwise, we wait until the post-event window has passed before ending
866
+ # this motion event and start looking for a new one.
867
+ #
868
+ # TODO(#72): We should wait until the max of *both* the pre-event and post-
869
+ # event windows have passed. Right now we just consider the post-event window.
870
+ else:
871
+ num_frames_post_event += 1
872
+ if num_frames_post_event >= post_event_len:
873
+ in_motion_event = False
874
+
875
+ logger.debug(
876
+ "event %d high score %f" % (1 + self._num_events, self._highscore)
877
+ )
878
+ if self._thumbnails == "highscore":
879
+ video_name = self._input.paths[0].stem
880
+ output_path: Path = (
881
+ self._comp_file
882
+ if self._comp_file
883
+ else Path(
884
+ OUTPUT_FILE_TEMPLATE.format(
885
+ VIDEO_NAME=video_name,
886
+ EVENT_NUMBER="%04d" % (1 + self._num_events),
887
+ EXTENSION="jpg",
888
+ )
889
+ )
890
+ )
891
+ if self._output_dir:
892
+ output_path = self._output_dir / output_path
893
+ cv2.imwrite(str(output_path), self._highframe)
894
+ self._highscore = 0
895
+ self._highframe = None
896
+
897
+ # Calculate event end based on the last frame we had with motion plus
898
+ # the post event length time. We also need to compensate for the number
899
+ # of frames that we skipped that could have had motion.
900
+ # We also add 1 to include the presentation duration of the last frame.
901
+ if not self._use_pts:
902
+ event_end = FrameTimecode(
903
+ 1
904
+ + last_frame_above_threshold
905
+ + self._post_event_len.frame_num
906
+ + self._frame_skip,
907
+ self._input.framerate,
908
+ )
909
+ assert event_end.frame_num >= event_start.frame_num
910
+ else:
911
+ event_end = FrameTimecode(
912
+ (last_frame_above_threshold_ms / 1000)
913
+ + self._post_event_len.get_seconds(),
914
+ self._input.framerate,
915
+ )
916
+ assert event_end.get_seconds() >= event_start.get_seconds()
917
+ event_list.append(MotionEvent(start=event_start, end=event_end))
918
+ if self._output_mode != OutputMode.SCAN_ONLY:
919
+ encode_queue.put(MotionEvent(start=event_start, end=event_end))
920
+
921
+ # Send frame to encode thread.
922
+ if in_motion_event and self._output_mode == OutputMode.OPENCV:
923
+ encode_queue.put(
924
+ EncodeFrameEvent(
925
+ frame_bgr=frame.frame_bgr,
926
+ timecode=frame.timecode,
927
+ bounding_box=bounding_box,
928
+ score=frame_score,
929
+ )
930
+ )
931
+ # Not already in a motion event, look for a new one.
932
+ else:
933
+ # Buffer the required amount of frames and overlay data until we find an event.
934
+ if self._output_mode == OutputMode.OPENCV:
935
+ buffered_frames.append(
936
+ EncodeFrameEvent(
937
+ frame_bgr=frame.frame_bgr,
938
+ timecode=frame.timecode,
939
+ bounding_box=bounding_box,
940
+ score=frame_score,
941
+ )
942
+ )
943
+ buffered_frames = buffered_frames[-buff_len:]
944
+ # Start a new event once all frames in the event window have motion.
945
+ if len(event_window) >= min_event_len and all(
946
+ score >= self._threshold for score in event_window
947
+ ):
948
+ in_motion_event = True
949
+ progress_bar.set_description(
950
+ PROGRESS_BAR_DESCRIPTION % (1 + len(event_list)), refresh=False
951
+ )
952
+ event_window = []
953
+ num_frames_post_event = 0
954
+ frames_since_last_event = frame.timecode.frame_num - event_end.frame_num
955
+ last_frame_above_threshold = frame.timecode.frame_num
956
+
957
+ if not self._use_pts:
958
+ shift_amount = min(frames_since_last_event, start_event_shift)
959
+ shifted_start = max(
960
+ start_frame, frame.timecode.frame_num + 1 - shift_amount
961
+ )
962
+ event_start = FrameTimecode(shifted_start, self._input.framerate)
963
+ else:
964
+ ms_since_last_event = pts - (event_end.get_seconds() * 1000)
965
+ last_frame_above_threshold_ms = pts
966
+ # TODO: not sure all of this is actually necessary?
967
+ shift_amount_ms = min(ms_since_last_event, start_event_shift_ms)
968
+ shifted_start_ms = max(start_frame_ms, pts - shift_amount_ms)
969
+ event_start = FrameTimecode(shifted_start_ms / 1000, self._input.framerate)
970
+ # Send buffered frames to encode thread.
971
+ for encode_frame in buffered_frames:
972
+ # We have to be careful here. Since we're putting multiple items
973
+ # into the queue, we have to keep making sure the encode thread
974
+ # is running. Otherwise, the queue will never empty we will block
975
+ # indefinitely here waiting for a spot.
976
+ if self._stop.is_set():
977
+ break
978
+ encode_queue.put(encode_frame)
979
+ buffered_frames = []
980
+
981
+ frames_processed += 1 + self._frame_skip
982
+ progress_bar.update(1 + self._frame_skip)
983
+
984
+ # Close the progress bar before producing any more output.
985
+ progress_bar.close()
986
+
987
+ # Unblock any remaining puts, wait for decode thread, and re-raise any exceptions.
988
+ while not decode_queue.empty():
989
+ _ = decode_queue.get_nowait()
990
+ decode_thread.join()
991
+ if self._decode_thread_exception is not None:
992
+ raise self._decode_thread_exception[1].with_traceback(self._decode_thread_exception[2])
993
+
994
+ # Video ended, finished processing frames. If we're still in a motion event,
995
+ # compute the duration and ending timecode and add it to the event list.
996
+ if in_motion_event and not self._stop.is_set():
997
+ # curr_pos already includes the presentation duration of the frame.
998
+ if not self._use_pts:
999
+ event_end = FrameTimecode(self._input.position.frame_num, self._input.framerate)
1000
+ else:
1001
+ event_end = FrameTimecode((pts / 1000), self._input.framerate)
1002
+ event_list.append(MotionEvent(start=event_start, end=event_end))
1003
+
1004
+ logger.debug("event %d high score %f" % (1 + self._num_events, self._highscore))
1005
+ if self._thumbnails == "highscore":
1006
+ video_name = self._input.paths[0].stem
1007
+ output_path = (
1008
+ self._comp_file
1009
+ if self._comp_file
1010
+ else Path(
1011
+ OUTPUT_FILE_TEMPLATE.format(
1012
+ VIDEO_NAME=video_name,
1013
+ EVENT_NUMBER="%04d" % (1 + self._num_events),
1014
+ EXTENSION="jpg",
1015
+ )
1016
+ )
1017
+ )
1018
+ if self._output_dir:
1019
+ output_path = self._output_dir / output_path
1020
+ cv2.imwrite(str(output_path), self._highframe)
1021
+ self._highscore = 0
1022
+ self._highframe = None
1023
+
1024
+ if self._output_mode != OutputMode.SCAN_ONLY:
1025
+ encode_queue.put(MotionEvent(start=event_start, end=event_end))
1026
+
1027
+ # Push sentinel to queue, wait for encode thread, and re-raise any exceptions.
1028
+ if encode_thread is not None:
1029
+ encode_queue.put(None)
1030
+ encode_thread.join()
1031
+ if self._encode_thread_exception is not None:
1032
+ raise self._encode_thread_exception[1].with_traceback(
1033
+ self._encode_thread_exception[2]
1034
+ )
1035
+
1036
+ # Display an error if we got more than one decode failure / corrupt frame.
1037
+ # TODO: This will also fire if no frames are decoded. Add a check to make sure
1038
+ # the fourCC is valid. Also figure out a better way to handle the case where NO frames
1039
+ # are decoded (rather than reporting X frames failed to decode).
1040
+ if self._input.decode_failures > 1:
1041
+ logger.error(
1042
+ "Failed to decode %d frame(s) from video, timestamps may be incorrect. Try"
1043
+ " re-encoding or remuxing video (e.g. ffmpeg -i video.mp4 -c:v copy out.mp4). "
1044
+ "See https://github.com/Breakthrough/DVR-Scan/issues/62 for details.",
1045
+ self._input.decode_failures,
1046
+ )
1047
+
1048
+ return DetectionResult(event_list, frames_processed)
1049
+
1050
+ def _decode_thread(self, decode_queue: queue.Queue):
1051
+ try:
1052
+ while not self._stop.is_set():
1053
+ if self._end_time is not None and self._input.position >= self._end_time:
1054
+ break
1055
+ for _ in range(self._frame_skip):
1056
+ if self._input.read(decode=False) is None:
1057
+ break
1058
+ frame_bgr = self._input.read()
1059
+ if frame_bgr is None:
1060
+ break
1061
+ # self._input.position points to the time at the end of the current frame (i.e. the
1062
+ # first frame has a frame_num of 1), so we correct that for presentation time.
1063
+ assert self._input.position.frame_num > 0
1064
+ if not self._use_pts:
1065
+ presentation_time = FrameTimecode(
1066
+ timecode=self._input.position.frame_num - 1,
1067
+ fps=self._input.framerate,
1068
+ )
1069
+ else:
1070
+ presentation_time = FrameTimecode(
1071
+ self._input.position_ms / 1000, self._input.framerate
1072
+ )
1073
+ if not self._stop.is_set():
1074
+ decode_queue.put(DecodeEvent(frame_bgr, presentation_time))
1075
+
1076
+ # We'll re-raise any exceptions from the main thread.
1077
+ except: # noqa: E722
1078
+ self._stop.set()
1079
+ logger.critical("Fatal error: Exception raised in decode thread.")
1080
+ logger.debug(sys.exc_info())
1081
+ self._decode_thread_exception = sys.exc_info()
1082
+ finally:
1083
+ # Make sure main thread stops processing loop.
1084
+ decode_queue.put(None)
1085
+
1086
+ def _init_video_writer(self, path: Path, frame_size: ty.Tuple[int, int]) -> cv2.VideoWriter:
1087
+ """Create a new cv2.VideoWriter using the correct framerate."""
1088
+ if self._output_dir:
1089
+ path = self._output_dir / path
1090
+ effective_framerate = (
1091
+ self._input.framerate
1092
+ if self._frame_skip < 1
1093
+ else self._input.framerate / (1 + self._frame_skip)
1094
+ )
1095
+ return cv2.VideoWriter(str(path), self._fourcc, effective_framerate, frame_size)
1096
+
1097
+ def _on_encode_frame_event(self, event: EncodeFrameEvent):
1098
+ size = (event.frame_bgr.shape[1], event.frame_bgr.shape[0])
1099
+ if size != self._input.resolution:
1100
+ time = event.timecode
1101
+ video = self._input.resolution
1102
+ logger.warn(
1103
+ f"WARNING: Failed to write event at frame {time.frame_num} [{time.get_timecode()}] "
1104
+ f"due to size mismatch: {size[0]}x{size[1]}, expected {video[0]}x{video[1]}"
1105
+ )
1106
+ return
1107
+ if self._video_writer is None:
1108
+ # Use the first input video name as a filename template.
1109
+ video_name = self._input.paths[0].stem
1110
+ output_path = (
1111
+ self._comp_file
1112
+ if self._comp_file
1113
+ else Path(
1114
+ OUTPUT_FILE_TEMPLATE.format(
1115
+ VIDEO_NAME=video_name,
1116
+ EVENT_NUMBER="%04d" % (1 + self._num_events),
1117
+ EXTENSION="avi",
1118
+ )
1119
+ )
1120
+ )
1121
+ self._video_writer = self._init_video_writer(output_path, size)
1122
+ # *NOTE*: Overlays are currently rendered in-place by modifying the event itself.
1123
+ self._draw_overlays(event.frame_bgr, event.timecode, event.score, event.bounding_box)
1124
+ # Encode and write frame to disk.
1125
+
1126
+ self._video_writer.write(event.frame_bgr)
1127
+
1128
+ def _draw_overlays(
1129
+ self,
1130
+ frame: np.ndarray,
1131
+ timecode: FrameTimecode,
1132
+ frame_score: float,
1133
+ bounding_box: ty.Optional[ty.Tuple[int, int, int, int]],
1134
+ use_shift=True,
1135
+ ):
1136
+ if self._timecode_overlay is not None:
1137
+ self._timecode_overlay.draw(frame, text=timecode.get_timecode())
1138
+ if self._metrics_overlay is not None:
1139
+ to_display = "Frame: %04d\nScore: %3.2f" % (
1140
+ timecode.get_frames(),
1141
+ frame_score,
1142
+ )
1143
+ self._metrics_overlay.draw(frame, text=to_display)
1144
+ if self._bounding_box is not None and bounding_box is not None:
1145
+ self._bounding_box.draw(frame, bounding_box, use_shift)
1146
+
1147
+ def _on_mask_event(self, event: MotionMaskEvent):
1148
+ # Initialize the VideoWriter used for mask output.
1149
+ if self._mask_writer is None:
1150
+ self._mask_size = event.motion_mask.shape[1], event.motion_mask.shape[0]
1151
+ self._mask_writer = self._init_video_writer(self._mask_file, self._mask_size)
1152
+ # Write the motion mask to the output file.
1153
+ size = (event.motion_mask.shape[1], event.motion_mask.shape[0])
1154
+ if size != self._mask_size:
1155
+ time = event.timecode
1156
+ logger.warn(
1157
+ f"WARNING: Failed to write mask at frame {time.frame_num} [{time.get_timecode()}] "
1158
+ f"due to size mismatch: {size[0]}x{size[1]}, "
1159
+ f" expected {self._mask_size[0]}x{self._mask_size[1]}"
1160
+ )
1161
+ return
1162
+ out_frame = cv2.cvtColor(event.motion_mask, cv2.COLOR_GRAY2BGR)
1163
+ self._draw_overlays(
1164
+ out_frame, event.timecode, event.score, event.bounding_box, use_shift=False
1165
+ )
1166
+ self._mask_writer.write(out_frame)
1167
+
1168
+ def _on_motion_event(self, event: MotionEvent):
1169
+ self._num_events += 1
1170
+ # Handle case where we're not using Ffmpeg.
1171
+ if self._output_mode not in (OutputMode.FFMPEG, OutputMode.COPY):
1172
+ # If we're at the end of the event, close the current VideoWriter to output the next
1173
+ # event to a new file (unless we're concatenating all events in the same output file).
1174
+ if not self._comp_file and self._video_writer is not None:
1175
+ self._video_writer.release()
1176
+ self._video_writer = None
1177
+ return
1178
+ # Output motion event using Ffmpeg.
1179
+ output_args = (
1180
+ self._ffmpeg_output_args
1181
+ if self._output_mode == OutputMode.FFMPEG
1182
+ else COPY_MODE_OUTPUT_ARGS
1183
+ )
1184
+ # Use the first input video name as a filename template.
1185
+ video_name = self._input.paths[0].stem
1186
+ output_path = (
1187
+ self._comp_file
1188
+ if self._comp_file
1189
+ else Path(
1190
+ OUTPUT_FILE_TEMPLATE.format(
1191
+ VIDEO_NAME=video_name,
1192
+ EVENT_NUMBER="%04d" % self._num_events,
1193
+ EXTENSION="mp4",
1194
+ )
1195
+ )
1196
+ )
1197
+ if self._output_dir:
1198
+ output_path = self._output_dir / output_path
1199
+ # Only log the args passed to ffmpeg on the first event, to reduce log spam.
1200
+ log_args = False
1201
+ if self._num_events == 1:
1202
+ logger.info("Splitting events using ffmpeg, first event:")
1203
+ log_args = True
1204
+ _extract_event_ffmpeg(
1205
+ input_path=self._input.paths[0],
1206
+ output_path=output_path,
1207
+ start_time=event.start,
1208
+ end_time=event.end,
1209
+ ffmpeg_input_args=self._ffmpeg_input_args,
1210
+ ffmpeg_out_args=output_args,
1211
+ log_args=log_args,
1212
+ )
1213
+
1214
+ def _encode_thread(self, encode_queue: queue.Queue):
1215
+ try:
1216
+ while True:
1217
+ event: ty.Optional[ty.Union[EncodeFrameEvent, MotionMaskEvent, MotionEvent]] = (
1218
+ encode_queue.get()
1219
+ )
1220
+ if event is None:
1221
+ break
1222
+ if isinstance(event, EncodeFrameEvent):
1223
+ self._on_encode_frame_event(event)
1224
+ elif isinstance(event, MotionMaskEvent):
1225
+ self._on_mask_event(event)
1226
+ elif isinstance(event, MotionEvent):
1227
+ self._on_motion_event(event)
1228
+ # We'll re-raise any exceptions from the main thread.
1229
+ except: # noqa: E722
1230
+ self._stop.set()
1231
+ logger.critical("Fatal error: Exception raised in encode thread.")
1232
+ logger.debug(sys.exc_info())
1233
+ self._encode_thread_exception = sys.exc_info()
1234
+ finally:
1235
+ if self._video_writer is not None:
1236
+ self._video_writer.release()
1237
+ if self._mask_writer is not None:
1238
+ self._mask_writer.release()
1239
+ # Unblock any waiting puts if we stopped early.
1240
+ while not encode_queue.empty():
1241
+ _ = encode_queue.get_nowait()
1242
+
1243
+ # TODO(v2.0): Remove deprecated function, replaced by Region Editor.
1244
+ def _select_roi_deprecated(self) -> bool:
1245
+ # area selection
1246
+ if self._show_roi_window_deprecated:
1247
+ logger.warning(
1248
+ "**WARNING**: -roi/--region-of-interest is deprecated and will be removed.\n\n"
1249
+ "Use -r/--region-editor instead.\n"
1250
+ )
1251
+ logger.info("Selecting area of interest:")
1252
+ frame_for_crop = self._input.read()
1253
+ scale_factor = None
1254
+ if self._max_roi_size_deprecated is None:
1255
+ self._max_roi_size_deprecated = get_min_screen_bounds()
1256
+ if self._max_roi_size_deprecated is not None:
1257
+ frame_h, frame_w = (frame_for_crop.shape[0], frame_for_crop.shape[1])
1258
+ max_w, max_h = self._max_roi_size_deprecated
1259
+ # Downscale the image if it's too large for the screen.
1260
+ if frame_h > max_h or frame_w > max_w:
1261
+ factor_h = frame_h / float(max_h)
1262
+ factor_w = frame_w / float(max_w)
1263
+ scale_factor = max(factor_h, factor_w)
1264
+ new_height = round(frame_h / scale_factor)
1265
+ new_width = round(frame_w / scale_factor)
1266
+ frame_for_crop = cv2.resize(
1267
+ frame_for_crop,
1268
+ (new_width, new_height),
1269
+ interpolation=cv2.INTER_CUBIC,
1270
+ )
1271
+ roi = cv2.selectROI("DVR-Scan ROI Selection", frame_for_crop)
1272
+ cv2.destroyAllWindows()
1273
+ if any([coord == 0 for coord in roi[2:]]):
1274
+ logger.info("ROI selection cancelled. Aborting...")
1275
+ return False
1276
+ # Unscale coordinates if we downscaled the image.
1277
+ if scale_factor:
1278
+ roi = [round(x * scale_factor) for x in roi]
1279
+ self._roi_deprecated = roi
1280
+ if self._roi_deprecated:
1281
+ logger.info(
1282
+ "ROI set: (x,y)/(w,h) = (%d,%d)/(%d,%d)",
1283
+ self._roi_deprecated[0],
1284
+ self._roi_deprecated[1],
1285
+ self._roi_deprecated[2],
1286
+ self._roi_deprecated[3],
1287
+ )
1288
+ x, y, w, h = self._roi_deprecated
1289
+ region = [
1290
+ Point(x, y),
1291
+ Point(x + w, y),
1292
+ Point(x + w, y + h),
1293
+ Point(x, y + h),
1294
+ ]
1295
+ region_arg = " ".join(f"{x} {y}" for x, y in region)
1296
+ logger.warning(
1297
+ "**WARNING**: region-of-interest (-roi) is deprecated and will be removed.\n\n"
1298
+ "You can use the following equivalent region:\n"
1299
+ f"--add-region {region_arg}\n"
1300
+ "For config files, save this region to a file and set the load-region option.\n"
1301
+ )
1302
+ self._regions += [region]
1303
+ return True