title: How can I use Keras OCR example to inference a new image?
body: <p>I am trying to implement a OCR project by Keras.So I try to learn from <a href="https://github.com/keras-team/keras/blob/master/examples/image_ocr.py" rel="nofollow noreferrer">Keras OCR example</a>.I have use my own train data to train a new model and get the .H5 modelfile.
  Now I want to test a new image to see my model performance，so I code a
   test.py like this:  </p>
  
  <pre><code>from keras.models import Model
  import cv2
  from keras.preprocessing.image import img_to_array
  import numpy as np
  from keras.models import load_model
  from keras import backend as K
  from allNumList import alphabet
  
  def labels_to_text(labels):
      ret = []
      for c in labels:
          if c == len(alphabet):  # CTC Blank
              ret.append("")
          else:
              ret.append(alphabet[c])
      return "".join(ret)
  
  def decode_predict_ctc(out, top_paths = 1):
      results = []
      beam_width = 5
      if beam_width &lt; top_paths:
        beam_width = top_paths
      for i in range(top_paths):
        lables = K.get_value(K.ctc_decode(out, input_length=np.ones(out.shape[0])*out.shape[1],
                             greedy=False, beam_width=beam_width, top_paths=top_paths)[0][i])[0]
        text = labels_to_text(lables)
        results.append(text)
      return results
  
  def test(modelPath,testPicTest):
      img=cv2.imread(testPicTest)
      img=cv2.resize(img,(128,64))
      img=img_to_array(img)
      img=np.array(img,dtype='float')/255.0
      img=np.expand_dims(img, axis=0)
      img=img.swapaxes(1,2)   
  
      model=load_model(modelPath,custom_objects = {'&lt;lambda&gt;': lambda y_true, y_pred: y_pred})
      net_out_value = model.predict(img)
      top_pred_texts = decode_predict_ctc(net_out_value)
      return top_pred_texts
  
  result=test(r'D:\code\testAndExperiment\py\KerasOcr\weights.h5',r'D:\code\testAndExperiment\py\KerasOcr\test\avo.jpg') 
  print(result)  
  </code></pre>
  
  <p>but I get a error like this:</p>
  
  <pre><code>Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 4 array(s), but instead got the following list of 1 arrays: [array([[[[1., 1., 1.],          [1., 1., 1.],          [1., 1., 1.],          ...,          [1., 1., 1.],          [1., 1., 1.],          [1., 1., 1.]],          [[1., 1., 1.],          [1., 1., 1.],...
  </code></pre>
  
  <p>I have references some material：<br>
  <a href="https://stackoverflow.com/a/49537697/10689350">https://stackoverflow.com/a/49537697/10689350</a><br>
  <a href="https://www.dlology.com/blog/how-to-train-a-keras-model-to-recognize-variable-length-text/" rel="nofollow noreferrer">https://www.dlology.com/blog/how-to-train-a-keras-model-to-recognize-variable-length-text/</a><br>
  <a href="https://stackoverflow.com/questions/46461701/how-to-predict-the-results-for-ocr-using-keras-image-ocr-example">How to predict the results for OCR using keras image_ocr example?</a>   </p>
  
  <p>some answer show that we should use 4 inputs <code>[input_data, labels, input_length, label_length]</code> in training but besides <code>input_data</code>, everything else is information used only for calculating the loss,so in testing maybe use the input_data is enough.So I just use a picture without <code>labels, input_length, label_length</code>.But I get the error above.</p>
  
  <p>I am confused about if the model needs 4 inputs or 1 in testing?<br>
  It doesn't seem reasonable to require 4 inputs during the testing process.and now I have model.h5,what should I do next?<br>
  Thanks in advance.</p>
  
  <p>My code is Here:<a href="https://github.com/hqabcxyxz/KerasOCR/tree/master" rel="nofollow noreferrer">https://github.com/hqabcxyxz/KerasOCR/tree/master</a></p>