<jupyter_start><jupyter_text>Install Dependencies<jupyter_code># Might need to install tesseract into your system separately
# !pip install pytesseract
!pip install easyocr<jupyter_output>Collecting easyocr
  Downloading easyocr-1.1.9-py3-none-any.whl (48.4 MB)
[K     |████████████████████████████████| 48.4 MB 11.4 MB/s eta 0:00:01   |█▏                              | 1.7 MB 561 kB/s eta 0:01:24     |██▉                             | 4.3 MB 5.8 MB/s eta 0:00:08     |██████▌                         | 9.8 MB 5.8 MB/s eta 0:00:07     |██████████████████████████████▋ | 46.3 MB 4.9 MB/s eta 0:00:01
[?25hRequirement already satisfied: torch in /opt/anaconda3/lib/python3.8/site-packages (from easyocr) (1.6.0)
Requirement already satisfied: scipy in /opt/anaconda3/lib/python3.8/site-packages (from easyocr) (1.5.0)
Requirement already satisfied: Pillow in /opt/anaconda3/lib/python3.8/site-packages (from easyocr) (7.2.0)
Collecting opencv-python
  Downloading opencv_python-4.4.0.42-cp38-cp38-macosx_10_13_x86_64.whl (52.2 MB)
[K     |████████████████████████████████| 52.2 MB 10.3 MB/s eta 0:00:01   |█████▏                          | 8.4 MB 9.4 MB/s eta 0:00:05     |████████████[...]<jupyter_text>Write Text Detection Class<jupyter_code>import easyocr
reader = easyocr.Reader(['en']) # need to run only once to load model into memory
result = reader.readtext('images/plate_number.jpg')
result
# result without text
result = reader.readtext('images/no_text_image1.jpg')
result == []
import easyocr
class TextDetectionFromImage:
    """
    Simple python class to detet images from object
    """
    def __init__(self):
        self.reader = easyocr.Reader(['en']) # need to run only once to load model into memory    

    def detect_text(self, im):
        result = self.reader.readtext(im)
        text_exists = result != []
          
        return {'has_text':text_exists, 'text_and_location': result}

text_detector = TextDetectionFromImage()
%%time result = text_detector.detect_text('images/plate_number.jpg')<jupyter_output><empty_output><jupyter_text>Write service file<jupyter_code>%%writefile text_detection_service.py

from bentoml import BentoService, api, env, artifacts
from bentoml.artifact import PickleArtifact
from bentoml.adapters import FileInput

@artifacts([PickleArtifact('model')])
@env(pip_dependencies=['easyocr'], 
     conda_channels=["conda-forge"], 
     conda_dependencies=["ruamel.yaml"])
class TextDetectionService(BentoService):
    
    @api(input=FileInput())
    def predict(self, image):
        result = self.artifacts.model.detect_text(image[0])
        return result<jupyter_output>Overwriting text_detection_service.py<jupyter_text>Package Model and Serve<jupyter_code># 1) import the custom BentoService defined above
from text_detection_service import TextDetectionService

# 2) `pack` it with required artifacts
bento_service = TextDetectionService()
bento_service.pack("model", text_detector)

# # 3) save your BentoService to file archive
saved_path = bento_service.save()
saved_path
from bentoml import load

service = load(saved_path)

print(service.predict('images/japan72.jpg'))
print(service.predict('images/plate_number.jpg'))
!pip install {saved_path}
# Ultracold class published as a package
import TextDetectionService

text_detect = TextDetectionService.load()
text_detect.predict('images/plate_number.jpg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14357017028772753, 'fraction_numerical': 0.03611274221961245, 'mean_word_length': 4.217457886676876, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '659112', 'license': 'MIT', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1164, 'n_words': 261, 'path': 'text_detect_serve_nb.ipynb', 'repo_name': 'xmpuspus/image_text_extraction', 'size': '3406'}