Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def apply_patches():
|
|
| 46 |
else:
|
| 47 |
print("⚠️ tracker_patch.py not found, skipping patches")
|
| 48 |
|
| 49 |
-
def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids, conf_threshold
|
| 50 |
"""Run object tracking on the uploaded video."""
|
| 51 |
try:
|
| 52 |
# Create temporary workspace
|
|
@@ -73,11 +73,6 @@ def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids,
|
|
| 73 |
"--exist-ok"
|
| 74 |
]
|
| 75 |
|
| 76 |
-
# Add line counter parameters
|
| 77 |
-
cmd.extend(["--line-pos", str(line_position)])
|
| 78 |
-
cmd.extend(["--line-direction", line_orientation])
|
| 79 |
-
cmd.extend(["--count-objects"])
|
| 80 |
-
|
| 81 |
# Add class filtering if specific classes are provided
|
| 82 |
if class_ids and class_ids.strip():
|
| 83 |
# Parse the comma-separated class IDs
|
|
@@ -137,22 +132,10 @@ def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids,
|
|
| 137 |
return None, "Output video was generated but has zero size."
|
| 138 |
|
| 139 |
# Copy to permanent location with unique name
|
| 140 |
-
permanent_path = os.path.join(OUTPUT_DIR, f"
|
| 141 |
shutil.copy(output_file, permanent_path)
|
| 142 |
print(f"Copied output to permanent location: {permanent_path}")
|
| 143 |
|
| 144 |
-
# Extract counting information from output
|
| 145 |
-
count_summary = "Object counting complete"
|
| 146 |
-
try:
|
| 147 |
-
# Try to find any count results in the process output
|
| 148 |
-
output_text = process.stdout
|
| 149 |
-
if "counts" in output_text or "Counted" in output_text:
|
| 150 |
-
count_lines = [line for line in output_text.split('\n') if "counts" in line or "Counted" in line]
|
| 151 |
-
if count_lines:
|
| 152 |
-
count_summary = "\n".join(count_lines)
|
| 153 |
-
except:
|
| 154 |
-
pass
|
| 155 |
-
|
| 156 |
# Ensure the file is in MP4 format for better compatibility with Gradio
|
| 157 |
if not permanent_path.lower().endswith('.mp4'):
|
| 158 |
mp4_path = os.path.splitext(permanent_path)[0] + '.mp4'
|
|
@@ -169,7 +152,7 @@ def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids,
|
|
| 169 |
print(f"Failed to convert to MP4: {str(e)}")
|
| 170 |
# Continue with original file if conversion fails
|
| 171 |
|
| 172 |
-
return permanent_path,
|
| 173 |
else:
|
| 174 |
print(f"Output file not found at {output_file}")
|
| 175 |
return None, "Output file was referenced but doesn't exist on disk."
|
|
@@ -180,14 +163,13 @@ def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids,
|
|
| 180 |
return None, f"Error: {str(e)}"
|
| 181 |
|
| 182 |
# Define the Gradio interface
|
| 183 |
-
def process_video(video_path, yolo_model, reid_model, tracking_method, class_ids, conf_threshold
|
| 184 |
# Validate inputs
|
| 185 |
if not video_path:
|
| 186 |
return None, "Please upload a video file"
|
| 187 |
|
| 188 |
print(f"Processing video: {video_path}")
|
| 189 |
print(f"Parameters: model={yolo_model}, reid={reid_model}, tracker={tracking_method}, classes={class_ids}, conf={conf_threshold}")
|
| 190 |
-
print(f"Line counter: {line_orientation} at position {line_position}")
|
| 191 |
|
| 192 |
output_path, status = run_tracking(
|
| 193 |
video_path,
|
|
@@ -195,9 +177,7 @@ def process_video(video_path, yolo_model, reid_model, tracking_method, class_ids
|
|
| 195 |
reid_model,
|
| 196 |
tracking_method,
|
| 197 |
class_ids,
|
| 198 |
-
conf_threshold
|
| 199 |
-
line_position,
|
| 200 |
-
line_orientation
|
| 201 |
)
|
| 202 |
|
| 203 |
if output_path:
|
|
@@ -213,29 +193,15 @@ def process_video(video_path, yolo_model, reid_model, tracking_method, class_ids
|
|
| 213 |
yolo_models = ["yolov8n.pt", "yolov8s.pt", "yolov8m.pt"]
|
| 214 |
reid_models = ["osnet_x0_25_msmt17.pt"]
|
| 215 |
tracking_methods = ["bytetrack", "botsort", "ocsort", "strongsort"]
|
| 216 |
-
line_orientations = ["horizontal", "vertical"]
|
| 217 |
|
| 218 |
# Ensure dependencies and apply patches at startup
|
| 219 |
ensure_dependencies()
|
| 220 |
apply_patches()
|
| 221 |
|
| 222 |
# Create the Gradio interface
|
| 223 |
-
with gr.Blocks(title="Object Tracking
|
| 224 |
-
gr.Markdown("# 🚀 Object Tracking
|
| 225 |
-
gr.Markdown("Upload a video file to detect
|
| 226 |
-
|
| 227 |
-
# Add instructions for line counter
|
| 228 |
-
with gr.Accordion("Line Counter Instructions", open=True):
|
| 229 |
-
gr.Markdown("""
|
| 230 |
-
## Line Counter Feature
|
| 231 |
-
|
| 232 |
-
Use the line counter to count objects that cross a specified line in the video:
|
| 233 |
-
|
| 234 |
-
1. **Line Orientation**: Choose 'horizontal' for a line across the video or 'vertical' for a line from top to bottom
|
| 235 |
-
2. **Line Position**: Adjust the slider to position the line (0.1 = top/left, 0.9 = bottom/right)
|
| 236 |
-
|
| 237 |
-
The count will appear in the output video and in the status box below.
|
| 238 |
-
""")
|
| 239 |
|
| 240 |
# Add class reference information
|
| 241 |
with gr.Accordion("YOLO Class Reference", open=False):
|
|
@@ -295,31 +261,15 @@ with gr.Blocks(title="Object Tracking with Line Counter") as app:
|
|
| 295 |
label="Confidence Threshold"
|
| 296 |
)
|
| 297 |
|
| 298 |
-
# Line counter settings
|
| 299 |
-
gr.Markdown("### Line Counter Settings")
|
| 300 |
-
line_orientation = gr.Dropdown(
|
| 301 |
-
choices=line_orientations,
|
| 302 |
-
value="horizontal",
|
| 303 |
-
label="Line Orientation"
|
| 304 |
-
)
|
| 305 |
-
line_position = gr.Slider(
|
| 306 |
-
minimum=0.1,
|
| 307 |
-
maximum=0.9,
|
| 308 |
-
value=0.5,
|
| 309 |
-
step=0.05,
|
| 310 |
-
label="Line Position (0.1 = top/left, 0.9 = bottom/right)"
|
| 311 |
-
)
|
| 312 |
-
|
| 313 |
process_btn = gr.Button("Process Video", variant="primary")
|
| 314 |
|
| 315 |
with gr.Column(scale=1):
|
| 316 |
-
output_video = gr.Video(label="Output Video with Tracking
|
| 317 |
-
status_text = gr.Textbox(label="
|
| 318 |
|
| 319 |
process_btn.click(
|
| 320 |
fn=process_video,
|
| 321 |
-
inputs=[input_video, yolo_model, reid_model, tracking_method, class_ids, conf_threshold,
|
| 322 |
-
line_position, line_orientation],
|
| 323 |
outputs=[output_video, status_text]
|
| 324 |
)
|
| 325 |
|
|
|
|
| 46 |
else:
|
| 47 |
print("⚠️ tracker_patch.py not found, skipping patches")
|
| 48 |
|
| 49 |
+
def run_tracking(video_file, yolo_model, reid_model, tracking_method, class_ids, conf_threshold):
|
| 50 |
"""Run object tracking on the uploaded video."""
|
| 51 |
try:
|
| 52 |
# Create temporary workspace
|
|
|
|
| 73 |
"--exist-ok"
|
| 74 |
]
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Add class filtering if specific classes are provided
|
| 77 |
if class_ids and class_ids.strip():
|
| 78 |
# Parse the comma-separated class IDs
|
|
|
|
| 132 |
return None, "Output video was generated but has zero size."
|
| 133 |
|
| 134 |
# Copy to permanent location with unique name
|
| 135 |
+
permanent_path = os.path.join(OUTPUT_DIR, f"output_{os.path.basename(video_file)}")
|
| 136 |
shutil.copy(output_file, permanent_path)
|
| 137 |
print(f"Copied output to permanent location: {permanent_path}")
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Ensure the file is in MP4 format for better compatibility with Gradio
|
| 140 |
if not permanent_path.lower().endswith('.mp4'):
|
| 141 |
mp4_path = os.path.splitext(permanent_path)[0] + '.mp4'
|
|
|
|
| 152 |
print(f"Failed to convert to MP4: {str(e)}")
|
| 153 |
# Continue with original file if conversion fails
|
| 154 |
|
| 155 |
+
return permanent_path, "Processing completed successfully!"
|
| 156 |
else:
|
| 157 |
print(f"Output file not found at {output_file}")
|
| 158 |
return None, "Output file was referenced but doesn't exist on disk."
|
|
|
|
| 163 |
return None, f"Error: {str(e)}"
|
| 164 |
|
| 165 |
# Define the Gradio interface
|
| 166 |
+
def process_video(video_path, yolo_model, reid_model, tracking_method, class_ids, conf_threshold):
|
| 167 |
# Validate inputs
|
| 168 |
if not video_path:
|
| 169 |
return None, "Please upload a video file"
|
| 170 |
|
| 171 |
print(f"Processing video: {video_path}")
|
| 172 |
print(f"Parameters: model={yolo_model}, reid={reid_model}, tracker={tracking_method}, classes={class_ids}, conf={conf_threshold}")
|
|
|
|
| 173 |
|
| 174 |
output_path, status = run_tracking(
|
| 175 |
video_path,
|
|
|
|
| 177 |
reid_model,
|
| 178 |
tracking_method,
|
| 179 |
class_ids,
|
| 180 |
+
conf_threshold
|
|
|
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
if output_path:
|
|
|
|
| 193 |
yolo_models = ["yolov8n.pt", "yolov8s.pt", "yolov8m.pt"]
|
| 194 |
reid_models = ["osnet_x0_25_msmt17.pt"]
|
| 195 |
tracking_methods = ["bytetrack", "botsort", "ocsort", "strongsort"]
|
|
|
|
| 196 |
|
| 197 |
# Ensure dependencies and apply patches at startup
|
| 198 |
ensure_dependencies()
|
| 199 |
apply_patches()
|
| 200 |
|
| 201 |
# Create the Gradio interface
|
| 202 |
+
with gr.Blocks(title="YOLO Object Tracking") as app:
|
| 203 |
+
gr.Markdown("# 🚀 YOLO Object Tracking")
|
| 204 |
+
gr.Markdown("Upload a video file to detect and track objects. Processing may take a few minutes depending on video length.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# Add class reference information
|
| 207 |
with gr.Accordion("YOLO Class Reference", open=False):
|
|
|
|
| 261 |
label="Confidence Threshold"
|
| 262 |
)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
process_btn = gr.Button("Process Video", variant="primary")
|
| 265 |
|
| 266 |
with gr.Column(scale=1):
|
| 267 |
+
output_video = gr.Video(label="Output Video with Tracking")
|
| 268 |
+
status_text = gr.Textbox(label="Status", value="Ready to process video")
|
| 269 |
|
| 270 |
process_btn.click(
|
| 271 |
fn=process_video,
|
| 272 |
+
inputs=[input_video, yolo_model, reid_model, tracking_method, class_ids, conf_threshold],
|
|
|
|
| 273 |
outputs=[output_video, status_text]
|
| 274 |
)
|
| 275 |
|