Upload folder using huggingface_hub
Browse files- README.md +8 -0
- best.pt +3 -0
- handler.py +44 -0
- requirements.txt +4 -0
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language_bcp47:
|
| 3 |
+
- en
|
| 4 |
+
- vi-VN
|
| 5 |
+
pipeline_tag: object-detection
|
| 6 |
+
library_name: ultralytics
|
| 7 |
+
license: mit
|
| 8 |
+
---
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2e7d536e4c3e994bcff3fc39e6659e94caca910b18b631aad29b1eeb2dd3d349
|
| 3 |
+
size 19182483
|
handler.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict
|
| 2 |
+
import io, base64, requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
class EndpointHandler:
|
| 7 |
+
def __init__(self, path: str = ""):
|
| 8 |
+
self.model = YOLO(f"{path}/best.pt")
|
| 9 |
+
|
| 10 |
+
def _load_image(self, data: Dict) -> Image.Image:
|
| 11 |
+
if "inputs" in data: # HF UI/SDK đôi khi gửi trường "inputs"
|
| 12 |
+
data = data["inputs"]
|
| 13 |
+
|
| 14 |
+
if isinstance(data, dict) and data.get("image_url"):
|
| 15 |
+
img_bytes = requests.get(data["image_url"], timeout=15).content
|
| 16 |
+
return Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 17 |
+
|
| 18 |
+
if isinstance(data, dict) and data.get("image_base64"):
|
| 19 |
+
img_bytes = base64.b64decode(data["image_base64"])
|
| 20 |
+
return Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 21 |
+
|
| 22 |
+
if isinstance(data, (bytes, bytearray)):
|
| 23 |
+
return Image.open(io.BytesIO(data)).convert("RGB")
|
| 24 |
+
|
| 25 |
+
raise ValueError("No image provided. Use 'image_url' or 'image_base64' or raw bytes.")
|
| 26 |
+
|
| 27 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 28 |
+
conf = float(data.get("conf", 0.5)) if isinstance(data, dict) else 0.5
|
| 29 |
+
img = self._load_image(data)
|
| 30 |
+
res = self.model.predict(img, conf=conf, verbose=False)
|
| 31 |
+
r = res[0]
|
| 32 |
+
boxes_out = []
|
| 33 |
+
if r.boxes is not None:
|
| 34 |
+
for b in r.boxes:
|
| 35 |
+
xyxy = b.xyxy[0].tolist()
|
| 36 |
+
confv = float(b.conf[0])
|
| 37 |
+
cls = int(b.cls[0])
|
| 38 |
+
boxes_out.append({"xyxy": xyxy, "conf": confv, "cls": cls})
|
| 39 |
+
return {
|
| 40 |
+
"num_detections": len(boxes_out),
|
| 41 |
+
"boxes": boxes_out,
|
| 42 |
+
"max_confidence": max([b["conf"] for b in boxes_out], default=0.0),
|
| 43 |
+
"is_diseased": len(boxes_out) > 0
|
| 44 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.3.179
|
| 2 |
+
torch==2.8.0
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|