Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from ultralytics import YOLO | |
| model = YOLO("yolov8n.pt") | |
| def detect_persons(image): | |
| image_np = np.array(image) | |
| results = model(image_np) | |
| persons = [] | |
| for r in results: | |
| for box, cls, conf in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.conf): | |
| # class 0 in COCO = person | |
| if int(cls) != 0: | |
| continue | |
| if conf < 0.4: | |
| continue | |
| xmin, ymin, xmax, ymax = box.cpu().numpy() | |
| cx = (xmin + xmax) / 2 | |
| cy = (ymin + ymax) / 2 | |
| persons.append({ | |
| "cx": float(cx), | |
| "cy": float(cy), | |
| "confidence": float(conf), | |
| "box": { | |
| "xmin": float(xmin), | |
| "ymin": float(ymin), | |
| "xmax": float(xmax), | |
| "ymax": float(ymax) | |
| } | |
| }) | |
| return persons | |
| iface = gr.Interface( | |
| fn=detect_persons, | |
| inputs=gr.Image(type="pil"), | |
| outputs="json" | |
| ) | |
| iface.launch() |