Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,23 @@ from PIL import Image
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load YOLO model
|
| 10 |
model = YOLO("yolov8n.pt")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def detect_objects_image(image):
|
| 13 |
"""Process image with YOLO detection."""
|
| 14 |
if image is None:
|
|
@@ -38,32 +51,49 @@ def detect_objects_video(video):
|
|
| 38 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 39 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 40 |
|
| 41 |
-
# Create output video file
|
| 42 |
-
output_path =
|
| 43 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 44 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
return output_path
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Create Gradio interface
|
| 68 |
with gr.Blocks(title="YOLO Object Detection", theme=gr.themes.Soft()) as demo:
|
| 69 |
gr.Markdown("# π YOLO Object Detection App")
|
|
@@ -86,13 +116,6 @@ with gr.Blocks(title="YOLO Object Detection", theme=gr.themes.Soft()) as demo:
|
|
| 86 |
label="Detection Results",
|
| 87 |
height=400
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
# Image examples
|
| 91 |
-
gr.Examples(
|
| 92 |
-
examples=[], # You can add example image paths here
|
| 93 |
-
inputs=image_input,
|
| 94 |
-
label="Example Images"
|
| 95 |
-
)
|
| 96 |
|
| 97 |
# Video Detection Tab
|
| 98 |
with gr.TabItem("π₯ Video Detection"):
|
|
@@ -140,6 +163,9 @@ with gr.Blocks(title="YOLO Object Detection", theme=gr.themes.Soft()) as demo:
|
|
| 140 |
inputs=video_input,
|
| 141 |
outputs=video_output
|
| 142 |
)
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Launch the app
|
| 145 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
+
import atexit
|
| 9 |
+
import shutil
|
| 10 |
|
| 11 |
# Load YOLO model
|
| 12 |
model = YOLO("yolov8n.pt")
|
| 13 |
|
| 14 |
+
# Create a temporary directory for this session
|
| 15 |
+
TEMP_DIR = tempfile.mkdtemp()
|
| 16 |
+
|
| 17 |
+
def cleanup_temp_files():
|
| 18 |
+
"""Clean up temporary files on exit."""
|
| 19 |
+
if os.path.exists(TEMP_DIR):
|
| 20 |
+
shutil.rmtree(TEMP_DIR, ignore_errors=True)
|
| 21 |
+
|
| 22 |
+
# Register cleanup function
|
| 23 |
+
atexit.register(cleanup_temp_files)
|
| 24 |
+
|
| 25 |
def detect_objects_image(image):
|
| 26 |
"""Process image with YOLO detection."""
|
| 27 |
if image is None:
|
|
|
|
| 51 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 52 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 53 |
|
| 54 |
+
# Create output video file in our temp directory
|
| 55 |
+
output_path = os.path.join(TEMP_DIR, f"output_{os.urandom(8).hex()}.mp4")
|
| 56 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 57 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 58 |
|
| 59 |
+
try:
|
| 60 |
+
# Process each frame
|
| 61 |
+
while cap.isOpened():
|
| 62 |
+
ret, frame = cap.read()
|
| 63 |
+
if not ret:
|
| 64 |
+
break
|
| 65 |
+
|
| 66 |
+
# Perform detection
|
| 67 |
+
results = model(frame)
|
| 68 |
+
annotated_frame = results[0].plot()
|
| 69 |
+
|
| 70 |
+
# Convert RGB to BGR for cv2.VideoWriter
|
| 71 |
+
annotated_frame_bgr = cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR)
|
| 72 |
+
|
| 73 |
+
# Write annotated frame
|
| 74 |
+
out.write(annotated_frame_bgr)
|
| 75 |
+
finally:
|
| 76 |
+
cap.release()
|
| 77 |
+
out.release()
|
| 78 |
|
| 79 |
return output_path
|
| 80 |
|
| 81 |
+
# Periodic cleanup function to remove old processed videos
|
| 82 |
+
def periodic_cleanup():
|
| 83 |
+
"""Remove files older than 1 hour from temp directory."""
|
| 84 |
+
import time
|
| 85 |
+
current_time = time.time()
|
| 86 |
+
for filename in os.listdir(TEMP_DIR):
|
| 87 |
+
filepath = os.path.join(TEMP_DIR, filename)
|
| 88 |
+
if os.path.isfile(filepath):
|
| 89 |
+
file_age = current_time - os.path.getmtime(filepath)
|
| 90 |
+
# Delete files older than 1 hour (3600 seconds)
|
| 91 |
+
if file_age > 3600:
|
| 92 |
+
try:
|
| 93 |
+
os.remove(filepath)
|
| 94 |
+
except Exception:
|
| 95 |
+
pass
|
| 96 |
+
|
| 97 |
# Create Gradio interface
|
| 98 |
with gr.Blocks(title="YOLO Object Detection", theme=gr.themes.Soft()) as demo:
|
| 99 |
gr.Markdown("# π YOLO Object Detection App")
|
|
|
|
| 116 |
label="Detection Results",
|
| 117 |
height=400
|
| 118 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Video Detection Tab
|
| 121 |
with gr.TabItem("π₯ Video Detection"):
|
|
|
|
| 163 |
inputs=video_input,
|
| 164 |
outputs=video_output
|
| 165 |
)
|
| 166 |
+
|
| 167 |
+
# Run periodic cleanup every time the interface loads
|
| 168 |
+
demo.load(periodic_cleanup)
|
| 169 |
|
| 170 |
# Launch the app
|
| 171 |
if __name__ == "__main__":
|