/
main.py
277 lines (198 loc) · 9.51 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import CGAN
import kivy
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
# to use buttons:
from kivy.uix.button import Button
from kivy.uix.switch import Switch
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
import sys
from kivy.core.window import Window
from kivy.config import Config
from kivy.uix.slider import Slider
from kivy.uix.filechooser import FileChooserListView
from kivy.utils import get_color_from_hex
kivy.require("1.10.1")
Config.set('graphics', 'width', '640')
Config.set('graphics', 'height', '800')
model_file_path = ''
alt_path = None
is_training = False
default_font_size = 50
class FirstPage(GridLayout):
# runs on initialization
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.next_button = Button(size=(640, 300), pos=(0, 400), text="Next", font_size=default_font_size)
self.next_button.bind(on_press=self.next)
self.add_widget(self.next_button)
self.choose_file_button = Button(size=(640, 300), pos=(0, 50), text="Choose weight file", font_size=default_font_size)
self.choose_file_button.bind(on_press=self.choose_file)
self.add_widget(self.choose_file_button)
def next(self, instance):
chat_app.screen_manager.current = 'MainMenu'
def choose_file(self, instance):
global alt_path
from plyer import filechooser
path = filechooser.open_file(title="Pick a ckpt file..",
filters=[("weight files", "*.ckpt*")])[0]
alt_path = path[:path.find("ckpt") + 4].replace('//', '/')
print(alt_path)
class MainPage(GridLayout):
# runs on initialization
def __init__(self, **kwargs):
super().__init__(**kwargs)
# self.cols = 1 # used for our grid
self.random_face_button = Button(size=(640, 240), pos=(0, 510), text="Generate Random Face", font_size=default_font_size)
self.random_face_button.bind(on_press=self.random_face)
# self.add_widget(Label()) # just take up the spot.
self.add_widget(self.random_face_button)
self.sketch_artist_button = Button(size=(640, 240), pos=(0, 260), text="Sketch artist", font_size=default_font_size)
self.sketch_artist_button.bind(on_press=self.sketch_artist)
# self.add_widget(Label()) # just take up the spot.
self.add_widget(self.sketch_artist_button)
# add our button.
self.face_manipulation_button = Button(size=(640, 240), pos=(0, 10), text="Face Manipulations", font_size=default_font_size)
self.face_manipulation_button.bind(on_press=self.face_manipulation)
# self.add_widget(Label()) # just take up the spot.
self.add_widget(self.face_manipulation_button)
def random_face(self, instance):
chat_app.screen_manager.current = 'RandomFace'
def sketch_artist(self, instance):
chat_app.screen_manager.current = 'SketchArtist'
def face_manipulation(self, instance):
chat_app.screen_manager.current = 'FaceManipulation'
class RamdomFacePage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.size = (640, 700)
self.cross_img = Image(source="", size=(640, 640), pos=(0, 120))
self.add_widget(self.cross_img)
self.noise_slider = Slider(min=-1, max=1, value=0, pos=(290, 40))
self.add_widget(self.noise_slider)
self.Generate_button = Button(text="Generate", size=(320, 60), pos=(0, 0), font_size=default_font_size)
self.Generate_button.bind(on_press=self.Generate_image)
self.add_widget(self.Generate_button)
self.back_button = Button(text="Back", size=(320, 60), pos=(320, 0), font_size=default_font_size)
self.back_button.bind(on_press=self.back)
self.add_widget(self.back_button)
self.switch = Switch(size=(50, 60), pos=(30, 50))
self.switch.bind(active=self.switch_training)
self.add_widget(self.switch)
def Generate_image(self, instance):
if cgan.test(is_training=is_training, desc='', noise=self.noise_slider.value, alt_path=alt_path):
# self.cross_img = Image(source=cgan.test_dir + cgan.version + '/test.jpg', size=(640, 640), pos=(0, 120))
self.cross_img.source = cgan.test_dir + cgan.version + '/test.jpg'
print(self.cross_img.source)
self.cross_img.reload()
def back(self, instance):
chat_app.screen_manager.current = 'MainMenu'
def switch_training(self, instance, value):
global is_training
is_training = value
class SketchArtistPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.size = (640, 760)
self.cross_img = Image(source="/home/lidor/Desktop/FDGAN/CGANv1/samples/epoch_60_batch_270.jpg", size=(640, 640),
pos=(0, 120))
self.add_widget(self.cross_img)
# self.add_widget(Label(font_size='30sp', text='facial features:', pos=(60, 45))) # widget #1, top left
self.input1 = TextInput(size=(640, 60), text="female with glasses and blond hair...", pos=(0, 60),
multiline=False)
self.add_widget(self.input1)
self.Generate_button = Button(text="Generate", size=(320, 60), pos=(0, 0), font_size=default_font_size)
self.Generate_button.bind(on_press=self.Generate_image)
self.add_widget(self.Generate_button)
self.back_button = Button(text="Back", size=(320, 60), pos=(320, 0), font_size=default_font_size)
self.back_button.bind(on_press=self.back)
self.add_widget(self.back_button)
self.noise_slider = Slider(min=-1, max=1, value=0)
self.add_widget(self.noise_slider)
def Generate_image(self, instance):
if cgan.test(noise=self.noise_slider.value, desc=self.input1.text, alt_path=alt_path):
if self.input1.text == "":
self.cross_img.source = cgan.test_dir + cgan.version + '/test.jpg'
else:
self.cross_img.source = cgan.test_dir + cgan.version + '/{}.jpg'.format(self.input1.text).replace(" ", '_')
self.cross_img.reload()
def back(self, instance):
chat_app.screen_manager.current = 'MainMenu'
class FaceManipulationPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.size = (640, 760)
cross_img = Image(source="/home/lidor/Desktop/FDGAN/CGANv1/samples/epoch_60_batch_270.jpg", size=(640, 640),
pos=(0, 120))
self.add_widget(cross_img)
# self.add_widget(Label(font_size='30sp', text='facial features:', pos=(60, 45))) # widget #1, top left
self.input1 = TextInput(size=(640, 60), text="female with glasses and blond hair...", pos=(0, 60),
multiline=False)
self.add_widget(self.input1)
self.Generate_button = Button(text="Generate", size=(320, 60), pos=(0, 0))
self.Generate_button.bind(on_press=self.Generate_image)
self.add_widget(self.Generate_button)
self.back_button = Button(text="Back", size=(320, 60), pos=(320, 0))
self.back_button.bind(on_press=self.back)
self.add_widget(self.back_button)
def Generate_image(self, instance):
input_description = self.input1.text
print(input_description)
def back(self, instance):
chat_app.screen_manager.current = 'MainMenu'
from kivy.graphics import Color, Rectangle
class EpicApp(App):
def build(self):
Window.size = (640, 700)
# We are going to use screen manager, so we can add multiple screens
# and switch between them
self.screen_manager = ScreenManager()
Window.bind(on_dropfile=self._on_file_drop)
self.first_page = FirstPage()
screen = Screen(name='FirstPage')
screen.add_widget(self.first_page)
self.screen_manager.add_widget(screen)
self.main_page = MainPage()
screen = Screen(name='MainMenu')
screen.add_widget(self.main_page)
self.screen_manager.add_widget(screen)
self.random_face_page = RamdomFacePage()
screen = Screen(name='RandomFace')
screen.add_widget(self.random_face_page)
self.screen_manager.add_widget(screen)
self.sketch_artist_page = SketchArtistPage()
screen = Screen(name='SketchArtist')
screen.add_widget(self.sketch_artist_page)
self.screen_manager.add_widget(screen)
self.face_manipulation_page = FaceManipulationPage()
screen = Screen(name='FaceManipulation')
screen.add_widget(self.face_manipulation_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
def _on_file_drop(self, window, file_path):
global alt_path
file_path = file_path.decode("utf-8")
if "ckpt" in file_path:
alt_path = file_path[:file_path.find("ckpt") + 4].replace('//', '/')
print(alt_path)
else:
print("Wrong file!")
return
def show_error(message):
chat_app.info_page.update_info(message)
chat_app.screen_manager.current = 'Info'
Clock.schedule_once(sys.exit, 10)
from kivy.core.window import Window
if __name__ == "__main__":
cgan = CGAN.Cgan(mode="shit")
cgan.build_model()
chat_app = EpicApp()
chat_app.run()