This repo shall track related work or steps required with training custom recognition models using necessary library. After gone through all the steps, will be able to create an OCR with the capability to recognize custom font that are required.
Suggest using two virtualenv for data generation and model trainning instead of installing all in one virtualenv.
python -m venv /path/to/new/virtual/environment
To generate our own dataset for training recognition model we are using TextRecognitionDataGenerator
- Activate one of the created virtualenv by running:
source .venv/bin/activate - Install the pypi package:
pip install trdg - Next clone the repo in your local machine
git clone https://github.com/Belval/TextRecognitionDataGenerator - Place the font that has been collected in
trdg/fonts/latin - Check the function available by running or for more information:
python trdg/run.py -h - Refer to documentation
Minor changes is needed when generating text with random function.
In line 482 of run.py change
os.path.join(args.output_dir, "labels.txt"), "w", encoding="utf8" to os.path.join(args.output_dir, "labels.csv"), "w", encoding="utf8"
In line 489 change f.write("{} {}\n".format(file_name, label) to f.write("{},{}\n".format(file_name, label)
In line 120 of string_generator.py remove comma symbol in order to avoid unnecessary error while trainning recognition model.
Sample
python TextRecognitionDataGenerator/trdg/run.py -c 2000 -w 5 -f 200 -l en -b 1 -ft TextRecognitionDataGenerator/trdg/fonts/latin/PIXEARG_.TTF -na 2 -rs -cs 3
Download split.py
Install library
pip install scikit-learn
pip install pandas
Change the original file value to the image folder that had been generated and csv file location to image folder path.
Run the split.py file
python split.py
Two new folder will be created: train_folder and test_folder
- Create a new folder and create new virtualenv
- Clone EasyOCR repo
git clone https://github.com/JaidedAI/EasyOCR/blob/master/custom_model.md - Install library
pip intall easyocr - Copy and Paste train_folder content in all_data folder
cp /path/to/train_folder/* /path/to/EasyOCR/trainer/all_data/train/ - Copy and Paste test_folder content in all_data folder
cp /path/to/test_folder/* /path/to/EasyOCR/trainer/all_data/test/ - Make changes on EasyOCR/trainer/config_files/*.yaml
- experiment_name change the value to your experiment name
- valid_data: all_data/test
- saved_model: 'Path to pretrained model eg: .pth'
- select_data: train
- imgH & imgW try any value to test it out. Not necessary 200600
Sample refer to sample.yaml - Run trainer.ipynb from top to bottom.
If any changes to the config file name, please change the value in opt to new file name
Model will be saved under saved_models with the experiment name.
Three files are needed in order to use custom model.
- A .pth file that we get from training.
- A .yaml file that define input
- A .py file define the neural network These three files have to share the same name (i.e. exp.pth, exp.yaml,exp.py) that you will then use to call your model with EasyOCR API.
Place .pth file in model directory and for .yaml and .py place in user_directory.
import easyocr, cv2,numpy as np, os
`reader=easyocr.Reader(['en'], recog_network='exp_13',gpu=False, model_storage_directory="/path/to/model", user_network_directory='/path/to/user_network')
img=cv2.imread("/path/to/A1.png")
result =reader.readtext(img, detail=1)
Always refer to Link for more information.