Spaces:
Runtime error
Runtime error
| 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() | |