o
    U6i                  	   @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 de
eB d	efd
dZdde
eB de
eB ded	efddZde
eB d	efddZdS )z4Face and mouth region detection using Gemini Vision.    N)Path   )
get_client)GEMINI_MODEL)types
image_pathreturnc           
   
   C   s  t  }t| } t| d}| }W d   n1 sw   Y  tjj|d| jdd   d}d}|j	j
t||gd}|j }|dr^|dd }|d	rZ|d
d }| }zt|}d|t| dW S  tjy }	 zdd|	 |jt| dW  Y d}	~	S d}	~	ww )z
    Detect face and mouth regions in an image using Gemini Vision.

    Args:
        image_path: Path to the image file

    Returns:
        dict with detected regions (normalized coordinates 0-1)
    rbNzimage/r   )dataZ	mime_typeaE  Analyze this face image and identify the regions.

Return a JSON object with the following structure:
{
    "face": {
        "x": <left edge 0-1>,
        "y": <top edge 0-1>,
        "width": <width 0-1>,
        "height": <height 0-1>
    },
    "mouth": {
        "x": <left edge 0-1>,
        "y": <top edge 0-1>,
        "width": <width 0-1>,
        "height": <height 0-1>
    },
    "eyes": {
        "left": {"x": <center x>, "y": <center y>},
        "right": {"x": <center x>, "y": <center y>}
    },
    "face_angle": "frontal" | "left_profile" | "right_profile" | "three_quarter_left" | "three_quarter_right",
    "art_style": "realistic" | "anime" | "cartoon" | "pixel" | "other",
    "confidence": <0-1>
}

All coordinates should be normalized between 0 and 1, where (0,0) is top-left.
Only return the JSON, no other text.)Zmodelcontentsz```json   T)successregionsr   FzFailed to parse regions: )r   errorZraw_responser   )r   r   openreadr   ZPart
from_bytessuffixlowerZmodelsZgenerate_contentr   textstrip
startswithsplitr   loadsstrZJSONDecodeError)
r   ZclientfZ
image_dataZ
image_partpromptZresponser   r   e r   P/var/www/tkim.planitai.co.jp/blog/20251208-make-apng-tool/./src/face_detector.pydetect_face_regions	   sD   





r!   皙?output_pathpaddingc                 C   sd  ddl m} t| } t|}t| }|d s td|d |d }|di }|s0td|| }|j\}}	|d	d
}
|dd}|dd}|dd}td|
||  }
td|||  }t	d|
 |dd|   }t	d| |dd|   }t
|
| }t
||	 }t
|
| | }t
|| |	 }|||||f}|jjddd || |S )a  
    Crop the mouth region from an image.

    Args:
        image_path: Path to the source image
        output_path: Path to save the cropped image
        padding: Extra padding around the mouth region (0-1)

    Returns:
        Path to the cropped image
    r   )Imager   zFailed to detect regions: r   r   mouthzNo mouth region detectedxg333333?yg333333?widthg?heightr"   r      T)parentsexist_ok)PILr%   r   r!   
ValueErrorgetr   sizemaxminintcropparentmkdirsave)r   r#   r$   r%   resultr   r&   imgr)   r*   r'   r(   whlefttoprightbottomZcroppedr   r   r    crop_mouth_regionX   s8   


rA   c                 C   s   t | }|d sdddgdS |d }g }|dd}|d	vr$|d
 |dd}|dk r3|d |di }|rC|dddk rH|d t|dkpQ|dk|||dS )z
    Validate that an image is suitable for lip sync generation.

    Args:
        image_path: Path to the image file

    Returns:
        dict with validation results and recommendations
    r   FzCould not detect face regionsz'Use a clearer image with a visible face)validreasonrecommendationsr   
face_angleunknown)ZfrontalZthree_quarter_leftZthree_quarter_rightz5Use a frontal or 3/4 view face image for best results
confidencer   gffffff?z3Image quality or face visibility may affect resultsr&   r)   g?z(Mouth region is too small or not visibleg      ?)rB   r   rD   rG   )r!   r0   appendlen)r   r9   r   rD   rE   rG   r&   r   r   r    validate_face_image   s,   



rJ   )r"   )__doc__r   pathlibr   Zgemini_clientr   Zconfigr   Zgoogle.genair   r   dictr!   floatrA   rJ   r   r   r   r    <module>   s    $O: