Dynamically install and load detector dependencies (#24156)

* Dynamically install and load detector dependencies

* Cleanup

* Cleanup
This commit is contained in:
Nicolas Mowen
2026-09-12 07:30:04 -06:00
parent 53b04f44ef
commit d183f03fee
18 changed files with 1386 additions and 115 deletions
+35 -21
View File
@@ -1,7 +1,9 @@
"""RKNN model conversion utility for Frigate."""
import importlib
import logging
import os
import site
import subprocess
import sys
import time
@@ -101,30 +103,42 @@ def ensure_torch_dependencies() -> bool:
except ImportError:
logger.info("PyTorch not found, attempting to install...")
try:
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--break-system-packages",
"setuptools<81",
"torch",
"torchvision",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
result = subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"--break-system-packages",
"setuptools<81",
"torch",
"torchvision",
],
capture_output=True,
text=True,
)
import torch # type: ignore # noqa: F401
logger.info("PyTorch installed successfully")
return True
except (subprocess.CalledProcessError, ImportError) as e:
logger.error(f"Failed to install PyTorch: {e}")
if result.returncode != 0:
logger.error("Failed to install PyTorch:\n%s", result.stderr[-4000:])
return False
# as an unprivileged user pip falls back to the user site, which is
# only on sys.path at startup if it already existed
user_site = site.getusersitepackages()
if os.path.isdir(user_site) and user_site not in sys.path:
site.addsitedir(user_site)
importlib.invalidate_caches()
try:
import torch # type: ignore # noqa: F401
except ImportError as e:
logger.error(f"Failed to import PyTorch after installing it: {e}")
return False
logger.info("PyTorch installed successfully")
return True
def ensure_rknn_toolkit() -> bool:
"""Ensure RKNN toolkit is available."""