EasyOCR (Optical Character Recognition) on Bacalhau
Introduction
In this example tutorial, we use Bacalhau and Easy OCR to digitize paper records or for recognizing characters or extract text data from images stored on IPFS/Filecoin or on the web. EasyOCR is a ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc. With easy OCR you use the pre-trained models or use your own fine-tuned model.
TD:LR
Using Bacalhau and Easy OCR to extract text data from images stored on the web.
Prerequisite
To get started, you need to install the Bacalhau client, see more information here
Running Easy OCR Locally
Install the required dependencies
Load the different example images
List all the images
To displaying an image from the list
Next, we create a reader to do OCR to get coordinates which represent a rectangle containing text and the text itself
Containerize your Script using Docker
:::tip You can skip this step and go straight to running a Bacalhau job :::
We will use the Dockerfile
that is already created in the Easy OCR repo. Use the command below to clone the repo
Build the Container
The docker build
command builds Docker images from a Dockerfile.
Before running the command replace;
hub-user with your docker hub username, If you don’t have a docker hub account follow these instructions to create docker account, and use the username of the account you created
repo-name with the name of the container, you can name it anything you want
tag this is not required but you can use the latest tag
Push the container
Next, upload the image to the registry. This can be done by using the Docker hub username, repo name, or tag.
Running a Bacalhau Job to Generate Easy OCR output
After the repo image has been pushed to Docker Hub, we can now use the container for running on Bacalhau. To submit a job, run the following Bacalhau command:
Since the model and the image aren't present in the container we will mount the image from an URL and the model from IPFS. You can find models to download from here. You can choose the model you want to use in this case we will be using the zh_sim_g2 model
Structure of the command
-i ipfs://bafybeibvc......
: Mounting the model from IPFS-i https://raw.githubusercontent.com.........
Mounting the Input Image from a URL--gpu 1
: Specifying the no of GPUsjsacex/easyocr
: Name of the Docker image
Breaking up the easyocr command
-- easyocr -l ch_sim en -f ./inputs/chinese.jpg --detail=1 --gpu=True
-l
: the name of the model which is ch_sim-f
: path to the input Image or directory--detail=1
: level of detail--gpu=True
: we set this flag to true since we are running inference on a GPU, if you run this on a CPU you set this to false
When a job is submitted, Bacalhau prints out the related job_id
. We store that in an environment variable so that we can reuse it later on.
Checking the State of your Jobs
Job status: You can check the status of the job using
bacalhau list
.
When it says Completed
, that means the job is done, and we can get the results.
Job information: You can find out more information about your job by using
bacalhau describe
.
Job download: You can download your job results directly by using
bacalhau get
. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory.
After the download has finished you should see the following contents in the results directory
Viewing your Job Output
To view the file, run the following command: