Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
-
import
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
map_location="cpu"
|
| 10 |
-
)
|
| 11 |
-
model.eval()
|
| 12 |
-
|
| 13 |
-
# Define labels
|
| 14 |
-
labels = ["car", "bike", "mountain", "road"] # update to match your model
|
| 15 |
|
| 16 |
def predict(image):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
predicted = torch.argmax(output, dim=1).item()
|
| 28 |
|
| 29 |
-
return
|
| 30 |
|
| 31 |
gr.Interface(
|
| 32 |
fn=predict,
|
| 33 |
inputs=gr.Image(type="pil"),
|
| 34 |
-
outputs=gr.
|
| 35 |
-
title="Car/Bike/Mountain/Road Detector"
|
| 36 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
from PIL import Image
|
| 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 |
+
output = []
|
| 15 |
+
for box in result.boxes:
|
| 16 |
+
label = result.names[int(box.cls)]
|
| 17 |
+
confidence = float(box.conf)
|
| 18 |
+
output.append(f"{label}: {confidence:.2f}")
|
| 19 |
|
| 20 |
+
if not output:
|
| 21 |
+
return "No objects detected"
|
|
|
|
| 22 |
|
| 23 |
+
return "\n".join(output)
|
| 24 |
|
| 25 |
gr.Interface(
|
| 26 |
fn=predict,
|
| 27 |
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=gr.Text(label="Detections"),
|
| 29 |
+
title="Car / Bike / Mountain / Road Detector"
|
| 30 |
).launch()
|