forked from rahulharlalka/image_captioning
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (55 loc) · 1.98 KB
/
app.py
File metadata and controls
70 lines (55 loc) · 1.98 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
# Importing utility python libraries
import os
from werkzeug.utils import secure_filename
# Importing flask libraries
from flask import Flask, flash, request, redirect
from flask import render_template as rt
# Importing ML libraries
import keras
import tensorflow as tf
from tensorflow.python.keras.backend import set_session
# Importing caption generator function
from predict_main import generate_caption
# Setting session instances
graph = tf.get_default_graph()
session = keras.backend.get_session()
init = tf.global_variables_initializer()
session.run(init)
# Initializing flask application
app = Flask(__name__)
# Configuring flask settings
UPLOAD_FOLDER = "static/uploads/"
app.secret_key = "secret_key"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg"])
# Function for checking uploaded file extension
def allowed_file(f):
return "." in f and f.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
# Setting route for handling 'GET' requests
@app.route("/")
def index():
return rt("index.html")
# Setting route for handling 'POST' requests
@app.route("/", methods=["POST"])
def upload_image():
if "file" not in request.files:
return redirect(request.url)
else:
file = request.files["file"]
if file.filename == "":
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
filepath = "./static/uploads/" + filename
global graph
with graph.as_default():
set_session(session)
caption = generate_caption(filepath)
return rt("index.html", filename=filepath, caption=caption)
else:
flash("Allowed image types are : .png, .jpg, .jpeg")
return redirect(request.url)
if __name__ == "__main__":
app.run()