forked from thedevankit/Grape-Plant-Disease
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.py
48 lines (47 loc) · 1.75 KB
/
simple.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
import os
from flask import Flask, render_template, request
import tensorflow as tf, sys
from PIL import Image
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
return render_template("upload.html")
@app.route("/upload", methods=['POST'])
def upload():
target = os.path.join(APP_ROOT,'images/')
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("file"):
print(file)
filename = file.filename
destination = "/".join([target, filename])
print(destination)
file.save(destination)
img=destination
image_data = tf.gfile.FastGFile(img, 'rb').read()
label_lines = [line.rstrip() for line
in tf.gfile.GFile("retrained_labels.txt")]
with tf.gfile.FastGFile("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]
name=[]
scores=[]
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
score=score*100
if (score>20):
print('%s (score = %.5f)' % (human_string, score))
name.append(human_string)
scores.append(score)
return render_template("complete.html",name=name,scores=scores)
if __name__ == "__main__":
app.run(port=4555, debug=True)