Ë
    »dæj;J  ã                   ó&  — d Z ddlZddlZddlmZ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 ddlmZ e	j0                  Zej4                  Zej8                  Zej<                  Z G d„ dej@                  «      Z! G d„ dej@                  «      Z" ejF                  dejH                   ejJ                  e"«      ejL                  ejN                  «      Z( G d„ dej@                  «      Z) ejT                  d ejJ                  e)«       ejJ                  ejL                  «      f«       ejT                  dejL                  ejL                   ejJ                  ejV                  «       ejJ                  e"«      f«       ejT                  dejL                  ejL                   ejJ                  ejV                  «      ejN                   ejJ                  e"«      f«       ejT                  dejL                  ejL                   ejJ                  ejV                  «      ejN                  f«       ejT                  dejL                   ejJ                  e!«      f«       ejX                  d ejJ                  e"«      gd«       ejT                  dejL                  f«       ejX                  dejL                  gd«      fZ-ej\                   G d„ d«      «       Z/ej\                   G d„ d «      «       Z0 G d!„ d"«      Z1y)#zMediaPipe image segmenter task.é    N)ÚCallableÚOptional)Úasync_result_dispatcher)Úbase_options)Úbase_options_c)Úmediapipe_c_bindings)Úmediapipe_c_utils)Úserial_dispatcher)Údoc_controls)Úimage)Úimage_processing_options)Úimage_processing_options_c)Úvision_task_running_modec                   ód   — e Zd Zd ej                  ej
                  «      fdej                  fgZy)ÚMpStringListCÚstringsÚnum_stringsN)Ú__name__Ú
__module__Ú__qualname__ÚctypesÚPOINTERÚc_char_pÚc_intÚ_fields_© ó    úp/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/vision/image_segmenter.pyr   r   &   s,   „ à�.�&—.‘. §¡Ó1Ð2Ø�f—l‘lÐ#ð�(r   r   c                   óì   — e Zd Zd ej                  ej
                  «      fdej                  fdej
                  fdej                  fd ej                  ej                  «      fdej                  fgZy)ÚImageSegmenterResultCÚconfidence_masksÚconfidence_masks_countÚcategory_maskÚhas_category_maskÚquality_scoresÚquality_scores_countN)	r   r   r   r   r   Úc_void_pÚc_uint32Úc_floatr   r   r   r   r    r    -   sg   „ à˜>˜6Ÿ>™>¨&¯/©/Ó:Ð;Ø §¡Ð1Ø˜Ÿ™Ð(Ø˜FŸO™OÐ,Ø˜˜Ÿ™¨¯©Ó7Ð8Ø˜vŸ™Ð/ð�(r   r    c                   óø   — e Zd ZdZdej
                  fdej                  fdej                  fdej                  fdej                  fde
fgZeej                  dej
                  dededede
dd fd	„«       «       Zy
)ÚImageSegmenterOptionsCz"C types for ImageSegmenterOptions.r   Úrunning_modeÚdisplay_names_localeÚoutput_confidence_masksÚoutput_category_maskÚresult_callbackÚreturnc                 ó\   — t        j                  d«      } | ||j                  ||||¬«      S )z@Creates an ImageSegmenterOptionsC object from the given options.r   )r   r,   r-   r.   r/   r0   )r   r   Úctype)Úclsr   r,   r.   r/   r0   Úempty_strings          r   Úfrom_c_optionsz%ImageSegmenterOptionsC.from_c_optionsK   s7   € ô —?‘? 3Ó'€LÙØ!Ø!×'Ñ'Ø)Ø 7Ø1Ø'ôð r   N)r   r   r   Ú__doc__r   ÚBaseOptionsCr   r   r   Úc_boolÚ_C_TYPES_RESULT_CALLBACKr   Úclassmethodr   Údo_not_generate_docsÚ_RunningModeÚboolr6   r   r   r   r+   r+   @   sµ   „ Ù*à�~×2Ñ2Ð3Ø�v—|‘|Ð$Ø˜vŸ™Ð/Ø  &§-¡-Ð0Ø˜vŸ}™}Ð-ØÐ2Ð3ð€(ð Ø×$Ñ$ðà"×/Ñ/ðð !ðð  $ð	ð
 !ðð 0ðð  òó %ó ñr   r+   ÚMpImageSegmenterCreateÚMpImageSegmenterSegmentImageÚMpImageSegmenterSegmentForVideoÚMpImageSegmenterSegmentAsyncÚMpImageSegmenterGetLabelsÚMpImageSegmenterCloseResultÚMpImageSegmenterCloseÚMpStringListFreec                   ó    — e Zd ZU dZdZeeej                        e	d<   dZ
eej                     e	d<   eej                  dedd fd„«       «       Zy)ÚImageSegmenterResultaW  Output result of ImageSegmenter.

  confidence_masks: multiple masks of float image where, for each mask, each
  pixel represents the prediction confidence, usually in the [0, 1] range.

  category_mask: a category mask of uint8 image where each pixel represents the
  class which the pixel in the original image was predicted to belong to.
  Nr!   r#   Úc_resultr1   c                 óN  — d}d}|j                   dkD  rLt        |j                   «      D �cg c].  }t        j                  j	                  |j
                  |   «      ‘Œ0 }}|j                  dkD  r)t        j                  j	                  |j                  «      } | ||¬«      S c c}w )zFCreates an `ImageSegmenterResult` object from the given ctypes struct.Nr   )r!   r#   )r"   ÚrangeÚimage_moduleÚImageÚcreate_from_ctypesr!   r$   r#   )r4   rI   r!   r#   Úis        r   Úfrom_ctypesz ImageSegmenterResult.from_ctypes­   s­   € ð ÐØ€MØ×'Ñ'¨!Ò+ô ˜×8Ñ8Ó9öàô ×
Ñ
×
/Ñ
/°×0IÑ0IÈ!Ñ0LÕ
MðÐð ð ×!Ñ! AÒ%Ü"×(Ñ(×;Ñ;Ø
×
 Ñ
 ó€mñ Ø)Ø#ôð ùòs   «3B")r   r   r   r7   r!   r   ÚlistrL   rM   Ú__annotations__r#   r;   r   r<   r    rP   r   r   r   rH   rH   Ÿ   si   … ñð :>Ð�H˜T ,×"4Ñ"4Ñ5Ñ6Ó=Ø04€-�˜,×,Ñ,Ñ-Ó4àØ×$Ñ$ðØ*ðàòó %ó ñr   rH   c                   ó’   — e Zd ZU dZeed<   ej                  Zeed<   dZ	e
ed<   dZe
ed<   dZeeeej"                  egdf      ed	<   y)
ÚImageSegmenterOptionsa7  Options for the image segmenter task.

  Attributes:
    base_options: Base options for the image segmenter task.
    running_mode: The running mode of the task. Default to the image mode. Image
      segmenter task has three running modes: 1) The image mode for segmenting
      objects on single image inputs. 2) The video mode for segmenting objects
      on the decoded frames of a video. 3) The live stream mode for segmenting
      objects on a live stream of input data, such as from camera.
    output_confidence_masks: Whether to output confidence masks.
    output_category_mask: Whether to output category mask.
    result_callback: The user-defined result callback for processing live stream
      data. The result callback should only be specified when the running mode
      is set to the live stream mode.
  r   r,   Tr.   Fr/   Nr0   )r   r   r   r7   Ú_BaseOptionsrR   r=   ÚIMAGEr,   r.   r>   r/   r0   r   r   rH   rL   rM   Úintr   r   r   rT   rT   Å   sf   … ñð  ÓØ+×1Ñ1€,�Ó1Ø"&Ð˜4Ó&Ø$Ð˜Ó$ð ð �8ØÐ$ l×&8Ñ&8¸#Ð>ÀÐDÑEñô r   rT   c            
       ó¬  — e Zd ZU dZej
                  ed<   ej                  ed<   e	ed<   e
ed<   dej
                  dej                  de	d	e
d
df
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fd„Z	 ddej$                  dedee   d
efd„Z	 ddej$                  dedee   d
dfd„Zed
ee   fd„«       Zd„ Zd„ Zd„ Zd„ Z y)ÚImageSegmentera‘  Class that performs image segmentation on images.

  The API expects a TFLite model with mandatory TFLite Model Metadata.

  Input tensor:
    (kTfLiteUInt8/kTfLiteFloat32)
    - image input of size `[batch x height x width x channels]`.
    - batch inference is not supported (`batch` is required to be 1).
    - RGB and greyscale inputs are supported (`channels` is required to be
      1 or 3).
    - if type is kTfLiteFloat32, NormalizationOptions are required to be
      attached to the metadata for input normalization.
  Output tensors:
    (kTfLiteUInt8/kTfLiteFloat32)
    - list of segmented masks.
    - if `output_category_mask` is True, uint8 Image, Image vector of size 1.
    - if `output_confidence_masks` is True, float32 Image list of size
      `channels`.
    - batch is always 1

  An example of such model can be found at:
  https://tfhub.dev/tensorflow/lite-model/deeplabv3/1/metadata/2
  Ú_libÚ_handleÚ_dispatcherÚ_async_callbackÚlibÚhandleÚ
dispatcherÚasync_callbackr1   Nc                 óJ   — || _         || _        || _        || _        d| _        y)a.  Initializes the `ImageSegmenter` object.

    Args:
      lib: The dispatch library to use for the image segmenter.
      handle: The C pointer to the image segmenter.
      dispatcher: The async result handler for the image segmenter.
      async_callback: The c callback for the image segmenter.
    N)rZ   r[   r\   r]   Ú_labels)Úselfr^   r_   r`   ra   s        r   Ú__init__zImageSegmenter.__init__þ   s)   € ð €D„IØ€D„LØ!€DÔØ)€DÔØ€D…Lr   Ú
model_pathc                 ór   — t        |¬«      }t        |t        j                  ¬«      }| j	                  |«      S )a[  Creates an `ImageSegmenter` object from a TensorFlow Lite model and the default `ImageSegmenterOptions`.

    Note that the created `ImageSegmenter` instance is in image mode, for
    performing image segmentation on single image inputs.

    Args:
      model_path: Path to the model.

    Returns:
      `ImageSegmenter` object that's created from the model file and the default
      `ImageSegmenterOptions`.

    Raises:
      ValueError: If failed to create `ImageSegmenter` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )Úmodel_asset_path)r   r,   )rU   rT   r=   rV   Úcreate_from_options)r4   rf   r   Úoptionss       r   Úcreate_from_model_pathz%ImageSegmenter.create_from_model_path  s6   € ô&  °Ô<€LÜ#Ø!´×0BÑ0Bô€Gð ×"Ñ" 7Ó+Ð+r   rj   c                 óÚ  — t        j                  |j                  |j                  «       t	        j
                  t        «      }dt        j                  t        «      dt        j                  dt        dt        t        t        j                  t        f   fd„}t!        |¬«      }|j#                  |j                  t$        «      }t&        j)                  |j*                  j-                  «       |j                  |j.                  |j0                  |¬«      }t        j                  «       }|j3                  t        j4                  |«      t        j4                  |«      «        | ||||¬«      S )	a’  Creates the `ImageSegmenter` object from image segmenter options.

    Args:
      options: Options for the image segmenter task.

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

    Raises:
      ValueError: If failed to create `ImageSegmenter` object from
        `ImageSegmenterOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    Úc_result_ptrÚ	image_ptrÚtimestamp_msr1   c                 ó~   — | d   }t         j                  |«      }t        j                  j	                  |«      }|||fS )z;Converts the C data types to the desired Python data types.r   )rH   rP   rL   rM   rN   )rm   rn   ro   rI   Ú	py_resultÚpy_images         r   Úconvert_resultz:ImageSegmenter.create_from_options.<locals>.convert_resultD  s@   € ð ˜a‘€hÜ&×2Ñ2°8Ó<€iÜ×#Ñ#×6Ñ6°yÓA€hØ˜ <Ð0Ð0r   )Ú	converter)r   r,   r.   r/   r0   )r^   r_   r`   ra   )Úrunning_mode_moduleÚvalidate_running_moder,   r0   r   Úload_shared_libraryÚ_CTYPES_SIGNATURESr   r   r    r'   rW   ÚtuplerH   rL   rM   Ú_AsyncResultDispatcherÚwrap_callbackr:   r+   r6   r   Ú	to_ctypesr.   r/   r?   Úbyref)r4   rj   r^   rs   r`   Ú
c_callbackÚ	c_optionsÚsegmenter_handles           r   ri   z"ImageSegmenter.create_from_options,  s?  € ô$ ×-Ñ-Ø×Ñ˜g×5Ñ5ôô ×
2Ñ
2Ô3EÓ
F€Cð	1Ü—n‘nÔ%:Ó;ð	1ä—?‘?ð	1ô ð	1ô 
Ô#¤\×%7Ñ%7¼Ð<Ñ	=ó		1ô (°.ÔA€JØ×)Ñ)Ø×ÑÔ!9ó€Jô '×5Ñ5Ø×)Ñ)×3Ñ3Ó5Ø×)Ñ)Ø '× ?Ñ ?Ø$×9Ñ9Ø"ð 6ó €Iô —‘Ó(ÐØ×ÑÜ�‰�YÓ¤§¡Ð.>Ó!?ôñ ØØØØ!ô	ð r   r   r   c                 ó|  — |j                   }t        «       }|r#t        j                  |j	                  «       «      nd}| j
                  j                  | j                  ||t        j                  |«      «       t        j                  |«      }| j
                  j                  t        j                  |«      «       |S )aŠ  Performs the actual segmentation task on the provided MediaPipe Image.

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

    Returns:
      If the output_type is CATEGORY_MASK, the returned vector of images is
      per-category segmented image mask.
      If the output_type is CONFIDENCE_MASK, the returned vector of images
      contains only one confidence image mask. A segmentation result object that
      contains a list of segmentation masks as images.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If image segmentation failed to run.
    N)Ú
_image_ptrr    r   r}   r|   rZ   r@   r[   rH   rP   rD   )rd   r   r   Úc_imagerI   Ú	options_cÚresults          r   ÚsegmentzImageSegmenter.segmente  s›   € ð, ×Ñ€GÜ$Ó&€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×*Ñ*Ø�‰ØØÜ�‰�XÓô	ô "×-Ñ-¨hÓ7€FØ‡I�I×)Ñ)¬&¯,©,°xÓ*@ÔAØ€Mr   ro   c           	      ó~  — |j                   }t        «       }|r#t        j                  |j	                  «       «      nd}| j
                  j                  | j                  |||t        j                  |«      «       t        j                  |«      }| j
                  j                  t        j                  |«      «       |S )aç  Performs segmentation on the provided video frames.

    Only use this method when the ImageSegmenter is created with the video
    running mode. It's required to provide the video frame's timestamp (in
    milliseconds) along with the video frame. The input timestamps should be
    monotonically increasing for adjacent calls of this method.

    Args:
      image: MediaPipe Image.
      timestamp_ms: The timestamp of the input video frame in milliseconds.
      image_processing_options: Options for image processing.

    Returns:
      If the output_type is CATEGORY_MASK, the returned vector of images is
      per-category segmented image mask.
      If the output_type is CONFIDENCE_MASK, the returned vector of images
      contains only one confidence image mask. A segmentation result object that
      contains a list of segmentation masks as images.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If image segmentation failed to run.
    N)r‚   r    r   r}   r|   rZ   rA   r[   rH   rP   rD   )rd   r   ro   r   rƒ   rI   r„   r…   s           r   Úsegment_for_videoz ImageSegmenter.segment_for_videoŒ  sž   € ð: ×Ñ€GÜ$Ó&€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×-Ñ-Ø�‰ØØØÜ�‰�XÓôô "×-Ñ-¨hÓ7€FØ‡I�I×)Ñ)¬&¯,©,°xÓ*@ÔAØ€Mr   c                 óº   — |j                   }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  |||«       y)añ  Sends live image data (an Image with a unique timestamp) to perform image segmentation.

    Only use this method when the ImageSegmenter is created with the live stream
    running mode. The input timestamps should be monotonically increasing for
    adjacent calls of this method. This method will return immediately after the
    input image is accepted. The results will be available via the
    `result_callback` provided in the `ImageSegmenterOptions`. The
    `segment_async` method is designed to process live stream data such as
    camera input. To lower the overall latency, image segmenter may drop the
    input images if needed. In other words, it's not guaranteed to have output
    per input image.

    The `result_callback` prvoides:
      - A segmentation result object that contains a list of segmentation masks
        as images.
      - The input image that the image segmenter runs on.
      - The input timestamp in milliseconds.

    Args:
      image: MediaPipe Image.
      timestamp_ms: The timestamp of the input image in milliseconds.
      image_processing_options: Options for image processing.

    Raises:
      ValueError: If the current input timestamp is smaller than what the image
        segmenter has already processed.
    N)r‚   r   r}   r|   rZ   rB   r[   )rd   r   ro   r   rƒ   r„   s         r   Úsegment_asynczImageSegmenter.segment_async»  sY   € ðB ×Ñ€Gñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×*Ñ*Ø�‰ØØØõ	r   c                 óò  — | j                   sàt        «       }| j                  j                  | j                  t        j                  |«      «       g | _         t        |j                  «      D ]P  }|j                  |   }t        j                  |«      j                  d«      }| j                   j                  |«       ŒR | j                  j                  t        j                  |«      «       | j                   S )a|  Get the category label list the ImageSegmenter can recognize.

    For CATEGORY_MASK type, the index in the category mask corresponds to the
    category in the label list.
    For CONFIDENCE_MASK type, the output mask list at index corresponds to the
    category in the label list.

    If there is no label map provided in the model file, empty label list is
    returned.
    zutf-8)rc   r   rZ   rC   r[   r   r}   rK   r   r   Ú	string_atÚdecodeÚappendrF   )rd   Úc_labelsrO   Úc_labelÚlabels        r   ÚlabelszImageSegmenter.labelsé  s¾   € ð �<Š<Ü“€hØ
‡i�i×)Ñ)Ø
�,‰,Ü
�,‰,�xÓ
 ôð €d„lÜ�X×)Ñ)Ó*ò #ˆ!Ø×"Ñ" 1Ñ%ˆÜ× Ñ  Ó)×0Ñ0°Ó9ˆØ�‰×Ñ˜EÕ"ð#ð ‡i�i× Ñ ¤§¡¨hÓ!7Ô8Ø�<‰<Ðr   c                 óÞ   — | j                   sy| j                  j                  | j                   «       d| _         | j                  j	                  «        | j                  j	                  «        y)zCloses ImageSegmenter.N)r[   rZ   rE   r\   Úclose©rd   s    r   r”   zImageSegmenter.close  sI   € à�<Š<ØØ‡I�I×#Ñ# D§L¡LÔ1Ø€D„LØ×Ñ×ÑÔØ‡I�I‡O�OÕr   c                 ó   — | S )z1Returns `self` upon entering the runtime context.r   r•   s    r   Ú	__enter__zImageSegmenter.__enter__  s   € à€Kr   c                 ó$   — | j                  «        y)aš  Closes ImageSegmenters and exits the context manager.

    Args:
      exc_type: The exception type that caused the context manager to exit.
      exc_value: The exception value that caused the context manager to exit.
      traceback: The exception traceback that caused the context manager to
        exit.

    Raises:
      RuntimeError: If the MediaPipe Gesture Recognizer task failed to
      close.
    N©r”   )rd   Úexc_typeÚ	exc_valueÚ	tracebacks       r   Ú__exit__zImageSegmenter.__exit__  s   € ð 	‡J�J…Lr   c                 ó$   — | j                  «        y ©Nr™   r•   s    r   Ú__del__zImageSegmenter.__del__  s   € Ø‡J�J…Lr   rŸ   )!r   r   r   r7   r
   ÚSerialDispatcherrR   r   r'   rz   r:   re   r;   Ústrrk   rT   ri   rL   rM   r   Ú_ImageProcessingOptionsrH   r†   rW   rˆ   rŠ   ÚpropertyrQ   r’   r”   r—   r�   r    r   r   r   rY   rY   à   s˜  … ñð0 	×*Ñ*Ó*Ø�?‰?ÓØ%Ó%Ø+Ó+ðà×-Ñ-ðð �o‰oðð )ð	ð
 /ðð óð* ð,¨cð ,Ð6Fò ,ó ð,ð0 ð6Ø)ð6àò6ó ð6ðv EIñ%à×Ñð%ð !)Ð)@Ñ Að%ð ó	%ðV EIñ	-à×Ñð-ð ð-ð !)Ð)@Ñ Að	-ð
 ó-ðf EIñ	,à×Ñð,ð ð,ð !)Ð)@Ñ Að	,ð
 ó,ð\ ð�d˜3‘iò ó ðò2òòór   rY   )2r7   r   ÚdataclassesÚtypingr   r   Úmediapipe.tasks.python.corer   r   Úbase_options_moduler   r   r	   r
   Ú1mediapipe.tasks.python.core.optional_dependenciesr   Ú"mediapipe.tasks.python.vision.corer   rL   r   Úimage_processing_options_moduler   r   ru   ÚBaseOptionsrU   ÚImageProcessingOptionsr£   ÚVisionTaskRunningModer=   ÚAsyncResultDispatcherrz   Ú	Structurer   r    Ú	CFUNCTYPEÚc_int32r   r'   Úc_int64r:   r+   ÚCStatusFunctionÚImageProcessingOptionsCÚ	CFunctionrx   Ú	dataclassrH   rT   rY   r   r   r   ú<module>r¸      sî  ðñ &ã Û ß %å ?Ý KÝ 6Ý <Ý 9Ý 9Ý JÝ DÝ jÝ IÝ ^à"×.Ñ.€Ø9×PÑPÐ Ø"×8Ñ8€Ø0×FÑFÐ ô�F×$Ñ$ô ô˜F×,Ñ,ô ð ,˜6×+Ñ+ØØ
‡N�NØ€F‡N�NÐ(Ó)Ø
‡O�OØ
‡N�NóÐ ô˜V×-Ñ-ô ðD &Ð×%Ñ%Ø àˆF�N‰NÐ1Ó2ØˆF�N‰N˜6Ÿ?™?Ó+ð	
óð &Ð×%Ñ%Ø&à�O‰OØ�O‰OØˆF�N‰NÐ5×MÑMÓNØˆF�N‰NÐ0Ó1ð		
óð &Ð×%Ñ%Ø)à�O‰OØ�O‰OØˆF�N‰NÐ5×MÑMÓNØ�N‰NØˆF�N‰NÐ0Ó1ð	
ó	ð &Ð×%Ñ%Ø&à�O‰OØ�O‰OØˆF�N‰NÐ5×MÑMÓNØ�N‰Nð		
óð &Ð×%Ñ%Ø#à�O‰OØˆF�N‰N˜=Ó)ð	
óð  Ð×ÑØ%Ø	ˆ�‰Ð-Ó	.Ð/Øóð
 &Ð×%Ñ%ØØ	�‰Ðóð  Ð×ÑØà�O‰Oð	
ð 	óði;Ð ð| ×Ñ÷"ð "ó ð"ðJ ×Ñ÷ð ó ð÷4@ò @r   