Update inference.py
Browse files- inference.py +29 -4
inference.py
CHANGED
|
@@ -1,4 +1,29 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import cv2
|
| 4 |
+
|
| 5 |
+
# Load the model once when the container starts
|
| 6 |
+
model = YOLO("best.pt") # HF resolves this path inside the repo
|
| 7 |
+
|
| 8 |
+
def predict(image, conf: float = 0.25, iou: float = 0.45):
|
| 9 |
+
"""
|
| 10 |
+
Args:
|
| 11 |
+
image: raw bytes or PIL.Image provided by the API
|
| 12 |
+
conf : confidence threshold (default 0.25)
|
| 13 |
+
iou : IoU threshold for NMS (default 0.45)
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
PIL.Image with bounding boxes drawn.
|
| 17 |
+
"""
|
| 18 |
+
# Make sure we have a PIL.Image
|
| 19 |
+
if not isinstance(image, Image.Image):
|
| 20 |
+
image = Image.open(image)
|
| 21 |
+
|
| 22 |
+
# Run inference
|
| 23 |
+
results = model(image, conf=conf, iou=iou)[0]
|
| 24 |
+
|
| 25 |
+
# Ultralytics returns a BGR NumPy array from .plot()
|
| 26 |
+
annotated = results.plot()
|
| 27 |
+
annotated = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB) # BGR ➜ RGB
|
| 28 |
+
|
| 29 |
+
return Image.fromarray(annotated)
|