Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,17 @@ from io import BytesIO
|
|
| 6 |
import base64
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import cv2
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
|
@@ -27,8 +38,7 @@ from more_itertools import chunked
|
|
| 27 |
from sklearn.cluster import KMeans
|
| 28 |
import umap
|
| 29 |
|
| 30 |
-
from
|
| 31 |
-
from inference_sdk.http.errors import HTTPCallErrorError
|
| 32 |
|
| 33 |
# ==============================================
|
| 34 |
# ENVIRONMENT VARIABLES
|
|
@@ -59,10 +69,12 @@ print(f"π Pitch config units - Length: {CONFIG.length}, Width: {CONFIG.width}
|
|
| 59 |
print(f"π Scale factors - X: {SCALE_X:.6f} m/unit, Y: {SCALE_Y:.6f} m/unit")
|
| 60 |
|
| 61 |
# ==============================================
|
| 62 |
-
#
|
| 63 |
# ==============================================
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
PLAYER_DETECTION_MODEL_ID = "football-players-detection-3zvbc/11"
|
| 68 |
FIELD_DETECTION_MODEL_ID = "football-field-detection-f07vi/14"
|
|
@@ -71,28 +83,8 @@ FIELD_DETECTION_MODEL_ID = "football-field-detection-f07vi/14"
|
|
| 71 |
BALL_ID, GOALKEEPER_ID, PLAYER_ID, REFEREE_ID = 0, 1, 2, 3
|
| 72 |
|
| 73 |
|
| 74 |
-
def initialize_models():
|
| 75 |
-
"""Initialize detection models with local inference (more reliable than HTTP API)"""
|
| 76 |
-
global PLAYER_DETECTION_MODEL, FIELD_DETECTION_MODEL
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
print("π¦ Loading detection models locally...")
|
| 80 |
-
PLAYER_DETECTION_MODEL = get_model(
|
| 81 |
-
model_id=PLAYER_DETECTION_MODEL_ID,
|
| 82 |
-
api_key=ROBOFLOW_API_KEY
|
| 83 |
-
)
|
| 84 |
-
FIELD_DETECTION_MODEL = get_model(
|
| 85 |
-
model_id=FIELD_DETECTION_MODEL_ID,
|
| 86 |
-
api_key=ROBOFLOW_API_KEY
|
| 87 |
-
)
|
| 88 |
-
print("β
Models loaded successfully")
|
| 89 |
-
except Exception as e:
|
| 90 |
-
print(f"β Failed to load models: {e}")
|
| 91 |
-
raise
|
| 92 |
-
|
| 93 |
-
|
| 94 |
def infer_with_confidence(
|
| 95 |
-
|
| 96 |
frame: np.ndarray,
|
| 97 |
confidence_threshold: float = 0.3,
|
| 98 |
max_retries: int = 3
|
|
@@ -101,7 +93,7 @@ def infer_with_confidence(
|
|
| 101 |
Run inference with retry logic for transient errors.
|
| 102 |
|
| 103 |
Args:
|
| 104 |
-
|
| 105 |
frame: Input frame
|
| 106 |
confidence_threshold: Confidence threshold for detections
|
| 107 |
max_retries: Maximum number of retry attempts
|
|
@@ -111,7 +103,7 @@ def infer_with_confidence(
|
|
| 111 |
"""
|
| 112 |
for attempt in range(max_retries):
|
| 113 |
try:
|
| 114 |
-
result =
|
| 115 |
detections = sv.Detections.from_inference(result)
|
| 116 |
if len(detections) > 0:
|
| 117 |
detections = detections[detections.confidence > confidence_threshold]
|
|
@@ -126,10 +118,6 @@ def infer_with_confidence(
|
|
| 126 |
# Return empty detections to continue processing
|
| 127 |
return None, sv.Detections.empty()
|
| 128 |
|
| 129 |
-
|
| 130 |
-
# Initialize models at startup
|
| 131 |
-
initialize_models()
|
| 132 |
-
|
| 133 |
# ==============================================
|
| 134 |
# SIGLIP MODEL (Embeddings)
|
| 135 |
# ==============================================
|
|
@@ -725,7 +713,7 @@ def analyze_football_video(video_path: str, progress=gr.Progress()
|
|
| 725 |
if not ret:
|
| 726 |
break
|
| 727 |
if frame_idx % STRIDE == 0:
|
| 728 |
-
_, detections = infer_with_confidence(
|
| 729 |
detections = detections.with_nms(threshold=0.5, class_agnostic=True)
|
| 730 |
players_detections = detections[detections.class_id == PLAYER_ID]
|
| 731 |
if len(players_detections.xyxy) > 0:
|
|
@@ -808,7 +796,7 @@ def analyze_football_video(video_path: str, progress=gr.Progress()
|
|
| 808 |
desc=f"π¬ Processing frame {frame_idx}/{total_frames}")
|
| 809 |
|
| 810 |
# --- detections ---
|
| 811 |
-
_, detections = infer_with_confidence(
|
| 812 |
if len(detections.xyxy) == 0:
|
| 813 |
out.write(frame)
|
| 814 |
ball_path_raw.append(np.empty((0, 2)))
|
|
@@ -853,7 +841,7 @@ def analyze_football_video(video_path: str, progress=gr.Progress()
|
|
| 853 |
|
| 854 |
# --- field homography ---
|
| 855 |
try:
|
| 856 |
-
result_field, _ = infer_with_confidence(
|
| 857 |
key_points = sv.KeyPoints.from_inference(result_field)
|
| 858 |
|
| 859 |
filter_mask = key_points.confidence[0] > 0.5
|
|
|
|
| 6 |
import base64
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
# Suppress optional model warnings BEFORE importing inference
|
| 10 |
+
os.environ["CORE_MODEL_SAM_ENABLED"] = "False"
|
| 11 |
+
os.environ["CORE_MODEL_SAM2_ENABLED"] = "False"
|
| 12 |
+
os.environ["CORE_MODEL_SAM3_ENABLED"] = "False"
|
| 13 |
+
os.environ["CORE_MODEL_GAZE_ENABLED"] = "False"
|
| 14 |
+
os.environ["CORE_MODEL_GROUNDINGDINO_ENABLED"] = "False"
|
| 15 |
+
os.environ["CORE_MODEL_YOLO_WORLD_ENABLED"] = "False"
|
| 16 |
+
|
| 17 |
+
# Set stable ONNX providers
|
| 18 |
+
os.environ["ONNXRUNTIME_EXECUTION_PROVIDERS"] = "CPUExecutionProvider"
|
| 19 |
+
|
| 20 |
import cv2
|
| 21 |
import numpy as np
|
| 22 |
from PIL import Image
|
|
|
|
| 38 |
from sklearn.cluster import KMeans
|
| 39 |
import umap
|
| 40 |
|
| 41 |
+
from inference_sdk import InferenceHTTPClient
|
|
|
|
| 42 |
|
| 43 |
# ==============================================
|
| 44 |
# ENVIRONMENT VARIABLES
|
|
|
|
| 69 |
print(f"π Scale factors - X: {SCALE_X:.6f} m/unit, Y: {SCALE_Y:.6f} m/unit")
|
| 70 |
|
| 71 |
# ==============================================
|
| 72 |
+
# ROBOFLOW INFERENCE CLIENT
|
| 73 |
# ==============================================
|
| 74 |
+
CLIENT = InferenceHTTPClient(
|
| 75 |
+
api_url="https://detect.roboflow.com",
|
| 76 |
+
api_key=ROBOFLOW_API_KEY
|
| 77 |
+
)
|
| 78 |
|
| 79 |
PLAYER_DETECTION_MODEL_ID = "football-players-detection-3zvbc/11"
|
| 80 |
FIELD_DETECTION_MODEL_ID = "football-field-detection-f07vi/14"
|
|
|
|
| 83 |
BALL_ID, GOALKEEPER_ID, PLAYER_ID, REFEREE_ID = 0, 1, 2, 3
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def infer_with_confidence(
|
| 87 |
+
model_id: str,
|
| 88 |
frame: np.ndarray,
|
| 89 |
confidence_threshold: float = 0.3,
|
| 90 |
max_retries: int = 3
|
|
|
|
| 93 |
Run inference with retry logic for transient errors.
|
| 94 |
|
| 95 |
Args:
|
| 96 |
+
model_id: The model ID to use
|
| 97 |
frame: Input frame
|
| 98 |
confidence_threshold: Confidence threshold for detections
|
| 99 |
max_retries: Maximum number of retry attempts
|
|
|
|
| 103 |
"""
|
| 104 |
for attempt in range(max_retries):
|
| 105 |
try:
|
| 106 |
+
result = CLIENT.infer(frame, model_id=model_id)
|
| 107 |
detections = sv.Detections.from_inference(result)
|
| 108 |
if len(detections) > 0:
|
| 109 |
detections = detections[detections.confidence > confidence_threshold]
|
|
|
|
| 118 |
# Return empty detections to continue processing
|
| 119 |
return None, sv.Detections.empty()
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# ==============================================
|
| 122 |
# SIGLIP MODEL (Embeddings)
|
| 123 |
# ==============================================
|
|
|
|
| 713 |
if not ret:
|
| 714 |
break
|
| 715 |
if frame_idx % STRIDE == 0:
|
| 716 |
+
_, detections = infer_with_confidence(PLAYER_DETECTION_MODEL_ID, frame, 0.3)
|
| 717 |
detections = detections.with_nms(threshold=0.5, class_agnostic=True)
|
| 718 |
players_detections = detections[detections.class_id == PLAYER_ID]
|
| 719 |
if len(players_detections.xyxy) > 0:
|
|
|
|
| 796 |
desc=f"π¬ Processing frame {frame_idx}/{total_frames}")
|
| 797 |
|
| 798 |
# --- detections ---
|
| 799 |
+
_, detections = infer_with_confidence(PLAYER_DETECTION_MODEL_ID, frame, 0.3)
|
| 800 |
if len(detections.xyxy) == 0:
|
| 801 |
out.write(frame)
|
| 802 |
ball_path_raw.append(np.empty((0, 2)))
|
|
|
|
| 841 |
|
| 842 |
# --- field homography ---
|
| 843 |
try:
|
| 844 |
+
result_field, _ = infer_with_confidence(FIELD_DETECTION_MODEL_ID, frame, 0.3)
|
| 845 |
key_points = sv.KeyPoints.from_inference(result_field)
|
| 846 |
|
| 847 |
filter_mask = key_points.confidence[0] > 0.5
|