Skip to content

Commit 5161d6f

Browse files
authored
Add image output and update dependencies list for replicate to work (#19)
Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
1 parent e23df22 commit 5161d6f

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

yolo11n/cog.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
build:
44
gpu: false
5-
python_version: "3.11"
5+
python_version: "3.12"
66
system_packages:
77
- "libgl1-mesa-glx"
88
- "libglib2.0-0"
9+
- "ffmpeg"
910
python_packages:
10-
- ultralytics>=8.3.0
11-
- torch>=2.0.0
12-
- torchvision
13-
- pillow
11+
- "ultralytics>=8.3.0"
12+
- "torch==2.3.1"
13+
- "torchvision"
14+
- "numpy>=1.24.0"
15+
- "opencv-python"
16+
- "pillow"
17+
1418
predict: predict.py:Predictor
1519
image: r8.im/ultralytics/yolo11n

yolo11n/predict.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
22

33
import json
4-
from typing import Any, Dict
5-
6-
from cog import BasePredictor, Input, Path
4+
from typing import Optional
75
from ultralytics import YOLO
6+
from cog import BasePredictor, Input, Path, BaseModel
7+
8+
9+
class Output(BaseModel):
10+
image: Optional[Path] = None
11+
json_str: Optional[str] = None
812

913

1014
class Predictor(BasePredictor):
@@ -21,13 +25,16 @@ def predict(
2125
iou: float = Input(description="IoU threshold for NMS", default=0.45, ge=0.0, le=1.0),
2226
imgsz: int = Input(description="Image size", default=640, choices=[320, 416, 512, 640, 832, 1024, 1280]),
2327
return_json: bool = Input(description="Return detection results as JSON", default=False),
24-
) -> Dict[str, Any] | Path:
28+
) -> Output:
2529
"""Run inference and return annotated image with optional JSON results."""
2630
result = self.model(str(image), conf=conf, iou=iou, imgsz=imgsz)[0]
2731
image_path = "output.png"
2832
result.save(image_path)
2933

3034
if return_json:
31-
return {"image": Path(image_path), "results": json.loads(result.to_json())}
35+
return Output(
36+
image=Path(image_path),
37+
json_str=result.to_json()
38+
)
3239
else:
33-
return Path(image_path)
40+
return Output(image=Path(image_path))

0 commit comments

Comments
 (0)