Skip to content

Commit 8e682c2

Browse files
committed
[cli] Fix reporting unusable subtractor options
1 parent 7dc5473 commit 8e682c2

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

dvr_scan/cli/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import dvr_scan
2525
from dvr_scan.config import CHOICE_MAP, USER_CONFIG_FILE_PATH, ConfigRegistry
26-
from dvr_scan.platform import get_system_version_info
26+
from dvr_scan.platform import HAS_MOG2_CUDA, get_system_version_info
2727
from dvr_scan.region import RegionValidator
2828

2929
# Version string shown for the -v/--version CLI argument.
@@ -41,6 +41,9 @@
4141
VALID_OUTPUT_MODES = [mode for mode in CHOICE_MAP["output-mode"] if mode != SCAN_ONLY_MODE]
4242

4343

44+
BACKGROUND_SUBTRACTORS = ["MOG2", "CNT", "MOG2_CUDA"] if HAS_MOG2_CUDA else ["MOG2", "CNT"]
45+
46+
4447
def timecode_type_check(metavar: Optional[str] = None):
4548
"""Creates an argparse type for a user-inputted timecode.
4649
@@ -517,14 +520,15 @@ def get_cli_parser(user_config: ConfigRegistry):
517520
),
518521
)
519522

523+
MOG2_CUDA = ", MOG2_CUDA (Nvidia GPU)" if HAS_MOG2_CUDA else ""
520524
parser.add_argument(
521525
"-b",
522526
"--bg-subtractor",
523527
metavar="type",
524-
type=string_type_check(["MOG2", "CNT", "MOG2_CUDA"], False, "type"),
528+
type=string_type_check(BACKGROUND_SUBTRACTORS, False, "type"),
525529
help=(
526530
"The type of background subtractor to use, must be one of: "
527-
" MOG2 (default), CNT (parallel), MOG2_CUDA (Nvidia GPU).%s"
531+
f" MOG2 (default), CNT (parallel){MOG2_CUDA}.%s"
528532
)
529533
% user_config.get_help_string("bg-subtractor"),
530534
)

dvr_scan/cli/controller.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import argparse
1717
import glob
1818
import logging
19-
import sys
2019
import time
2120
import typing as ty
2221

@@ -182,19 +181,12 @@ def parse_settings(args: ty.List[str] = None) -> ty.Optional[ProgramSettings]:
182181
logger.error("Error: Unknown background subtraction type: %s", detector_type)
183182
return None
184183
if not bg_subtractor.value.is_available():
185-
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
186-
# This should only happen for MOG2_CUDA (currently).
187-
assert bg_subtractor == DetectorType.MOG2_CUDA
188-
logger.error(
189-
"Method %s is not available. To enable it, use a CUDA-enabled build of DVR-Scan.",
190-
bg_subtractor.name,
191-
)
184+
if bg_subtractor == DetectorType.MOG2_CUDA:
185+
logger.error("MOG2_CUDA requires a CUDA-enabled build of the `opencv-python` package!")
192186
else:
193187
logger.error(
194-
"Method %s is not available. To enable CNT, you can try `pip install "
195-
"opencv-contrib-python`. MOG2_CUDA requires building OpenCV with Python and CUDA "
196-
"support enabled.",
197-
bg_subtractor.name,
188+
f"Method {bg_subtractor.name} is not available, you may need to run: "
189+
"`pip install opencv-contrib-python`"
198190
)
199191
return None
200192

dvr_scan/platform.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@
3333
except ImportError:
3434
tkinter = None
3535

36+
try:
37+
import cv2
38+
import cv2.cuda
39+
40+
HAS_MOG2_CUDA = bool(hasattr(cv2.cuda, "createBackgroundSubtractorMOG2"))
41+
except: # noqa: E722
42+
# We make sure importing OpenCV succeeds elsewhere so it's okay to suppress any exceptions here.
43+
HAS_MOG2_CUDA = False
44+
3645

3746
HAS_TKINTER = tkinter is not None
3847

48+
IS_FROZEN = bool(not (getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")))
49+
3950

4051
def get_min_screen_bounds():
4152
"""Attempts to get the minimum screen resolution of all monitors using the `screeninfo` package.
@@ -181,13 +192,18 @@ def get_system_version_info(separator_width: int = 40) -> str:
181192
out_lines.append(output_template.format(module_name, not_found_str))
182193

183194
# External Tools
184-
out_lines += ["", "Tools", line_separator]
195+
out_lines += ["", "Features", line_separator]
185196

186-
tool_version_info = (("ffmpeg", get_ffmpeg_version()),)
197+
ffmpeg_version = get_ffmpeg_version()
198+
feature_version_info = [("ffmpeg", ffmpeg_version)] if ffmpeg_version else []
199+
feature_version_info += [("tkinter", "Installed")] if HAS_TKINTER else []
200+
feature_version_info += [("cv2.cuda", "Installed")] if HAS_MOG2_CUDA else []
187201

188-
for tool_name, tool_version in tool_version_info:
202+
for feature_name, feature_version in feature_version_info:
189203
out_lines.append(
190-
output_template.format(tool_name, tool_version if tool_version else not_found_str)
204+
output_template.format(
205+
feature_name, feature_version if feature_version else not_found_str
206+
)
191207
)
192208

193209
return "\n".join(out_lines)

0 commit comments

Comments
 (0)