Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
from pathlib import Path
|
| 9 |
import tempfile
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Class labels for the model
|
| 12 |
CLASS_NAMES = {
|
|
@@ -30,35 +31,46 @@ class SoccerDetector:
|
|
| 30 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
print(f"Using device: {self.device}")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def process_image(self, image, confidence_threshold=0.5):
|
| 41 |
"""Process a single image and return detections"""
|
| 42 |
# Convert to PIL if needed
|
| 43 |
if isinstance(image, np.ndarray):
|
|
|
|
| 44 |
image = Image.fromarray(image)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 48 |
|
| 49 |
# Run inference
|
| 50 |
-
|
| 51 |
-
outputs = self.model(**inputs)
|
| 52 |
-
|
| 53 |
-
# Post-process
|
| 54 |
-
target_sizes = torch.tensor([image.size[::-1]]).to(self.device)
|
| 55 |
-
results = self.processor.post_process_object_detection(
|
| 56 |
-
outputs,
|
| 57 |
-
target_sizes=target_sizes,
|
| 58 |
-
threshold=confidence_threshold
|
| 59 |
-
)[0]
|
| 60 |
|
| 61 |
-
return results, image
|
| 62 |
|
| 63 |
def draw_detections(self, image, results):
|
| 64 |
"""Draw bounding boxes on image"""
|
|
@@ -69,19 +81,28 @@ class SoccerDetector:
|
|
| 69 |
except:
|
| 70 |
font = ImageFont.load_default()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
color = CLASS_COLORS.get(label_id, (255, 255, 255))
|
| 79 |
|
| 80 |
# Draw box
|
| 81 |
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
|
| 82 |
|
| 83 |
# Draw label
|
| 84 |
-
text = f"{class_name}: {
|
| 85 |
|
| 86 |
# Draw text background
|
| 87 |
bbox = draw.textbbox((x1, y1), text, font=font)
|
|
@@ -90,21 +111,29 @@ class SoccerDetector:
|
|
| 90 |
|
| 91 |
return image
|
| 92 |
|
| 93 |
-
def create_detections_dataframe(self, results
|
| 94 |
"""Create a pandas DataFrame from detection results"""
|
| 95 |
data = []
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
class_name = CLASS_NAMES.get(label_id, f"class_{label_id}")
|
| 103 |
|
| 104 |
data.append({
|
| 105 |
'class_name': class_name,
|
| 106 |
-
'class_id':
|
| 107 |
-
'confidence':
|
| 108 |
'x1': float(x1),
|
| 109 |
'y1': float(y1),
|
| 110 |
'x2': float(x2),
|
|
@@ -153,7 +182,7 @@ class SoccerDetector:
|
|
| 153 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 154 |
|
| 155 |
# Run detection
|
| 156 |
-
results
|
| 157 |
|
| 158 |
# Draw detections
|
| 159 |
pil_image = Image.fromarray(rgb_frame)
|
|
@@ -161,28 +190,31 @@ class SoccerDetector:
|
|
| 161 |
annotated_frame = cv2.cvtColor(np.array(annotated_image), cv2.COLOR_RGB2BGR)
|
| 162 |
|
| 163 |
# Save detections to list
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
out.write(annotated_frame)
|
| 188 |
else:
|
|
@@ -214,7 +246,7 @@ def process_image_interface(image, confidence_threshold):
|
|
| 214 |
|
| 215 |
results, original_image = detector.process_image(image, confidence_threshold)
|
| 216 |
annotated_image = detector.draw_detections(original_image.copy(), results)
|
| 217 |
-
df = detector.create_detections_dataframe(results
|
| 218 |
|
| 219 |
return annotated_image, df
|
| 220 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
from pathlib import Path
|
| 8 |
import tempfile
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
# Class labels for the model
|
| 13 |
CLASS_NAMES = {
|
|
|
|
| 31 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
print(f"Using device: {self.device}")
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
+
# Try to download the model file from Hugging Face
|
| 36 |
+
print("Downloading model from Hugging Face...")
|
| 37 |
+
model_path = hf_hub_download(
|
| 38 |
+
repo_id="julianzu9612/RFDETR-Soccernet",
|
| 39 |
+
filename="best.pt" # or "model.pt" - we'll need to check
|
| 40 |
+
)
|
| 41 |
+
print(f"Model downloaded to: {model_path}")
|
| 42 |
+
|
| 43 |
+
# Load with Ultralytics YOLO (RF-DETR is YOLO-based)
|
| 44 |
+
from ultralytics import RTDETR
|
| 45 |
+
self.model = RTDETR(model_path)
|
| 46 |
+
print("Model loaded successfully!")
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error loading model: {e}")
|
| 50 |
+
print("\nTrying alternative loading method...")
|
| 51 |
+
|
| 52 |
+
# Alternative: Try loading directly from hub
|
| 53 |
+
from ultralytics import RTDETR
|
| 54 |
+
try:
|
| 55 |
+
self.model = RTDETR("julianzu9612/RFDETR-Soccernet")
|
| 56 |
+
print("Model loaded via direct hub access!")
|
| 57 |
+
except Exception as e2:
|
| 58 |
+
print(f"Alternative method failed: {e2}")
|
| 59 |
+
raise Exception("Could not load model. Please check the model repository structure.")
|
| 60 |
|
| 61 |
def process_image(self, image, confidence_threshold=0.5):
|
| 62 |
"""Process a single image and return detections"""
|
| 63 |
# Convert to PIL if needed
|
| 64 |
if isinstance(image, np.ndarray):
|
| 65 |
+
image_array = image
|
| 66 |
image = Image.fromarray(image)
|
| 67 |
+
else:
|
| 68 |
+
image_array = np.array(image)
|
|
|
|
| 69 |
|
| 70 |
# Run inference
|
| 71 |
+
results = self.model(image_array, conf=confidence_threshold, verbose=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
return results[0], image
|
| 74 |
|
| 75 |
def draw_detections(self, image, results):
|
| 76 |
"""Draw bounding boxes on image"""
|
|
|
|
| 81 |
except:
|
| 82 |
font = ImageFont.load_default()
|
| 83 |
|
| 84 |
+
# Get boxes, scores, and classes
|
| 85 |
+
boxes = results.boxes
|
| 86 |
+
|
| 87 |
+
if boxes is None or len(boxes) == 0:
|
| 88 |
+
return image
|
| 89 |
+
|
| 90 |
+
for box in boxes:
|
| 91 |
+
# Get coordinates
|
| 92 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 93 |
+
|
| 94 |
+
# Get class and confidence
|
| 95 |
+
cls = int(box.cls[0].cpu().numpy())
|
| 96 |
+
conf = float(box.conf[0].cpu().numpy())
|
| 97 |
|
| 98 |
+
class_name = CLASS_NAMES.get(cls, f"class_{cls}")
|
| 99 |
+
color = CLASS_COLORS.get(cls, (255, 255, 255))
|
|
|
|
| 100 |
|
| 101 |
# Draw box
|
| 102 |
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
|
| 103 |
|
| 104 |
# Draw label
|
| 105 |
+
text = f"{class_name}: {conf:.2f}"
|
| 106 |
|
| 107 |
# Draw text background
|
| 108 |
bbox = draw.textbbox((x1, y1), text, font=font)
|
|
|
|
| 111 |
|
| 112 |
return image
|
| 113 |
|
| 114 |
+
def create_detections_dataframe(self, results):
|
| 115 |
"""Create a pandas DataFrame from detection results"""
|
| 116 |
data = []
|
| 117 |
|
| 118 |
+
boxes = results.boxes
|
| 119 |
+
|
| 120 |
+
if boxes is None or len(boxes) == 0:
|
| 121 |
+
return pd.DataFrame()
|
| 122 |
+
|
| 123 |
+
for box in boxes:
|
| 124 |
+
# Get coordinates
|
| 125 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 126 |
+
|
| 127 |
+
# Get class and confidence
|
| 128 |
+
cls = int(box.cls[0].cpu().numpy())
|
| 129 |
+
conf = float(box.conf[0].cpu().numpy())
|
| 130 |
|
| 131 |
+
class_name = CLASS_NAMES.get(cls, f"class_{cls}")
|
|
|
|
| 132 |
|
| 133 |
data.append({
|
| 134 |
'class_name': class_name,
|
| 135 |
+
'class_id': cls,
|
| 136 |
+
'confidence': conf,
|
| 137 |
'x1': float(x1),
|
| 138 |
'y1': float(y1),
|
| 139 |
'x2': float(x2),
|
|
|
|
| 182 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 183 |
|
| 184 |
# Run detection
|
| 185 |
+
results = self.model(rgb_frame, conf=confidence_threshold, verbose=False)[0]
|
| 186 |
|
| 187 |
# Draw detections
|
| 188 |
pil_image = Image.fromarray(rgb_frame)
|
|
|
|
| 190 |
annotated_frame = cv2.cvtColor(np.array(annotated_image), cv2.COLOR_RGB2BGR)
|
| 191 |
|
| 192 |
# Save detections to list
|
| 193 |
+
boxes = results.boxes
|
| 194 |
+
if boxes is not None and len(boxes) > 0:
|
| 195 |
+
for box in boxes:
|
| 196 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 197 |
+
cls = int(box.cls[0].cpu().numpy())
|
| 198 |
+
conf = float(box.conf[0].cpu().numpy())
|
| 199 |
+
|
| 200 |
+
class_name = CLASS_NAMES.get(cls, f"class_{cls}")
|
| 201 |
+
|
| 202 |
+
all_detections.append({
|
| 203 |
+
'frame': frame_num,
|
| 204 |
+
'timestamp': frame_num / fps,
|
| 205 |
+
'class_name': class_name,
|
| 206 |
+
'class_id': cls,
|
| 207 |
+
'confidence': conf,
|
| 208 |
+
'x1': float(x1),
|
| 209 |
+
'y1': float(y1),
|
| 210 |
+
'x2': float(x2),
|
| 211 |
+
'y2': float(y2),
|
| 212 |
+
'width': float(x2 - x1),
|
| 213 |
+
'height': float(y2 - y1),
|
| 214 |
+
'center_x': float((x1 + x2) / 2),
|
| 215 |
+
'center_y': float((y1 + y2) / 2),
|
| 216 |
+
'area': float((x2 - x1) * (y2 - y1))
|
| 217 |
+
})
|
| 218 |
|
| 219 |
out.write(annotated_frame)
|
| 220 |
else:
|
|
|
|
| 246 |
|
| 247 |
results, original_image = detector.process_image(image, confidence_threshold)
|
| 248 |
annotated_image = detector.draw_detections(original_image.copy(), results)
|
| 249 |
+
df = detector.create_detections_dataframe(results)
|
| 250 |
|
| 251 |
return annotated_image, df
|
| 252 |
|