File size: 5,826 Bytes
3f75a4f
2459a6e
6736291
2459a6e
 
6736291
3f75a4f
f878f1b
2459a6e
 
 
 
 
 
f878f1b
6736291
64bd1c5
 
 
 
2459a6e
dab3797
 
 
6736291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab3797
6736291
dab3797
 
2459a6e
 
6736291
f878f1b
 
6736291
 
 
 
 
c6f69c7
 
6736291
c6f69c7
6736291
b2fcecf
6736291
 
 
 
2459a6e
6736291
 
2459a6e
6736291
 
 
 
 
 
dab3797
b2fcecf
 
 
a567563
6736291
 
 
2459a6e
3f75a4f
 
 
 
 
 
 
 
 
2459a6e
b2fcecf
 
 
 
 
 
 
2459a6e
3f75a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2459a6e
 
 
 
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
import argparse
import gradio as gr
from gradio_i18n import Translate, gettext as _

from modules.live_portrait.live_portrait_inferencer import LivePortraitInferencer
from modules.utils.paths import *
from modules.utils.helper import str2bool
from modules.utils.constants import *


class App:
    def __init__(self,
                 args=None):
        self.args = args
        self.app = gr.Blocks(css=GRADIO_CSS)
        self.i18n = Translate(I18N_YAML_PATH)
        self.inferencer = LivePortraitInferencer(
            model_dir=args.model_dir if args else MODELS_DIR,
            output_dir=args.output_dir if args else OUTPUTS_DIR
        )

    @staticmethod
    def create_parameters():
        return [
            gr.Slider(label=_("Rotate Pitch"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Rotate Yaw"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Rotate Roll"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Blink"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Eyebrow"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Wink"), minimum=0, maximum=25, step=0.5, value=0),
            gr.Slider(label=_("Pupil X"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("Pupil Y"), minimum=-20, maximum=20, step=0.5, value=0),
            gr.Slider(label=_("AAA"), minimum=-30, maximum=120, step=1, value=0),
            gr.Slider(label=_("EEE"), minimum=-20, maximum=20, step=0.2, value=0),
            gr.Slider(label=_("WOO"), minimum=-20, maximum=20, step=0.2, value=0),
            gr.Slider(label=_("Smile"), minimum=-2.0, maximum=2.0, step=0.01, value=0),
            gr.Slider(label=_("Source Ratio"), minimum=0, maximum=1, step=0.01, value=1),
            gr.Slider(label=_("Sample Ratio"), minimum=-0.2, maximum=1.2, step=0.01, value=1),
            gr.Dropdown(label=_("Sample Parts"),
                        choices=["OnlyExpression", "OnlyRotation", "OnlyMouth", "OnlyEyes", "All"], value="All"),
            gr.Slider(label=_("Crop Factor"), minimum=1.5, maximum=2.5, step=0.1, value=1.7)
        ]

    def launch(self):
        with self.app:
            with self.i18n:
                gr.Markdown(REPO_MARKDOWN, elem_id="md_project")

                with gr.Row():
                    with gr.Column():
                        img_ref = gr.Image(label=_("Reference Image"))
                with gr.Row():
                    btn_gen = gr.Button("GENERATE", visible=False)
                with gr.Row(equal_height=True):
                    with gr.Column(scale=9):
                        img_out = gr.Image(label=_("Output Image"))
                    with gr.Column(scale=1):
                        expression_parameters = self.create_parameters()
                        btn_openfolder = gr.Button('📂')
                        with gr.Accordion("Opt in features", visible=False):
                            img_sample = gr.Image()
                            img_motion_link = gr.Image()
                            tb_exp = gr.Textbox()

                params = expression_parameters + [img_ref]
                opt_in_features_params = [img_sample, img_motion_link, tb_exp]

                gr.on(
                    triggers=[param.change for param in params],
                    fn=self.inferencer.edit_expression,
                    inputs=params + opt_in_features_params,
                    outputs=img_out
                )

                btn_openfolder.click(
                    fn=lambda: self.open_folder(self.args.output_dir), inputs=None, outputs=None
                )

                btn_gen.click(self.inferencer.edit_expression,
                              inputs=params + opt_in_features_params,
                              outputs=img_out)

            gradio_launch_args = {
                "inbrowser": self.args.inbrowser,
                "share": self.args.share,
                "server_name": self.args.server_name,
                "server_port": self.args.server_port,
                "root_path": self.args.root_path,
                "auth": (self.args.username, self.args.password) if self.args.username and self.args.password else None,
            }
            self.app.queue().launch(**gradio_launch_args)

    @staticmethod
    def open_folder(folder_path: str):
        if not os.path.exists(folder_path):
            os.makedirs(folder_path, exist_ok=True)
            print(f"The directory path {folder_path} has newly created.")
        os.system(f"start {folder_path}")


if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('--share', type=str2bool, default=False, nargs='?', const=True, help='Gradio share value')
    parser.add_argument('--inbrowser', type=str2bool, default=True, nargs='?', const=True,
                        help='Whether to automatically starts on the browser or not')
    parser.add_argument('--server_name', type=str, default=None, help='Gradio server host')
    parser.add_argument('--server_port', type=int, default=None, help='Gradio server port')
    parser.add_argument('--root_path', type=str, default=None, help='Gradio root path')
    parser.add_argument('--username', type=str, default=None, help='Gradio authentication username')
    parser.add_argument('--password', type=str, default=None, help='Gradio authentication password')
    parser.add_argument('--model_dir', type=str, default=MODELS_DIR,
                        help='Directory path of the LivePortrait models')
    parser.add_argument('--output_dir', type=str, default=OUTPUTS_DIR,
                        help='Directory path of the outputs')
    _args = parser.parse_args()

    app = App(args=_args)
    app.launch()