-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (58 loc) · 1.94 KB
/
Copy pathapp.py
File metadata and controls
72 lines (58 loc) · 1.94 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
# Imports
import os
import torch
from flask import Flask, render_template, send_file
from main import DEVICE, Z_DIM, NGF, N_CHANNELS, IMAGE_GEN_BATCH_SIZE
from src.dcgan import Generator
from src.train import generate_test_images
from src.vis_utils import visualise_fake_images
# Set constants
MODEL_PATH = 'checkpoint/gl-epoch150/checkpoint_epoch_130.pth.tar' # Replace with appropriate checkpoint file path
SAVE_DIR = 'images/app'
app = Flask(__name__)
# Ensure directory exists
os.makedirs(SAVE_DIR, exist_ok=True)
# Initialise generator
generator = Generator(
z_dim=Z_DIM,
ngf=NGF,
n_channels=N_CHANNELS,
device=DEVICE,
checkpoint_path=MODEL_PATH
)
def uniquify_filename(save_dir):
"""Generate a unique filename by appending a number as required."""
i = 0
while True:
if i > 0:
filename = f'impressgan_images_{i}.png'
else:
filename = f'impressgan_images.png'
if not os.path.exists(os.path.join(save_dir, filename)):
return filename
i += 1
def generate_images():
"""Generates a grid of fake images, and saves it with a unique filename. Returns the path to the saved image."""
fixed_noise = torch.randn(IMAGE_GEN_BATCH_SIZE, Z_DIM, 1, 1, device=DEVICE)
fake_images = generate_test_images(
generator=generator,
fixed_noise=fixed_noise
)
filename = uniquify_filename(save_dir=SAVE_DIR)
path_image = os.path.join(SAVE_DIR, filename)
visualise_fake_images(
fake_images=fake_images,
path_image=path_image
)
return path_image
@app.route('/', methods=['GET'])
def index():
"""Render the main page."""
return render_template('index.html')
@app.route('/generate', methods=['POST'])
def generate():
"""Generate images and return the generated image."""
image_path = generate_images()
return send_file(image_path, mimetype='image/png')
if __name__ == '__main__':
app.run(debug=True)