o
    ॵiD                     @   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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G dd deZ dS )    N)AnyDict)	Pipelines)
align_face)
OutputKeys)pipeline)InputPipeline)	PIPELINES)	LoadImage)	ModelFileTasks)
get_loggerc                       s   e Zd Zdef fddZ				ddd	Zd
edeeef fddZ	d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 )FaceProcessingBasePipelinemodelc                    s.   t  jdd|i| d}ttj|d| _dS )z
        use `model` to create a face processing pipeline and output cropped img, scores, bbox and lmks.

        Args:
            model: model id on modelscope hub.

        r   z*damo/cv_ddsar_face-detection_iclr23-damofd)r   N )super__init__r   r   face_detection)selfr   kwargsdet_model_id	__class__r   i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/face_processing_base_pipeline.pyr      s
   z#FaceProcessingBasePipeline.__init__
      FNc              	   C   s  t |tj }t |tj }t |tj }|jd dkr&td dS g }	t	|jd D ]}
||
 }|d |d  |krN|d |d  |krN|	|
g7 }	q/t
|	dkrctd| d| d	 dS ||	 }||	 }||	 }t |}|dddf |dddf  |dddf |dddf   }t || d }|dkr|r|jd dkr|r|d d |d d g}td
}d}|D ]6}|| }t t |d |d  d |d  t t |d |d  d |d   }||k r|}|}q|g}|d }|| || || fS )aC  
        choose face with maximum area
        Args:
            det_result: output of face detection pipeline
            min_face: minimum size of valid face w/h
            top_face: take faces with top max areas
            center_face: choose the most centerd face from multi faces, only valid if top_face > 1
        r   zWarning: No face detected!N      r   z)Warning: Face size not enough, less than x!inf)nparrayr   BOXES	KEYPOINTSSCORESshapeloggerinforangelenargsortfloatsquareabs)r   
det_resultmin_facetop_facecenter_face	img_shapebboxes	landmarksscoresface_idxiboxboxesareasort_idx
img_centermin_distsel_idx_idxdistmain_idxr   r   r   _choose_face'   sV   
(

@  z'FaceProcessingBasePipeline._choose_faceinputreturnc                 C   s   t |}|d d d d d d df }| | }| j||jd}|d urQ|\}}}|dd}t|d|\}}	i }
t	||
d< |g|
d< ||
d< ||
d	< |
S d S )
Nr"   )r5      r   )p   rI   imgr8   bboxlmks)
r   convert_to_ndarrayr   copyrE   r(   reshaper   r#   ascontiguousarray)r   rF   rJ   r1   rtnr8   r6   	face_lmks	align_img_resultr   r   r   
preprocess^   s   


z%FaceProcessingBasePipeline.preprocess      c           !      C   s  t |d}|d u rd S |jdkrD|j\}}t j||dft jd}| |d d d d df<  |d d d d df< |d d d d df< |}|d d d d ddf }|dd d df }t jd	t jd}|}	|	jd }
|
dkr|	d d dd
f }t |jdd }d}|
dkr|d }t 	|d d df |d d df  d |d  |d d df |d d df  d |d  g}t 
t |dd}t d|d  }|	|dd
f }|d  d7  < n|d  d7  < |d u r|d  d7  < d S t|d t|d t|d t|d g}|d t|d |d  d | d d  }|d t|d |d  d | d d  }|d t|d |d  d | d d  }|d t|d |d  d | d d  }td||d< td||d< t|jd d ||d< t|jd d ||d< |d |d  d }|d |d  d }||krt|| d }|d | |d< |d | d |d< td|d |d< t|jd d |d |d< |}n3t|| d }|d | |d< |d | d |d< td|d |d< t|jd d |d |d< |}t j||df|t jd}t||d |d  d  d }t||d |d  d  d }|}||d  d |d  }|}||d  d |d  } ||d |d d |d |d d d d f ||||| d d f< tj|dtjd}|S )N)r"      r   r   )dtyper   r   .r"   )rH   rY   g       @g      ?rI   )   r[   )interpolation)r#   rO   ndimr(   emptyuint8zerosint32asarrayvstacksumpowerargmaxintmaxminfullcv2resizeINTER_LINEAR)!r   rJ   rectpadding_size	pad_pixelwhretnrofbounding_boxes
nrof_facesdetimg_sizebindexr?   offsetsoffset_dist_squared_bboxx1x2y1y2	padding_h	padding_woffsetdst_sizedstdst_x_offsetdst_y_offsety_starty_endx_startx_endr   r   r   align_face_paddingq   s   

B
**,
  z-FaceProcessingBasePipeline.align_face_paddingc              	   C   sP   t j|d   t j|d   t j|d   gt j|d   giS )NrJ   r8   rK   rL   )r   
OUTPUT_IMGcpunumpyr'   tolistr%   r&   )r   rF   r   r   r   forward   s
   z"FaceProcessingBasePipeline.forwardinputsc                 C   s   |S )Nr   )r   r   r   r   r   postprocess   s   z&FaceProcessingBasePipeline.postprocess)r   r   FN)rW   rX   )__name__
__module____qualname__strr   rE   r   r   r   rV   r   r   r   __classcell__r   r   r   r   r      s    
7
"O*r   )!os.pathpathosptypingr   r   rk   r   r#   PILtorchmodelscope.metainfor   0modelscope.models.cv.face_recognition.align_facer   modelscope.outputsr   modelscope.pipelinesr   modelscope.pipelines.baser   r	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r)   r   r   r   r   r   <module>   s"   