Update pipeline_full.py
Browse files- pipeline_full.py +52 -4
pipeline_full.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
# pipeline_full.py
|
| 2 |
import os
|
|
|
|
| 3 |
import base64
|
| 4 |
from io import BytesIO
|
| 5 |
from typing import List, Dict, Any
|
|
@@ -30,7 +31,7 @@ from sports.annotators.soccer import (
|
|
| 30 |
)
|
| 31 |
|
| 32 |
# ------------------------------------------------------------------
|
| 33 |
-
# Globals –
|
| 34 |
# ------------------------------------------------------------------
|
| 35 |
|
| 36 |
PLAYER_DETECTION_MODEL = None
|
|
@@ -47,6 +48,31 @@ REFEREE_ID = 3
|
|
| 47 |
|
| 48 |
MODELS_READY = False
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def ensure_models_loaded():
|
| 52 |
"""
|
|
@@ -80,7 +106,7 @@ def ensure_models_loaded():
|
|
| 80 |
|
| 81 |
# SigLIP embeddings
|
| 82 |
SIGLIP_MODEL_PATH = "google/siglip-base-patch16-224"
|
| 83 |
-
device =
|
| 84 |
EMBEDDINGS_MODEL = SiglipVisionModel.from_pretrained(SIGLIP_MODEL_PATH).to(device)
|
| 85 |
EMBEDDINGS_PROCESSOR = AutoProcessor.from_pretrained(SIGLIP_MODEL_PATH)
|
| 86 |
|
|
@@ -370,7 +396,7 @@ def resolve_goalkeepers_team_id(players: sv.Detections, goalkeepers: sv.Detectio
|
|
| 370 |
return np.array(goalkeepers_team_id)
|
| 371 |
|
| 372 |
|
| 373 |
-
# -------------------- 5. Voronoi blend helper
|
| 374 |
|
| 375 |
|
| 376 |
def draw_pitch_voronoi_diagram_2(
|
|
@@ -747,21 +773,43 @@ def run_full_pipeline(video_path: str, job_dir: str) -> Dict[str, Any]:
|
|
| 747 |
Run the full notebook-equivalent pipeline on a video and save all artifacts
|
| 748 |
into job_dir. Returns paths + stats for the FastAPI app.
|
| 749 |
"""
|
|
|
|
|
|
|
| 750 |
ensure_models_loaded()
|
|
|
|
| 751 |
os.makedirs(job_dir, exist_ok=True)
|
| 752 |
|
|
|
|
| 753 |
siglip_out = step_siglip_clustering(video_path, os.path.join(job_dir, "siglip"))
|
|
|
|
|
|
|
| 754 |
train_team_classifier_on_video(video_path)
|
| 755 |
|
|
|
|
| 756 |
basic_paths = step_basic_frames(video_path, os.path.join(job_dir, "frames"))
|
|
|
|
|
|
|
| 757 |
adv_paths = step_single_frame_advanced(video_path, os.path.join(job_dir, "advanced"))
|
|
|
|
|
|
|
| 758 |
ball_paths = step_ball_path(video_path, os.path.join(job_dir, "ball_path"))
|
|
|
|
|
|
|
| 759 |
stats = process_video_stats(video_path)
|
| 760 |
|
| 761 |
-
|
| 762 |
"basic": basic_paths,
|
| 763 |
"advanced": adv_paths,
|
| 764 |
"ball": ball_paths,
|
| 765 |
"stats": stats,
|
| 766 |
"siglip_html": siglip_out["plot_html"],
|
| 767 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# pipeline_full.py
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
| 6 |
from typing import List, Dict, Any
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
# ------------------------------------------------------------------
|
| 34 |
+
# Globals – initialized lazily so build/startup doesn't crash
|
| 35 |
# ------------------------------------------------------------------
|
| 36 |
|
| 37 |
PLAYER_DETECTION_MODEL = None
|
|
|
|
| 48 |
|
| 49 |
MODELS_READY = False
|
| 50 |
|
| 51 |
+
# progress tracking
|
| 52 |
+
CURRENT_JOB_DIR: str | None = None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def set_job_dir(job_dir: str):
|
| 56 |
+
global CURRENT_JOB_DIR
|
| 57 |
+
CURRENT_JOB_DIR = job_dir
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def update_progress(stage: str, progress: float, message: str = ""):
|
| 61 |
+
"""
|
| 62 |
+
Write a small JSON status file in the current job dir so the UI can poll.
|
| 63 |
+
"""
|
| 64 |
+
if not CURRENT_JOB_DIR:
|
| 65 |
+
return
|
| 66 |
+
status = {
|
| 67 |
+
"stage": stage,
|
| 68 |
+
"progress": float(progress),
|
| 69 |
+
"message": message,
|
| 70 |
+
}
|
| 71 |
+
os.makedirs(CURRENT_JOB_DIR, exist_ok=True)
|
| 72 |
+
status_path = os.path.join(CURRENT_JOB_DIR, "status.json")
|
| 73 |
+
with open(status_path, "w", encoding="utf-8") as f:
|
| 74 |
+
json.dump(status, f)
|
| 75 |
+
|
| 76 |
|
| 77 |
def ensure_models_loaded():
|
| 78 |
"""
|
|
|
|
| 106 |
|
| 107 |
# SigLIP embeddings
|
| 108 |
SIGLIP_MODEL_PATH = "google/siglip-base-patch16-224"
|
| 109 |
+
device = get_device()
|
| 110 |
EMBEDDINGS_MODEL = SiglipVisionModel.from_pretrained(SIGLIP_MODEL_PATH).to(device)
|
| 111 |
EMBEDDINGS_PROCESSOR = AutoProcessor.from_pretrained(SIGLIP_MODEL_PATH)
|
| 112 |
|
|
|
|
| 396 |
return np.array(goalkeepers_team_id)
|
| 397 |
|
| 398 |
|
| 399 |
+
# -------------------- 5. Voronoi blend helper --------------------
|
| 400 |
|
| 401 |
|
| 402 |
def draw_pitch_voronoi_diagram_2(
|
|
|
|
| 773 |
Run the full notebook-equivalent pipeline on a video and save all artifacts
|
| 774 |
into job_dir. Returns paths + stats for the FastAPI app.
|
| 775 |
"""
|
| 776 |
+
set_job_dir(job_dir)
|
| 777 |
+
update_progress("initializing", 0.0, "Loading models...")
|
| 778 |
ensure_models_loaded()
|
| 779 |
+
|
| 780 |
os.makedirs(job_dir, exist_ok=True)
|
| 781 |
|
| 782 |
+
update_progress("siglip", 0.15, "Running SigLIP clustering...")
|
| 783 |
siglip_out = step_siglip_clustering(video_path, os.path.join(job_dir, "siglip"))
|
| 784 |
+
|
| 785 |
+
update_progress("team_classifier", 0.30, "Training TeamClassifier...")
|
| 786 |
train_team_classifier_on_video(video_path)
|
| 787 |
|
| 788 |
+
update_progress("basic_frames", 0.45, "Generating basic annotated frames...")
|
| 789 |
basic_paths = step_basic_frames(video_path, os.path.join(job_dir, "frames"))
|
| 790 |
+
|
| 791 |
+
update_progress("advanced_views", 0.60, "Generating advanced radar / Voronoi views...")
|
| 792 |
adv_paths = step_single_frame_advanced(video_path, os.path.join(job_dir, "advanced"))
|
| 793 |
+
|
| 794 |
+
update_progress("ball_path", 0.80, "Computing ball path and heatmap...")
|
| 795 |
ball_paths = step_ball_path(video_path, os.path.join(job_dir, "ball_path"))
|
| 796 |
+
|
| 797 |
+
update_progress("stats", 0.90, "Calculating stats...")
|
| 798 |
stats = process_video_stats(video_path)
|
| 799 |
|
| 800 |
+
result = {
|
| 801 |
"basic": basic_paths,
|
| 802 |
"advanced": adv_paths,
|
| 803 |
"ball": ball_paths,
|
| 804 |
"stats": stats,
|
| 805 |
"siglip_html": siglip_out["plot_html"],
|
| 806 |
}
|
| 807 |
+
|
| 808 |
+
# Save a copy for the UI result page
|
| 809 |
+
result_path = os.path.join(job_dir, "result.json")
|
| 810 |
+
with open(result_path, "w", encoding="utf-8") as f:
|
| 811 |
+
json.dump(result, f)
|
| 812 |
+
|
| 813 |
+
update_progress("done", 1.0, "Completed")
|
| 814 |
+
|
| 815 |
+
return result
|