Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,58 +2,24 @@ from ultralytics import YOLO
|
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
-
import tempfile
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
# Load the trained YOLOv8 model for human detection
|
| 10 |
-
model = YOLO("best.pt") # Replace with your
|
| 11 |
|
| 12 |
-
# Define function
|
| 13 |
-
def detect_humans(
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
result_img = results[0].plot()
|
| 18 |
-
return Image.fromarray(result_img.astype(np.uint8))
|
| 19 |
-
|
| 20 |
-
# If input is a video file path
|
| 21 |
-
elif isinstance(input_file, str) and os.path.isfile(input_file):
|
| 22 |
-
cap = cv2.VideoCapture(input_file)
|
| 23 |
-
|
| 24 |
-
# Video writer setup
|
| 25 |
-
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 26 |
-
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 27 |
-
out = cv2.VideoWriter(temp_output.name, fourcc, int(cap.get(cv2.CAP_PROP_FPS)),
|
| 28 |
-
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
| 29 |
-
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
|
| 30 |
-
|
| 31 |
-
while True:
|
| 32 |
-
ret, frame = cap.read()
|
| 33 |
-
if not ret:
|
| 34 |
-
break
|
| 35 |
-
results = model(frame)
|
| 36 |
-
annotated_frame = results[0].plot()
|
| 37 |
-
out.write(annotated_frame)
|
| 38 |
-
|
| 39 |
-
cap.release()
|
| 40 |
-
out.release()
|
| 41 |
-
|
| 42 |
-
return temp_output.name # Return path to processed video file
|
| 43 |
-
|
| 44 |
-
else:
|
| 45 |
-
return None
|
| 46 |
|
| 47 |
-
# Build Gradio interface
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=detect_humans,
|
| 50 |
-
inputs=gr.
|
| 51 |
-
outputs=
|
| 52 |
-
gr.Image(type="pil", label="Detected Humans (Image)"),
|
| 53 |
-
gr.Video(label="Detected Humans (Video)")
|
| 54 |
-
],
|
| 55 |
title="Human Detection (YOLOv8)",
|
| 56 |
-
description="Upload an image
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
interface.launch(debug=True)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load the trained YOLOv8 model for human detection
|
| 7 |
+
model = YOLO("best.pt") # Replace with the correct path to your human detection model
|
| 8 |
|
| 9 |
+
# Define prediction function
|
| 10 |
+
def detect_humans(image):
|
| 11 |
+
results = model(image) # Perform inference
|
| 12 |
+
result_img = results[0].plot() # Draw bounding boxes
|
| 13 |
+
return Image.fromarray(result_img.astype(np.uint8)) # Return image with boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Build Gradio interface
|
| 16 |
interface = gr.Interface(
|
| 17 |
fn=detect_humans,
|
| 18 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 19 |
+
outputs=gr.Image(type="pil", label="Detected Humans"),
|
|
|
|
|
|
|
|
|
|
| 20 |
title="Human Detection (YOLOv8)",
|
| 21 |
+
description="Upload an image. The model will detect humans using your trained YOLOv8 model."
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Launch the interface
|
| 25 |
interface.launch(debug=True)
|