title: How can I use the Keras OCR example?
body: <p>I found <a href="https://github.com/fchollet/keras/blob/master/examples/image_ocr.py" rel="noreferrer"><code>examples/image_ocr.py</code></a> which seems to for OCR. Hence it should be possible to give the model an image and receive text. However, I have no idea how to do so. How do I feed the model with a new image? Which kind of preprocessing is necessary? </p>
  
  <h2>What I did</h2>
  
  <p>Installing the depencencies:</p>
  
  <ul>
  <li>Install <a href="https://pypi.python.org/pypi/cairocffi" rel="noreferrer"><code>cairocffi</code></a>: <code>sudo apt-get install python-cairocffi</code></li>
  <li>Install <code>editdistance</code>: <code>sudo -H pip install editdistance</code></li>
  <li>Change <code>train</code> to return the model and save the trained model.</li>
  <li>Run the script to train the model.</li>
  </ul>
  
  <p>Now I have a <code>model.h5</code>. What's next?</p>
  
  <p>See <a href="https://github.com/MartinThoma/algorithms/tree/master/ML/ocr/keras" rel="noreferrer">https://github.com/MartinThoma/algorithms/tree/master/ML/ocr/keras</a> for my current code. I know how to load the model (see below) and this seems to work. The problem is that I don't know how to feed new scans of images with text to the model.</p>
  
  <h2>Related side questions</h2>
  
  <ul>
  <li>What is CTC? <a href="http://www.cs.toronto.edu/~graves/icml_2006.pdf" rel="noreferrer">Connectionist Temporal Classification</a>?</li>
  <li>Are there algorithms which reliably detect the rotation of a document?</li>
  <li>Are there algorithms which reliably detect lines / text blocks / tables / images (hence make a reasonable segmentation)? I guess edge detection with smoothing and line-wise histograms already works reasonably well for that?</li>
  </ul>
  
  <h2>What I tried</h2>
  
  <pre><code>#!/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))
  </code></pre>
  
  <p>this gives</p>
  
  <pre><code>2017-07-05 22:07:58.695665: I tensorflow/core/common_runtime/gpu/gpu_device.cc:996] Creating TensorFlow device (/gpu:0) -&gt; (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 &lt;module&gt;
      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)
  </code></pre>