title: Changing label_img.py in inception image classifier to accept numpy arrays (opencv images) instead of jpg files
body: <p>I am building an image classifier using the tensorflow for poets inception pretrained model. My model is trained and it works well. However, when integrating it into my application, I have run into a problem. The existing code reads a jpg (or other image format) file and creates a tensor which it applies to the model. I want to input a numpy array (opencv image) instead of reading the image from disk.</p>
  
  <p>I have tried convert_to_tensor (<a href="https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor" rel="nofollow noreferrer">https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor</a>) but i am not able to use the result in the rest of the code.  Other methods refer to an older example from a deprecated repo that use something like </p>
  
  <p>tfImage = tf.gfile.FastGFile(imageFileWithPath, 'rb').read()
  run the network to get the predictions
  predictions = sess.run(finalTensor, {'DecodeJpeg/contents:0': tfImage})</p>
  
  <p>I have screwed up the indentation while pasting but you should be able to get the idea</p>
  
  <pre><code>def read_tensor_from_image_file(file_name, input_height=299, 
  input_width=299,
              input_mean=0, input_std=255):
  input_name = "file_reader"
  output_name = "normalized"
  
  if type(file_name) is str:
   file_reader = tf.read_file(file_name, input_name)
  if file_name.endswith(".png"):
    image_reader = tf.image.decode_png(file_reader, channels = 3,
                                       name='png_reader')
  elif file_name.endswith(".gif"):
    image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
                                                  name='gif_reader'))
  elif file_name.endswith(".bmp"):
    image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
  else:
    image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
                                        name='jpeg_reader')
  float_caster = tf.cast(image_reader, tf.float32)
  dims_expander = tf.expand_dims(float_caster, 0);
  resized = tf.image.resize_bilinear(dims_expander, [input_height, 
  input_width])
  
  elif type(file_name) is np.ndarray:
   tf_img = tf.convert_to_tensor(file_name)
   resized = tf.image.resize_bilinear(tf_img, [input_height, input_width])
   normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
  sess = tf.Session()
  result = sess.run(normalized)
  
  return result
  
  def classify(file_name,model_file = 
   "tf_files/retrained_graph.pb",label_file = 
   "tf_files/retrained_labels.txt",output_layer = "final_result"):
  
  
  
   input_height = 299
   input_width = 299
   input_mean = 128
   input_std = 128
   input_layer = "Mul"
  
   graph = load_graph(model_file)
  
   t = read_tensor_from_image_file(file_name,
                                input_height=input_height,
                                input_width=input_width,
                                input_mean=input_mean,
                                input_std=input_std)
  
  
   input_name = "import/" + input_layer
   output_name = "import/" + output_layer
   input_operation = graph.get_operation_by_name(input_name);
   output_operation = graph.get_operation_by_name(output_name);
  
  
  
   with tf.Session(graph=graph) as sess:
    start = time.time()
    results = sess.run(output_operation.outputs[0],
                    {input_operation.outputs[0]: t})
    end=time.time()
   results = np.squeeze(results)
  
  top_k = results.argsort()[-5:][::-1]
  labels = load_labels(label_file)
  
  print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
  template = "{} (score={:0.5f})"
  for i in top_k:
   print(template.format(labels[i], results[i]))
  </code></pre>
  
  <p>The above code works when given a path to a file like it was intended, but my modifications to read_tensor_from_image_file use a numpy array give errors. I suspect i need to replace 't' to change it to a tensor operator for output_operations. Any help is appreciated.</p>