This repository accompanies the blog post Using TensorFlowTM to create your own handwriting recognition engine.
This project uses the MNIST tutorials from the TensorFlow website. The two tutorials, the beginner tutorial and the expert tutorial, use different deep learning models. The python scripts ending with _1 use the model from the beginner tutorial. The scripts ending with _2 use the model from the advanced tutorial. As expected scripts using the model from the expert tutorial give better results.
This projects consists of four scripts:
- create_model_1.py – creates a model model.ckpt file based on the beginners tutorial.
- create_model_1.py – creates a model model2.ckpt file based on the expert tutorial.
- predict_1.py – uses the model.ckpt (beginners tutorial) file to predict the correct integer form a handwritten number in a .png file.
- predict_2.py – uses the model2.ckpt (expert tutorial) file to predict the correct integer form a handwritten number in a .png file.
The following Python libraries are required.
- sys - should be installed by default
- tensorflow - TensorFlow
- PIL – Pillow
Of course TensorFlow needs to be installed. The TensorFlow website has a good manual .
The Python Image Library (PIL) is no longer available. Luckily there is a good fork called Pillow. Installing is as easy as:
sudo pip install Pillow
Or look at the Pillow documentation for other installation options.
The easiest way the use the scripts is to put all four scripts in the same folder. If TensorFlow is installed correctly the images to train the model are downloaded automatically.
Running is based on the steps:
- create the model file
- create an image file containing a handwritten number
- predict the integer
The easiest way is to cd to the directory where the python files are located. Then run:
python create_model_1.py
or
python create_model_2.py
to create the model based on the MNIST beginners tutorial (model_1) or the model based on the expert tutorial (model_2).
You have to create a PNG file that contains a handwritten number. The background has to be white and the number has to be black. Any paint program should be able to do this. Also the image has to be auto cropped so that there is no border around the number.
The easiest way again is to put the image file from the previous step (step 2) in the same directory as the python scripts and cd to the directory where the python files are located.
The predict scripts require one argument: the file location of the image file containing the handwritten number. For example when the image file is ‘number1.png’ and is in the same location as the script, run:
python predict_1.py "predict_img/number1.png"
or
python predict_2.py "predict_img/number1.png"
The first script, predict_1.py, uses the model.ckpt file created by the create_model_1.py script. The second script, predict_2.py, uses the model2.ckpt file created by the create_model_2.py script.