openable webcam pipeline
This commit is contained in:
parent
ee0071284e
commit
e1da9a6dca
4 changed files with 101 additions and 50 deletions
36
app.py
36
app.py
|
@ -60,6 +60,24 @@ WORKER_TIMEOUT_MS = 10000
|
|||
streams_lock = threading.Lock()
|
||||
models_lock = threading.Lock()
|
||||
|
||||
# Add helper to download mpta ZIP file from a remote URL
|
||||
def download_mpta(url: str, dest_path: str) -> str:
|
||||
try:
|
||||
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
||||
response = requests.get(url, stream=True)
|
||||
if response.status_code == 200:
|
||||
with open(dest_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
logging.info(f"Downloaded mpta file from {url} to {dest_path}")
|
||||
return dest_path
|
||||
else:
|
||||
logging.error(f"Failed to download mpta file (status code {response.status_code})")
|
||||
return None
|
||||
except Exception as e:
|
||||
logging.error(f"Exception downloading mpta file from {url}: {e}")
|
||||
return None
|
||||
|
||||
####################################################
|
||||
# Detection and frame processing functions
|
||||
####################################################
|
||||
|
@ -212,7 +230,7 @@ async def detect(websocket: WebSocket):
|
|||
payload = data.get("payload", {})
|
||||
camera_id = payload.get("cameraIdentifier")
|
||||
rtsp_url = payload.get("rtspUrl")
|
||||
model_url = payload.get("modelUrl") # ZIP file URL
|
||||
model_url = payload.get("modelUrl") # may be remote or local
|
||||
modelId = payload.get("modelId")
|
||||
modelName = payload.get("modelName")
|
||||
|
||||
|
@ -221,12 +239,22 @@ async def detect(websocket: WebSocket):
|
|||
if camera_id not in models:
|
||||
models[camera_id] = {}
|
||||
if modelId not in models[camera_id]:
|
||||
logging.info(f"Downloading model from {model_url}")
|
||||
logging.info(f"Loading model from {model_url}")
|
||||
extraction_dir = os.path.join("models", camera_id, str(modelId))
|
||||
os.makedirs(extraction_dir, exist_ok=True)
|
||||
model_tree = load_pipeline_from_zip(model_url, extraction_dir)
|
||||
# If model_url is remote, download it first.
|
||||
parsed = urlparse(model_url)
|
||||
if parsed.scheme in ("http", "https"):
|
||||
local_mpta = os.path.join(extraction_dir, os.path.basename(parsed.path))
|
||||
local_path = download_mpta(model_url, local_mpta)
|
||||
if not local_path:
|
||||
logging.error("Failed to download the remote mpta file.")
|
||||
continue
|
||||
model_tree = load_pipeline_from_zip(local_path, extraction_dir)
|
||||
else:
|
||||
model_tree = load_pipeline_from_zip(model_url, extraction_dir)
|
||||
if model_tree is None:
|
||||
logging.error("Failed to load model from ZIP file.")
|
||||
logging.error("Failed to load model from mpta file.")
|
||||
continue
|
||||
models[camera_id][modelId] = model_tree
|
||||
logging.info(f"Loaded model {modelId} for camera {camera_id}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue