Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import tensorflow as tf, sys
  2. import simplejson as json
  3. from flask import Flask, jsonify
  4.  
  5. app = Flask(__name__)
  6.  
  7.  
  8. def start_tf_session():
  9.  
  10. # Loads label file, strips off carriage return
  11. label_lines = [line.rstrip() for line
  12. in tf.gfile.GFile("tfmodel/retrained_labels_cats.txt")]
  13.  
  14. # Unpersists graph from file
  15. with tf.gfile.FastGFile("tfmodel/retrained_graph_cats.pb", 'rb') as f:
  16. graph_def = tf.GraphDef()
  17. graph_def.ParseFromString(f.read())
  18. _ = tf.import_graph_def(graph_def, name='')
  19.  
  20. print('Tensorflow session (for cats) created.')
  21. return tf.Session()
  22.  
  23.  
  24. # Create the "Tensor"
  25. def get_tensor(sess):
  26. print('softmax_tensor (for cats) got gotten.')
  27. return sess.graph.get_tensor_by_name('final_result:0')
  28.  
  29.  
  30. # This function gets called by the web service. It runs the Tensor model.
  31. def image_labels(image_name):
  32.  
  33. # Load the image from disk
  34. print('loading image')
  35. image_data = tf.gfile.FastGFile(image_name, 'rb').read()
  36.  
  37. # Use TensorFlow!
  38. print('running prediction')
  39. predictions = sess.run(softmax_tensor, \
  40. {'DecodeJpeg/contents:0': image_data})
  41.  
  42.  
  43. print('sorting and organizing results')
  44. # Sort to show labels of first prediction in order of confidence
  45. top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
  46.  
  47. results = dict()
  48.  
  49. # Save the results from the TensorFlow function as a Dictionary
  50. for node_id in top_k:
  51. human_string = label_lines[node_id]
  52. score = predictions[0][node_id]
  53. print('%s (score = %.5f)' % (human_string, score))
  54. results[human_string] = str(round(score,5))
  55.  
  56. return results
  57.  
  58. # Start the app!
  59. print('Starting Tensorflow (for cats)')
  60.  
  61. # Start a TensorFlow session, and save the session as a variable
  62. sess = start_tf_session()
  63.  
  64. # Get a reference to the softmax tensor with the newly created Session
  65. softmax_tensor = get_tensor(sess)
  66.  
  67. # Load the labels file from the custom trained model
  68. label_lines = [line.rstrip() for line in tf.gfile.GFile("tfmodel/retrained_labels_cats.txt")]
  69.  
  70.  
  71. # Create a web service to analyze the raspberry pi image
  72. @app.route("/lola")
  73. def lola():
  74. print('Analyzing image for Lola.')
  75.  
  76. # Get the labels associated with the input image
  77. labels = image_labels("rpicam.jpg")
  78.  
  79. # Return a JSON object of labels and probabilities
  80. print('returning json')
  81. return jsonify(**labels)
  82.  
  83.  
  84. # Run Flask web service
  85. if __name__ == "__main__":
  86. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement