-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.py
33 lines (25 loc) · 1.04 KB
/
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 typing
import pathlib
import bentoml
import numpy
Image = typing.Annotated[pathlib.Path, bentoml.validators.ContentType('image/*')]
with bentoml.importing():
import easyocr
@bentoml.service(
resources={'gpu': 1}, image=bentoml.images.PythonImage(python_version='3.11').requirements_file('requirements.txt')
)
class OCRService:
models = bentoml.models.BentoModel('easyocr--ch-en')
def __init__(self):
self.reader = easyocr.Reader(
['ch_sim', 'en'], model_storage_directory=self.models.path, download_enabled=False, gpu=True
)
@bentoml.api()
def detect(self, image: Image) -> list[dict]:
detections = self.reader.readtext(str(image))
return [{'text': text, 'bbox': numpy.array(bbox).tolist()} for (bbox, text, _) in detections]
@bentoml.api()
def classify(self, image: Image) -> list[dict]:
detections = self.reader.readtext(str(image))
return [{'text': text, 'confidence': confidence} for (_, text, confidence) in detections]