File size: 5,127 Bytes
1c09667
952bab9
1a3227d
 
 
1c09667
1a3227d
952bab9
 
1c09667
 
 
 
 
 
 
952bab9
1a3227d
1c09667
 
 
 
 
 
 
1a3227d
 
1c09667
 
99c4346
 
1a3227d
1c09667
1a3227d
 
 
1c09667
1a3227d
 
 
1c09667
1a3227d
 
99c4346
3b731e7
1c09667
 
 
 
1a3227d
 
 
1c09667
1a3227d
 
 
 
 
 
952bab9
 
 
 
1c09667
 
 
 
1a3227d
 
 
 
 
 
 
 
 
 
 
 
952bab9
1c09667
 
 
 
 
99c4346
 
 
952bab9
 
1c09667
952bab9
 
 
1c09667
952bab9
1c09667
952bab9
1c09667
952bab9
 
 
1c09667
 
 
 
 
 
952bab9
 
1c09667
 
 
952bab9
 
 
 
1c09667
 
 
 
 
 
952bab9
 
 
1a3227d
3b731e7
99c4346
 
 
1c09667
99c4346
1c09667
99c4346
1a3227d
 
 
afe3854
 
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
from glob import glob

import gradio as gr
from gradio_client import Client

from utils import make_flatten_background

REPO_ID = "leonelhs/faceshine"

clients = {
    "GFPGAN": "leonelhs/GFPGAN",
    "ZeroScratches": "leonelhs/ZeroScratches",
    "Deoldify": "leonelhs/deoldify",
    "EnhanceLight": "leonelhs/Zero-DCE",
    "ZeroBackground": "leonelhs/rembg",
}


def load_client(space):
    try:
        return Client(space)
    except ValueError as err:
        print(err)
        logger.value.append(f"Space: {space}, log: {err}")
        pass


def gfpgan_face(image, version, scale):
    return clients["GFPGAN"].predict(image, version, scale, fn_index=0)[0]


def zero_scratches(image):
    return clients["ZeroScratches"].predict(image, api_name="/predict")


def colorize_photo(image):
    return clients["Deoldify"].predict(image, api_name="/predict")


def enhance_light(image):
    return clients["EnhanceLight"].predict(image, api_name="/predict")


def zero_background(image, new_bgr=None):
    # Fixme: cant find predict function by name
    # return clients["ZeroBackground"].predict(image, new_bgr, fn_index=0)[1]
    # return clients["ZeroBackground"].predict(image, fn_index=0)
    img, mask = clients["ZeroBackground"].predict(image, "U2NET Human Seg", False, fn_index=9)
    return make_flatten_background(img, mask)


def parse_face(image):
    return clients["FaceParser"].predict(image, api_name="/predict")


def mirror(x):
    return x


def active_first():
    return gr.Tabs.update(selected=0)


def clear():
    return None, None


footer = r"""
<center>
<p>This App is running on a CPU, help us to upgrade a GPU or just give us a <a href='https://github.com/leonelhs/face-shine' target='_blank'>Github ⭐</a></p>
</br>
<a href="https://www.buymeacoffee.com/leonelhs">
    <img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=leonelhs&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" />
</a>
</center>
</br>
<center><span>[email protected]</span></center>
"""

with gr.Blocks(title="Face Shine") as app:
    logger = gr.State(value=[])

    for client, endpoint in clients.items():
        clients[client] = load_client(endpoint)

    with gr.Row():
        gr.HTML("<center><h1>Face Shine</h1></center>")

    with gr.Tabs() as tabs:
        with gr.TabItem("Photo restorer", id=0):
            with gr.Row(equal_height=False):
                with gr.Column(scale=1):
                    btn_eraser = gr.Button(value="Erase scratches")
                    btn_color = gr.Button(value="Colorize photo")
                    btn_hires = gr.Button(value="Enhance face")
                    btn_light = gr.Button(value="Enhance light")
                    btn_clear = gr.Button(value="Flatten background")

                with gr.Column(scale=2):
                    with gr.Row():
                        img_input = gr.Image(label="Input", type="filepath")
                    with gr.Row():
                        btn_reset = gr.Button(value="Reset", variant="stop")
                        btn_swap = gr.Button(value="Ok", variant="primary")

                with gr.Column(scale=2):
                    with gr.Row():
                        img_output = gr.Image(label="Result", type="filepath", interactive=False)

        with gr.TabItem("Examples", id=1):
            gr.Examples(examples=glob("lowres/*"), inputs=[img_input], label="Low resolution")
            gr.Examples(examples=glob("gray/*"), inputs=[img_input], label="Gray scale")
            gr.Examples(examples=glob("scratch/*"), inputs=[img_input], label="Scratched")
            gr.Button(value="Ok", variant="primary").click(active_first, None, tabs)

        with gr.TabItem("Settings", id=2):
            with gr.Accordion("Image restoration settings", open=False):
                enhancer = gr.Dropdown(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'],
                                       label='GFPGAN face restoration algorithm',
                                       type="value", value='RestoreFormer',
                                       info="version")
                rescale = gr.Dropdown(["1", "2", "3", "4"],
                                      type="value", value="2", label="Rescaling factor")
            with gr.Accordion("Logs info", open=False):
                text_logger = gr.Textbox(label="login", lines=5, show_label=False)
            gr.Button("Save settings")

    btn_hires.click(gfpgan_face, inputs=[img_input, enhancer, rescale], outputs=[img_output])
    btn_eraser.click(zero_scratches, inputs=[img_input], outputs=[img_output])
    btn_color.click(colorize_photo, inputs=[img_input], outputs=[img_output])
    btn_light.click(enhance_light, inputs=[img_input], outputs=[img_output])
    btn_clear.click(zero_background, inputs=[img_input], outputs=[img_output])
    btn_swap.click(mirror, inputs=[img_output], outputs=[img_input])
    btn_reset.click(clear, outputs=[img_input, img_output])

    with gr.Row():
        gr.HTML(footer)

app.launch(share=False, debug=True, show_error=True)
app.queue()