-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
41 lines (33 loc) · 1.06 KB
/
app.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
34
35
36
37
38
39
40
41
from typing import Annotated
from io import BytesIO
from configparser import ConfigParser
from litestar import Litestar, post, status_codes
from litestar.datastructures import UploadFile
from litestar.enums import RequestEncodingType
from litestar.params import Body
from ultralytics import YOLO
from PIL import Image
model = None
class Prediction:
boxes: list
confidences: list
@post("/detect", status_code=status_codes.HTTP_200_OK)
async def index(
data: Annotated[UploadFile, Body(media_type=RequestEncodingType.MULTI_PART)]
) -> Prediction:
content = await data.read()
predictions = None
with Image.open(BytesIO(content)) as img:
predictions = model(img, max_det=2000)
return {
"boxes": predictions[0].boxes.xyxy.tolist(),
"confidences": predictions[0].boxes.conf.tolist()
}
config = ConfigParser()
config.read("config.ini")
model_path = config.get(
"main",
"model_path",
fallback="../yolov8n-freeclimbs-detect-2/yolov8n-freeclimbs-detect-2.pt")
model = YOLO(model_path)
app = Litestar([index])