Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
|
| 5 |
|
| 6 |
-
# Load YOLO model correctly
|
| 7 |
model_path = hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt")
|
| 8 |
model = YOLO(model_path)
|
| 9 |
|
| 10 |
def predict(image):
|
| 11 |
results = model(image)
|
| 12 |
result = results[0]
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
for box in result.boxes:
|
| 16 |
-
label = result.names[int(box.cls)]
|
| 17 |
confidence = float(box.conf)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return "\n".join(
|
| 24 |
|
| 25 |
gr.Interface(
|
| 26 |
fn=predict,
|
| 27 |
inputs=gr.Image(type="pil"),
|
| 28 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 29 |
title="Car / Bike / Mountain / Road Detector"
|
| 30 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from ultralytics import YOLO
|
| 4 |
+
import numpy as np
|
| 5 |
|
|
|
|
| 6 |
model_path = hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt")
|
| 7 |
model = YOLO(model_path)
|
| 8 |
|
| 9 |
def predict(image):
|
| 10 |
results = model(image)
|
| 11 |
result = results[0]
|
| 12 |
+
|
| 13 |
+
# Get annotated image with boxes drawn
|
| 14 |
+
annotated = result.plot()
|
| 15 |
+
|
| 16 |
+
labels = []
|
| 17 |
for box in result.boxes:
|
|
|
|
| 18 |
confidence = float(box.conf)
|
| 19 |
+
if confidence < 0.5:
|
| 20 |
+
continue
|
| 21 |
+
label = result.names[int(box.cls)]
|
| 22 |
+
labels.append(f"{label}: {confidence:.2f}")
|
| 23 |
+
|
| 24 |
+
return annotated, "\n".join(labels) if labels else "No objects detected"
|
| 25 |
|
| 26 |
gr.Interface(
|
| 27 |
fn=predict,
|
| 28 |
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=[
|
| 30 |
+
gr.Image(label="Detected Objects"), # image with boxes
|
| 31 |
+
gr.Text(label="Labels") # text results
|
| 32 |
+
],
|
| 33 |
title="Car / Bike / Mountain / Road Detector"
|
| 34 |
).launch()
|