Create predict.py
Browse files- predict.py +27 -0
predict.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the model
|
| 7 |
+
model = YOLO("best.pt")
|
| 8 |
+
|
| 9 |
+
def predict(image_path):
|
| 10 |
+
# Load image
|
| 11 |
+
image = cv2.imread(image_path)
|
| 12 |
+
|
| 13 |
+
# Inference
|
| 14 |
+
results = model(image)
|
| 15 |
+
|
| 16 |
+
# Parse results into CVAT format (adjust as needed)
|
| 17 |
+
annotations = []
|
| 18 |
+
for box in results[0].boxes.data:
|
| 19 |
+
x1, y1, x2, y2, confidence, class_id = box[:6]
|
| 20 |
+
annotations.append({
|
| 21 |
+
"x1": int(x1), "y1": int(y1),
|
| 22 |
+
"x2": int(x2), "y2": int(y2),
|
| 23 |
+
"confidence": float(confidence),
|
| 24 |
+
"class": int(class_id)
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
+
return annotations
|