-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.py
33 lines (24 loc) · 885 Bytes
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import annotations
import json
import os
import typing as t
from pathlib import Path
import bentoml
from bentoml.validators import ContentType
Image = t.Annotated[Path, ContentType("image/*")]
@bentoml.service(resources={"gpu": 1})
class YoloV8:
def __init__(self):
from ultralytics import YOLO
yolo_model = os.getenv("YOLO_MODEL", "yolov8x.pt")
self.model = YOLO(yolo_model)
@bentoml.api(batchable=True)
def predict(self, images: list[Image]) -> list[list[dict]]:
results = self.model.predict(source=images)
return [json.loads(result.tojson()) for result in results]
@bentoml.api
def render(self, image: Image) -> Image:
result = self.model.predict(image)[0]
output = image.parent.joinpath(f"{image.stem}_result{image.suffix}")
result.save(str(output))
return output