Commit
ยท
ea89515
1
Parent(s):
3f9a142
๐ Added Gradio YOLOv8 app
Browse files- app.py +59 -0
- requirements.txt +4 -0
- runtime.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import cv2, tempfile
|
| 6 |
+
|
| 7 |
+
# Load YOLO model from HF Hub
|
| 8 |
+
model_path = hf_hub_download(repo_id="utkarsh-23/yolov8m-garbage-pothole-detector", filename="best.pt")
|
| 9 |
+
model = YOLO(model_path)
|
| 10 |
+
|
| 11 |
+
# Image detection
|
| 12 |
+
def detect_image(image):
|
| 13 |
+
results = model(image)
|
| 14 |
+
return results[0].plot()
|
| 15 |
+
|
| 16 |
+
# Video detection
|
| 17 |
+
def detect_video(video_path):
|
| 18 |
+
cap = cv2.VideoCapture(video_path)
|
| 19 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 20 |
+
out_path = tempfile.mktemp(suffix=".mp4")
|
| 21 |
+
out = cv2.VideoWriter(
|
| 22 |
+
out_path, fourcc, cap.get(cv2.CAP_PROP_FPS),
|
| 23 |
+
(int(cap.get(3)), int(cap.get(4)))
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
while cap.isOpened():
|
| 27 |
+
ret, frame = cap.read()
|
| 28 |
+
if not ret:
|
| 29 |
+
break
|
| 30 |
+
results = model(frame)
|
| 31 |
+
annotated_frame = results[0].plot()
|
| 32 |
+
out.write(annotated_frame)
|
| 33 |
+
|
| 34 |
+
cap.release()
|
| 35 |
+
out.release()
|
| 36 |
+
return out_path
|
| 37 |
+
|
| 38 |
+
# Interfaces
|
| 39 |
+
image_interface = gr.Interface(
|
| 40 |
+
fn=detect_image,
|
| 41 |
+
inputs=gr.Image(type="numpy"),
|
| 42 |
+
outputs=gr.Image(type="numpy"),
|
| 43 |
+
title="Garbage & Pothole Detector (Image)"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
video_interface = gr.Interface(
|
| 47 |
+
fn=detect_video,
|
| 48 |
+
inputs=gr.Video(),
|
| 49 |
+
outputs=gr.Video(),
|
| 50 |
+
title="Garbage & Pothole Detector (Video)"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
demo = gr.TabbedInterface(
|
| 54 |
+
[image_interface, video_interface],
|
| 55 |
+
["Image Detection", "Video Detection"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.3.202
|
| 2 |
+
gradio==5.46.0
|
| 3 |
+
huggingface_hub==0.35.0
|
| 4 |
+
opencv-python==4.12.0.88
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.10
|