vsham001 commited on
Commit
220a66a
·
verified ·
1 Parent(s): c4c140f

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +29 -4
inference.py CHANGED
@@ -1,4 +1,29 @@
1
- curl -X POST \
2
- -H "Authorization: Bearer <HF_API_TOKEN>" \
3
- -F "image=@/path/to/image.jpg" \
4
- https://api-inference.huggingface.co/models/vsham001/Yolo298B
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)