-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (83 loc) · 3.23 KB
/
app.py
File metadata and controls
114 lines (83 loc) · 3.23 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from server.models.image import Image
from server.database.database import Database
from flask import Flask, render_template, request, json, jsonify
import codecs
import tensorflow as tf
import numpy as np
import os
DIR = 'server/cnn_saved_model/saved_model/'
LABELS_ = ["Airplane",
"Automobile",
"Bird",
"Cat",
"Deer",
"Dog",
"Frog",
"Hourse",
"Ship",
"Truck"]
app = Flask(__name__, template_folder="static/")
app.config['MONGO_DBNAME'] = 'gallery_instagram'
app.config['MONGO_URI'] = 'mongodb://pavlo_kuzina:silverok911@ds141406.mlab.com:41406/gallery_instagram'
app.secret_key = "secretverysecetkeyishere"
@app.before_first_request
def initiliae_database():
Database.initialize(app)
@app.route("/")
def index(images=[]):
return render_template("index.html", images=images)
@app.route("/api/get_images")
def get_by_label():
images = Database.get_all('images')
list_images = []
for img in images:
label_file = img['label']
fs_file = Database.FS.get(img["fields"])
base64_data = codecs.encode(fs_file.read(), 'base64')
image = base64_data.decode('utf-8')
img_str = "data:image/png;base64," + image
# jsonImg = json.dumps(img_str)
list_images.append({"url": img_str, "label": label_file})
return jsonify(images=list_images)
@app.route("/api/get_images/<string:label>")
def get_images_by_label(label):
batch_images = Image.get_by_label(label)
# return jsonify(images=batch_images)
return json.dumps(batch_images)
@app.route("/upload", methods=["POST", "GET"])
def upload_image():
img_file = request.files['img']
content_type = img_file.content_type
filename = img_file.filename
current_image_id = Image.save_to_mongo(img_file, content_type, filename, label='' )
picture = Database.FS.get(current_image_id)
saved_image = Image.find_image(current_image_id)
image = tf.image.decode_jpeg(picture.read(), channels=3)
image = tf.image.resize_images(image,[ 32, 32])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
tf.get_default_graph().as_graph_def()
image_value = sess.run([image])
reshaped_image = np.reshape(image_value[0], [1, 32, 32, 3]).astype(np.float32)
saver = tf.train.import_meta_graph(os.path.join(DIR,'model-ckpt-4900.meta'))
saver.restore(sess, os.path.join(DIR,"model-ckpt-4900"))
x = tf.get_collection('training_data_input')[0]
y_true = tf.get_collection('training_data_outpuy')[0]
y_predicted = tf.get_collection('prediction')[0]
keep_prob = tf.get_collection('keep_prob')[0]
pred = sess.run(y_predicted, feed_dict={x: reshaped_image , keep_prob: 1.0})
sess.close()
label_idx = np.argmax(pred[0])
label = LABELS_[label_idx]
saved_image.set_label(label)
recognizedImge = Database.FS.get(saved_image.fields)
base64_data = codecs.encode(recognizedImge.read(), 'base64')
image = base64_data.decode('utf-8')
img_str = "data:image/png;base64," + image
response = {
"url": img_str,
"label" : label
}
return json.dumps(response)
if __name__ == '__main__':
app.run(debug=True)