| from typing import Dict, List, Any |
| from ultralytics import YOLO |
| from PIL import Image |
| import torch |
| import io |
|
|
| class EndpointHandler(): |
| def __init__(self, path=""): |
| |
| |
| self.model = YOLO(f"{path}/best.pt") |
|
|
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
| """ |
| data args: |
| inputs (:obj: `bytes`) : The raw image bytes sent from Supabase |
| Return: |
| A :obj:`list` | `dict`: The detection results |
| """ |
| |
| inputs = data.pop("inputs", data) |
|
|
| |
| img = Image.open(io.BytesIO(inputs)) |
|
|
| |
| results = self.model(img, conf=0.25) |
|
|
| |
| payload = [] |
| for r in results: |
| for box in r.boxes: |
| payload.append({ |
| "label": self.model.names[int(box.cls)], |
| "confidence": float(box.conf), |
| "points": box.xyxy[0].tolist(), |
| }) |
|
|
| return payload |
|
|