
    WpfU:                        d 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j"        Z#ej$        Z%ej&        Z'ej(        Z)ej*        Z+ej,        Z-ej.        Z/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9ej:         G d d                      Z; G d dej<                  Z=dS )z MediaPipe audio classifier task.    N)CallableMappingListOptional)packet_creator)packet_getter)packet)"audio_classifier_graph_options_pb2)classifications_pb2)classifier_options_pb2)audio_task_running_mode)base_audio_task_api)
audio_data)classification_result)base_options)	task_info)doc_controlsaudio_inAUDIOclassifications_outCLASSIFICATIONSsample_rate_inSAMPLE_RATEz;mediapipe.tasks.audio.audio_classifier.AudioClassifierGraphtimestamped_classifications_outTIMESTAMPED_CLASSIFICATIONSi  c                   $   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egdf                  ed
<   ej        defd            ZdS )AudioClassifierOptionsaG  Options for the audio classifier task.

  Attributes:
    base_options: Base options for the audio classifier task.
    running_mode: The running mode of the task. Default to the audio clips mode.
      Audio classifier task has two running modes: 1) The audio clips mode for
      running classification on independent audio clips. 2) The audio stream
      mode for running classification on the audio stream, such as from
      microphone. In this mode,  the "result_callback" below must be specified
      to receive the classification results asynchronously.
    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,
      classification 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, classification
      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 audio
      stream data. The result callback should only be specified when the running
      mode is set to the audio stream mode.
  r   running_modeNdisplay_names_localemax_resultsscore_thresholdcategory_allowlistcategory_denylistresult_callbackreturnc                     | j                                         }| j        t          j        k    rdnd|_        t          | j        | j        | j	        | j
        | j                  }t          ||          S )z4Generates an AudioClassifierOptions protobuf object.FT)r!   r"   r#   r   r    )r   classifier_options)r   to_pb2r   _RunningModeAUDIO_CLIPSuse_stream_mode_ClassifierOptionsProtor!   r"   r#   r   r    !_AudioClassifierGraphOptionsProto)selfbase_options_protoclassifier_options_protos      m/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/tasks/python/audio/audio_classifier.pyr(   zAudioClassifierOptions.to_pb2\   s     *1133262C|G_2_2_ei&6,20!6$ &  &  & -'35 5 5 5    )__name__
__module____qualname____doc___BaseOptions__annotations__r)   r*   r   r   r   strr    intr!   floatr"   r   r#   r$   r   AudioClassifierResultr   do_not_generate_docsr-   r(    r2   r1   r   r   5   s         8 +7,777(,,,,#+x}###%)/8E?))),0htCy)000+/Xd3i(///LP/8H&;S%A4%GHIPPP$57 5 5 5 %$5 5 5r2   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e	de
e         fdZd	e	d
eddfdZdS )AudioClassifiera  Class that performs audio classification on audio data.

  This API expects a TFLite model with mandatory TFLite Model Metadata that
  contains the mandatory AudioProperties of the solo input audio tensor and the
  optional (but recommended) category labels as AssociatedFiles with type
  TENSOR_AXIS_LABELS per output classification tensor.

  Input tensor:
    (kTfLiteFloat32)
    - input audio buffer of size `[batch * samples]`.
    - batch inference is not supported (`batch` is required to be 1).
    - for multi-channel models, the channels must be interleaved.
  At least one output tensor with:
    (kTfLiteFloat32)
    - `[1 x N]` array with `N` represents the number of categories.
    - optional (but recommended) category labels as AssociatedFiles with type
      TENSOR_AXIS_LABELS, containing one label per line. The first such
      AssociatedFile (if any) is used to fill the `category_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
      `AudioClassifierOptions` 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.
  
model_pathr%   c                     t          |          }t          |t          j                  }|                     |          S )aY  Creates an `AudioClassifier` object from a TensorFlow Lite model and the default `AudioClassifierOptions`.

    Note that the created `AudioClassifier` instance is in audio clips mode, for
    classifying on independent audio clips.

    Args:
      model_path: Path to the model.

    Returns:
      `AudioClassifier` object that's created from the model file and the
      default `AudioClassifierOptions`.

    Raises:
      ValueError: If failed to create `AudioClassifier` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )model_asset_path)r   r   )r7   r   r)   r*   create_from_options)clsrA   r   optionss       r1   create_from_model_pathz&AudioClassifier.create_from_model_path   sG    &  <<<L$!0HJ J JG""7+++r2   rF   c           	         dt           t          t          j        f         ffd}t	          t
          d                    t          t          g          d                    t          t          g          gd                    t          t          g          d                    t          t          g          g          } | |                    d          j        j        r|nd          S )a  Creates the `AudioClassifier` object from audio classifier options.

    Args:
      options: Options for the audio classifier task.

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

    Raises:
      ValueError: If failed to create `AudioClassifier` object from
        `AudioClassifierOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    output_packetsc                    | t                    j        j        t          z  }| t                                                    r&                    t          g           |           d S t          j                    }|	                    t          j        | t                                                             t                              |          |           d S )N)classifications)_CLASSIFICATIONS_STREAM_NAME	timestampvalue_MICRO_SECONDS_PER_MILLISECONDis_emptyr$   r<   r   ClassificationResultCopyFromr   	get_protocreate_from_pb2)rI   timestamp_msclassification_result_protorF   s      r1   packets_callbackz=AudioClassifier.create_from_options.<locals>.packets_callback   s    #
&((1%;YZl	4	5	>	>	@	@ !"555|	E 	E 	E$7$L$N$N!!**

!.1M"N
O
OQ Q Q

/
/0K
L
L
    r2   :)
task_graphinput_streamsoutput_streamstask_optionsF)enable_flow_limitingN)r   r9   r	   Packet	_TaskInfo_TASK_GRAPH_NAMEjoin
_AUDIO_TAG_AUDIO_IN_STREAM_NAME_SAMPLE_RATE_TAG_SAMPLE_RATE_IN_STREAM_NAME_CLASSIFICATIONS_TAGrL    _TIMESTAMPED_CLASSIFICATIONS_TAG(_TIMESTAMPED_CLASSIFICATIONS_STREAM_NAMEgenerate_graph_configr   r$   )rE   rF   rW   r   s    `  r1   rD   z#AudioClassifier.create_from_options   s    "fm1C)D       #HHj"7899HH&(CDEE

 HH*,HIJJHH08  
   I 3 	''U'CC#3=? ? ?r2   
audio_clipc                    |j         j        st          d          |                     t          t          j        |j        d          t          t          j	        |j         j                  i          }g }t          j        |t                             }|D ]W}t          j                    }|                    |           |                    t"                              |                     X|S )a
  Performs audio classification on the provided audio clip.

    The audio clip is represented as a MediaPipe AudioData. The method accepts
    audio clips with various length and audio sample rate. It's required to
    provide the corresponding audio sample rate within the `AudioData` object.

    The input audio clip may be longer than what the model is able to process
    in a single inference. When this occurs, the input audio clip is split into
    multiple chunks starting at different timestamps. For this reason, this
    function returns a vector of ClassificationResult objects, each associated
    ith a timestamp corresponding to the start (in milliseconds) of the chunk
    data that was classified, e.g:

    ClassificationResult #0 (first chunk of data):
      timestamp_ms: 0 (starts at 0ms)
      classifications #0 (single head model):
        category #0:
          category_name: "Speech"
          score: 0.6
        category #1:
          category_name: "Music"
          score: 0.2
    ClassificationResult #1 (second chunk of data):
      timestamp_ms: 800 (starts at 800ms)
      classifications #0 (single head model):
        category #0:
          category_name: "Speech"
          score: 0.5
       category #1:
         category_name: "Silence"
         score: 0.1

    Args:
      audio_clip: MediaPipe AudioData.

    Returns:
      An `AudioClassifierResult` object that contains a list of
      classification result objects, each associated with a timestamp
      corresponding to the start (in milliseconds) of the chunk data that was
      classified.

    Raises:
      ValueError: If any of the input arguments is invalid, such as the sample
        rate is not provided in the `AudioData` object.
      RuntimeError: If audio classification failed to run.
    1Must provide the audio sample rate in audio data.T	transpose)audio_formatsample_rate
ValueError_process_audio_cliprc   r   create_matrixbufferre   create_doubler   get_proto_listrh   r   rQ   rR   appendr<   rT   )r.   rj   rI   output_list classification_result_proto_listprotorV   s          r1   classifyzAudioClassifier.classify   s    ^ ". LJKKK--():dKKK#()@)LMM	/  N K'4'C?@(B (B$1 N N$7$L$N$N!!**5111

/
/0K
L
LN N N Nr2   audio_blockrU   Nc                    |j         j        st          d          | j        s2|j         j        | _        |                     t
          | j                   n:|j         j        | j        k    r%t          d|j         j         d| j         d          |                     t          t          j	        |j
        d                              |t          z            i           dS )a  Sends audio data (a block in a continuous audio stream) to perform audio classification.

    Only use this method when the AudioClassifier is created with the audio
    stream running mode. The input timestamps should be monotonically increasing
    for adjacent calls of this method. This method will return immediately after
    the input audio data is accepted. The results will be available via the
    `result_callback` provided in the `AudioClassifierOptions`. The
    `classify_async` method is designed to process auido stream data such as
    microphone input.

    The input audio data may be longer than what the model is able to process
    in a single inference. When this occurs, the input audio block is split
    into multiple chunks. For this reason, the callback may be called multiple
    times (once per chunk) for each call to this function.

    The `result_callback` provides:
      - An `AudioClassifierResult` object that contains a list of
        classifications.
      - The input timestamp in milliseconds.

    Args:
      audio_block: MediaPipe AudioData.
      timestamp_ms: The timestamp of the input audio data in milliseconds.

    Raises:
      ValueError: If any of the followings:
        1) The sample rate is not provided in the `AudioData` object or the
        provided sample rate is inconsistent with the previously received.
        2) The current input timestamp is smaller than what the audio
        classifier has already processed.
    rl   z.The audio sample rate provided in audio data: z/ is inconsistent with the previously received: .Trm   N)ro   rp   rq   _default_sample_rate_set_sample_ratere   _send_audio_stream_datarc   r   rs   rt   atrO   )r.   r|   rU   s      r1   classify_asynczAudioClassifier.classify_async  s   @ #/ LJKKK$ D"-":"Fd
7 57 7 7 7		!	-1J	J	JC%1C C&*&?C C CD D D
 	  ();tLLLOO==? ?"     r2   )r3   r4   r5   r6   classmethodr9   rG   r   rD   
_AudioDatar   r<   r{   r:   r   r>   r2   r1   r@   r@   m   s         2 ,c ,6G , , , ;,. 1?#91?>O1? 1? 1? ;1?f? ?5J0K ? ? ? ?B0
 0# 0$ 0 0 0 0 0 0r2   r@   )>r6   dataclassestypingr   r   r   r   mediapipe.pythonr   r   $mediapipe.python._framework_bindingsr	   /mediapipe.tasks.cc.audio.audio_classifier.protor
   .mediapipe.tasks.cc.components.containers.protor   .mediapipe.tasks.cc.components.processors.protor   !mediapipe.tasks.python.audio.corer   running_mode_moduler   ,mediapipe.tasks.python.components.containersr   audio_data_moduler   classification_result_modulemediapipe.tasks.python.corer   base_options_moduler   task_info_module1mediapipe.tasks.python.core.optional_dependenciesr   rQ   r<   AudioClassifierGraphOptionsr-   	AudioDatar   BaseOptionsr7   ClassifierOptionsr,   AudioTaskRunningModer)   TaskInfor_   rc   rb   rL   rf   re   rd   r`   rh   rg   rO   	dataclassr   BaseAudioTaskApir@   r>   r2   r1   <module>r      s   ' &     4 4 4 4 4 4 4 4 4 4 4 4 + + + + + + * * * * * * 7 7 7 7 7 7 ^ ^ ^ ^ ^ ^ N N N N N N Q Q Q Q Q Q \ \ \ \ \ \ A A A A A A X X X X X X n n n n n n K K K K K K E E E E E E J J J J J J4I $F$b !(
".0B "7%	" 
4 ( .   P +L (#@  !%  45 45 45 45 45 45 45 45nW W W W W): W W W W Wr2   