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()