-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapplication.py
83 lines (66 loc) · 2.16 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask
from flask import request
app = Flask(__name__)
import os
import uuid
import cv2
import sys
import tensorflow as tf
import json
import preprocess
@app.route('/', methods=['GET'])
def hello_world():
return "hello world\n"
@app.route('/', methods=['POST'])
def post_img():
result = {}
try:
f = open('img.jpg', 'wb')
f.write(request.get_data())
f.close()
roi = preprocess.process('img.jpg')
cv2.imwrite('cropped.jpg', roi)
image_data = tf.gfile.FastGFile('cropped.jpg', 'rb').read()
label_lines = [line.rstrip() for line in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
best_guess = top_k[0]
guesses = []
result['error'] = False
result['guesses'] = []
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
result['guesses'].append({
'name': human_string,
'confidence': "%0.5f" % score
})
except Exception as e:
result = {
"error" : True,
"guesses" : []
}
app.logger.warning(e)
return json.dumps(result) + "\n"
@app.route('/store', methods=['POST'])
def store_img():
if not os.path.exists('saved'):
os.mkdir('saved')
filename = 'saved/' + str(uuid.uuid4()) + ".jpg"
f = open(filename, 'wb')
f.write(request.get_data())
f.close()
result = {
'error': False,
'name': filename,
'confidence': ""
}
return json.dumps(result) + "\n"
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')