-
Notifications
You must be signed in to change notification settings - Fork 47
/
app.py
79 lines (58 loc) · 2.25 KB
/
app.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
'''
Flask backend for image-to-image search pipeline.
'''
#Import dependencies
import os
import tensorflow as tf
import numpy as np
import pickle
import config as cfg
from model import ImageSearchModel
from inference import simple_inference
#import Flask dependencies
from flask import Flask, request, render_template, send_from_directory
#Set root dir
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
#Define our model
model = ImageSearchModel(learning_rate=cfg.LEARNING_RATE, image_size=cfg.IMAGE_SIZE, number_of_classes=cfg.NUMBER_OF_CLASSES)
#Start tf.Session()
session = tf.Session()
session.run(tf.global_variables_initializer())
#Restore session
saver = tf.train.Saver()
saver.restore(session, 'saver/model_epoch_5.ckpt')
#Load training set vectors
with open('hamming_train_vectors.pickle', 'rb') as f:
train_vectors = pickle.load(f)
#Load training set paths
with open('train_images_pickle.pickle', 'rb') as f:
train_images_paths = pickle.load(f)
#Define Flask app
app = Flask(__name__, static_url_path='/static')
#Define apps home page
@app.route("/") #www.image-search.com/
def index():
return render_template("index.html")
#Define upload function
@app.route("/upload", methods=["POST"])
def upload():
upload_dir = os.path.join(APP_ROOT, "uploads/")
if not os.path.isdir(upload_dir):
os.mkdir(upload_dir)
for img in request.files.getlist("file"):
img_name = img.filename
destination = "/".join([upload_dir, img_name])
img.save(destination)
#inference
result = np.array(train_images_paths)[simple_inference(model, session, train_vectors, os.path.join(upload_dir, img_name), cfg.IMAGE_SIZE)]
result_final = []
for img in result:
result_final.append("images/"+img.split("/")[-1]) #example: dataset/train/0_frog.png -> [dataset, train, 0_frog.png] -> [-1] = 0_frog.png
return render_template("result.html", image_name=img_name, result_paths=result_final[:-2]) #added [:-2] just to have equal number of images in the result page per row
#Define helper function for finding image paths
@app.route("/upload/<filename>")
def send_image(filename):
return send_from_directory("uploads", filename)
#Start the application
if __name__ == "__main__":
app.run(port=5000, debug=True)