¿Cómo puedo usar el ejemplo de Keras OCR?

encontréexamples/image_ocr.py lo que parece ser para OCR. Por lo tanto, debería ser posible darle al modelo una imagen y recibir texto. Sin embargo, no tengo idea de cómo hacerlo. ¿Cómo le doy al modelo una nueva imagen? ¿Qué tipo de preprocesamiento es necesario?

Lo que hice

Instalación de las dependencias:

Instalar en pccairocffi: sudo apt-get install python-cairocffiInstalar en pceditdistance: sudo -H pip install editdistanceCambiotrain para devolver el modelo y guardar el modelo entrenado.Ejecute el script para entrenar al modelo.

Ahora tengo unmodel.h5. ¿Que sigue?

Verhttps://github.com/MartinThoma/algorithms/tree/master/ML/ocr/keras para mi código actual Sé cómo cargar el modelo (ver más abajo) y esto parece funcionar. El problema es que no sé cómo alimentar nuevos escaneos de imágenes con texto al modelo.

Preguntas secundarias relacionadas¿Qué es el CTC?Clasificación temporal conexionista?¿Existen algoritmos que detecten de manera confiable la rotación de un documento?¿Existen algoritmos que detecten de manera confiable líneas / bloques de texto / tablas / imágenes (por lo tanto, hacen una segmentación razonable)? Supongo que la detección de bordes con histogramas de suavizado e línea ya funciona razonablemente bien para eso.Lo que probé
#!/usr/bin/env python

from keras import backend as K
import keras
from keras.models import load_model
import os

from image_ocr import ctc_lambda_func, create_model, TextImageGenerator
from keras.layers import Lambda
from keras.utils.data_utils import get_file
import scipy.ndimage
import numpy

img_h = 64
img_w = 512
pool_size = 2
words_per_epoch = 16000
val_split = 0.2
val_words = int(words_per_epoch * (val_split))
if K.image_data_format() == 'channels_first':
    input_shape = (1, img_w, img_h)
else:
    input_shape = (img_w, img_h, 1)

fdir = os.path.dirname(get_file('wordlists.tgz',
                                origin='http://www.mythic-ai.com/datasets/wordlists.tgz', untar=True))

img_gen = TextImageGenerator(monogram_file=os.path.join(fdir, 'wordlist_mono_clean.txt'),
                             bigram_file=os.path.join(fdir, 'wordlist_bi_clean.txt'),
                             minibatch_size=32,
                             img_w=img_w,
                             img_h=img_h,
                             downsample_factor=(pool_size ** 2),
                             val_split=words_per_epoch - val_words
                             )
print("Input shape: {}".format(input_shape))
model, _, _ = create_model(input_shape, img_gen, pool_size, img_w, img_h)

model.load_weights("my_model.h5")

x = scipy.ndimage.imread('example.png', mode='L').transpose()
x = x.reshape(x.shape + (1,))

# Does not work
print(model.predict(x))

esto da

2017-07-05 22:07:58.695665: I tensorflow/core/common_runtime/gpu/gpu_device.cc:996] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX TITAN Black, pci bus id: 0000:01:00.0)
Traceback (most recent call last):
  File "eval_example.py", line 45, in <module>
    print(model.predict(x))
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1567, in predict
    check_batch_axis=False)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 106, in _standardize_input_data
    'Found: array with shape ' + str(data.shape))
ValueError: The model expects 4 arrays, but only received one array. Found: array with shape (512, 64, 1)

Respuestas a la pregunta(3)

Su respuesta a la pregunta