-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
119 lines (90 loc) · 3.75 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
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
115
116
117
118
119
# Importing the required libraries and modules
import os
from flask import Flask, render_template, request, redirect, url_for
from model import NST
# Global Variables
ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'JPG']
UPLOAD_FOLDER = os.path.join('static', 'Imgs')
CONTENT_IMAGE_PATH = ''
STYLE_IMAGE_PATH = ''
STYLIZED_IMAGE_PATH = ''
app = Flask(__name__)
@app.after_request
def add_header(r):
"""
Function which prevents Chrome from caching the static resources
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route('/')
def index():
"""
Function which renders the home page
"""
if os.path.isfile(CONTENT_IMAGE_PATH):
os.remove(CONTENT_IMAGE_PATH)
if os.path.isfile(STYLE_IMAGE_PATH):
os.remove(STYLE_IMAGE_PATH)
if os.path.isfile(STYLIZED_IMAGE_PATH):
os.remove(STYLIZED_IMAGE_PATH)
return render_template('index.html')
@app.route('/img_upload/<image>', methods=['GET', 'POST'])
def img_upload(image):
"""
Function which is used for uploading images from our local file system
"""
render_image, paths = {}, {}
if image == 'content':
img_name = "content_img"
else:
img_name = "style_img"
if request.method == 'POST':
if img_name not in request.files:
print('No File Part')
return render_template('index.html')
img = request.files[img_name]
if img.filename == '':
print('No File Selected')
return render_template('index.html')
file_extension = img.filename.split('.')[-1]
if img and file_extension in ALLOWED_EXTENSIONS:
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
if image == 'content':
global CONTENT_IMAGE_PATH
CONTENT_IMAGE_PATH = os.path.join(UPLOAD_FOLDER, f"{img_name}.{file_extension}")
img.save(CONTENT_IMAGE_PATH)
elif image == 'style':
global STYLE_IMAGE_PATH
STYLE_IMAGE_PATH = os.path.join(UPLOAD_FOLDER, f"{img_name}.{file_extension}")
img.save(STYLE_IMAGE_PATH)
print(f"File Uploaded Successfully - {img_name}")
render_image = {'content_image': os.path.isfile(CONTENT_IMAGE_PATH),
'style_image': os.path.isfile(STYLE_IMAGE_PATH)}
paths = {'content_image': CONTENT_IMAGE_PATH,
'style_image': STYLE_IMAGE_PATH,
'stylized_image': STYLIZED_IMAGE_PATH}
return render_template('index.html', render_image = render_image, paths=paths)
@app.route('/result', methods=["POST"])
def transfer_style():
"""
Function which applies the style to the content image
"""
render_image, paths = {}, {}
if os.path.isfile(CONTENT_IMAGE_PATH) and os.path.isfile(STYLE_IMAGE_PATH):
stylized_image = NST(CONTENT_IMAGE_PATH, STYLE_IMAGE_PATH)
global STYLIZED_IMAGE_PATH
STYLIZED_IMAGE_PATH = os.path.join(UPLOAD_FOLDER, 'stylized_image.png')
stylized_image.save(STYLIZED_IMAGE_PATH)
render_image = {'content_image': os.path.isfile(CONTENT_IMAGE_PATH),
'style_image': os.path.isfile(STYLE_IMAGE_PATH),
'stylized_image': os.path.isfile(STYLIZED_IMAGE_PATH)}
paths = {'content_image': CONTENT_IMAGE_PATH,
'style_image': STYLE_IMAGE_PATH,
'stylized_image': STYLIZED_IMAGE_PATH}
return render_template('index.html', render_image=render_image, paths=paths)
if __name__ == "__main__":
app.run(debug=True)