Ë
    »dæj“O  ã                   ó¨  — d Z ddlZddlZddlmZmZ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! e
jD                  Z#ejH                  Z%e!jL                  Z'ejP                  Z)ejT                  Z+ejX                  Z- ej\                  dej^                   ej`                  ejb                  «      ejd                  ejf                  «      Z4 G d„ dejj                  «      Z6 ejn                  d ej`                  e6«       ej`                  ejd                  «      f«       ejn                  dejd                  ejd                   ej`                  ejp                  «       ej`                  ejb                  «      f«       ejn                  dejd                  ejd                   ej`                  ejp                  «      ejf                   ej`                  ejb                  «      f«       ejn                  dejd                  ejd                   ej`                  ejp                  «      ejf                  f«       ejr                  d ej`                  ejb                  «      gd«       ejn                  dejd                  f«      fZ:ejv                   G d„ d«      «       Z< G d„ d«      Z=y)zMediaPipe object detector task.é    N)ÚCallableÚListÚOptionalÚTuple)Ú
detections)Údetections_c)Ú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                   óÖ  — 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j                  ej                  «      fdej                  fd	 ej                  ej                  «      fd
ej                  fdefg
Zeej                  dej
                  dedee   dee   dee   deee      d	eee      dedd fd„«       «       Zy)ÚObjectDetectorOptionsCz.The object detector options used in the C API.r
   Úrunning_modeÚdisplay_names_localeÚmax_resultsÚscore_thresholdÚcategory_allowlistÚcategory_allowlist_countÚcategory_denylistÚcategory_denylist_countÚresult_callbackÚreturnc	                 óÊ   — t        j                  |«      }	t        j                  |«      }
|rt        |«      nd}|rt        |«      nd} | ||j                  ||||	||
||¬«
      S )z@Creates an ObjectDetectorOptionsC object from the given options.r   )
r
   r   r   r   r   r   r   r   r   r   )Úmediapipe_c_bindings_c_moduleÚconvert_strings_to_ctypes_arrayÚlenÚctype)Úclsr
   r   r   r   r   r   r   r   Úcategory_allowlist_cÚcategory_denylist_cÚcategory_allowlist_count_cÚcategory_denylist_count_cs                úp/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/vision/object_detector.pyÚfrom_c_optionsz%ObjectDetectorOptionsC.from_c_optionsC   s�   € ô 	&×EÑEØó	
ð ô 	&×EÑEØó	
ð ñ $6ŒÐÔ¸1ð ñ #4ŒÐÔ¸ð ñ Ø!Ø!×'Ñ'Ø1ØØ'Ø/Ø!;Ø-Ø 9Ø'ôð ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úbase_options_c_moduleÚBaseOptionsCÚctypesÚc_intÚc_char_pÚc_floatÚPOINTERÚ_C_TYPES_RESULT_CALLBACKÚ_fields_Úclassmethodr   Údo_not_generate_docsÚ_RunningModer   ÚstrÚintÚfloatr   r+   © r,   r*   r   r   3   s:  „ Ù6ð Ð,×9Ñ9Ð:Ø�v—|‘|Ð$Ø˜vŸ™Ð/Ø�f—l‘lÐ#Ø˜&Ÿ.™.Ð)Ø˜^˜VŸ^™^¨F¯O©OÓ<Ð=Ø! 6§<¡<Ð0Ø˜N˜FŸN™N¨6¯?©?Ó;Ð<Ø  &§,¡,Ð/ØÐ2Ð3ð€(ð Ø×$Ñ$ð)à)×6Ñ6ð)ð !ð)ð % S™Mð	)ð
 ˜C‘=ð)ð   ‘ð)ð # 4¨¡9Ñ-ð)ð " $ s¡)Ñ,ð)ð 0ð)ð  ò)ó %ó ñ)r,   r   ÚMpObjectDetectorCreateÚMpObjectDetectorDetectImageÚMpObjectDetectorDetectForVideoÚMpObjectDetectorDetectAsyncÚMpObjectDetectorCloseResultÚMpObjectDetectorClosec                   óú   — 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d	<   dZe
ee      ed
<   dZe
ee      ed<   dZe
eej,                  ej0                  egdf      ed<   y)ÚObjectDetectorOptionsa÷  Options for the object detector task.

  Attributes:
    base_options: Base options for the object detector task.
    running_mode: The running mode of the task. Default to the image mode.
      Object detector task has three running modes: 1) The image mode for
      detecting objects on single image inputs. 2) The video mode for detecting
      objects on the decoded frames of a video. 3) The live stream mode for
      detecting objects on a live stream of input data, such as from camera.
    display_names_locale: The locale to use for display names specified through
      the TFLite Model Metadata.
    max_results: The maximum number of top-scored classification results to
      return.
    score_threshold: Overrides the ones provided in the model metadata. Results
      below this value are rejected.
    category_allowlist: Allowlist of category names. If non-empty, detection
      results whose category name is not in this set will be filtered out.
      Duplicate or unknown category names are ignored. Mutually exclusive with
      `category_denylist`.
    category_denylist: Denylist of category names. If non-empty, detection
      results whose category name is in this set will be filtered out. Duplicate
      or unknown category names are ignored. Mutually exclusive with
      `category_allowlist`.
    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   g        r   r   r   r   )r-   r.   r/   r0   Ú_BaseOptionsÚ__annotations__r<   ÚIMAGEr   r   r   r=   r   r>   r   r?   r   r   r   r   r   Údetections_moduleÚDetectionResultÚimage_moduleÚImager@   r,   r*   rH   rH   §   s«   … ñð8 ÓØ+×1Ñ1€,�Ó1Ø(,Ð˜ ™Ó,Ø!€+ˆx˜‰}Ó!Ø%(€/�8˜E‘?Ó(Ø,0Ð�h˜t C™yÑ)Ó0Ø+/Ð�X˜d 3™iÑ(Ó/ð
 ð	 �8ØØ×,Ñ,¨l×.@Ñ.@À#Ð
FÈÐ
Lññô r,   rH   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
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d„ Zd„ Zd„ Zy)ÚObjectDetectoraÞ  Class that performs object detection 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).
    - 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.
  Output tensors must be the 4 outputs of a `DetectionPostProcess` op, i.e:
    (kTfLiteFloat32)
    - locations tensor of size `[num_results x 4]`, the inner array
      representing bounding boxes in the form [top, left, right, bottom].
    - BoundingBoxProperties are required to be attached to the metadata
      and must specify type=BOUNDARIES and coordinate_type=RATIO.
    (kTfLiteFloat32)
    - classes tensor of size `[num_results]`, each value representing the
      integer index of a class.
    - optional (but recommended) label map(s) can be attached as
      AssociatedFile-s with type TENSOR_VALUE_LABELS, containing one label per
      line. The first such AssociatedFile (if any) is used to fill the
      `class_name` field of the results. The `display_name` field is filled
      from the AssociatedFile (if any) whose locale matches the
      `display_names_locale` field of the `ObjectDetectorOptions` used at
      creation time ("en" by default, i.e. English). If none of these are
      available, only the `index` field of the results will be filled.
    (kTfLiteFloat32)
    - scores tensor of size `[num_results]`, each value representing the score
      of the detected object.
    - optional score calibration can be attached using ScoreCalibrationOptions
      and an AssociatedFile with type TENSOR_AXIS_SCORE_CALIBRATION. See
      metadata_schema.fbs [1] for more details.
    (kTfLiteFloat32)
    - integer num_results as a tensor of size `[1]`

  An example of such model can be found at:
  https://tfhub.dev/google/lite-model/object_detection/mobile_object_localizer_v1/1/metadata/1

  [1]:
  https://github.com/google/mediapipe/blob/6cdc6443b6a7ed662744e2a2ce2d58d9c83e6d6f/mediapipe/tasks/metadata/metadata_schema.fbs#L456
  Ú_libÚ_handleÚ_dispatcherÚ_async_callbackÚlibÚhandleÚ
dispatcherÚasync_callbackc                 ó<   — || _         || _        || _        || _        y)a&  Initializes the object detector.

    Args:
      lib: The dispatch library to use for the object detector.
      handle: The C pointer to the object detector.
      dispatcher: The async result handler for the object detector.
      async_callback: The c callback for the object detector.
    N)rS   rT   rU   rV   )ÚselfrW   rX   rY   rZ   s        r*   Ú__init__zObjectDetector.__init__  s"   € ð €D„IØ€D„LØ!€DÔØ)€DÕr,   Ú
model_pathr   c                 ór   — t        |¬«      }t        |t        j                  ¬«      }| j	                  |«      S )aO  Creates an `ObjectDetector` object from a TensorFlow Lite model and the default `ObjectDetectorOptions`.

    Note that the created `ObjectDetector` instance is in image mode, for
    detecting objects on single image inputs.

    Args:
      model_path: Path to the model.

    Returns:
      `ObjectDetector` object that's created from the model file and the default
      `ObjectDetectorOptions`.

    Raises:
      ValueError: If failed to create `ObjectDetector` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )Úmodel_asset_path)r
   r   )rJ   rH   r<   rL   Úcreate_from_options)r%   r^   r
   Úoptionss       r*   Úcreate_from_model_pathz%ObjectDetector.create_from_model_path  s6   € ô&  °Ô<€LÜ#Ø!´×0BÑ0Bô€Gð ×"Ñ" 7Ó+Ð+r,   rb   c                 ó6  — 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%                  |j                  t&        «      }t(        j+                  |j,                  j/                  «       |j                  |j0                  |j2                  |j4                  |j6                  |j8                  |¬«      }t        j                  «       }|j;                  t        j<                  |«      t        j<                  |«      «       t?        ||||¬«      S )	a’  Creates the `ObjectDetector` object from object detector options.

    Args:
      options: Options for the object detector task.

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

    Raises:
      ValueError: If failed to create `ObjectDetector` object from
        `ObjectDetectorOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    Úc_result_ptrÚ	image_ptrÚtimestamp_msr   c                 ó~   — | d   }t         j                  |«      }t        j                  j	                  |«      }|||fS )Nr   )ÚObjectDetectorResultÚfrom_ctypesrO   rP   Úcreate_from_ctypes)re   rf   rg   Úc_resultÚ	py_resultÚpy_images         r*   Úconvert_resultz:ObjectDetector.create_from_options.<locals>.convert_resultJ  s@   € ð
 ˜a‘€hÜ&×2Ñ2°8Ó<€iÜ×#Ñ#×6Ñ6°yÓA€hØ˜ <Ð0Ð0r,   )Ú	converter)r
   r   r   r   r   r   r   r   )rW   rX   rY   rZ   ) Úrunning_mode_moduleÚvalidate_running_moder   r   r!   Úload_shared_libraryÚ_CTYPES_SIGNATURESr3   r7   Údetections_c_moduleÚDetectionResultCÚc_void_pr>   r   ri   rO   rP   Ú_AsyncResultDispatcherÚwrap_callbackr8   r   r+   r
   Ú	to_ctypesr   r   r   r   r   rA   ÚbyrefrR   )r%   rb   rW   ro   rY   Ú
c_callbackÚctypes_optionsÚdetector_handles           r*   ra   z"ObjectDetector.create_from_options2  s_  € ô$ ×-Ñ-Ø×Ñ˜g×5Ñ5ôô (×
;Ñ
;Ô<NÓ
O€Cð1Ü—n‘nÔ%8×%IÑ%IÓJð1ä—?‘?ð1ô ð1ô 
Ô#¤\×%7Ñ%7¼Ð<Ñ	=ó	1ô (°.ÔA€JØ×)Ñ)Ø×ÑÔ!9ó€Jô ,×:Ñ:Ø×)Ñ)×3Ñ3Ó5Ø×)Ñ)Ø$×9Ñ9Ø×'Ñ'Ø×/Ñ/Ø"×5Ñ5Ø!×3Ñ3Ø"ð ;ó 	€Nô —o‘oÓ'€OØ×ÑÜ�‰�^Ó$Ü�‰�_Ó%ôô
 ØØØØ!ô	ð r,   Nr   r   c                 ó�  — |j                   }t        j                  «       }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  ||t        j                  |«      «       t        j                  |«      }| j                  j                  t        j                  |«      «       |S )aÀ  Performs object detection on the provided MediaPipe Image.

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

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

    Returns:
      A detection result object that contains a list of detections, each
      detection has a bounding box that is expressed in the unrotated input
      frame of reference coordinates system, i.e. in `[0,image_width) x [0,
      image_height)`, which are the dimensions of the underlying image data.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If object detection failed to run.
    N)Ú
_image_ptrru   rv   r3   r{   rz   rS   rB   rT   ri   rj   rE   )r\   r   r   Úc_imagerl   Úc_image_processing_optionsrm   s          r*   ÚdetectzObjectDetector.detectq  s¢   € ð2 ×Ñ€GÜ"×3Ñ3Ó5€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×)Ñ)Ø�‰ØØ"Ü�‰�XÓô	ô %×0Ñ0°Ó:€IØ‡I�I×)Ñ)¬&¯,©,°xÓ*@ÔAØÐr,   rg   c           	      ó’  — |j                   }t        j                  «       }|r#t        j                  |j                  «       «      nd}| j                  j                  | j                  |||t        j                  |«      «       t        j                  |«      }| j                  j                  t        j                  |«      «       |S )aÏ  Performs object detection on the provided video frames.

    Only use this method when the ObjectDetector 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:
      A detection result object that contains a list of detections, each
      detection has a bounding box that is expressed in the unrotated input
      frame of reference coordinates system, i.e. in `[0,image_width) x [0,
      image_height)`, which are the dimensions of the underlying image data.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If object detection failed to run.
    N)r€   ru   rv   r3   r{   rz   rS   rC   rT   ri   rj   rE   )r\   r   rg   r   r�   rl   r‚   rm   s           r*   Údetect_for_videozObjectDetector.detect_for_video�  s¥   € ð8 ×Ñ€GÜ"×3Ñ3Ó5€Hñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×,Ñ,Ø�‰ØØ"ØÜ�‰�XÓôô %×0Ñ0°Ó:€IØ‡I�I×)Ñ)¬&¯,©,°xÓ*@ÔAØÐr,   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 object detection.

    Only use this method when the ObjectDetector 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 `ObjectDetectorOptions`. The
    `detect_async` method is designed to process live stream data such as camera
    input. To lower the overall latency, object detector 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 detection result object that contains a list of detections, each
        detection has a bounding box that is expressed in the unrotated input
        frame of reference coordinates system, i.e. in `[0,image_width) x [0,
        image_height)`, which are the dimensions of the underlying image data.
      - The input image that the object detector 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 object
        detector has already processed.
      RuntimeError: If object detection failed to run.
    N)r€   r3   r{   rz   rS   rD   rT   )r\   r   rg   r   r�   r‚   s         r*   Údetect_asynczObjectDetector.detect_asyncÍ  sY   € ðH ×Ñ€Gñ $ô 	�‰Ð-×7Ñ7Ó9Ô:àð ð
 	‡I�I×)Ñ)Ø�‰ØØ"Øõ	r,   c                 óÞ   — | j                   ra| j                  j                  | j                   «       d| _         | j                  j	                  «        | j                  j	                  «        yy)z'Shuts down the MediaPipe task instance.N)rT   rS   rF   rU   Úclose©r\   s    r*   r‰   zObjectDetector.closeÿ  sK   € à‡|‚|Ø
‡i�i×%Ñ% d§l¡lÔ3Ø€d„lØ
×Ñ×ÑÔØ
‡i�i‡o�oÕð	 r,   c                 ó   — | S )z1Returns `self` upon entering the runtime context.r@   rŠ   s    r*   Ú	__enter__zObjectDetector.__enter__  s   € à€Kr,   c                 ó*   — ~~~| j                  «        y)a^  Shuts down the MediaPipe task instance on exit of the context manager.

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

    Raises:
      RuntimeError: If the MediaPipe FaceDetector task failed to close.
    N©r‰   )r\   Úexc_typeÚ	exc_valueÚ	tracebacks       r*   Ú__exit__zObjectDetector.__exit__  s   € ð 	�)˜YØ‡J�J…Lr,   c                 ó$   — | j                  «        y ©NrŽ   rŠ   s    r*   Ú__del__zObjectDetector.__del__  s   € Ø‡J�J…Lr,   r”   )r-   r.   r/   r0   r   ÚSerialDispatcherrK   r3   rw   rx   r8   r]   r:   r=   rc   rH   ra   rO   rP   r   Ú_ImageProcessingOptionsri   rƒ   r>   r…   r‡   r‰   rŒ   r’   r•   r@   r,   r*   rR   rR   Ó   sp  … ñ*ðX 	×*Ñ*Ó*Ø�?‰?ÓØ%Ó%Ø+Ó+ð*à×-Ñ-ð*ð �o‰oð*ð )ð	*ð
 /ó*ð( ð,¨cð ,Ð6Fò ,ó ð,ð0 ð;Ø)ð;àò;ó ð;ðB EIñ*à×Ñð*ð !)Ð)@Ñ Að*ð ó	*ð` EIñ	.à×Ñð.ð ð.ð !)Ð)@Ñ Að	.ð
 ó.ðh EIñ	0à×Ñð0ð ð0ð !)Ð)@Ñ Að	0ð
 ó0òdòòór,   rR   )>r0   r3   ÚdataclassesÚtypingr   r   r   r   Ú,mediapipe.tasks.python.components.containersr   rM   r   ru   Úmediapipe.tasks.python.corer	   r
   Úbase_options_moduler   r1   r   r!   r   r   Ú1mediapipe.tasks.python.core.optional_dependenciesr   Ú"mediapipe.tasks.python.vision.corer   rO   r   Úimage_processing_options_moduler   Ú!image_processing_options_c_moduler   rq   rN   ri   ÚBaseOptionsrJ   ÚVisionTaskRunningModer<   ÚImageProcessingOptionsr—   ÚAsyncResultDispatcherrx   ÚLiveStreamPacketÚ_LiveStreamPacketÚ	CFUNCTYPEÚc_int32r7   rv   rw   Úc_int64r8   Ú	Structurer   ÚCStatusFunctionÚImageProcessingOptionsCÚ	CFunctionrt   Ú	dataclassrH   rR   r@   r,   r*   ú<module>r¯      s™  ðñ &ã Û ß 2Ó 2å XÝ \Ý ?Ý KÝ OÝ ]Ý 9Ý 9Ý JÝ DÝ jÝ nÝ ^à(×8Ñ8Ð Ø"×.Ñ.€Ø"×8Ñ8€Ø9×PÑPÐ Ø0×FÑFÐ Ø+×<Ñ<Ð ð ,˜6×+Ñ+ØØ
‡N�NØ€F‡N�NÐ&×7Ñ7Ó8Ø
‡O�OØ
‡N�NóÐ ô;˜V×-Ñ-ô ;ð~ &Ð×%Ñ%Ø àˆF�N‰NÐ1Ó2ØˆF�N‰N˜6Ÿ?™?Ó+ð	
óð &Ð×%Ñ%Ø%à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð ˆF�N‰NÐ.×?Ñ?Ó@ð	
ó
ð &Ð×%Ñ%Ø(à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð �N‰NØˆF�N‰NÐ.×?Ñ?Ó@ð	
óð &Ð×%Ñ%Ø%à�O‰OØ�O‰OØˆF�N‰NØ1×IÑIóð �N‰Nð	
ó
ð  Ð×ÑØ%Ø	ˆ�‰Ð+×<Ñ<Ó	=Ð>Øóð
 &Ð×%Ñ%ØØ	�‰Ðóð_3Ð ðl ×Ñ÷(ð (ó ð(÷VGò Gr,   