Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import pickle
|
3 |
+
import os
|
4 |
+
import numpy as np
|
5 |
+
import PIL.Image
|
6 |
+
import IPython.display
|
7 |
+
from IPython.display import Image
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
import cv2
|
12 |
+
|
13 |
+
sys.path.insert(0, "StyleGAN2-GANbanales")
|
14 |
+
|
15 |
+
import dnnlib
|
16 |
+
import dnnlib.tflib as tflib
|
17 |
+
|
18 |
+
##############################################################################
|
19 |
+
# Generation functions
|
20 |
+
|
21 |
+
def seed2vec(Gs, seed):
|
22 |
+
rnd = np.random.RandomState(seed)
|
23 |
+
return rnd.randn(1, *Gs.input_shape[1:])
|
24 |
+
|
25 |
+
def init_random_state(Gs, seed):
|
26 |
+
rnd = np.random.RandomState(seed)
|
27 |
+
noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
|
28 |
+
tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
|
29 |
+
|
30 |
+
def generate_image(Gs, z, truncation_psi, prefix="image", save=False, show=False):
|
31 |
+
# Render images for dlatents initialized from random seeds.
|
32 |
+
Gs_kwargs = {
|
33 |
+
'output_transform': dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
|
34 |
+
'randomize_noise': False
|
35 |
+
}
|
36 |
+
if truncation_psi is not None:
|
37 |
+
Gs_kwargs['truncation_psi'] = truncation_psi
|
38 |
+
|
39 |
+
label = np.zeros([1] + Gs.input_shapes[1][1:])
|
40 |
+
images = Gs.run(z, label, **Gs_kwargs) # [minibatch, height, width, channel]
|
41 |
+
|
42 |
+
if save == True:
|
43 |
+
path = f"{prefix}.png"
|
44 |
+
PIL.Image.fromarray(images[0], 'RGB').save(path)
|
45 |
+
|
46 |
+
if show == True:
|
47 |
+
return images[0]
|
48 |
+
|
49 |
+
##############################################################################
|
50 |
+
# Function concatenate
|
51 |
+
|
52 |
+
def concatenate(img_array):
|
53 |
+
|
54 |
+
zeros = np.zeros([256,256,3], dtype=np.uint8)
|
55 |
+
zeros.fill(255)
|
56 |
+
white_img = zeros
|
57 |
+
|
58 |
+
a = 1
|
59 |
+
for i in img_array:
|
60 |
+
cv2.imwrite('imagenes/' + str(a) + '.png', i)
|
61 |
+
a+=1
|
62 |
+
|
63 |
+
# 1 - 2 images
|
64 |
+
if len(img_array) <= 2:
|
65 |
+
row_img = img_array[0]
|
66 |
+
for i in img_array[1:]:
|
67 |
+
row_img = cv2.hconcat([row_img, i])
|
68 |
+
|
69 |
+
final_img = row_img
|
70 |
+
|
71 |
+
# 3 - 4 images
|
72 |
+
elif len(img_array) >= 3 and len(img_array) <= 4:
|
73 |
+
row1_img = img_array[0]
|
74 |
+
for i in img_array[1:2]:
|
75 |
+
row1_img = cv2.hconcat([row1_img, i])
|
76 |
+
|
77 |
+
cv2.imwrite('imagenes/row1.png', row1_img)
|
78 |
+
|
79 |
+
row2_img = img_array[2]
|
80 |
+
for i in img_array[3:]:
|
81 |
+
row2_img = cv2.hconcat([row2_img, i])
|
82 |
+
|
83 |
+
cv2.imwrite('imagenes/row2_before.png', row2_img)
|
84 |
+
|
85 |
+
for i in range(4-len(img_array)):
|
86 |
+
row2_img = cv2.hconcat([row2_img, white_img])
|
87 |
+
|
88 |
+
cv2.imwrite('imagenes/row2_after.png', row2_img)
|
89 |
+
|
90 |
+
final_img = cv2.vconcat([row1_img, row2_img])
|
91 |
+
|
92 |
+
# 5 - 6 images
|
93 |
+
elif len(img_array) >= 4 and len(img_array) <= 6:
|
94 |
+
row1_img = img_array[0]
|
95 |
+
for i in img_array[1:3]:
|
96 |
+
row1_img = cv2.hconcat([row1_img, i])
|
97 |
+
|
98 |
+
row2_img = img_array[3]
|
99 |
+
for i in img_array[4:]:
|
100 |
+
row2_img = cv2.hconcat([row2_img, i])
|
101 |
+
|
102 |
+
for i in range(6-len(img_array)):
|
103 |
+
row2_img = cv2.hconcat([row2_img, white_img])
|
104 |
+
|
105 |
+
final_img = cv2.vconcat([row1_img, row2_img])
|
106 |
+
|
107 |
+
# 7 - 9 images
|
108 |
+
elif len(img_array) >= 7:
|
109 |
+
row1_img = img_array[0]
|
110 |
+
for i in img_array[1:3]:
|
111 |
+
row1_img = cv2.hconcat([row1_img, i])
|
112 |
+
|
113 |
+
row2_img = img_array[3]
|
114 |
+
for i in img_array[4:6]:
|
115 |
+
row2_img = cv2.hconcat([row2_img, i])
|
116 |
+
|
117 |
+
row3_img = img_array[6]
|
118 |
+
for i in img_array[7:9]:
|
119 |
+
row3_img = cv2.hconcat([row3_img, i])
|
120 |
+
|
121 |
+
for i in range(9-len(img_array)):
|
122 |
+
row3_img = cv2.hconcat([row3_img, white_img])
|
123 |
+
|
124 |
+
final_img = cv2.vconcat([row1_img, row2_img])
|
125 |
+
final_img = cv2.vconcat([final_img, row3_img])
|
126 |
+
|
127 |
+
return final_img
|
128 |
+
|
129 |
+
##############################################################################
|
130 |
+
# Function initiate
|
131 |
+
def initiate(seed, n_imgs, text):
|
132 |
+
|
133 |
+
pkl_file = "networks/experimento_2.pkl"
|
134 |
+
tflib.init_tf()
|
135 |
+
|
136 |
+
with open(pkl_file, 'rb') as pickle_file:
|
137 |
+
_G, _D, Gs = pickle.load(pickle_file)
|
138 |
+
|
139 |
+
img_array = []
|
140 |
+
first_seed = seed
|
141 |
+
|
142 |
+
for i in range(seed, seed+n_imgs):
|
143 |
+
init_random_state(Gs, 10)
|
144 |
+
z = seed2vec(Gs, seed)
|
145 |
+
img = generate_image(Gs, z, 1.0, show=True)
|
146 |
+
|
147 |
+
img_array.append(img)
|
148 |
+
seed+=1
|
149 |
+
|
150 |
+
final_img = concatenate(img_array)
|
151 |
+
|
152 |
+
return final_img, "Imágenes generadas"
|
153 |
+
|
154 |
+
##############################################################################
|
155 |
+
# Gradio code
|
156 |
+
|
157 |
+
iface = gr.Interface(
|
158 |
+
fn=initiate,
|
159 |
+
inputs=[gr.inputs.Slider(0, 99999999, "image"), gr.inputs.Slider(1, 9, "images"), "text"],
|
160 |
+
outputs=["image", "text"],
|
161 |
+
examples=[
|
162 |
+
[40, 1, "Edificios al anochecer"],
|
163 |
+
[37, 1, "Fuente de día"],
|
164 |
+
[426, 1, "Edificios con cielo oscuro"],
|
165 |
+
[397, 1, "Edificios de día"],
|
166 |
+
[395, 1, "Edificios desde anfiteatro"],
|
167 |
+
[281, 1, "Edificios con luces encendidas"],
|
168 |
+
[230, 1, "Edificios con luces encendidas y vegetación"],
|
169 |
+
[221, 1, "Edificios con vegetación"],
|
170 |
+
[214, 1, "Edificios al atardecer con luces encendidas"],
|
171 |
+
[198, 1, "Edificio al anochecer con luces en el pasillo"]
|
172 |
+
],
|
173 |
+
title="GANbanales",
|
174 |
+
description="Una GAN para generar imágenes del campus universitario de Rabanales, Córdoba."
|
175 |
+
)
|
176 |
+
|
177 |
+
|
178 |
+
if __name__ == "__main__":
|
179 |
+
app, local_url, share_url = iface.launch(debug=True, share=True)
|