abdullah123456 commited on
Commit
7dd7b93
·
verified ·
1 Parent(s): 4dc15c4

Creating Inference.py file

Browse files
Files changed (1) hide show
  1. 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()