harishhirthi commited on
Commit
675e00b
·
verified ·
1 Parent(s): 9994352

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -70
app.py CHANGED
@@ -1,70 +1,70 @@
1
- import os
2
- from flask import Flask, render_template, request
3
- from werkzeug.utils import secure_filename
4
- import torch
5
- from models.Transform_net import TransFormerNet
6
- import utils as utils
7
-
8
- UPLOAD_FOLDER = os.path.join('static', 'uploads') # Folder to save the uploaded input image
9
- os.makedirs(UPLOAD_FOLDER, exist_ok = True)
10
- ALLOWED_EXTENSIONS = {'jpg'}
11
- app = Flask(__name__)
12
- app.config['UPLOAD'] = UPLOAD_FOLDER
13
-
14
- binaries_path = os.path.join('models', 'binaries')
15
- img_save_path = os.path.join('static', 'output_images') # Folder to save the stylized image
16
- Inference_config = dict()
17
- Inference_config['save_folder'] = img_save_path
18
- os.makedirs(img_save_path, exist_ok = True)
19
-
20
- def allowed_file(filename):
21
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
22
-
23
- def get_default_device() -> None:
24
- """Use GPU if available, else CPU"""
25
- if torch.cuda.is_available():
26
- for i in range(torch.cuda.device_count()):
27
- print(torch.cuda.get_device_properties(i))
28
- return torch.device('cuda')
29
- else:
30
- return torch.device('cpu')
31
-
32
- """Function to stylize the image and save it in a folder"""
33
- def stylize(Inference_config, model_name):
34
- device = get_default_device()
35
-
36
- """Initializing Transformer model that stylizes the image"""
37
- style_net = TransFormerNet().to(device)
38
- trained_state = torch.load(os.path.join(binaries_path, model_name))
39
- binary = trained_state['state_dict']
40
- style_net.load_state_dict(binary, strict = True)
41
- style_net.eval()
42
-
43
- with torch.no_grad():
44
- content_img_path = Inference_config['image_path']
45
- content_img = utils.process_img(content_img_path, target_shape = 700)
46
- content_img = content_img.to(device)
47
- stylized_img = style_net(content_img).detach().cpu().numpy().squeeze(0)
48
- utils.save_and_display(Inference_config, stylized_img)
49
-
50
- """Function to get a input image and show the stylized image"""
51
- @app.route('/', methods = ['GET', 'POST'])
52
- def upload_file():
53
- if request.method == 'POST':
54
- file = request.files['image']
55
- model_name = request.form.get('Modelname') # Used to get the model name.
56
- if file and allowed_file(file.filename):
57
- filename = secure_filename(file.filename)
58
- Inference_config['content_img_name'] = filename
59
- Inference_config['image_path'] = os.path.join(app.config['UPLOAD'], filename)
60
- file.save(os.path.join(app.config['UPLOAD'], filename))
61
- stylize(Inference_config, model_name)
62
- image = os.path.join(Inference_config['save_folder'], f"Stylized-image-{filename.split('.')[0]}.jpg")
63
- return render_template('render.html', image = image)
64
- return render_template('render.html')
65
-
66
- if __name__ == '__main__':
67
- app.run(debug = True, host = "0.0.0.0")
68
-
69
-
70
-
 
1
+ import os
2
+ from flask import Flask, render_template, request
3
+ from werkzeug.utils import secure_filename
4
+ import torch
5
+ from models.Transform_net import TransFormerNet
6
+ import utils as utils
7
+
8
+ UPLOAD_FOLDER = os.path.join('static', 'uploads') # Folder to save the uploaded input image
9
+ os.makedirs(UPLOAD_FOLDER, exist_ok = True)
10
+ ALLOWED_EXTENSIONS = {'jpg'}
11
+ app = Flask(__name__)
12
+ app.config['UPLOAD'] = UPLOAD_FOLDER
13
+
14
+ binaries_path = os.path.join('models', 'binaries')
15
+ img_save_path = os.path.join('static', 'output_images') # Folder to save the stylized image
16
+ Inference_config = dict()
17
+ Inference_config['save_folder'] = img_save_path
18
+ os.makedirs(img_save_path, exist_ok = True)
19
+
20
+ def allowed_file(filename):
21
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
22
+
23
+ def get_default_device() -> None:
24
+ """Use GPU if available, else CPU"""
25
+ if torch.cuda.is_available():
26
+ for i in range(torch.cuda.device_count()):
27
+ print(torch.cuda.get_device_properties(i))
28
+ return torch.device('cuda')
29
+ else:
30
+ return torch.device('cpu')
31
+
32
+ """Function to stylize the image and save it in a folder"""
33
+ def stylize(Inference_config, model_name):
34
+ device = get_default_device()
35
+
36
+ """Initializing Transformer model that stylizes the image"""
37
+ style_net = TransFormerNet().to(device)
38
+ trained_state = torch.load(os.path.join(binaries_path, model_name), map_location = torch.device('cpu'))
39
+ binary = trained_state['state_dict']
40
+ style_net.load_state_dict(binary, strict = True)
41
+ style_net.eval()
42
+
43
+ with torch.no_grad():
44
+ content_img_path = Inference_config['image_path']
45
+ content_img = utils.process_img(content_img_path, target_shape = 700)
46
+ content_img = content_img.to(device)
47
+ stylized_img = style_net(content_img).detach().cpu().numpy().squeeze(0)
48
+ utils.save_and_display(Inference_config, stylized_img)
49
+
50
+ """Function to get a input image and show the stylized image"""
51
+ @app.route('/', methods = ['GET', 'POST'])
52
+ def upload_file():
53
+ if request.method == 'POST':
54
+ file = request.files['image']
55
+ model_name = request.form.get('Modelname') # Used to get the model name.
56
+ if file and allowed_file(file.filename):
57
+ filename = secure_filename(file.filename)
58
+ Inference_config['content_img_name'] = filename
59
+ Inference_config['image_path'] = os.path.join(app.config['UPLOAD'], filename)
60
+ file.save(os.path.join(app.config['UPLOAD'], filename))
61
+ stylize(Inference_config, model_name)
62
+ image = os.path.join(Inference_config['save_folder'], f"Stylized-image-{filename.split('.')[0]}.jpg")
63
+ return render_template('render.html', image = image)
64
+ return render_template('render.html')
65
+
66
+ if __name__ == '__main__':
67
+ app.run(debug = True, host = "0.0.0.0")
68
+
69
+
70
+