
    uki                        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dl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ej2                  Zej6                  Zej:                  Zej>                  Z ejB                  Z"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*ejV                   G d d             Z, G d dejZ                        Z.y)zMediaPipe face aligner task.    N)Optional)packet_creator)packet_getter)image)face_stylizer_graph_options_pb2base_options)	task_info)doc_controls)base_vision_task_api)image_processing_options)vision_task_running_modeface_alignmentFACE_ALIGNMENTnorm_rect_in	NORM_RECTimage_in	image_outIMAGEz6mediapipe.tasks.vision.face_stylizer.FaceStylizerGraphc                   F    e Zd ZU dZeed<   ej                  defd       Z	y)FaceAlignerOptionsznOptions for the face aligner task.

  Attributes:
    base_options: Base options for the face aligner task.
  r	   returnc                 \    | j                   j                         }d|_        t        |      S )z0Generates a FaceStylizerOptions protobuf object.Fr   )r	   to_pb2use_stream_mode_FaceStylizerGraphOptionsProto)selfbase_options_protos     e/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/vision/face_aligner.pyr   zFaceAlignerOptions.to_pb2:   s.     **113).&)7IJJ    N)
__name__
__module____qualname____doc___BaseOptions__annotations__r   do_not_generate_docsr   r    r    r   r   r   0   s2     $$K4 K %Kr    r   c                       e Zd ZdZededd fd       Zededd fd       Z	 dde	j                  d	ee   de	j                  fd
Zy)FaceAlignerz-Class that performs face alignment on images.
model_pathr   c                 T    t        |      }t        |      }| j                  |      S )aY  Creates a `FaceAligner` object from a face landmarker task bundle and the default `FaceAlignerOptions`.

    Note that the created `FaceAligner` instance is in image mode, for
    aligning one face on a single image input.

    Args:
      model_path: Path to the face landmarker task bundle.

    Returns:
      `FaceAligner` object that's created from the model file and the default
      `FaceAlignerOptions`.

    Raises:
      ValueError: If failed to create `FaceAligner` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )model_asset_pathr   )r%   r   create_from_options)clsr+   r	   optionss       r   create_from_model_pathz"FaceAligner.create_from_model_pathE   s)    &  <L l;G""7++r    r0   c           	      H   t        t        dj                  t        t        g      dj                  t
        t        g      gdj                  t        t        g      dj                  t        t        g      g|      } | |j                  d      t        j                  d      S )a  Creates the `FaceAligner` object from face aligner options.

    Args:
      options: Options for the face aligner task.

    Returns:
      `FaceAligner` object that's created from `options`.

    Raises:
      ValueError: If failed to create `FaceAligner` object from
        `FaceAlignerOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    :)
task_graphinput_streamsoutput_streamstask_optionsF)enable_flow_limitingN)	_TaskInfo_TASK_GRAPH_NAMEjoin
_IMAGE_TAG_IMAGE_IN_STREAM_NAME_NORM_RECT_TAG_NORM_RECT_STREAM_NAME_FACE_ALIGNMENT_IMAGE_TAG_FACE_ALIGNMENT_IMAGE_NAME_IMAGE_OUT_STREAM_NAMEgenerate_graph_config_RunningModer   )r/   r0   r
   s      r   r.   zFaceAligner.create_from_options\   s     #HHj"789HHn&<=>

 HH/1KLMHHj"89:
 I ''U'C r    Nr   r   c           	      0   | j                  ||      }| j                  t        t        j                  |      t
        t        j                  |j                               i      }|t           j                         ryt        j                  |t                 S )a#  Performs face alignment on the provided MediaPipe Image.

    Only use this method when the FaceAligner is created with the image
    running mode.

    Args:
      image: MediaPipe Image.
      image_processing_options: Options for image processing.

    Returns:
      The aligned face image. The aligned output image size is the same as the
      model output size. None if no face is detected on the input image.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If face alignment failed to run.
    N)convert_to_normalized_rect_process_image_datar=   r   create_imager?   create_protor   rA   is_emptyr   	get_image)r   r   r   normalized_rectoutput_packetss        r   alignzFaceAligner.align}   s    , 55 %O --~::5A ; ;""$!
/ N 01::<"">2L#MNNr    )N)r!   r"   r#   r$   classmethodstrr1   r   r.   image_moduleImager   _ImageProcessingOptionsrN   r(   r    r   r*   r*   B   s    5,c ,m , ,, (: }  F EI!O!O !))@ A!O 	!Or    r*   )/r$   dataclassestypingr   mediapipe.pythonr   r   $mediapipe.python._framework_bindingsr   rQ   -mediapipe.tasks.cc.vision.face_stylizer.protor   mediapipe.tasks.python.corer	   base_options_moduler
   task_info_module1mediapipe.tasks.python.core.optional_dependenciesr   "mediapipe.tasks.python.vision.corer   r   image_processing_options_moduler   running_mode_moduleBaseOptionsr%   FaceStylizerGraphOptionsr   VisionTaskRunningModerD   ImageProcessingOptionsrS   TaskInfor9   rA   r@   r?   r>   r=   rB   r<   r:   	dataclassr   BaseVisionTaskApir*   r(   r    r   <module>rg      s    #   + * F Y K E J C j ^"..#<<  #889PP %%	- , ' " $ 
K  K K K"\O&88 \Or    