openable webcam pipeline

This commit is contained in:
Siwat Sirichai 2025-02-23 20:58:48 +07:00
parent ee0071284e
commit e1da9a6dca
4 changed files with 101 additions and 50 deletions

View file

@ -5,11 +5,13 @@ import torch
import cv2
import requests
import zipfile
import shutil
from ultralytics import YOLO
from urllib.parse import urlparse
def load_pipeline_node(node_config: dict, models_dir: str) -> dict:
def load_pipeline_node(node_config: dict, mpta_dir: str) -> dict:
# Recursively load a model node from configuration.
model_path = os.path.join(models_dir, node_config["modelFile"])
model_path = os.path.join(mpta_dir, node_config["modelFile"])
if not os.path.exists(model_path):
logging.error(f"Model file {model_path} not found.")
raise FileNotFoundError(f"Model file {model_path} not found.")
@ -27,25 +29,29 @@ def load_pipeline_node(node_config: dict, models_dir: str) -> dict:
"branches": []
}
for child in node_config.get("branches", []):
node["branches"].append(load_pipeline_node(child, models_dir))
node["branches"].append(load_pipeline_node(child, mpta_dir))
return node
def load_pipeline_from_zip(zip_url: str, target_dir: str) -> dict:
# Download, extract, and load a pipeline configuration from a zip (.mpta) file.
def load_pipeline_from_zip(zip_source: str, target_dir: str) -> dict:
os.makedirs(target_dir, exist_ok=True)
zip_path = os.path.join(target_dir, "pipeline.mpta")
try:
response = requests.get(zip_url, stream=True)
if response.status_code == 200:
with open(zip_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
logging.info(f"Downloaded .mpta file from {zip_url} to {zip_path}")
# Parse the source; only local files are supported here.
parsed = urlparse(zip_source)
if parsed.scheme in ("", "file"):
local_path = parsed.path if parsed.scheme == "file" else zip_source
if os.path.exists(local_path):
try:
shutil.copy(local_path, zip_path)
logging.info(f"Copied local .mpta file from {local_path} to {zip_path}")
except Exception as e:
logging.error(f"Failed to copy local .mpta file from {local_path}: {e}")
return None
else:
logging.error(f"Failed to download .mpta file (status {response.status_code})")
logging.error(f"Local file {local_path} does not exist.")
return None
except Exception as e:
logging.error(f"Exception downloading .mpta file from {zip_url}: {e}")
else:
logging.error("HTTP download functionality has been moved. Use a local file path here.")
return None
try:
@ -58,8 +64,10 @@ def load_pipeline_from_zip(zip_url: str, target_dir: str) -> dict:
finally:
if os.path.exists(zip_path):
os.remove(zip_path)
pipeline_json_path = os.path.join(target_dir, "pipeline.json")
pipeline_name = os.path.basename(zip_source)
pipeline_name = os.path.splitext(pipeline_name)[0]
mpta_dir = os.path.join(target_dir, pipeline_name)
pipeline_json_path = os.path.join(mpta_dir, "pipeline.json")
if not os.path.exists(pipeline_json_path):
logging.error("pipeline.json not found in the .mpta file")
return None
@ -67,7 +75,7 @@ def load_pipeline_from_zip(zip_url: str, target_dir: str) -> dict:
try:
with open(pipeline_json_path, "r") as f:
pipeline_config = json.load(f)
return load_pipeline_node(pipeline_config["pipeline"], target_dir)
return load_pipeline_node(pipeline_config["pipeline"], mpta_dir)
except Exception as e:
logging.error(f"Error loading pipeline.json: {e}")
return None