File size: 4,353 Bytes
72b443b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces
import gradio as gr


'''

'''
from gradio_utils import clear_old_files,read_file
from face_mesh3d import process_image3d


def process_images(image,smooth_mesh,depto_ratio,inner_eyes,inner_mouth,
        progress=gr.Progress(track_tqdm=True)):
    
    clear_old_files()

    result = process_image3d(image,smooth_mesh,depto_ratio,inner_eyes,inner_mouth)
    
    return result


css="""
#col-left {
    margin: 0 auto;
    max-width: 640px;
}
#col-right {
    margin: 0 auto;
    max-width: 640px;
}
.grid-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap:10px
}

.image {
  width: 128px; 
  height: 128px; 
  object-fit: cover; 
}

.text {
  font-size: 16px;
}
"""

#css=css,
from glibvision.cv2_utils  import pil_to_bgr_image
from mp_utils import extract_landmark
from scipy.spatial.transform import Rotation as R


def change_viewer3d_mode(mode):
     if mode=="solid":
          return gr.Model3D(display_mode="solid")
     else:
          print("wireframe")
          return gr.Model3D(display_mode="wireframe")
     
with gr.Blocks(css=css, elem_id="demo-container") as demo:
    with gr.Column():
        gr.HTML(read_file("demo_header.html"))
        gr.HTML(read_file("demo_tools.html"))
    with gr.Row():
                with gr.Column():
                    image = gr.Image(height=800,sources=['upload','clipboard'],image_mode='RGB',elem_id="image_upload", type="pil", label="Image")

                    with gr.Row(elem_id="prompt-container",  equal_height=False):
                        with gr.Row():
                            btn = gr.Button("3D Mesh", elem_id="run_button",variant="primary")
                    
                    
                        
                    with gr.Accordion(label="Advanced Settings", open=True):
                        with gr.Row( equal_height=True):
                             inner_eyes=gr.Checkbox(label="Inner Eyes",value=True)
                             inner_mouth=gr.Checkbox(label="Inner Mouth",value=True)
                        with gr.Row( equal_height=True):
                            
                            smooth_mesh = gr.Checkbox(label="Smooth mesh",value=True,info="smooth or blockly")
                            depto_ratio = gr.Slider(
                                    label="Depth Ratio",info="If you feel nose height strange change this",
                                    minimum=0.01,
                                    maximum=1,
                                    step=0.01,
                                    value=0.8)
                           
                        animation_column = gr.Column(visible=True)
                        

                      
                                 
                                
                             
                                
                              

                                         
                with gr.Column():#camera_position=(0,0,1.5)
                    result_3d = gr.Model3D(height=800,label="Result",display_mode="solid",elem_id="output-3d",value="files/mesh.obj",clear_color=[0.5,0.5,0.5,1])

    btn.click(fn=process_images, inputs=[image,smooth_mesh,depto_ratio,inner_eyes,inner_mouth
                                         ],outputs=[result_3d] ,api_name='infer')
    
    example_images = [
                     ["examples/02316230.jpg"],
                    ["examples/00003245_00.jpg"],
                   ["examples/00827009.jpg"],
                     ["examples/00002062.jpg"],
                    ["examples/00824008.jpg"],
                    ["examples/00825000.jpg"],
                    ["examples/00826007.jpg"],
                     ["examples/00824006.jpg"],
                    ["examples/00828003.jpg"],
                     ["examples/00002200.jpg"],
                    ["examples/00005259.jpg"],
                    ["examples/00018022.jpg"],
                    ["examples/img-above.jpg"],
                     ["examples/00100265.jpg"],
                      ["examples/00039259.jpg"],
                     
                ]
    example1=gr.Examples(
                examples = example_images,label="Image",
                inputs=[image],examples_per_page=8
    )
  
    gr.HTML(read_file("demo_footer.html"))

    if __name__ == "__main__":
        demo.launch()