o
    *jk                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlZd dl	Z
d dl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mZ d dlmZ d dlmZ d d	lmZ e Zejejejd
G dd deZ dS )    N)AnyDictListUnion)File)	Pipelines)
OutputKeys)
InputModelPipeline)	PIPELINES)Tasks)
get_logger)module_namec                	       s   e Zd ZdZdef fddZ			ddeeje	f de
d	ed
efddZde	fddZ	ddejdeeje	f fddZdeeje	f fddZdeejejf deejejf defddZ  ZS )ResNet_PipelineaZ  Speaker Verification Inference Pipeline
    use `model` to create a Speaker Verification pipeline.

    Args:
        model (SpeakerVerificationPipeline): A model instance, or a model local dir, or a model id in the model hub.
        kwargs (dict, `optional`):
            Extra kwargs passed into the pipeline's constructor.
    Example:
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> p = pipeline(
    >>>    task=Tasks.speaker_verification, model='iic/speech_resnet34_sv_zh-cn_3dspeaker_16k')
    >>> print(p([audio_1, audio_2]))

    modelc                    s@   t  jdd|i| | jj| _| jj| _| jd | _i | _dS )zuse `model` to create a speaker verification pipeline for prediction
        Args:
            model (str): a valid official model id
        r   ZyesOrno_thrN )super__init__r   model_configZother_configconfigthr	save_dict)selfr   kwargs	__class__r   /var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/speaker_verification_resnet_pipeline.pyr   +   s
   


zResNet_Pipeline.__init__NF	in_audiossave_dir
output_embr   c                 C   sx   |d ur|| _ | j dk s| j dkrtd| j  | |}| |}| |||}|r:|| jd< | | jd< | jS |S )N   zImodelscope error: the thr value should be in [-1, 1], but found to be %f.outputsembs)r   
ValueError
preprocessforwardpostprocessr   numpy)r   r   r   r   r   Zwavsr#   r"   r   r   r   __call__6   s    


zResNet_Pipeline.__call__inputsc                 C   s,   g }|D ]
}| | | qt|}|S N)appendr   torchcat)r   r*   r#   xr   r   r   r&   K   s
   
zResNet_Pipeline.forwardc           
   	   C   s   t |d tr8|d ur8tj|dd t|D ]!\}}tj|dtj|ddd  }t	
|||   qt|dkra| |d |d }t|d}|| jkrUd	}nd
}tj|tj|i}	|	S tjdi}	|	S )Nr   T)exist_okz%s.npy.r!         yesnozNo similarity score output)
isinstancestrosmakedirs	enumeratepathjoinbasenamersplitnpsaver(   lencompute_cos_similarityroundr   r   ZSCOREZTEXT)
r   r*   r   r   ipZ	save_pathZscoreZansoutputr   r   r   r'   R   s&   


zResNet_Pipeline.postprocessc                 C   sD  g }t t|D ]}t|| tret|| }tjt|dd\}}t|j	dkr3|d d df }t
|d}|| jd kr_td| jd   tjj||dt| jd ggd\}}|d}n5t|| tjrt|| j	d	kszJ d
|| }|jdv r|d d}n|d}t
|}ntd|| q|S )NZfloat32)dtyper2   r   Zsample_ratez0The sample rate of audio is not %d, resample it.Zrate)Zeffectsr!   z.modelscope error: Input array should be [N, T])Zint16Zint32Zint64i   zOmodelscope error: The input type is restricted to audio address and nump array.)rangerA   r6   r7   r   readsfioBytesIOshaper-   
from_numpy	unsqueezer   loggerwarning
torchaudioZsox_effectsZapply_effects_tensorZsqueezer?   ndarrayrG   Zastyper$   r,   )r   r*   rF   rD   Z
file_bytesdatafsr   r   r   r%   m   sP   


zResNet_Pipeline.preprocessemb1emb2returnc                 C   s   t |tjrt|}t |tjrt|}t|jr |d}t|jr*|d}t|jdkr8t|jdks:J tjj	ddd}|||}|
 S )Nr   r2   r!   gư>)dimeps)r6   r?   rS   r-   rN   rA   rM   rO   nnZCosineSimilarityitem)r   rV   rW   cosZcosiner   r   r   rB      s   





 
z&ResNet_Pipeline.compute_cos_similarity)NFNr+   )__name__
__module____qualname____doc__r	   r   r   r?   rS   listr7   boolfloatr)   r&   r-   ZTensorr'   r%   rB   __classcell__r   r   r   r   r      s8    


&r   )!rK   r8   typingr   r   r   r   r(   r?   Z	soundfilerJ   r-   rR   Zmodelscope.fileior   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   rP   Zregister_moduleZspeaker_verificationZspeaker_verification_resnetr   r   r   r   r   <module>   s(   