From a97e8d54bb718c66bf5177e1684b9c276adf78fd Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Sun, 15 Mar 2026 21:46:34 -0500 Subject: [PATCH] dynamic conda finding --- readme.md | 4 +- tracking_re_id/launch/_conda_utils.py | 66 +++++++++++++++++++ tracking_re_id/launch/full_pipeline.launch.py | 9 ++- tracking_re_id/launch/ground_plane.launch.py | 9 ++- tracking_re_id/launch/reid_headless.launch.py | 7 +- tracking_re_id/launch/reid_pipeline.launch.py | 7 +- .../launch/single_person_demo.launch.py | 9 ++- .../launch/single_person_headless.launch.py | 9 ++- 8 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 tracking_re_id/launch/_conda_utils.py diff --git a/readme.md b/readme.md index 71d8e93..13a35c7 100644 --- a/readme.md +++ b/readme.md @@ -24,4 +24,6 @@ mim install "mmdet==3.2.0" mim install "mmpose==1.2.0" pip install "numpy<2" pip install "opencv-python==4.11.0.86" -``` \ No newline at end of file +``` + +**This installation is referenced in each launchfile by finding the location of your conda base environment, then finding an environment named "mmpose" - if you use a different name, you must change each launch file.** \ No newline at end of file diff --git a/tracking_re_id/launch/_conda_utils.py b/tracking_re_id/launch/_conda_utils.py new file mode 100644 index 0000000..12377f5 --- /dev/null +++ b/tracking_re_id/launch/_conda_utils.py @@ -0,0 +1,66 @@ +"""Utilities for locating conda-environment Python interpreters at launch time.""" + +import os + + +def find_conda_python(env_name: str) -> str: + """Return the path to ``python3`` inside a named conda environment. + + Resolution order + ---------------- + 1. ``_PYTHON`` environment variable (explicit override). + e.g. for *mmpose*: ``MMPOSE_PYTHON=/path/to/python3`` + 2. ``CONDA_EXE`` environment variable — set by ``conda init`` and points to + the conda binary inside the base installation. The base directory is two + levels up (``/bin/conda → ``). + 3. A scan of common conda base-directory locations. + + Parameters + ---------- + env_name: + Name of the conda environment (e.g. ``"mmpose"``). + + Returns + ------- + str + Absolute path to the Python 3 interpreter. + + Raises + ------ + FileNotFoundError + If no interpreter is found through any of the above methods. + """ + # 1. Explicit override via environment variable + override_var = f"{env_name.upper()}_PYTHON" + if p := os.environ.get(override_var): + return p + + rel = os.path.join("envs", env_name, "bin", "python3") + + # 2. Derive conda base from CONDA_EXE (most reliable when conda is initialised) + if conda_exe := os.environ.get("CONDA_EXE"): + base = os.path.dirname(os.path.dirname(conda_exe)) + candidate = os.path.join(base, rel) + if os.path.isfile(candidate): + return candidate + + # 3. Scan common install locations + common_bases = [ + "~/miniconda3", + "~/anaconda3", + "~/mambaforge", + "~/miniforge3", + "~/opt/miniconda3", + "/opt/conda", + "/opt/miniconda3", + "/opt/anaconda3", + ] + for base in common_bases: + candidate = os.path.join(os.path.expanduser(base), rel) + if os.path.isfile(candidate): + return candidate + + raise FileNotFoundError( + f"Cannot locate Python for conda env '{env_name}'. " + f"Set the {override_var} environment variable to the full path of the interpreter." + ) diff --git a/tracking_re_id/launch/full_pipeline.launch.py b/tracking_re_id/launch/full_pipeline.launch.py index fdb7bfd..dcf2d42 100644 --- a/tracking_re_id/launch/full_pipeline.launch.py +++ b/tracking_re_id/launch/full_pipeline.launch.py @@ -15,14 +15,17 @@ To view the annotated image: """ import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser( - '~/miniconda3/envs/mmpose/bin/python3' - ) + python_exe = find_conda_python('mmpose') return LaunchDescription([ diff --git a/tracking_re_id/launch/ground_plane.launch.py b/tracking_re_id/launch/ground_plane.launch.py index 0d6e14a..3dca741 100644 --- a/tracking_re_id/launch/ground_plane.launch.py +++ b/tracking_re_id/launch/ground_plane.launch.py @@ -6,14 +6,17 @@ Python environment. """ import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser( - '~/miniconda3/envs/mmpose/bin/python3' - ) + python_exe = find_conda_python('mmpose') return LaunchDescription([ diff --git a/tracking_re_id/launch/reid_headless.launch.py b/tracking_re_id/launch/reid_headless.launch.py index b7a0261..91800fd 100644 --- a/tracking_re_id/launch/reid_headless.launch.py +++ b/tracking_re_id/launch/reid_headless.launch.py @@ -6,12 +6,17 @@ To view output: """ import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser('~/miniconda3/envs/mmpose/bin/python3') + python_exe = find_conda_python('mmpose') keyreID_path = os.path.expanduser('~/KeyRe-ID') return LaunchDescription([ diff --git a/tracking_re_id/launch/reid_pipeline.launch.py b/tracking_re_id/launch/reid_pipeline.launch.py index 9ebba19..d70d682 100644 --- a/tracking_re_id/launch/reid_pipeline.launch.py +++ b/tracking_re_id/launch/reid_pipeline.launch.py @@ -23,12 +23,17 @@ Viewing the output """ import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser('~/miniconda3/envs/mmpose/bin/python3') + python_exe = find_conda_python('mmpose') keyreID_path = os.path.expanduser('~/KeyRe-ID') return LaunchDescription([ diff --git a/tracking_re_id/launch/single_person_demo.launch.py b/tracking_re_id/launch/single_person_demo.launch.py index 9b7d0d9..bf49ed7 100644 --- a/tracking_re_id/launch/single_person_demo.launch.py +++ b/tracking_re_id/launch/single_person_demo.launch.py @@ -1,14 +1,17 @@ """Launch single_person_loc_node using the mmpose conda environment's Python.""" import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser( - '~/miniconda3/envs/mmpose/bin/python3' - ) + python_exe = find_conda_python('mmpose') node_module = 'tracking_re_id.single_person_loc_node' diff --git a/tracking_re_id/launch/single_person_headless.launch.py b/tracking_re_id/launch/single_person_headless.launch.py index 3bd4f4b..a3578f9 100644 --- a/tracking_re_id/launch/single_person_headless.launch.py +++ b/tracking_re_id/launch/single_person_headless.launch.py @@ -6,14 +6,17 @@ pipeline where visualisation is handled elsewhere. """ import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _conda_utils import find_conda_python # noqa: E402 + from launch import LaunchDescription from launch.actions import ExecuteProcess def generate_launch_description(): - python_exe = os.path.expanduser( - '~/miniconda3/envs/mmpose/bin/python3' - ) + python_exe = find_conda_python('mmpose') node_module = 'tracking_re_id.single_person_loc_node'