File size: 7,594 Bytes
c204f33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces
import gradio as gr


'''

'''
from gradio_utils import clear_old_files,read_file
from face_mesh_spinning import process_face_mesh_spinning
from mp_estimate  import mean_std_label,estimate_horizontal,estimate_vertical,estimate_horizontal_points,estimate_vertical_points

def process_images(image,draw_type,center_scaleup,animation_direction,
                   z_multiply,inner_eyes,inner_mouth,
        progress=gr.Progress(track_tqdm=True)):
    
    clear_old_files()

    if image==None:
        raise gr.Error("need image")

    result,face_landmarker_result,rotated_points = process_face_mesh_spinning(image,draw_type,center_scaleup,animation_direction,z_multiply,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;
}
"""

from glibvision.cv2_utils  import pil_to_bgr_image,copy_image
from mp_utils import extract_landmark,get_pixel_cordinate
import numpy as np
# TODO move mp_util
def extract_landmark_double_check(numpy_image,double_check=True,center_index=4,extract_matrix=True):#4 is nose-tip
    mp_image,face_landmarker_result = extract_landmark(numpy_image,"face_landmarker.task",0,0,extract_matrix)
    h,w = numpy_image.shape[:2]
    second_mp_image,first_landmarker_result = None,None
    numpy_view = mp_image.numpy_view()
    if double_check:
        root_cordinate = get_pixel_cordinate(face_landmarker_result.face_landmarks,center_index,w,h)
        diff_center_x = int(w/2 - root_cordinate[0])
        diff_center_y = int(h/2 - root_cordinate[1])
        base = np.zeros_like(numpy_view)
        copy_image(base,numpy_view,diff_center_x,diff_center_y)
        first_landmarker_result = face_landmarker_result
        second_mp_image,face_landmarker_result = extract_landmark(base,"face_landmarker.task",0,0,extract_matrix)
    return mp_image,face_landmarker_result,second_mp_image,first_landmarker_result

#css=css,

from scipy.spatial.transform import Rotation as R
def calculate_angle(image,double_check,ignore_x,order):
    cv2_base_image = pil_to_bgr_image(image)
    mp_image,face_landmarker_result,_,_ = extract_landmark_double_check(cv2_base_image,double_check)
    if len(face_landmarker_result.facial_transformation_matrixes)>0:
        transformation_matrix=face_landmarker_result.facial_transformation_matrixes[0]
            
        rotation_matrix, translation_vector = transformation_matrix[:3, :3],transformation_matrix[:3, 3]
        
        r = R.from_matrix(rotation_matrix)
        euler_angles = r.as_euler(order, degrees=True)
        label = f"Mediapipe Euler yxz: {euler_angles}"
        if ignore_x:
            euler_angles[1]=0

        result = [label,0,0,0]
        for i,ch in enumerate(order.lower()):
            if ch == "x":
                result[1] = -euler_angles[i]
            elif ch == "y":
                result[2] = euler_angles[i]
            elif ch == "z":
                result[3] = euler_angles[i]

        return result
        return label,-euler_angles[1],euler_angles[0],euler_angles[2]
    return "",0,0,0

def change_animation(animation):
     if animation:
          return gr.Column(visible=True),gr.Column(visible=False)
     else:
          return gr.Column(visible=False),gr.Column(visible=True)
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("Rotate Mesh", elem_id="run_button",variant="primary")
                    
                    
                        
                    with gr.Accordion(label="Advanced Settings", open=True):
                       
                        draw_type = gr.Radio(label="Draw type",choices=["Dot","Line","Line+Fill","Image"],value="Line",info="making image animation,take over 60 sec and limited frame only")
                        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):
                            
                            center_scaleup = gr.Checkbox(label="ScaleUp/Fit",value=True,info="center is nose-tip,Zoomed face usually make small")
                            z_multiply = gr.Slider(info="Nose height",
                            label="Depth-Multiply",
                            minimum=0.1,
                            maximum=1.5,
                            step=0.01,
                            value=0.8)
                        animation_column = gr.Column(visible=True)
                        with animation_column:
                            with gr.Row( equal_height=True):
                                animation_direction = gr.Radio(label="Animation Direction",choices=["X","Y","Z"],value="Y")

                        
                                
                              

                                         
                with gr.Column():
                    result_image = gr.Image(height=760,label="Result", elem_id="output-animation",image_mode='RGBA')
                   
                    
                    
                    
   
    btn.click(fn=process_images, inputs=[image,draw_type,center_scaleup,animation_direction,
                                         z_multiply,inner_eyes,inner_mouth,
                                         ],outputs=[result_image,
                                                    
                                                    ] ,api_name='infer')
    
    example_images = [
                     ["examples/02316230.jpg","examples/02316230.webp"],
                    ["examples/00003245_00.jpg","examples/00003245_00.webp"],
                   ["examples/00827009.jpg","examples/00827009.webp"],
                     ["examples/00002062.jpg","examples/00002062.webp"],
                    ["examples/00824008.jpg","examples/00824008.webp"],
                    ["examples/00825000.jpg","examples/00825000.webp"],
                    ["examples/00826007.jpg","examples/00826007.webp"],
                     ["examples/00824006.jpg","examples/00824006.webp"],
                   
                     ["examples/00002200.jpg","examples/00002200.webp"],
                    ["examples/00005259.jpg","examples/00005259.webp"],
                    ["examples/00018022.jpg","examples/00018022.webp"],
                    ["examples/img-above.jpg","examples/img-above.webp"],
                     ["examples/00100265.jpg","examples/00100265.webp"],
                      ["examples/00039259.jpg","examples/00039259.webp"],
                     
                ]
    example1=gr.Examples(
                examples = example_images,label="Image",
                inputs=[image,result_image],examples_per_page=8
    )
  
    gr.HTML(read_file("demo_footer.html"))

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