Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
from ultralytics import YOLO
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load the
|
| 7 |
-
model = YOLO("best.pt") #
|
| 8 |
|
| 9 |
# Define the inference function
|
| 10 |
-
def
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Define the Gradio interface
|
| 26 |
interface = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
-
inputs=gr.
|
| 29 |
-
outputs=gr.
|
| 30 |
-
title="
|
| 31 |
-
description="Upload an image
|
| 32 |
)
|
| 33 |
|
| 34 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Load the YOLO model
|
| 8 |
+
model = YOLO("best.pt") # Replace with the path to your model
|
| 9 |
|
| 10 |
# Define the inference function
|
| 11 |
+
def yolo_inference(input_file):
|
| 12 |
+
# Check if the input is an image or a video
|
| 13 |
+
if input_file.endswith((".jpg", ".jpeg", ".png")):
|
| 14 |
+
# Process as an image
|
| 15 |
+
img = cv2.imread(input_file)
|
| 16 |
+
results = model(img)
|
| 17 |
+
annotated_img = results[0].plot()
|
| 18 |
+
|
| 19 |
+
# Save the annotated image
|
| 20 |
+
output_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
|
| 21 |
+
cv2.imwrite(output_path, annotated_img)
|
| 22 |
+
return output_path
|
| 23 |
+
|
| 24 |
+
elif input_file.endswith((".mp4", ".avi", ".mov")):
|
| 25 |
+
# Process as a video
|
| 26 |
+
cap = cv2.VideoCapture(input_file)
|
| 27 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 28 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 29 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 30 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 31 |
+
|
| 32 |
+
# Create a temporary output video path
|
| 33 |
+
output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 34 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 35 |
+
|
| 36 |
+
while cap.isOpened():
|
| 37 |
+
ret, frame = cap.read()
|
| 38 |
+
if not ret:
|
| 39 |
+
break
|
| 40 |
+
|
| 41 |
+
# Run YOLO on each frame
|
| 42 |
+
results = model(frame)
|
| 43 |
+
annotated_frame = results[0].plot()
|
| 44 |
+
out.write(annotated_frame)
|
| 45 |
+
|
| 46 |
+
cap.release()
|
| 47 |
+
out.release()
|
| 48 |
+
return output_video_path
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
raise ValueError("Unsupported file format. Please upload an image or video.")
|
| 52 |
|
| 53 |
# Define the Gradio interface
|
| 54 |
interface = gr.Interface(
|
| 55 |
+
fn=yolo_inference,
|
| 56 |
+
inputs=gr.File(label="Upload an Image or Video"),
|
| 57 |
+
outputs=gr.File(label="Processed Image/Video"),
|
| 58 |
+
title="YOLO Object Detection",
|
| 59 |
+
description="Upload an image or video for object detection using YOLO."
|
| 60 |
)
|
| 61 |
|
| 62 |
# Launch the app
|