a
    d;                     @   s   U d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z	d dl
mZ d dlm  mZ d dlmZ g dZdZeed< G dd	 d	eZG d
d dZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZe	je	jee  e	jdddZ!G dd dZ"dS )    N)Enum)CallableDictListOptionalTuple)nms)FaceDetectorFaceDetectorResultFaceKeypointz7https://github.com/kornia/data/raw/main/yunet_final.pthurlc                   @   s$   e Zd ZdZdZdZdZdZdZdS )r   zkDefine the keypoints detected in a face.

    The left/right convention is based on the screen viewer.
    r               N)	__name__
__module____qualname____doc__EYE_LEFT	EYE_RIGHTNOSE
MOUTH_LEFTMOUTH_RIGHT r   r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/contrib/face_detection.pyr      s   r   c                   @   s0  e Zd ZdZejddddZd#eej eej	 d dddZ
eejd	d
dZeejd	ddZeejd	ddZeejd	ddZeejdddZeejd	ddZeejd	ddZeejd	ddZeejd	ddZeejd	ddZeejd	dd Zeejd	d!d"ZdS )$r
   zEncapsulate the results obtained by the :py:class:`kornia.contrib.FaceDetector`.

    Args:
        data: the encoded results coming from the feature detector with shape :math:`(14,)`.
    N)datareturnc                 C   s(   t |dk rtd|j d|| _d S )N   z.Result must comes as vector of size(14). Got: .)len
ValueErrorshape_data)selfr   r   r   r   __init__%   s    zFaceDetectorResult.__init__devicedtyper   c                 C   s   | j j||d| _ | S )z)Like :func:`torch.nn.Module.to()` method.r'   r(   )r#   tor$   r'   r(   r   r   r   r*   *   s    zFaceDetectorResult.tor   c                 C   s
   | j d S )z'The bounding box top-left x-coordinate..r   r#   r$   r   r   r   xmin/   s    zFaceDetectorResult.xminc                 C   s
   | j d S )z'The bounding box top-left y-coordinate..r   r.   r/   r   r   r   ymin4   s    zFaceDetectorResult.yminc                 C   s
   | j d S )z+The bounding box bottom-right x-coordinate.).r   r.   r/   r   r   r   xmax9   s    zFaceDetectorResult.xmaxc                 C   s
   | j d S )z+The bounding box bottom-right y-coordinate.).r   r.   r/   r   r   r   ymax>   s    zFaceDetectorResult.ymax)keypointr   c                 C   s   |t jkr| jd }nh|t jkr,| jd }nR|t jkrB| jd }n<|t jkrX| jd }n&|t jkrn| jd }ntd| d|S )zThe [x y] position of a given facial keypoint.

        Args:
            keypoint: the keypoint type to return the position.
        ).)r      ).)      ).)   	   ).)
      ).)      zNot valid keypoint type. Got: r   )r   r   r#   r   r   r   r   r!   )r$   r5   outr   r   r   get_keypointC   s    




zFaceDetectorResult.get_keypointc                 C   s
   | j d S )zThe detection score.).   r.   r/   r   r   r   scoreW   s    zFaceDetectorResult.scorec                 C   s   | j | j S )zThe bounding box width.)r3   r0   r/   r   r   r   width\   s    zFaceDetectorResult.widthc                 C   s   | j | j S )zThe bounding box height.)r4   r2   r/   r   r   r   heighta   s    zFaceDetectorResult.heightc                 C   s
   | j d S )BThe [x y] position of the top-left coordinate of the bounding box.).)r   r   r.   r/   r   r   r   top_leftf   s    zFaceDetectorResult.top_leftc                 C   s   | j }|d  | j7  < |S )rE   r-   )rF   rC   r$   r?   r   r   r   	top_rightk   s    zFaceDetectorResult.top_rightc                 C   s
   | j d S )zFThe [x y] position of the bottom-right coordinate of the bounding box.).)r   r   r.   r/   r   r   r   bottom_rightr   s    zFaceDetectorResult.bottom_rightc                 C   s   | j }|d  | j7  < |S )rE   r1   )rF   rD   rG   r   r   r   bottom_leftw   s    zFaceDetectorResult.bottom_left)NN)r   r   r   r   torchTensorr%   r   r'   r(   r*   propertyr0   r2   r3   r4   r   r@   rB   rC   rD   rF   rH   rI   rJ   r   r   r   r   r
      s4    r
   c                       sx   e Zd ZdZdeeeedd fddZejejd	d
dZ	e
eejf eeejdddZejejd	ddZ  ZS )r	   a  Detect faces in a given image using a CNN.

    By default, it uses the method described in :cite:`facedetect-yu`.

    Args:
        top_k: the maximum number of detections to return before the nms.
        confidence_threshold: the threshold used to discard detections.
        nms_threshold: the threshold used by the nms for iou.
        keep_top_k: the maximum number of detections to return after the nms.

    Return:
        A tensor of shape :math:`(N,15)` to be used with :py:class:`kornia.contrib.FaceDetectorResult`.

    Example:
        >>> img = torch.rand(1, 3, 320, 320)
        >>> detect = FaceDetector()
        >>> res = detect(img)
      333333?  N)top_kconfidence_thresholdnms_threshold
keep_top_kr   c                    s   t    || _|| _|| _|| _dg dddgddgg dgg dd	d
gdd| _g dddgddgg dg| _g d| _d	d
g| _	d| _
tddd| _t| _d S )NYuFaceDetectNet)r;             0   @   `   )         )r9   rV   rX   rZ   g?g?F)name	min_sizesstepsvariancecliptestT
pretrained)superr%   rQ   rR   rS   rT   configr`   ra   rb   rc   rU   model
nms_korniar   )r$   rQ   rR   rS   rT   	__class__r   r   r%      s"    


zFaceDetector.__init__)imager   c                 C   s   |S Nr   )r$   rm   r   r   r   
preprocess   s    zFaceDetector.preprocess)r   rD   rC   r   c                 C   sv  |d |d |d   }}}t j||||||||||||||g|j|jd}t| j| j| j||fd}||j|j}t	|| | j
}	|	| }	|d d df |d d df  }
}|
|dd	  }|| jk}|	| ||  }	}|jd
dd d | j }|	| ||  }	}t j|	|d d d f fdd}| |	d d d df || j}t|dkrh||d d f }|d | j S )Nlocconfiour)   )
image_sizer   r   g        g      ?T)Z
descendingZdimr   )rK   tensorr'   r(   	_PriorBoxr`   ra   rc   r*   _decoderb   clampsqrtrR   sortrQ   catr   rS   r    rT   )r$   r   rD   rC   rp   rq   rr   ZscalepriorsboxesZ
cls_scoresZ
iou_scoresZscoresZindsorderZdetsZkeepr   r   r   postprocess   s*    "
 zFaceDetector.postprocessc                 C   s.   |  |}| |}| ||jd |jd S )Nrt   )ro   ri   r   r"   )r$   rm   imgr?   r   r   r   forward   s    

zFaceDetector.forward)rN   rO   rO   rP   )r   r   r   r   intfloatr%   rK   rL   ro   r   strr   r   __classcell__r   r   rk   r   r	      s     %r	   c                       s   e Zd Zd fdd	Z  ZS )
ConvDPUnitTc                    sx   t    | dtj||dddddd | dtj||dddd|d |rt| dt| | d	tjdd
 d S )Nconv1r   r   Tbiasgroupsconv2r   ZbnreluZinplace)rg   r%   
add_modulennConv2dBatchNorm2dReLUr$   in_channelsout_channels
withBNRelurk   r   r   r%      s    
  zConvDPUnit.__init__)T)r   r   r   r%   r   r   r   rk   r   r      s   r   c                       s(   e Zd Zeeedd fddZ  ZS )	Conv_headN)r   mid_channelsr   r   c                    sf   t    | dtj||dddddd | dt| | dtjdd	 | d
t|| d S )Nr   r   r   r   Tr   Zbn1r   r   r   )rg   r%   r   r   r   r   r   r   )r$   r   r   r   rk   r   r   r%      s
    
 zConv_head.__init__)r   r   r   r   r%   r   r   r   rk   r   r      s   r   c                       s*   e Zd Zdeeedd fddZ  ZS )Conv4layerBlockTN)r   r   r   r   c                    s6   t    | dt||d | dt||| d S )Nr   Tr   )rg   r%   r   r   r   rk   r   r   r%      s    
zConv4layerBlock.__init__)T)r   r   r   r   boolr%   r   r   r   rk   r   r      s   r   c                       s>   e Zd Zed fddZejeeejf dddZ	  Z
S )rU   re   c              	      s^  t    || _d| _tddd| _tdd| _tdd| _tdd| _	tdd| _
tdd| _tdd| _ttdddtdddtdddtddd| _| jdkr&|  D ]r}t|tjr|jd urtj|jj |jjd	 n|jjd
d qt|tjr|jjd |jj  q|rRdd }tjjt|d}| j |dd | !  d S )Nr   r   rV   rZ   3   F"   Ztraing{Gz?r   g{Gz?r   c                 S   s   | S rn   r   )Zstoragerp   r   r   r   <lambda>      z*YuFaceDetectNet.__init__.<locals>.<lambda>)Zmap_locationT)strict)"rg   r%   phasenum_classesr   model0r   model1model2model3model4model5model6r   
Sequentialheadmodules
isinstancer   r   initZxavier_normal_Zweightr   Zfill_Znormal_r   Zzero_rK   ZhubZload_state_dict_from_urlr   Zload_state_dicteval)r$   r   rf   mZstorage_fcnZpretrained_dictrk   r   r   r%      s>    





zYuFaceDetectNet.__init__)xr   c              	   C   s  g g  }}|  |}t|d}| |}| |}t|d}| |}|| t|d}| |}|| t|d}| |}|| t|d}| 	|}|| t
| jD ].\}}||| }||dddd  qtdd |D d}||ddd}|jd	dd
\}}	}
| jdkrj|dd}tj|	d| jdd
}	|
dd}
n>||ddd}|	|	dd| j}	|
|
ddd}
||	|
dS )Nr   r   r   r   c                 S   s   g | ]}| |d dqS )r   rt   )viewsize).0or   r   r   
<listcomp>7  r   z+YuFaceDetectNet.forward.<locals>.<listcomp>rt      )rA   r   r   ru   rd   rA   )rp   rq   rr   )r   FZ
max_pool2dr   r   r   appendr   r   r   	enumerater   Zpermute
contiguousrK   r|   r   r   splitr   Zsoftmaxr   )r$   r   Zdetection_sourcesZ	head_listihZx_tmpZ	head_dataZloc_dataZ	conf_dataZiou_datar   r   r   r     s>    











zYuFaceDetectNet.forward)r   r   r   r   r%   rK   rL   r   r   r   r   r   r   rk   r   rU      s   'rU   )rp   r}   	variancesr   c                 C   s0  t |ddddf | ddddf |d  |ddddf   |ddddf t | ddddf |d   |ddddf | ddddf |d  |ddddf   |ddddf | ddddf |d  |ddddf   |ddddf | ddddf |d  |ddddf   |ddddf | dddd	f |d  |ddddf   |ddddf | ddd	d
f |d  |ddddf   fd}|ddddf |ddddf d  }t j||ddddf | |ddddf fddS )a  Decode locations from predictions using priors to undo the encoding we did for offset regression at train
    time.

    Args:
        loc:location predictions for loc layers. Shape: [num_priors,4].
        priors: Prior boxes in center-offset form. Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes.

    Return:
        Tensor containing decoded bounding box predictions.
    Nr   r   r   r   r7   r9   r;   r=   rA   rt   ru   )rK   r|   exp)rp   r}   r   r~   tmpr   r   r   rx   L  s    B4BBBBB	,rx   c                   @   s\   e Zd Zeee  ee eeeef ddddZej	ej
d dddZejdd	d
ZdS )rw   N)r`   ra   rc   rs   r   c                 C   sL  || _ || _|| _|| _td| _tj| _tdD ]&}| j| t	
d|d kr4tdq4tt| jd d d d tt| jd d d d g| _t| jd d t| jd d g| _t| jd d t| jd d g| _t| jd d t| jd d g| _t| jd d t| jd d g| _| j| j| j| jg| _d S )Ncpur   r   r   zsteps must be [8,16,32,64]r   r   )r`   ra   rc   rs   rK   r'   float32r(   rangemathpowr!   r   Zfeature_map_2thZfeature_map_3thZfeature_map_4thZfeature_map_5thZfeature_map_6thfeature_maps)r$   r`   ra   rc   rs   r   r   r   r   r%   j  s    
>&&&&z_PriorBox.__init__r&   c                 C   s   || _ || _| S rn   r)   r+   r   r   r   r*     s    z_PriorBox.tor,   c              	   C   s   g }t | jD ]\}}| j| }t|d D ]}t|d D ]r}|D ]h}|| jd  }|| jd  }	|d | j|  | jd  }
|d | j|  | jd  }||
|||	g7 }qDq<q,qtj|| j| j	d
dd}| jr|jddd}|S )Nr   r   g      ?r)   rt   r   )maxmin)r   r   r`   r   rs   ra   rK   rv   r'   r(   r   rc   ry   )r$   Zanchorskfr`   r   jZmin_sizeZs_kxZs_kyZcxcyoutputr   r   r   __call__  s    
z_PriorBox.__call__)r   r   r   r   r   r   r   r%   rK   r'   r(   r*   rL   r   r   r   r   r   rw   i  s   *rw   )#r   enumr   typingr   r   r   r   r   rK   Ztorch.nnr   Ztorch.nn.functionalZ
functionalr   Zkornia.geometry.bboxr   rj   __all__r   r   __annotations__r   r
   Moduler	   r   r   r   r   rU   rL   r   rx   rw   r   r   r   r   <module>   s"   
a[
	X