Creating Inference.py file
Browse files- inference.py +15 -0
inference.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import io, base64
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
MODEL = YOLO("best.pt")
|
| 7 |
+
|
| 8 |
+
def inference(image_bytes: bytes) -> bytes:
|
| 9 |
+
"""Takes raw image bytes, returns annotated PNG bytes."""
|
| 10 |
+
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 11 |
+
results = MODEL(img)
|
| 12 |
+
annotated = results[0].plot() # returns a NumPy array
|
| 13 |
+
buf = io.BytesIO()
|
| 14 |
+
Image.fromarray(annotated).save(buf, format="PNG")
|
| 15 |
+
return buf.getvalue()
|