Ë
    »dæjyE  ã                   óÖ  — 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 ddlmZ ddlmZ ddlmZ  ejB                  Z"ejF                  Z$e jJ                  Z&ejN                  Z(ejR                  Z*ejV                  Z, G d„ dejZ                  «      Z. G d„ dejZ                  «      Z/ ej`                  d ejb                  e/«       ejb                  ejd                  «      f«       ej`                  dejd                  ejd                   ejb                  ejf                  «       ejb                  e
jh                  «      f«       ej`                  dejd                  ejd                   ejb                  ejf                  «      ejj                   ejb                  e
jh                  «      f«       ej`                  dejd                  ejd                   ejb                  ejf                  «      ejj                  f«       ejl                  d ejb                  e
jh                  «      gd«       ej`                  dejd                  f«      fZ7 ejp                  dejr                   ejb                  e
jh                  «      ejd                  ejj                  «      Z:ejv                   G d„ d«      «       Z< G d„ d«      Z=y) zMediaPipe image embedder task.é    N)ÚCallableÚOptional)Úembedding_result)Úembedding_result_c©Úcosine_similarity)Ú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                   óH   — e Zd ZdZdej
                  fdej
                  fgZy)Ú_EmbedderOptionsCzC struct for embedder options.Úl2_normalizeÚquantizeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚctypesÚc_boolÚ_fields_© ó    úo/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/vision/image_embedder.pyr   r   +   s&   „ Ù&ð �v—}‘}Ð%Ø�6—=‘=Ð!ð�(r    r   c                   óì   — e Zd ZdZdej
                  fdej                  fdefd ej                  dej                   ej                  ej                  «      ej                  ej                  «      fgZy)ÚImageEmbedderOptionsCz7The MediaPipe Tasks ImageEmbedderOptions CTypes struct.r
   Úrunning_modeÚembedder_optionsÚresult_callbackN)r   r   r   r   Úbase_options_c_moduleÚBaseOptionsCr   Úc_intr   Ú	CFUNCTYPEÚc_int32ÚPOINTERÚembedding_result_c_moduleÚEmbeddingResultCÚc_void_pÚc_int64r   r   r    r!   r#   r#   4   sw   „ Ù?ð Ð,×9Ñ9Ð:Ø�v—|‘|Ð$ØÐ,Ð-à
Ø
ˆ&×
Ñ
ØØ�n‰nØˆf�n‰nÐ6×GÑGÓHØ�o‰oØ�n‰nóð	ð	�(r    r#   ÚMpImageEmbedderCreateÚMpImageEmbedderEmbedImageÚMpImageEmbedderEmbedForVideoÚMpImageEmbedderEmbedAsyncÚMpImageEmbedderCloseResultÚMpImageEmbedderClosec                   óV  — e Zd ZU dZeed<   ej                  Zeed<   dZ	e
e   ed<   dZe
e   ed<   dZe
eeej"                  egdf      ed<   dZe
eej*                  ej,                  ej,                  ej.                  gdf      ed<   ej2                  d	ej6                  d
efd„«       Zy)ÚImageEmbedderOptionsaù  Options for the image embedder task.

  Attributes:
    base_options: Base options for the image embedder task.
    running_mode: The running mode of the task. Default to the image mode. Image
      embedder task has three running modes: 1) The image mode for embedding
      image on single image inputs. 2) The video mode for embedding image on the
      decoded frames of a video. 3) The live stream mode for embedding image on
      a live stream of input data, such as from camera.
    l2_normalize: Whether to normalize the returned feature vector with L2 norm.
      Use this option only if the model does not already contain a native
      L2_NORMALIZATION TF Lite Op. In most cases, this is already the case and
      L2 norm is thus achieved through TF Lite inference.
    quantize: Whether the returned embedding should be quantized to bytes via
      scalar quantization. Embeddings are implicitly assumed to be unit-norm and
      therefore any dimension is guaranteed to have a value in [-1.0, 1.0]. Use
      the l2_normalize option if this is not the case.
    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$   Nr   r   r&   Ú_result_callback_cÚ
dispatcherÚreturnc                 ó  — |j                  | j                  t        «      | _        | j                  j                  «       }t        | j                  | j                  ¬«      }t        || j                  j                  || j                  ¬«      S )z*Generates an ImageEmbedderOptionsC object.)r   r   )r
   r$   r%   r&   )Úwrap_callbackr&   ÚC_TYPES_RESULT_CALLBACKr9   r
   Ú	to_ctypesr   r   r   r#   r$   Úctype)Úselfr:   r   Úembedder_options_cs       r!   r?   zImageEmbedderOptions.to_ctypes²   s€   € ð
 )×6Ñ6Ø×ÑÔ5ó€DÔð ×&Ñ&×0Ñ0Ó2€NÜ*Ø×&Ñ&°·±ôÐô !Ø#Ø×&Ñ&×,Ñ,Ø+Ø×/Ñ/ô	ð r    )r   r   r   r   Ú_BaseOptionsÚ__annotations__Ú_RunningModeÚIMAGEr$   r   r   Úboolr   r&   r   ÚImageEmbedderResultÚimage_moduleÚImageÚintr9   r   r+   r/   r0   r   Údo_not_generate_docsr	   ÚAsyncResultDispatcherr#   r?   r   r    r!   r8   r8   †   så   … ñð, ÓØ+×1Ñ1€,�Ó1Ø!%€,�˜‘Ó%Ø!€(ˆH�T‰NÓ!ð ð �8ØÐ# \×%7Ñ%7¸Ð=¸tÐCÑDñó ð ð �hØà�n‰nØ�o‰oØ�o‰oØ�n‰nð	ð ðññ
ó 
ð ×$Ñ$ðØ/×EÑEðàòó %ñr    r8   c            	       óì  — e Zd ZU dZej
                  ed<   ej                  ed<   e	j                  ed<   dej
                  dej                  de	j                  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d„ Zedej8                  dej8                  d
efd„«       Zd„ Zd„ Z d„ Z!y)ÚImageEmbeddera  Class that performs embedding extraction on images.

  The API expects a TFLite model with optional, but strongly recommended,
  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).
    - only RGB inputs are supported (`channels` is required to be 3).
    - if type is kTfLiteFloat32, NormalizationOptions are required to be
      attached to the metadata for input normalization.
  At least one output tensor with:
    (kTfLiteUInt8/kTfLiteFloat32)
    - `N` components corresponding to the `N` dimensions of the returned
      feature vector for this output layer.
    - Either 2 or 4 dimensions, i.e. `[1 x N]` or `[1 x 1 x 1 x N]`.
  Ú_libÚ_handleÚ_dispatcherÚlibÚhandler:   c                 ó.   — || _         || _        || _        y)zãInitializes a new ImageEmbedder instance.

    Args:
      lib: The dispatcher to use for all C function calls.
      handle: The C pointer to the underlying image embedder.
      dispatcher: The handler for async results.
    N)rP   rQ   rR   )rA   rS   rT   r:   s       r!   Ú__init__zImageEmbedder.__init__Þ   s   € ð €D„IØ€D„LØ!€DÕr    Ú
model_pathr;   c                 ór   — t        |¬«      }t        |t        j                  ¬«      }| j	                  |«      S )aG  Creates an `ImageEmbedder` object from a TensorFlow Lite model and the default `ImageEmbedderOptions`.

    Note that the created `ImageEmbedder` instance is in image mode, for
    embedding image on single image inputs.

    Args:
      model_path: Path to the model.

    Returns:
      `ImageEmbedder` object that's created from the model file and the default
      `ImageEmbedderOptions`.

    Raises:
      ValueError: If failed to create `ImageEmbedder` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )Úmodel_asset_path)r
   r$   )rC   r8   rE   rF   Úcreate_from_options)ÚclsrW   r
   Úoptionss       r!   Úcreate_from_model_pathz$ImageEmbedder.create_from_model_pathï   s6   € ô&  °Ô<€LÜ"Ø!´×0BÑ0Bô€Gð ×"Ñ" 7Ó+Ð+r    r\   c                 ó.  — t        j                  |j                  |j                  «       t	        j
                  t        «      }dt        j                  t        j                  «      dt        j                  dt        dt        t        t        j                   t        f   fd„}t#        |¬«      }|j%                  |«      }t        j                  «       }|j'                  t        j(                  |«      t        j(                  |«      «        | |||¬«      S )aŒ  Creates the `ImageEmbedder` object from image embedder options.

    Args:
      options: Options for the image embedder task.

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

    Raises:
      ValueError: If failed to create `ImageEmbedder` object from
        `ImageEmbedderOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    Úc_result_ptrÚ	image_ptrÚtimestamp_msr;   c                 ó’   — | d   }t         j                  j                  |«      }t        j                  j                  |«      }|||fS )a;  Converts an async C++ result to Python objects.

      Args:
        c_result_ptr: The pointer to the C result object.
        image_ptr: The pointer to the C input image.
        timestamp_ms: The timestamp of the input image in milliseconds.

      Returns:
       The connverted Python objects as a tuple.
      r   )Úembedding_result_moduleÚEmbeddingResultÚfrom_ctypesrI   rJ   Úcreate_from_ctypes)r_   r`   ra   Úc_resultÚ	py_resultÚpy_images         r!   Úconvert_resultz9ImageEmbedder.create_from_options.<locals>.convert_result  sF   € ð" ˜a‘€hÜ)×9Ñ9×EÑEÀhÓO€iÜ×#Ñ#×6Ñ6°yÓA€hØ˜ <Ð0Ð0r    )Ú	converter)r:   )Úrunning_mode_moduleÚvalidate_running_moder$   r&   r   Úload_shared_libraryÚ_CTYPES_SIGNATURESr   r,   r-   r.   r/   rK   ÚtuplerH   rI   rJ   Ú_AsyncResultDispatcherr?   r1   Úbyref)r[   r\   rS   rj   r:   Úctypes_optionsÚembedder_handles          r!   rZ   z!ImageEmbedder.create_from_options  sè   € ô" ×-Ñ-Ø×Ñ˜g×5Ñ5ôô ×
2Ñ
2Ô3EÓ
F€Cð1Ü—n‘nÜ%×6Ñ6ó
ð1ô —?‘?ð	1ô
 ð1ô 
Ô"¤L×$6Ñ$6¼Ð;Ñ	<ó1ô, (°.ÔA€JØ×&Ñ& zÓ2€Nä—o‘oÓ'€OØ×ÑÜ�‰�^Ó$¤f§l¡l°?Ó&Côñ ˆs�O°
Ô;Ð;r    Nr   r   c                 ó¤  — |j                   }t        j                  «       }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  ||t        j                  |«      «       t        j                  j                  |«      }| j                  j                  t        j                  |«      «       |S )a  Performs image embedding extraction on the provided MediaPipe Image.

     Extraction is performed on the region of interest specified by the `roi`
     argument if provided, or on the entire image otherwise.

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

    Returns:
      An embedding result object that contains a list of embeddings.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If image embedder failed to run.
    N)Ú
_image_ptrr-   r.   r   rr   r?   rP   r2   rQ   rc   rd   re   r5   )rA   r   r   Úc_imagerg   Ú	options_crh   s          r!   ÚembedzImageEmbedder.embed=  s¨   € ð* ×Ñ€GÜ(×9Ñ9Ó;€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×'Ñ'Ø�‰ØØÜ�‰�XÓô	ô (×7Ñ7×CÑCÀHÓM€IØ‡I�I×(Ñ(¬¯©°hÓ)?Ô@ØÐr    ra   c           	      ó¦  — |j                   }t        j                  «       }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  |||t        j                  |«      «       t        j                  j                  |«      }| j                  j                  t        j                  |«      «       |S )ay  Performs image embedding extraction on the provided video frames.

    Extraction is performed on the region of interested specified by the `roi`
    argument if provided, or on the entire image otherwise.

    Only use this method when the ImageEmbedder 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:
      An embedding result object that contains a list of embeddings.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If image embedder failed to run.
    N)rv   r-   r.   r   rr   r?   rP   r3   rQ   rc   rd   re   r5   )rA   r   ra   r   rw   rg   rx   rh   s           r!   Úembed_for_videozImageEmbedder.embed_for_videoc  s«   € ð8 ×Ñ€GÜ(×9Ñ9Ó;€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×*Ñ*Ø�‰ØØØÜ�‰�XÓôô (×7Ñ7×CÑCÀHÓM€IØ‡I�I×(Ñ(¬¯©°hÓ)?Ô@ØÐr    c                 óº   — |j                   }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  |||«       y)a•  Sends live image data to embedder.

    The results will be available via the "result_callback" provided in the
    ImageEmbedderOptions. Embedding extraction is performed on the region of
    interested specified by the `roi` argument if provided, or on the entire
    image otherwise.

    Only use this method when the ImageEmbedder 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 `ImageEmbedderOptions`. The
    `embed_async` method is designed to process live stream data such as
    camera input. To lower the overall latency, image embedder may drop the
    input images if needed. In other words, it's not guaranteed to have output
    per input image.

    The `result_callback` provides:
      - An embedding result object that contains a list of embeddings.
      - The input image that the image embedder 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
        embedder has already processed.
    N)rv   r   rr   r?   rP   r4   rQ   )rA   r   ra   r   rw   rx   s         r!   Úembed_asynczImageEmbedder.embed_async’  sY   € ðJ ×Ñ€Gñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×'Ñ'Ø�‰ØØØõ	r    c                 óÞ   — | j                   sy| j                  j                  | j                   «       d| _         | j                  j	                  «        | j                  j	                  «        y)zCloses the ImageEmbedder.N)rQ   rP   r6   rR   Úclose©rA   s    r!   r   zImageEmbedder.closeÄ  sI   € à�<Š<ØØ‡I�I×"Ñ" 4§<¡<Ô0Ø€D„LØ×Ñ×ÑÔØ‡I�I‡O�OÕr    ÚuÚvc                 ó.   — t        j                   ||«      S )a=  Utility function to compute cosine similarity between two embedding entries.

    May return an InvalidArgumentError if e.g. the feature vectors are of
    different types (quantized vs. float), have different sizes, or have an
    L2-norm of 0.

    Args:
      u: An embedding entry.
      v: An embedding entry.

    Returns:
      The cosine similarity for the two embeddings.

    Raises:
      ValueError: May return an error if e.g. the feature vectors are of
        different types (quantized vs. float), have different sizes, or have
        an L2-norm of 0.
    r   )r[   r�   r‚   s      r!   r   zImageEmbedder.cosine_similarityÍ  s   € ô0 ×.Ñ.¨q°!Ó4Ð4r    c                 ó   — | S )z1Returns `self` upon entering the runtime context.r   r€   s    r!   Ú	__enter__zImageEmbedder.__enter__ç  s   € à€Kr    c                 ó*   — ~~~| j                  «        y)zFShuts down the MediaPipe task instance on exit of the context manager.N©r   )rA   Úexc_typeÚ	exc_valueÚ	tracebacks       r!   Ú__exit__zImageEmbedder.__exit__ë  s   € à�)˜YØ‡J�J…Lr    c                 ó$   — | j                  «        y ©Nr‡   r€   s    r!   Ú__del__zImageEmbedder.__del__ð  s   € Ø‡J�J…Lr    r�   )"r   r   r   r   r   ÚSerialDispatcherrD   r   r/   r	   rM   rV   ÚclassmethodÚstrr]   r8   rZ   rI   rJ   r   Ú_ImageProcessingOptionsrH   ry   rK   r{   r}   r   rc   Ú	EmbeddingÚfloatr   r…   r‹   rŽ   r   r    r!   rO   rO   Æ   s¦  … ñð& 	×*Ñ*Ó*Ø�?‰?ÓØ&×<Ñ<Ó<ð"à×-Ñ-ð"ð �o‰oð"ð *×?Ñ?ó	"ð" ð,¨cð ,°oò ,ó ð,ð0 ð2<Ø(ð2<àò2<ó ð2<ðn EIñ$à×Ñð$ð !)Ð)@Ñ Að$ð ó	$ðT EIñ	-à×Ñð-ð ð-ð !)Ð)@Ñ Að	-ð
 ó-ðf EIñ	0à×Ñð0ð ð0ð !)Ð)@Ñ Að	0ð
 ó0òdð ð5à	 ×	*Ñ	*ð5ð 
!×	*Ñ	*ð5ð ò	5ó ð5ò2òó
r    rO   )>r   r   ÚdataclassesÚtypingr   r   Ú,mediapipe.tasks.python.components.containersr   rc   r   r-   Ú'mediapipe.tasks.python.components.utilsr   Úmediapipe.tasks.python.corer	   r
   Úbase_options_moduler   r'   r   r   r   Ú1mediapipe.tasks.python.core.optional_dependenciesr   Ú"mediapipe.tasks.python.vision.corer   rI   r   Úimage_processing_options_moduler   Ú!image_processing_options_c_moduler   rl   rd   rH   ÚBaseOptionsrC   ÚVisionTaskRunningModerE   ÚImageProcessingOptionsr’   rM   rq   ÚLiveStreamPacketÚ_LiveStreamPacketÚ	Structurer   r#   ÚCStatusFunctionr,   r/   ÚImageProcessingOptionsCr.   r0   Ú	CFunctionro   r*   r+   r>   Ú	dataclassr8   rO   r   r    r!   ú<module>r©      sª  ðñ %ã Û ß %å dÝ hÝ EÝ ?Ý KÝ OÝ <Ý 9Ý 9Ý JÝ DÝ jÝ nÝ ^à-×=Ñ=Ð Ø"×.Ñ.€Ø"×8Ñ8€Ø9×PÑPÐ Ø0×FÑFÐ Ø+×<Ñ<Ð ô˜×(Ñ(ô ô˜F×,Ñ,ô ð* &Ð×%Ñ%ØàˆF�N‰NÐ0Ó1ØˆF�N‰N˜6Ÿ?™?Ó+ð	
óð &Ð×%Ñ%Ø#à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð ˆF�N‰NÐ4×EÑEÓFð	
ó
ð &Ð×%Ñ%Ø&à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð �N‰NØˆF�N‰NÐ4×EÑEÓFð	
óð &Ð×%Ñ%Ø#à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð �N‰Nð	
ó
ð  Ð×ÑØ$Ø	ˆ�‰Ð1×BÑBÓ	CÐDØóð
 &Ð×%Ñ%ØØ	�‰Ðóð_3Ð ðj +˜&×*Ñ*ØØ
‡N�NØ€F‡N�NÐ,×=Ñ=Ó>Ø
‡O�OØ
‡N�NóÐ ð ×Ñ÷<ð <ó ð<÷~kò kr    