Digitizing handwritten vaccination records in Nigeria with AI and ML

Digitizing handwritten vaccination records in Nigeria with AI and MLPiotr KrosniakBlockedUnblockFollowFollowingMay 17IntroductionWorking in UNICEF Nigeria as Polio Data Scientist I came to the problem of errors in vaccination cards that were delivered by 20,000 Polio Volunteers and even bigger issue number of the vaccination cards to check.

Simple digitizing and giving everyone a tablet was not an option.

After research, I decided to use AI/ML and computer vision to “read” information from the cards and then provide feedback mechanism about the most common errors and predict correct information.

In this tutorial, You will see how to achieve this and what are the results and recommendations for future optimizing.

I will be using python libraries TensorFlow and OpenCV mainly and some support libraries.

InstallationInstallation using TensorFlow varies with the OS and hardware you are going to use.

Refer this article for general instructions hereFor this tutorial, I will be using the following packages:OS: Linux _X64 (Arch Linux)Python package manager: Anaconda or Miniconda (Installation instructions here)CUDA 10.

ymlRecreate the environment on another machineTo recreate the environment on another machine, use this after creating and activating the environment in another machineconda env create -f <environment-name>.

ymlRecognizing text using TensorflowThe first thing to understand is that the accuracy of this model is dependant on the samples you are going to use for training.

More samples are needed for better accuracy.

This also means that if you need to recognize written text by multiple people, you have to include sufficient text samples written by themThe entire tutorial code is uploaded in the GitHub repository.

Clone this repository using git clone if you need the final codegit clone git@github.

zip fil into the same folder (<root>/model)Then, run the training in the src directory.

The script here will build upon the previously trained model and improve its accuracy based on your datapython main.

py — trainThis may take a long while to run the training — more like 16–18 hours without a GPU.

The script runs the training batches called epochs till there is no appreciable increase in text recognition accuracy between consecutive batches.

After completion, You will see files generated under the model folder.

Fig 2: The Model folder with the TensorFlow trained modelsThe snapshots and checkpoints will be generated as aboveRunning the OCR scriptNow that the model is generated in the code folder, let us run the code to get a text from our images.

Make sure you have your input files in the Input folderFig 3: Input folder with your imagesRun the code in the src folder(inside a terminal)Python Demo.

pyThe code will run on the input images.

You will see the output in the terminal as belowFig 4: Sample Terminal output on running inferenceOnce the code has completed running, outputs will be present in the Output folder:Fig 5: Output folder after running OCR scriptThe folders will contain the table cells with each cell as a separate image.

We will get to use these generated images to further improve our accuracy in the next sectionHowever, based on your current models, the recognized text will be saved in the CSV files with the same names as the input images.

These CSV files can be opened in spreadsheet software like Microsoft Excel or google sheetsImproving the AccuracyThe individual table cells from your images are saved as separate images in the Output folder.

These images can help the model recognize the handwriting -> text mapping for your own data set.

Typically, this is necessary if you have a lot of uncommon English words like names or the handwriting style in the images differ largely from the IAM default dataset the model was trained onTo use these table cell images to train your dataset, follow the steps below:Preprocess the images to make it IAM dataset compliant.

This is absolutely necessary for the script to get properly trained with your images.

On a higher level, the following steps are performed:a.

Thickening faint lines in the textb.

Removing extra spaces around the word with word segmentation (refer this code)c.

Improving contrast through a technique for thresholdingRenaming and copying the images in the data folder in the format used by the Dataloader.

This is also the file name of the image you are mappingok -> result of word segmentationok: word was correctlyer: segmentation of word can be bad154 -> graylevel to binarize the line containing this word.

This is the contrast stretching/Thresholding step.

1 -> number of components for this word408 768 27 51 -> bounding box around this word in x,y,w,h formatAT -> the grammatical tag for this word, see thefile tagset.

txt for an explanationA -> the transcription for this word describing the text contents of the imageThe above will custom tailor the model for your images.

To improve the accuracy of the model itself, refer the improving accuracy section of this pageExplanation of the approachThe code perform three major steps:Match template and rotate imageRecognize rows in the table and cropRecognize text using python-tensorflowThe recognition algorithm is based on the simplified version of HTR system of text recognition.

If you are interested in the mechanism, you can refer this paperIt consists of 5 CNN layers, 2 RNN (LSTM) layers and the CTC loss and decoding layerThe input image is a gray-value image and has a size of 128×325 CNN layers map the input image to a feature sequence of size 32×2562 LSTM layers with 256 units propagate information through the sequence and map the sequence to a matrix of size 32×80.

Each matrix-element represents a score for one of the 80 characters at one of the 32 time-stepsThe CTC layer either calculates the loss value given the matrix and the ground-truth text (when training), or it decodes the matrix to the final text with best path decoding or beam search decoding (when inferring)Batch size is set to 50Fig 5: Mechanisms involved in the OCR step using tensorflowConclusionFollowing this tutorial, you now have a way to automate the digitization of handwritten texts in tabular format tables.

Countless hours can be saved once you train the model to recognize your handwriting and customize according to your needs.

However, be careful as the recognition is not 100% accurate.

So, a round of high-level proofreading after the spreadsheet generation might be needed before you are ready to share the final spreadsheetReference:Code Reference:https://github.