EasyOCR

Users can now use EasyOCR with BentoML with the following API: load, save, and load_runner as follow:

import bentoml
import easyocr
import numpy as np
import imageio

IMAGE_PATH: str = "./tests/utils/_static/chinese.jpg"
LANG_LIST = ["ch_sim", "en"]
RECOG_NETWORK = "zh_sim_g2"
DETECT_MODEL = "craft_mlt_25k"

model = easyocr.Reader(
    lang_list=LANG_LIST,
    gpu=False,
    download_enabled=True,
    recog_network=RECOG_NETWORK,
)

# `save` a given classifier and retrieve coresponding tag:
tag = bentoml.easyocr.save(
    "ocr_craft",
    model,
    lang_list=LANG_LIST,
    recog_network=RECOG_NETWORK,
    detect_model=DETECT_MODEL,
)

# retrieve metadata with `bentoml.models.get`:
metadata = bentoml.models.get(tag)

# `load` the model back in memory:
model = bentoml.easyocr.load("ocr_craft")

# Run a given model under `Runner` abstraction with `load_runner`
imarr = np.asarray(imageio.imread(IMAGE_PATH))
runner = bentoml.easyocr.load_runner(tag)
runner.run_batch(imarr)

Note

You can find more examples for EasyOCR in our gallery repo.

bentoml.easyocr.save(name, model, *, lang_list=None, recog_network='english_g2', detect_model='craft_mlt_25k', labels=None, custom_objects=None, metadata=None)

Save a model instance to BentoML modelstore.

Parameters
  • name (str) – Name for given model instance. This should pass Python identifier check.

  • model (easyocr.Reader) – Instance of model to be saved.

  • lang_list (List[str], optional, default to None) – Should be same as easyocr.Reader(lang_list=...)(). If None, then default to ["en"].

  • recog_network (str, optional, default to english_g2) – Should be same as easyocr.Reader(recog_network=…)

  • detect_model (str, optional, default to craft_mlt_25k) – Model used for detection pipeline.

  • labels (Dict[str, str], optional, default to None) – user-defined labels for managing models, e.g. team=nlp, stage=dev

  • custom_objects (Dict[str, Any]], optional, default to None) – user-defined additional python objects to be saved alongside the model, e.g. a tokenizer instance, preprocessor function, model configuration json

  • metadata (Dict[str, Any], optional, default to None) – Custom metadata for given model.

  • model_store (~bentoml._internal.models.ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

A tag with a format name:version where name is the user-defined model’s name, and a generated version by BentoML.

Return type

Tag

Examples:

import easyocr
import bentoml

language_list = ["ch_sim", "en"]
recog_network = "zh_sim_g2"

model = easyocr.Reader(
    lang_list=language_list,
    download_enabled=True,
    recog_network=recog_network,
)

tag = bentoml.easyocr.save(
    "my_easyocr_model",
    reader,
    lang_list=language_list,
    recog_network=recog_network
)

# load the booster back:
loaded = bentoml.easyocr.load("my_easyocr_model:latest") # or
loaded = bentoml.easyocr.load(tag)
bentoml.easyocr.load(tag, gpu=True, model_store=<simple_di.providers.SingletonFactory object>)

Load a model from BentoML local modelstore with given tag.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML local modelstore.

  • gpu (bool, optional, defaults to True) – Enable GPU support.

  • model_store (~bentoml._internal.models.ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

an instance of easyocr.Reader from BentoML modelstore.

Return type

easyocr.Reader

Examples:

import bentoml
booster = bentoml.easyocr.load("craft_model", model_params=dict(model_type="classifier"))
bentoml.easyocr.load_runner(tag, predict_fn_name='readtext_batched', *, name=None, predict_params=None)

Runner represents a unit of serving logic that can be scaled horizontally to maximize throughput. bentoml.easyocr.load_runner() implements a Runner class that wrap around a EasyOCR Reader model, which optimize it for the BentoML runtime.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML local modelstore.

  • predict_fn_name (str, default to readtext_batched) – Options for inference functions.

  • predict_params (Dict[str, Union[str, Any]], optional, default to None) – Parameters for prediction. Refers here for more information.

Returns

Runner instances for bentoml.easyocr model

Return type

Runner

Examples:

import bentoml
import pandas as pd

input_data = pd.from_csv("/path/to/csv")
runner = bentoml.easyocr.load_runner("my_easyocr_model")
runner.run(pd.DataFrame(input_data))