File size: 12,125 Bytes
247a0eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import flet as ft
from flet import (
    ElevatedButton,
    FilePicker,
    FilePickerResultEvent,
    Page,
    Row,
    Text,
    icons,
)
import os
import torch
import logging

from transparent_background.utils import *
from transparent_background.Remover import *

logging.basicConfig(level=logging.WARN)
logging.getLogger("flet_runtime").setLevel(logging.WARN)

options = {
    'output_type':'rgba',
    'mode':'base',
    'device':get_backend(),
    'r' : 0, 
    'g' : 0, 
    'b' : 0,
    'color' : "[0, 0, 0]",
    'ckpt':None,
    'threshold':None,
    'reverse': False,
    'resize': 'static',
    'format': None,
    'source':None,
    'dest':None,
    'use_custom':False,
    'jit':False,
    'abort':False,
}

def is_float(str):
    if str is None:
        return False
    try:
        tmp = float(str)
        return True
    except ValueError:
        return False

def main(page):
    def theme_changed(e):
        page.theme_mode = (
            ft.ThemeMode.DARK
            if page.theme_mode == ft.ThemeMode.LIGHT
            else ft.ThemeMode.LIGHT
        )
        page.update()

    def checkbox_changed(e):
        options['jit'] = jit_check.value
        options['reverse'] = reverse_check.value
        page.update()

    def dropdown_changed(e):
        options['output_type'] = type_dropdown.value
        options['mode'] = mode_dropdown.value
        options['device'] = device_dropdown.value
        options['resize'] = resize_dropdown.value
        # options['format'] = format_dropdown.value

        if options['output_type'] == 'custom' and not options['use_custom']:
            page.insert(1, ft.Row([r_field, g_field, b_field]))
            options['use_custom']=True

        elif options['output_type'] != 'custom' and options['use_custom']:
            options['use_custom']=False
            page.remove_at(1)

        output_text.value = 'Type: {}, Mode: {}, Device: {}, Threshold: {}, Resize: {}, Format: {}'.format(options['output_type'], options['mode'], options['device'], options['threshold'], options['resize'], options['format'])
        page.update()

    def color_changed(e):
        options['r'] = int(r_field.value) if len(r_field.value) > 0 and r_field.value.isdigit() else 0
        options['g'] = int(g_field.value) if len(g_field.value) > 0 and g_field.value.isdigit() else 0
        options['b'] = int(b_field.value) if len(b_field.value) > 0 and b_field.value.isdigit() else 0
        options['color'] = str([options['r'], options['g'], options['b']])
        output_text.value = 'Type: {}, Mode: {}, Device: {}, Threshold: {}, Resize: {}, Format: {}'.format(options['output_type'], options['color'], options['device'], options['threshold'], options['resize'], options['format'])
        page.update()

    def threshold_changed(e):
        options['threshold'] = float(threshold_field.value) if len(threshold_field.value) > 0 and is_float(threshold_field.value) else None
        options['threshold'] = None if is_float(options['threshold']) and (options['threshold'] < 0 or options['threshold'] > 1) else options['threshold']
        output_text.value = 'Type: {}, Mode: {}, Device: {}, Threshold: {}, Resize: {}, Format: {}'.format(options['output_type'], options['mode'], options['device'], options['threshold'], options['resize'], options['format'])
        page.update()
    
    def format_changed(e):
        options['format'] = format_field.value if format_field.value.endswith(IMG_EXTS) or format_field.value.endswith(VID_EXTS) else None
        output_text.value = 'Type: {}, Mode: {}, Device: {}, Threshold: {}, Resize: {}, Format: {}'.format(options['output_type'], options['mode'], options['device'], options['threshold'], options['resize'], options['format'])
        page.update()

    def pick_files_result(e: FilePickerResultEvent):
        file_path.update()
        options['source'] = e.files[0].path if e.files else 'Not Selected'
        file_path.value = options['source']
        file_path.update()
        if options['dest'] is None:
            options['dest'] = os.path.split(options['source'])[0]
            dest_path.value = options['dest']
            dest_path.update()

    # Open directory dialog
    def get_directory_result(e: FilePickerResultEvent):
        options['source'] = e.path if e.path else 'Not Selected'
        file_path.value = options['source']
        file_path.update()
        if options['dest'] is None:
            options['dest'] = os.path.split(options['source'])[0]
            dest_path.value = options['dest']
            dest_path.update()

    def get_dest_result(e: FilePickerResultEvent):
        options['dest'] = e.path if e.path else 'Not Selected'
        dest_path.value = options['dest']
        dest_path.update()

    def process(e):
        output_type = options['output_type']
        output_type = options['color'] if output_type == 'custom' else output_type
        options['abort'] = False
        entry_point(output_type, options['mode'], options['device'], options['ckpt'], options['source'], options['dest'], options['jit'], options['threshold'], options['resize'], options['format'], options['reverse'], progress_ring, page, preview, preview_out, options)

    def click_abort(e):
        options['abort'] = True
        page.update()

    page.window_width = 1000
    page.window_height = 650
    page.window_resizable = False

    page.theme_mode = ft.ThemeMode.LIGHT
    c = ft.Switch(label="Dark mode", on_change=theme_changed)

    output_text = ft.Text(color=ft.colors.BLACK)
    output_text.value = 'Type: {}, Mode: {}, Device: {}, Threshold: {}, Resize: {}, Format: {}'.format(options['output_type'], options['mode'], options['device'], options['threshold'], options['resize'], options['format'])
    output_text_container = ft.Container(
                    content=output_text,
                    margin=10,
                    padding=10,
                    bgcolor=ft.colors.GREEN_100,
                    border_radius=10,
                )

    jit_check = ft.Checkbox(label="use torchscript", value=False, on_change=checkbox_changed)
    reverse_check = ft.Checkbox(label="reverse", value=False, on_change=checkbox_changed)

    type_dropdown = ft.Dropdown(
        label='type',
        width=200,
        hint_text='output type',
        on_change=dropdown_changed,
        options=[
            ft.dropdown.Option("rgba"),
            ft.dropdown.Option("map"),
            ft.dropdown.Option("green"),
            ft.dropdown.Option("white"),
            ft.dropdown.Option("blur"),
            ft.dropdown.Option("overlay"),
            ft.dropdown.Option("custom"),
        ],
    )
    type_dropdown.value = options['output_type']

    resize_dropdown = ft.Dropdown(
        label='resize',
        width=200,
        hint_text='resize method',
        on_change=dropdown_changed,
        options=[
            ft.dropdown.Option("static"),
            ft.dropdown.Option("dynamic"),
        ],
    )
    resize_dropdown.value = options['resize']

    Remover() # init once

    cfg_path = os.environ.get('TRANSPARENT_BACKGROUND_FILE_PATH', os.path.abspath(os.path.expanduser('~')))
    home_dir = os.path.join(cfg_path, ".transparent-background")
    configs = load_config(os.path.join(home_dir, "config.yaml"))

    mode_dropdown = ft.Dropdown(
        label='mode',
        width=150,
        hint_text='mode',
        on_change=dropdown_changed,
        options=[ft.dropdown.Option(key) for key in configs.keys()],
    )
    mode_dropdown.value = options['mode']

    device_options = [ft.dropdown.Option("cpu")] 
    device_options += [ft.dropdown.Option("cuda:{}".format(i)) for i in range(torch.cuda.device_count())]
    device_options += ['mps:0'] if torch.backends.mps.is_available() else []

    device_dropdown = ft.Dropdown(
        label='device',
        width=150,
        hint_text='device',
        on_change=dropdown_changed,
        options=device_options
    )
    device_dropdown.value=options['device']

    r_field = ft.TextField(width=60, label='R', on_change=color_changed)
    g_field = ft.TextField(width=60, label='G', on_change=color_changed)
    b_field = ft.TextField(width=60, label='B', on_change=color_changed)

    r_field.value=str(options['r'])
    g_field.value=str(options['g'])
    b_field.value=str(options['b'])

    threshold_field = ft.TextField(width=150, label='threshold', on_change=threshold_changed)
    threshold_field.value = None

    format_field = ft.TextField(width=100, label='format', on_change=format_changed)
    format_field.value = None

    page.add(
        ft.Row(
            [
                ft.Image(src='https://raw.githubusercontent.com/plemeri/transparent-background/main/figures/logo.png', width=100, height=100),
                ft.Column(
                    [
                        ft.Row([c, jit_check, reverse_check, output_text_container]),
                        ft.Row([type_dropdown, mode_dropdown, device_dropdown, resize_dropdown, threshold_field, format_field])
                    ]
                )
            ]
        )
    )

    pick_files_dialog = FilePicker(on_result=pick_files_result)

    get_directory_dialog = FilePicker(on_result=get_directory_result)
    file_path = Text(color=ft.colors.BLACK)
    file_path.value = 'Input file or directory will be displayed'
    file_path_container = ft.Container(
                content=file_path,
                margin=10,
                padding=10,
                bgcolor=ft.colors.AMBER,
                border_radius=10,
            )

    get_dest_dialog = FilePicker(on_result=get_dest_result)
    dest_path = Text(color=ft.colors.BLACK)
    dest_path.value = 'Output file or directory will be displayed'
    dest_path_container = ft.Container(
            content=dest_path,
            margin=10,
            padding=10,
            bgcolor=ft.colors.CYAN_200,
            border_radius=10,
        )

    # hide all dialogs in overlay
    page.overlay.extend([pick_files_dialog, get_directory_dialog, get_dest_dialog])
    #progress_ring = ft.ProgressRing(width=16, height=16, stroke_width = 2)
    progress_ring = ft.ProgressBar(width=200, color='amber', bgcolor='#eeeeee')
    progress_ring.value = 0

    preview = ft.Image(src=".preview.png", )
    preview_out = ft.Image(src=".preview_out.png")

    page.add(
        Row(
            [
                ElevatedButton(
                    "Open File",
                    icon=icons.UPLOAD_FILE,
                    on_click=lambda _: pick_files_dialog.pick_files(
                        allow_multiple=False
                    ),
                ),
                ElevatedButton(
                    "Open Directory",
                    icon=icons.FOLDER_OPEN,
                    on_click=lambda _: get_directory_dialog.get_directory_path(),
                    disabled=page.web,
                ),
                file_path_container,
            ]
        ),
        Row(
            [
                ElevatedButton(
                    "Open Destination",
                    icon=icons.FOLDER_OPEN,
                    on_click=lambda _: get_dest_dialog.get_directory_path(),
                    disabled=page.web,
                ),
                dest_path_container
            ]
        ),
        Row(
            [
                ElevatedButton(
                    "Process",
                    icon=icons.SEND,
                    on_click=process,
                    disabled=page.web,
                ),
                ElevatedButton(
                    "Stop",
                    icon=icons.STOP,
                    on_click=click_abort,
                    disabled=page.web,
                ),
                progress_ring
            ]
        ),
    )

    page.add(
        Row(
            [
                preview,
                preview_out
            ]
        )
    )


def gui():
    ft.app(target=main)

    if os.path.isfile('.preview.png'):
        os.remove('.preview.png')

    if os.path.isfile('.preview_out.png'):
        os.remove('.preview_out.png')


if __name__ == "__main__":
    gui()