Spaces:
Runtime error
Runtime error
Create inference.py
Browse files- inference.py +21 -0
inference.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Load YOLOv8 model
|
| 7 |
+
model = YOLO("model.pt")
|
| 8 |
+
|
| 9 |
+
def predict(image_bytes):
|
| 10 |
+
img = Image.open(io.BytesIO(image_bytes))
|
| 11 |
+
results = model.predict(img)
|
| 12 |
+
|
| 13 |
+
output = []
|
| 14 |
+
for result in results:
|
| 15 |
+
for i in range(len(result.boxes)):
|
| 16 |
+
output.append({
|
| 17 |
+
"bbox": result.boxes.xyxy[i].tolist(),
|
| 18 |
+
"class": int(result.boxes.cls[i].item()),
|
| 19 |
+
"confidence": float(result.boxes.conf[i].item())
|
| 20 |
+
})
|
| 21 |
+
return output
|