Spaces:
Build error
Build error
File size: 907 Bytes
8b35551 58afe04 8b35551 8d24d80 58afe04 8b35551 c3d9495 8b35551 8d24d80 8b35551 58afe04 8b35551 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import gradio as gr
from ultralytics import YOLO
import json
model = YOLO("best.pt")
def predict(img):
results = model(img)
annotated_img = results[0].plot()
detections = []
# Safely extract boxes if they exist
if len(results) > 0 and len(results[0].boxes) > 0:
for box in results[0].boxes:
# Use .item() to safely convert PyTorch tensors to standard Python numbers
class_id = int(box.cls.item())
label = model.names[class_id]
conf = float(box.conf.item())
detections.append({
"label": label,
"confidence": conf
})
return annotated_img, json.dumps(detections)
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=[
gr.Image(type="pil"),
gr.Textbox(label="Detections")
]
)
demo.launch()
|