File size: 4,955 Bytes
1c09667
952bab9
1a3227d
1b008ec
1a3227d
1c09667
1a3227d
952bab9
 
1c09667
6c6d7ba
1c09667
 
 
 
 
952bab9
1a3227d
1c09667
 
 
 
 
 
 
1a3227d
 
1c09667
1b008ec
99c4346
1a3227d
1b008ec
1a3227d
 
1b008ec
1a3227d
 
1b008ec
1a3227d
 
99c4346
1b008ec
 
 
 
1a3227d
 
1c09667
1a3227d
 
2dd0c68
 
1a3227d
 
952bab9
 
 
 
1c09667
1a3227d
 
 
 
 
 
 
 
 
 
 
 
952bab9
1c09667
 
 
 
 
99c4346
 
 
952bab9
 
1c09667
952bab9
 
 
1c09667
952bab9
1c09667
952bab9
1c09667
952bab9
2dd0c68
1c09667
 
2dd0c68
 
 
952bab9
 
1c09667
 
 
952bab9
 
 
 
1c09667
 
 
 
 
 
952bab9
 
 
1a3227d
3b731e7
99c4346
 
 
1c09667
2dd0c68
99c4346
1a3227d
 
 
f64bc6d
2dd0c68
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
from glob import glob

import gradio as gr
from gradio_client import Client, handle_file

from utils import make_flatten_background

REPO_ID = "leonelhs/faceshine"

clients = {
    "GFPGAN": "leonelhs/superface",
    "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(img=handle_file(image), api_name="/predict")

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

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

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


def zero_background(image, new_bgr=None):
    img_cut, mask = clients["ZeroBackground"].predict(
        image=handle_file(image), session="U2NET Human Seg", smoot=False, api_name="/predict")
    return make_flatten_background(image, img_cut, mask)


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


def mirror(x, y):
    return x, y


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



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="Image workspace", type="filepath")
                with gr.Column(scale=2):
                    with gr.Row():
                        img_output = gr.Image(label="Image result", type="filepath", interactive=False)
                    with gr.Row():
                        btn_swap = gr.Button(value="<= Swap =>", variant="primary")

        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, img_input], outputs=[img_input, img_output])

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

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