o
    *j=
                     @   s   d dl mZ d dl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 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 d dlmZ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)	Pipelines)
align_face)	get_model)
OutputKeys)pipeline)InputPipeline)	PIPELINES)FaceProcessingBasePipeline)	LoadImage)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Zddef fddZdedeeef f fddZdeeef deeef fd	d
Z	deeef deeef fddZ
  ZS )FaceRecognitionPipelineTmodelc                    s   t  jd||d| ttj rdd nd}|| _tdddg}|tjt	
|tj|dd ||}|  || _td	 d
S )z
        use `model` to create a face recognition pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   use_detzcuda:r   cpuZIR_101p   T)Zmap_locationZweights_onlyzface recognition model loaded!N )super__init__torchdevicecudaZis_availabler   Zload_state_dictloadospjoinr   ZTORCH_MODEL_BIN_FILEtoeval
face_modelloggerinfo)selfr   r   kwargsr   r"   	__class__r   r/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/face_recognition_pipeline.pyr      s"   
z FaceRecognitionPipeline.__init__inputreturnc                    sf   t  |}|d }|d d d d d d df }tj|dd}|d d d }|tj}||d< |S )Nimg)   r      )Zaxesg     o@g      ?)r   
preprocessnpZ	transposeZastypeZfloat32)r%   r*   resultZ	align_imgZface_imgr'   r   r)   r0   4   s   z"FaceRecognitionPipeline.preprocessc                 C   s\   |d d usJ |d  d}| |   }|ttj|d ddd }tj	|iS )Nr,   r   r.   r-   T)Zkeepdims)
Z	unsqueezer"   detachr   numpyr1   sqrtsumr   ZIMG_EMBEDDING)r%   r*   r,   Zembr   r   r)   forward>   s
   
zFaceRecognitionPipeline.forwardinputsc                 C   s   |S )Nr   )r%   r8   r   r   r)   postprocessE   s   z#FaceRecognitionPipeline.postprocess)T)__name__
__module____qualname__strr   r	   r   r   r0   r7   r9   __classcell__r   r   r'   r)   r      s
    "
*r   )'Zos.pathpathr   typingr   r   Zcv2r4   r1   ZPILr   Zmodelscope.metainfor   Z0modelscope.models.cv.face_recognition.align_facer   Z7modelscope.models.cv.face_recognition.torchkit.backboner   Zmodelscope.outputsr   Zmodelscope.pipelinesr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Z5modelscope.pipelines.cv.face_processing_base_pipeliner   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r#   Zregister_moduleZface_recognitionr   r   r   r   r)   <module>   s,   