Git Product home page Git Product logo

vietnameseocr's Introduction

VietnameseOCR - Vietnamese Optical Character Recognition

Apply Deep Learning ( CNN networks ) to train a model uses for recognizing Vietnamese characters, it works well with Latin characters.

Dataset in big image ( 10.000 samples, 2800 x 2800 pixel)

Requirements

python 3.6.5
tensorflow
PIL

Model Summary

Layer Shape Kernel Stride Padding
INPUT [28, 28, 1]
CONV1 [3, 3, 32, 32] [1, 1] SAME
POOL1
CONV2 [3, 3, 32, 64] [1, 1] SAME
POOL2
CONV3 [3, 3, 64, 128] [1, 1] SAME
POOL3
FC1
FC2 [625, 190]

Results

Training...

......
Epoch: 38 cost = 0.312853018
Epoch: 39 cost = 0.298816641
Epoch: 40 cost = 0.293328794

Evaluation
------------------------------
Test Accuracy: 0.974867469544

Training

Prepare dataset for training

git clone https://github.com/miendinh/VietnameseOCR.git
cd VietnameseOCR/data/train/characters
unzip dataset.zip

Let's train.

python train.py

Create you own dataset

Prepare fonts for generating text-image
  • You could add more fonts
cd VietnameseOCR/data/train/characters
unzip google.zip
unzip win.zip
Create font list, then save it in fonts.list
source ./list.sh
Generate Text Image Dataset
python generate_data.py

Play with pretrained model

  • All pretrained weights of model is save to file vocr.brain
  • Let's test with random character in dataset
python predict.py

Further working

  • Character classification.
  • Dataset augmentation.
  • Improve accuracy.
  • Text location.
  • Text recognition.
  • Apply NLP for spell checking.

References

  1. STN-OCR: A single Neural Network for Text Detection and Text Recognition
  2. Automatic Dataset Augmentation
  3. VGG16 implementation in TensorFlow
  4. Vietnamese Dict (VietOCR3)

Author mien.hust [at] gmail [dot] com

vietnameseocr's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vietnameseocr's Issues

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 179: character maps to <undefined>

E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master>python predict.py
Done shuffle dataset!
Traceback (most recent call last):
File "predict.py", line 31, in
characters = DataGenerator().get_list_characters()
File "E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master\generate_dataset.py", line 35, in get_list_characters
for r in cf:
File "C:\Users\DELL\Anaconda3\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 179: character maps to

Using this data set for STN-OCR

Hello @miendinh

I have seen your introduction to include the STN-OCR and I am researching about it
I have 1 question is can i use your dataset for training STN-OCR and how can i config it to match STN-OCR

Thank you!

After training getting these lots of errors

E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master>python train.py
2018-10-08 11:08:33.824304: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Done shuffle dataset!
WARNING:tensorflow:From E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master\model.py:123: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See tf.nn.softmax_cross_entropy_with_logits_v2.

Training...
Epoch: 01 cost = 6.886777639
Epoch: 02 cost = 5.465170531
Epoch: 03 cost = 5.437775334
Epoch: 04 cost = 5.423924650
Epoch: 05 cost = 5.408748928
Epoch: 06 cost = 5.372752797
Epoch: 07 cost = 4.914058918
Epoch: 08 cost = 4.291090060
Epoch: 09 cost = 3.637334304
Epoch: 10 cost = 2.976472312
Epoch: 11 cost = 2.508758491
Epoch: 12 cost = 2.038119041
Epoch: 13 cost = 1.585568683
Epoch: 14 cost = 1.298715894
Epoch: 15 cost = 1.099691014
Epoch: 16 cost = 0.957667396
Epoch: 17 cost = 0.862675724
Epoch: 18 cost = 0.787035389
Epoch: 19 cost = 0.721239509
Epoch: 20 cost = 0.664576864
Epoch: 21 cost = 0.622479543
Epoch: 22 cost = 0.580160727
Epoch: 23 cost = 0.549833339
Epoch: 24 cost = 0.516690385
Epoch: 25 cost = 0.490436088
Epoch: 26 cost = 0.463827107
Epoch: 27 cost = 0.438583468
Epoch: 28 cost = 0.417645685
Epoch: 29 cost = 0.402110323
Epoch: 30 cost = 0.379509842
Epoch: 31 cost = 0.369229207
Epoch: 32 cost = 0.358186685
Epoch: 33 cost = 0.343487738
Epoch: 34 cost = 0.333546966
Epoch: 35 cost = 0.324272395
Epoch: 36 cost = 0.314847696
Epoch: 37 cost = 0.303071091
Epoch: 38 cost = 0.298055138
Epoch: 39 cost = 0.287200178
Epoch: 40 cost = 0.277841558
Training finished!
2018-10-08 13:18:32.800596: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at save_restore_v2_ops.cc:109 : Not found: Failed to create a directory: ; No such file or directory
Traceback (most recent call last):
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1292, in _do_call
return fn(*args)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1277, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1367, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: ; No such file or directory
[[{{node save/SaveV2}} = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, beta1_power, beta2_power, conv1/biases, conv1/biases/Adam, conv1/biases/Adam_1, conv1/weights, conv1/weights/Adam, conv1/weights/Adam_1, conv2/biases, conv2/biases/Adam, conv2/biases/Adam_1, conv2/weights, conv2/weights/Adam, conv2/weights/Adam_1, conv3/biases, conv3/biases/Adam, conv3/biases/Adam_1, conv3/weights, conv3/weights/Adam, conv3/weights/Adam_1, fc1/Variable, fc1/Variable/Adam, fc1/Variable/Adam_1, fc1w, fc1w/Adam, fc1w/Adam_1, fc2/Variable, fc2/Variable/Adam, fc2/Variable/Adam_1, fc2w, fc2w/Adam, fc2w/Adam_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1433, in save
{self.saver_def.filename_tensor_name: checkpoint_file})
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 887, in run
run_metadata_ptr)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1110, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1286, in _do_run
run_metadata)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1308, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: ; No such file or directory
[[{{node save/SaveV2}} = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, beta1_power, beta2_power, conv1/biases, conv1/biases/Adam, conv1/biases/Adam_1, conv1/weights, conv1/weights/Adam, conv1/weights/Adam_1, conv2/biases, conv2/biases/Adam, conv2/biases/Adam_1, conv2/weights, conv2/weights/Adam, conv2/weights/Adam_1, conv3/biases, conv3/biases/Adam, conv3/biases/Adam_1, conv3/weights, conv3/weights/Adam, conv3/weights/Adam_1, fc1/Variable, fc1/Variable/Adam, fc1/Variable/Adam_1, fc1w, fc1w/Adam, fc1w/Adam_1, fc2/Variable, fc2/Variable/Adam, fc2/Variable/Adam_1, fc2w, fc2w/Adam, fc2w/Adam_1)]]

Caused by op 'save/SaveV2', defined at:
File "train.py", line 9, in
vocr.train(learning_rate=0.001, training_epochs=40, batch_size=1000, keep_prob=0.7)
File "E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master\model.py", line 160, in train
saver = tf.train.Saver()
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1094, in init
self.build()
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1106, in build
self._build(self._filename, build_save=True, build_restore=True)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1143, in _build
build_save=build_save, build_restore=build_restore)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 784, in _build_internal
save_tensor = self._AddSaveOps(filename_tensor, saveables)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 284, in _AddSaveOps
save = self.save_op(filename_tensor, saveables)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 202, in save_op
tensors)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1803, in save_v2
shape_and_slices=shape_and_slices, tensors=tensors, name=name)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3272, in create_op
op_def=op_def)
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1768, in init
self._traceback = tf_stack.extract_stack()

NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directory
[[{{node save/SaveV2}} = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, beta1_power, beta2_power, conv1/biases, conv1/biases/Adam, conv1/biases/Adam_1, conv1/weights, conv1/weights/Adam, conv1/weights/Adam_1, conv2/biases, conv2/biases/Adam, conv2/biases/Adam_1, conv2/weights, conv2/weights/Adam, conv2/weights/Adam_1, conv3/biases, conv3/biases/Adam, conv3/biases/Adam_1, conv3/weights, conv3/weights/Adam, conv3/weights/Adam_1, fc1/Variable, fc1/Variable/Adam, fc1/Variable/Adam_1, fc1w, fc1w/Adam, fc1w/Adam_1, fc2/Variable, fc2/Variable/Adam, fc2/Variable/Adam_1, fc2w, fc2w/Adam, fc2w/Adam_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "train.py", line 9, in
vocr.train(learning_rate=0.001, training_epochs=40, batch_size=1000, keep_prob=0.7)
File "E:\Shezartech\OCR\Vietnamese\VietnameseOCR-master\model.py", line 161, in train
save_path = saver.save(self.sess, "viet_ocr_brain.ckpt")
File "C:\Users\DELL\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1450, in save
raise exc
ValueError: Parent directory of viet_ocr_brain.ckpt doesn't exist, can't save.

Checking custom image

I want to check whether the model is working fine or not for the image beside this dataset for this I'm using below code but I'm getting a very bad prediction, Am I doing anything wrong?

import numpy as np
from PIL import Image

img = Image.open('d.png').convert('RGBA')

new_width = 14
new_height = 14
img = img.resize((new_width, new_height), Image.ANTIALIAS)

arr = np.array(img)

flat_arr = arr.ravel()

type(flat_arr)

prob, idx = predict(flat_arr)

print('Predicted: ', characters[idx], ' with probability = ', prob, '%')
print('-' * 10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.