Download src/tools/detection_model.py from VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling: direct link, hf CLI and curl.
- Browser
- Download file 838 Bytes
-
https://huggingface.co/spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/resolve/main/src/tools/detection_model.py
- Command line
-
hf download hf://spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/src/tools/detection_model.py
-
curl -L -o detection_model.py https://huggingface.co/spaces/VLAI-AIVN/AIO2024M10_Tutorial_Tool_Calling/resolve/main/src/tools/detection_model.py
838 Bytes
| from ultralytics import YOLO | |
| detection_model_id = "yolo11n.pt" | |
| detection_model = YOLO(detection_model_id) | |
| def run_detection(image_path: str, is_visualize: bool = False): | |
| """YOLOv11: return list of {box, label, score} for a single image.""" | |
| results = detection_model(image_path) | |
| r = results[0] | |
| detections = [] | |
| for box in r.boxes: | |
| # box.xyxy is a 1×4 tensor, box.conf is a 1-element tensor, box.cls likewise | |
| coords = box.xyxy.cpu().numpy().flatten().tolist() | |
| score = float(box.conf.cpu().numpy().item()) | |
| cls_id = int(box.cls.cpu().numpy().item()) | |
| detections.append({ | |
| "box": coords, | |
| "label": r.names[cls_id], | |
| "score": score, | |
| }) | |
| if is_visualize: | |
| r.save() | |
| r.show() | |
| return {"detections": detections} | |