-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUI.py
331 lines (312 loc) · 16 KB
/
GUI.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import io
import os
import shutil
import PySimpleGUI as sg
from PIL import Image
import engine
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
class GUI:
def __init__(self):
self.action = None
self.refocus = None
self.viewpoint = None
self.data_path = None
self.slider_resolution = 3
self.output_temp = os.path.join(os.getcwd(), 'temp', 'out.png')
os.makedirs(os.path.dirname(self.output_temp), exist_ok=True)
def _update_refocus_state(self, values):
if values[0] != '':
self.refocus.motion_max = int(values[0])
if values[1] != '':
self.refocus.motion_min = int(values[1])
for i in [6, 5, 4, 3, 2]:
if values[i]:
self.slider_resolution = i - 1
break
self.refocus._interpolate(float(values['slider']))
b, g, r = cv.split(self.refocus.output_img)
im = cv.merge((r, g, b))
im = cv.resize(im, (400, 250))
plt.imsave(self.output_temp, im)
def _welcome_window(self):
sg.theme('Light Blue 2') # Add a touch of color
# All the stuff inside your window.
layout = [[sg.Text('Path (Video or Photo Album): '), ],
[sg.Button('Video'), sg.Button('Album')],
[sg.Checkbox('Right-to-Left?')],
[sg.Text('Which Kind of Application you want? choose one: ')],
[sg.Button('Refocusing'), sg.Button('Change Viewpoint')]]
# Create the Window
window = sg.Window('Welcome!', layout, element_justification='center', text_justification='center')
# Event Loop to process "events" and get the "values" of the inputs
self.src = ''
while True:
event, values = window.read()
# self.is_rtl = values[1]
self.is_rtl = values[0]
if event == None: # if user closes window or clicks cancel
break
elif event == 'Refocusing':
if not os.path.exists(self.src):
continue
self.refocus = engine.Engine()
self.refocus.refocus(self.src)
self.refocus._interpolate(150)
b, g, r = cv.split(self.refocus.output_img)
im = cv.merge((r, g, b))
im = cv.resize(im, (400, 250))
plt.imsave(self.output_temp, im)
self.action = 'Refocus'
break
elif event == 'Change Viewpoint':
if not os.path.exists(self.src):
continue
self.action = 'Viewpoint'
self.viewpoint = engine.Engine()
self.viewpoint.src = self.src
self.viewpoint.load_images()
self.viewpoint.init_viewpoint()
break
elif event == 'Album':
self.src = sg.popup_get_folder('Choose album directory')
elif event == 'Video':
self.src = sg.popup_get_file('Choose video file',
file_types=(("VIDEOS", ".mp4"),("VIDEOS", ".mov"),
("VIDEOS", ".flv"),("VIDEOS", ".mpeg") ,
("VIDEOS", ".mpg"),("VIDEOS", ".avi")))
window.close()
def _refocus_window(self):
sg.theme('Light Blue 2') # Add a touch of color
# All the stuff inside your window.
col = [[sg.Text('Object Motion ')],
[sg.Text('Nearest Object: '), sg.InputText()],
[sg.Text('Farthest Object: '), sg.InputText()],
[sg.Text('Sensitivity: ')],
[sg.CB('Highest'), sg.CB('High'), sg.CB('Normal'), sg.CB('Low'), sg.CB('Lowest')],
[sg.Button('Compute'), sg.Button('Save')]]
layout = [[sg.Text(' Refocusing ')],
[sg.Image(self.output_temp, key='image')],
[sg.Slider(range=(self.refocus.motion_min, self.refocus.motion_max), resolution=self.slider_resolution, default_value=self.refocus.motion, orientation='v', size=(8, 20), key='slider'), sg.Column(col)]]
# Create the Window
window = sg.Window('Refocus', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event == None: # if user closes window or clicks cancel
break
elif event == 'Compute':
self._update_refocus_state(values)
window.close()
self._refocus_window()
elif event == 'Save':
dst = sg.popup_get_file("Save output image to disk", title='Save Image',
save_as=True, default_extension=".png",
file_types=(("IMAGES", ".png"),("IMAGES", ".jpg")))
b, g, r = cv.split(self.refocus.output_img*255)
img = Image.fromarray(cv.merge((r, g,b)).astype(np.uint8))
img.save(dst)
window.close()
def _update_viewpoint(self, frame1, col1, frame2, col2, angle=None):
self.viewpoint.change_viewpoint(self.src, frame1, col1, frame2, col2, angle=angle, is_rtl=self.is_rtl)
w, h, _ = self.viewpoint.output_img.shape
b, g, r = cv.split(self.viewpoint.output_img)
w_out = 800
h_out = int(w*h/w_out)
img = Image.fromarray(np.dstack((r, g, b)), mode='RGB')
img.resize((w_out, h_out))
bio = io.BytesIO()
img.save(bio, format='PNG')
del img
return bio.getvalue()
def _viewpoint_display_window(self):
sg.theme('Light Blue 2') # Add a touch of color
# All the stuff inside your window.
w, h, _ = self.viewpoint.output_img.shape
b, g, r = cv.split(self.viewpoint.output_img)
w_out = 800
h_out = int(w*h/w_out)
img = Image.fromarray(np.dstack((r, g, b)), mode='RGB')
img.resize((h_out, w_out))
bio = io.BytesIO()
img.save(bio, format='PNG')
del img
layout = [[sg.Text('Change Viewpoint ')],
[sg.Button('Zoom In')],
[sg.Button('Previous'), sg.Image(data=bio.getvalue(), key='image'),
sg.Button('Next')],
[sg.Button('Viewpoint Left'), sg.Button('Zoom Out'),
sg.Button('Viewpoint Right')],
[sg.Text('First Frame:'), sg.InputText(size=(4, 4), default_text=f'{self.viewpoint.frame1+1}', key='frame1'),
sg.Text('Last Frame:'), sg.InputText(size=(4, 4), default_text=f'{self.viewpoint.frame2}', key='frame2'),
sg.Text(f'(between 1 and {self.viewpoint.num_frames}, inclusive)')],
[sg.Text('First column:'),
sg.InputText(size=(4, 4), default_text=f'{self.viewpoint.col1+1}', key='col1'),
sg.Text('Last column:'), sg.InputText(size=(4, 4), default_text=f'{self.viewpoint.col2}', key='col2'),
sg.Text(f'(between 1 and {self.viewpoint.width}, inclusive)')],
[sg.Button('Rotate Time Slice')],
[sg.Button('Save'), sg.Button('Update')]]
# Create the Window
window = sg.Window('Change Viewpoint', layout, element_justification='center', text_justification='center')
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event == None: # if user closes window or clicks cancel
break
elif event == 'Previous':
if self.viewpoint.frame1 == 0:
print("No previous frame")
else:
output = self._update_viewpoint(self.viewpoint.frame1 - 1, self.viewpoint.col1,
self.viewpoint.frame2 - 1, self.viewpoint.col2)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
elif event == 'Next':
num_frames = self.viewpoint.num_frames
if self.viewpoint.frame2 == num_frames:
print("No next frame")
else:
output = self._update_viewpoint(self.viewpoint.frame1 + 1, self.viewpoint.col1,
self.viewpoint.frame2 + 1, self.viewpoint.col2)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
elif event == 'Viewpoint Right':
if self.viewpoint.col1 == 0:
print("can't move right")
else:
output = self._update_viewpoint(self.viewpoint.frame1, self.viewpoint.col1 - 10,
self.viewpoint.frame2, self.viewpoint.col2 - 10)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
elif event == 'Viewpoint Left':
width = self.viewpoint.width
if self.viewpoint.col2 == width:
print("can't move left")
else:
output = self._update_viewpoint(self.viewpoint.frame1, self.viewpoint.col1 + 10,
self.viewpoint.frame2, self.viewpoint.col2 + 10)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
elif event == 'Zoom In':
diff = 10
col1 = self.viewpoint.col1 + diff
col2 = self.viewpoint.col2 - diff
if col1 > self.viewpoint.width:
col1 = self.viewpoint.width
elif col1 == self.viewpoint.width:
print("Can't zoom in anymore")
continue
if col2 < 0:
col2 = 0
elif col2 == 0:
print("Can't zoom in anymore")
continue
output = self._update_viewpoint(self.viewpoint.frame1, col1,
self.viewpoint.frame2, col2)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
elif event == 'Zoom Out':
diff = 10
col1 = self.viewpoint.col1 - diff
col2 = self.viewpoint.col2 + diff
if col2 > self.viewpoint.width:
col2 = self.viewpoint.width
if col1 < 0:
col1 = 0
if self.viewpoint.col2 != col2 or self.viewpoint.col1 != col1:
output = self._update_viewpoint(self.viewpoint.frame1, col1,
self.viewpoint.frame2, col2)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
else:
print("Can't zoom out anymore")
elif event == 'Update':
frame1 = int(values['frame1'])
frame2 = int(values['frame2'])
col1 = int(values['col1'])
col2 = int(values['col2'])
output = self._update_viewpoint(frame1, col1,
frame2, col2)
window['image'].Update(data=output)
elif event == 'Save':
dst = sg.popup_get_file("Save output image to disk",
title='Save Image', save_as=True,
default_extension=".png",
file_types=(("IMAGES", ".png"), ("IMAGES", ".jpg")))
if dst:
b, g, r = cv.split(self.viewpoint.output_img)
img = Image.fromarray(cv.merge((r, g, b)))
img.save(dst)
elif event == 'Rotate Time Slice':
val = sg.popup_get_text(f"Set time slice to N degrees, where N is an number in range [0,360)\n (0 is pushbroom panorama)", title="Rotate time slice angle")
if val is None:
continue
angle = float(val)
frame1 = int(values['frame1'])
frame2 = int(values['frame2'])
col1 = int(values['col1'])
col2 = int(values['col2'])
output = self._update_viewpoint(frame1, col1,
frame2, col2, angle)
window['image'].Update(data=output)
window['frame1'].Update(f'{self.viewpoint.frame1}')
window['frame2'].Update(f'{self.viewpoint.frame2}')
window['col1'].Update(f'{self.viewpoint.col1}')
window['col2'].Update(f'{self.viewpoint.col2}')
window.close()
def _viewpoint_selection_window(self):
sg.theme('Light Blue 2') # Add a touch of color
# All the stuff inside your window.
self.viewpoint.src = self.src
self.viewpoint.load_images()
layout = [[sg.Text(' Change Viewpoint ')],
[sg.Text('First Frame:'), sg.InputText(size=(4, 4), default_text='1'), sg.Text(f'(Of {self.viewpoint.num_frames} Frames, inclusive)'),
sg.Text('Last Frame:'), sg.InputText(size=(4, 4), default_text=str(self.viewpoint.num_frames)),
sg.Text(f'(Of {self.viewpoint.num_frames} Frames)')],
[sg.Text('First Column:'), sg.InputText(size=(4, 4), default_text='1'), sg.Text(f'(Of {self.viewpoint.width} columns, inclusive)'),
sg.Text('Last Column:'), sg.InputText(size=(4, 4), default_text=str(self.viewpoint.width)),
sg.Text(f'(Of {self.viewpoint.width} columns)')],
[sg.Button('Compute')]]
# Create the Window
window = sg.Window('Change Viewpoint', layout, text_justification='center', element_justification='center')
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
frame1 = int(values[0]) - 1
frame2 = int(values[1])
col1 = int(values[2]) - 1
col2 = int(values[3])
if event == None: # if user closes window or clicks cancel
break
elif event == 'Compute':
self._update_viewpoint(frame1, col1, frame2, col2)
break
window.close()
def loop(self):
self._welcome_window()
if self.action == 'Refocus':
self._refocus_window()
elif self.action == 'Viewpoint':
self._viewpoint_selection_window()
self._viewpoint_display_window()
def __del__(self):
shutil.rmtree(os.path.dirname(self.output_temp), ignore_errors=True)