A Tutorial on Multilingual OCR with EasyOCR

Bhadresh Savani
4 min readNov 10, 2020

Optical Character Recognition(OCR) has been an active area of research in AI and Computer Vision. It enables us to Recognize(identify) text from images or PDF. OCR has a variety of applications like Data Entry for Business, Number Plate Recognition, Automated Passport Recognition, Quick Document Verification, IoT Application, Task Automation, etc. This Application has the potential to increase revenue and save time.

The TesseractOCR library by Google is one of the most popular. It was originally written in C++. There are Python Wrapper-Opensource Projects available for this Library. But it will make the library a bit slow because of programming language conversion. It could not be helpful for all the OCR applications in the world in which 7,117 languages are spoken today. It does not support GPU inference.

Hopefully, EasyOCR comes to our rescue. It's one of the best open-source Multilingual libraries for OCR. It supports 70+ languages currently and more will be added soon. Due to the open-source nature and python support, it's easy to add new languages in Easy OCR. It is Built on top of PyTorch, ResNet, CTC, and beam-search-based decoder.

How to use test it?

Step1. Install EasyOCR.

If you are using Google Colab use the below code,

!pip install easyocr --no-deps # Colab already has all dependencies

For installation in windows or other platforms, details are given in the official Github repository. For using Jupyter Notebook a virtual environment must be created with the dependencies installed using requirements.txt from this GitHub repository.

Step2. Create OCR Reader Object

# import library
import easyocr
#specify shortform of language you want to extract,
# I am using Hindi(hi) and English(en) here by list of language ids
reader = easyocr.Reader(['hi','en'])

In the above section, I have created a Reader object for the Hindi and English Language after importing the library (I used Local Language to show the multilingual nature of OCR)

The list of language ids we need to specify in the function argument can be found on this page

EasyOCR supports GPU. If we use GPU for inference then it will be faster compared to CPU.

Step3. Read Image(Optional)

# Read Image
im = PIL.Image.open("Hindi_fonts.png") #provide your image path
im
The image I used for my experiment

PIL can help us reading an image with its image module. we need to provide an image path in the circular brackets.

Step4. Text Recognition

The best thing about this library is it will provide us detected text in the form of a list. It is very easy to handle in python. It will be easy to automate the text recognition task because of the list output.

# Doing OCR. Get bounding boxes.
bounds = reader.readtext('Hindi_fonts.png', detail=0) #detail=0 argument will only give text in array
print("Output:")
print(bounds)

In the above block, we used readtext() method with detail=0 argument to specify we only want to extract text. We need to give image path as a first argument of the function

Output:
['-00',
'l0nE',
'भारत माता की जय',
'Bold ७००',
'भारत माता की जय',
'L०० Italic',
'l0nE',
'भारत माता की जय',
'Bold ७०० Italic',
'भारत माता की जय']

Note: EasyOCR also supports text detection(Getting Coordinates or Location of text in the image)

Step5. Text Detection

# Doing OCR. Get bounding boxes. bounds = reader.readtext('Hindi_fonts.png', detail=1) #detail=1 
print("Output:")
print(bounds)

If we specify details=1 in the readtext() argument. It will give us a detailed output list of tuples [(coordinates, detected text, confidence threshold)]

Output:
[([[49, 29], [71, 29], [71, 41], [49, 41]], '-00', 0.40365132689476013),
([[14, 30], [44, 30], [44, 38], [14, 38]], 'l0nE', 0.00010203746205661446),
([[12, 48], [206, 48], [206, 78], [12, 78]],
'भारत माता की जय',
0.0797141045331955),
([[13, 107], [59, 107], [59, 119], [13, 119]],
'Bold ७००',
0.3968919515609741),
([[11, 124], [224, 124], [224, 156], [11, 156]],
'भारत माता की जय',
0.0513167679309845),
([[49, 183], [95, 183], [95, 195], [49, 195]],
'L०० Italic',
0.10786763578653336),
([[14, 184], [44, 184], [44, 192], [14, 192]],
'l0nE',
0.00010203746205661446),
([[14, 200], [228, 200], [228, 232], [14, 232]],
'भारत माता की जय',
0.030513780191540718),
([[13, 259], [83, 259], [83, 273], [13, 273]],
'Bold ७०० Italic',
0.5405768752098083),
([[13, 275], [229, 275], [229, 311], [13, 311]],
'भारत माता की जय',
0.08109372109174728)]

Step6. Drow Bounding box

# Draw bounding boxes
def draw_boxes(image, bounds, color='yellow', width=2):
draw = ImageDraw.Draw(image)
for bound in bounds:
# iterate though all the tuples of output
p0, p1, p2, p3 = bound[0]
# get coordinates
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
return image

draw_boxes(im, bounds)

Using the above simple code we will able to draw bounding boxes on the text on Image with the help of coordinates of text we get in the earlier output.

The output of the above code

You can find this entire code compatible with the Colab Notebook on my Github.

Let's list out the pros and cons of this library

Pros:

  • Supports GPU and CPU
  • Build using AI
  • Supports Python
  • Detection as well as recognition support
  • Wide Range of Language(70+) support
  • Opensource
  • Easy to use

This point makes EasyOCR, the best OCR Library

Cons:

  • Slow CPU Inference
  • Issue while detecting few fonts and handwritten text
  • Not work well on Very Low-Quality Images
  • Text Extraction Accuracy decreases with lower Font Size

If you want to add your local language in Easy OCR. Follow the Guideline for the new language request mentioned in the Official Github Repository. It involves adding two files related to language and creating a Pull Request.

I hope you enjoyed this tutorial and found it helpful. Keep experimenting and learning!

--

--