
    hI                         d dl Zd dlZd dlmZ d dlmc mZ  G d dej                        Z	 G d dej                        Z
 G d dej                        Zd Zd	 Zd
 Zy)    Nc                   &     e Zd Zd fd	Zd Z xZS )
BlazeBlockc                 v   t         t        |           || _        ||z
  | _        |dk(  rt        j                  ||      | _        d}n|dz
  dz  }t        j                  t        j                  ||||||d      t        j                  ||dddd            | _
        t        j                  d      | _        y )	N   )kernel_sizestrider      Tin_channelsout_channelsr   r   paddinggroupsbiasr   r   r   r   r   r   inplace)superr   __init__r   channel_padnn	MaxPool2dmax_pool
SequentialConv2dconvsReLUact)selfr   r   r   r   r   	__class__s         j/var/www/html/engine/venv/lib/python3.12/site-packages/face_alignment/detection/blazeface/net_blazeface.pyr   zBlazeBlock.__init__   s    j$(*'+5 Q;LLVFKDMG"Q1,G]]II+K"-fg(t5 II+L"#AqtE	

 774(    c           	      ,   | j                   dk(  r*t        j                  |ddd      }| j                  |      }n|}| j                  dkD  r(t        j                  |ddddd| j                  fdd      }| j                  | j                  |      |z         S )Nr   r   r   r   r   constantr   )r   Fpadr   r   r   r   r   xhs      r    forwardzBlazeBlock.forward    s    ;;!az15Aa AAaa!Q1a)9)9:JJAxx

1)**r!   )   r	   __name__
__module____qualname__r   r*   __classcell__r   s   @r    r   r      s    )0
+r!   r   c                   &     e Zd Zd fd	Zd Z xZS )FinalBlazeBlockc                     t         t        |           t        j                  t        j
                  |||dd|d      t        j
                  ||dddd            | _        t        j                  d      | _        y )Nr   r   Tr
   r	   r   r   )	r   r3   r   r   r   r   r   r   r   )r   channelsr   r   s      r    r   zFinalBlazeBlock.__init__-   sl    ot-/ ]]II("-a%D2 II("#AqtE	

 774(r!   c                 r    t        j                  |ddd      }| j                  | j                  |            S )Nr#   r$   r   )r%   r&   r   r   r'   s      r    r*   zFinalBlazeBlock.forward;   s-    EE!\:q1xx

1&&r!   )r+   r,   r1   s   @r    r3   r3   ,   s    )'r!   r3   c                   |     e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	d Z
dd	Zdd
Zd Zd Zd Zd Zd Zd Z xZS )	BlazeFacea  The BlazeFace face detection model from MediaPipe.

    The version from MediaPipe is simpler than the one in the paper;
    it does not use the "double" BlazeBlocks.

    Because we won't be training this model, it doesn't need to have
    batchnorm layers. These have already been "folded" into the conv
    weights by TFLite.

    The conversion to PyTorch is fairly straightforward, but there are
    some small differences between TFLite and PyTorch in how they handle
    padding on conv layers with stride 2.

    This version works on batches, while the MediaPipe version can only
    handle a single image at a time.

    Based on code from https://github.com/tkat0/PyTorch_BlazeFace/ and
    https://github.com/google/mediapipe/
    c                 0   t         t        |           d| _        d| _        d| _        d| _        || _        |r$d| _        d| _	        d| _
        d| _        d| _        n#d| _        d| _	        d| _
        d| _        d| _        d	| _        | j                          y )
Nr	   i     g      Y@g      p@g?g      `@g      ?g333333?)r   r8   r   num_classesnum_anchors
num_coordsscore_clipping_thresh
back_modelx_scaley_scaleh_scalew_scalemin_score_threshmin_suppression_threshold_define_layers)r   r?   r   s     r    r   zBlazeFace.__init__V   s    i')
 %*"$ DL DL DL DL$(D! DL DL DL DL$(D!),&r!   c           
      6   t        j                  t        j                  dddddd      t        j                  d      gt	        d	      D cg c]  }t        dd       c}t        ddd
      t	        d	      D cg c]  }t        dd       c}t        ddd
      t	        d	      D cg c]  }t        dd       c}t        ddd
      t	        d	      D cg c]  }t        dd       c} | _        t        d      | _        t        j                  dddd      | _	        t        j                  dddd      | _
        t        j                  dddd      | _        t        j                  dddd      | _        y c c}w c c}w c c}w c c}w )Nr+         r   r   Tr   r      r   0   `   r	   r          )r   r   r   r   ranger   backboner3   finalclassifier_8classifier_16regressor_8regressor_16)r   _s     r    _define_back_model_layersz#BlazeFace._define_back_model_layersq   sg   II!"!AWX_cdGGD!

 +0(3QjR 3

 r2a(	


 +0(3QjR 3

 r2a(

 +0(3QjR 3

 r2a(

 +0(3QjR 3

 %R(
IIb!QT:YYr1ad;99RQT:IIb"ad; 4333s   F
>F
/F
 F
c                 "   t        j                  t        j                  dddddd      t        j                  d      t	        dd      t	        dd	      t	        d	d
d      t	        d
d      t	        dd      t	        ddd      t	        dd      t	        dd      t	        dd      t	        dd      t	        dd            | _        t        j                  t	        ddd      t	        dd      t	        dd      t	        dd      t	        dd            | _        t        j                  dddd      | _        t        j                  dddd      | _        t        j                  dd
dd      | _	        t        j                  dddd      | _
        y )Nr+   rH   rI   r   r   Tr   r      rP   rK   $   *   rL   8   @   H   P   X   rM   r	   rN   rO   )r   r   r   r   r   	backbone1	backbone2rT   rU   rV   rW   r   s    r    _define_front_model_layersz$BlazeFace._define_front_model_layers   sM   II!"!6GGD!r2r2r2a(r2r2r2a(r2r2r2r2r2
$ r2a(r2r2r2r2
 IIb!QT:YYr1ad;99RQT:IIb"ad;r!   c                 ^    | j                   r| j                          y | j                          y N)r?   rY   rf   re   s    r    rF   zBlazeFace._define_layers   s!    ??**,++-r!   c                    t        j                  |ddd      }|j                  d   }| j                  r#| j	                  |      }| j                  |      }n"| j                  |      }| j                  |      }| j                  |      }|j                  dddd      }|j                  |dd      }| j                  |      }|j                  dddd      }|j                  |dd      }t        j                  ||fd      }| j                  |      }|j                  dddd      }|j                  |dd	      }| j                  |      }|j                  dddd      }|j                  |dd	      }t        j                  ||fd      }	|	|gS )
N)r	   r   r	   r   r$   r   r   r+   r	   dimr:   )r%   r&   shaper?   rR   rS   rc   rd   rT   permutereshaperU   torchcatrV   rW   )
r   r(   br)   c1c2cr1r2rs
             r    r*   zBlazeFace.forward   sn    EE!\:q1GGAJ??a A

1Aq!Aq!A
 q!ZZ1a#ZZ2q!"ZZ1a#ZZ2q!IIr2hA&a ZZ1a#ZZ2r"q!ZZ1a#ZZ2r"IIr2hA&1vr!   c                 B    | j                   j                  j                  S )z6Which device (CPU or GPU) is being used by this model?)rT   weightdevicere   s    r    _devicezBlazeFace._device   s      ''...r!   c                 l    | j                  t        j                  |             | j                          y rh   )load_state_dictrp   loadeval)r   paths     r    load_weightszBlazeFace.load_weights   s"    UZZ-.		r!   c                 r   |xs | j                         }t        j                  t        j                  |      t        j
                  |      | _        | j                  j                         dk(  sJ | j                  j                  d   | j                  k(  sJ | j                  j                  d   dk(  sJ y N)dtyper{   r   r   r	      )
r|   rp   tensornpr   float32anchors
ndimensionrm   r<   )r   r   r{   s      r    load_anchorszBlazeFace.load_anchors   s    )4<<>||GGDMv?||&&(A-.-||!!!$(8(8898||!!!$)*)r!   c                 L   |xs | j                         }t        j                  |t        j                  |      | _        | j                  j                         dk(  sJ | j                  j                  d   | j                  k(  sJ | j                  j                  d   dk(  sJ y r   )r|   rp   r   r   r   r   rm   r<   )r   arrr{   s      r    load_anchors_from_npyzBlazeFace.load_anchors_from_npy   s    )4<<>||u}}V5||&&(A-.-||!!!$(8(8898||!!!$)*)r!   c                 .    |j                         dz  dz
  S )z/Converts the image pixels to the range [-1, 1].g     _@g      ?)float)r   r(   s     r    _preprocesszBlazeFace._preprocess   s    wwy5 3&&r!   c                     t        |t        j                        r$t        j                  |      j                  d      }| j                  |j                  d            d   S )a.  Makes a prediction on a single image.

        Arguments:
            img: a NumPy array of shape (H, W, 3) or a PyTorch tensor of
                 shape (3, H, W). The image's height and width should be
                 128 pixels.

        Returns:
            A tensor with face detections.
        )r   r   r	   r   )
isinstancer   ndarrayrp   
from_numpyrn   predict_on_batch	unsqueeze)r   imgs     r    predict_on_imagezBlazeFace.predict_on_image   sL     c2::&""3'//	:C$$S]]1%56q99r!   c                 l   t        |t        j                        r$t        j                  |      j                  d      }|j                  d   dk(  sJ | j                  r(|j                  d   dk(  sJ |j                  d   dk(  s*J |j                  d   dk(  sJ |j                  d   dk(  sJ |j                  | j                               }| j                  |      }t        j                         5  | j                  |      }ddd       | j                  d   |d   | j                        }g }t        t!        |            D ]_  }| j#                  ||         }t!        |      dkD  rt        j$                  |      nt        j&                  d	      }|j)                  |       a |S # 1 sw Y   xY w)
af  Makes a prediction on a batch of images.

        Arguments:
            x: a NumPy array of shape (b, H, W, 3) or a PyTorch tensor of
               shape (b, 3, H, W). The height and width should be 128 pixels.

        Returns:
            A list containing a tensor of face detections for each image in
            the batch. If no faces are found for an image, returns a tensor
            of shape (0, 17).

        Each face detection is a PyTorch tensor consisting of 17 numbers:
            - ymin, xmin, ymax, xmax
            - x,y-coordinates for the 6 keypoints
            - confidence score
        )r   r+   r	   r   r	   r+   r         Nr   )r      )r   r   r   rp   r   rn   rm   r?   tor|   r   inference_mode__call___tensors_to_detectionsr   rQ   len_weighted_non_max_suppressionstackzerosappend)r   r(   out
detectionsfiltered_detectionsifacess          r    r   zBlazeFace.predict_on_batch   s   " a$  #++L9AwwqzQ??771:$$$771:$$$771:$$$771:$$$ DD Q !!# 	#--"C	# 00QQN
 !s:' 	.A66z!}EE*-++EKK& %G 4 &&u-		. #"	# 	#s   3F**F3c                 @   |j                         dk(  sJ |j                  d   | j                  k(  sJ |j                  d   | j                  k(  sJ |j                         dk(  sJ |j                  d   | j                  k(  sJ |j                  d   | j                  k(  sJ |j                  d   |j                  d   k(  sJ | j                  ||      }| j                  }|j                  | |      }|j                         j                  d      }|| j                  k\  }g }t        |j                  d         D ]]  }	||	||	   f   }
||	||	   f   j                  d      }|j                  t        j                  |
|fd      j!                  d             _ |S )a`  The output of the neural network is a tensor of shape (b, 896, 16)
        containing the bounding box regressor predictions, as well as a tensor
        of shape (b, 896, 1) with the classification confidences.

        This function converts these two "raw" tensors into proper detections.
        Returns a list of (num_detections, 17) tensors, one for each image in
        the batch.

        This is based on the source code from:
        mediapipe/calculators/tflite/tflite_tensors_to_detections_calculator.cc
        mediapipe/calculators/tflite/tflite_tensors_to_detections_calculator.proto
        r+   r	   r   r   rj   rk   cpu)r   rm   r<   r=   r;   _decode_boxesr>   clampsigmoidsqueezerD   rQ   r   r   rp   rq   r   )r   raw_box_tensorraw_score_tensorr   detection_boxesthreshdetection_scoresmaskoutput_detectionsr   boxesscoress               r    r   z BlazeFace._tensors_to_detections/  s    ((*a///##A&$*:*::::##A&$//999**,111%%a(D,<,<<<<%%a(D,<,<<<<##A&*:*@*@*CCCC,,^WE+++116'6B+335=="=E
  4#8#88 ~++A./ 	SA#AtAwJ/E%aaj1;;;CF$$UYYvB%G%J%J5%QR	S
 ! r!   c                    t        j                  |      }|d   | j                  z  |dddf   z  |dddf   z   }|d   | j                  z  |dddf   z  |dddf   z   }|d   | j                  z  |dddf   z  }|d	   | j
                  z  |dddf   z  }||d
z  z
  |d<   ||d
z  z
  |d<   ||d
z  z   |d<   ||d
z  z   |d	<   t        d      D ]n  }d|dz  z   }	|d|	f   | j                  z  |dddf   z  |dddf   z   }
|d|	dz   f   | j                  z  |dddf   z  |dddf   z   }|
|d|	f<   ||d|	dz   f<   p |S )z}Converts the predictions into actual coordinates using
        the anchor boxes. Processes the entire batch at once.
        ).r   Nr   r   ).r	   r+   r	   ).r   ).r+   g       @rO   r   .)rp   
zeros_liker@   rA   rC   rB   rQ   )r   	raw_boxesr   r   x_centery_centerwr)   koffset
keypoint_x
keypoint_ys               r    r   zBlazeFace._decode_boxes[  s      +V$t||3AqDM#AqDM*V$t||3AqDM#AqDM* f,wq!t}<f,wq!t}< 1r6)f 1r6)f 1r6)f 1r6)fq 	0AQYF"3;/&q!t}-/6q!t}=J"3
?3&q!t}-/6q!t}=J!+E#v+%/E#vz/"	0 r!   c                    t        |      dk(  rg S g }t        j                  |dddf   d      }t        |      dkD  r||d      }|dd }||ddf   }t        ||      }|| j                  kD  }||   }	||    }|j                         }
t        |	      dkD  rP||	ddf   }||	ddf   }|j                         }||z  j                  d	      |z  }||
dd |t        |	      z  |
d<   |j                  |
       t        |      dkD  r|S )
a  The alternative NMS method as mentioned in the BlazeFace paper:

        "We replace the suppression algorithm with a blending strategy that
        estimates the regression parameters of a bounding box as a weighted
        mean between the overlapping predictions."

        The original MediaPipe code assigns the score of the most confident
        detection to the weighted detection, but we take the average score
        of the overlapping detections.

        The input detections should be a Tensor of shape (count, 17).

        Returns a list of PyTorch tensors, one for each detected face.

        This is based on the source code from:
        mediapipe/calculators/util/non_max_suppression_calculator.cc
        mediapipe/calculators/util/non_max_suppression_calculator.proto
        r   Nr:   T)
descendingr   r	   r   rk   )r   rp   argsortoverlap_similarityrE   clonesumr   )r   r   r   	remaining	detection	first_boxother_boxesiousr   overlappingweighted_detectioncoordinatesr   total_scoreweighteds                  r    r   z'BlazeFace._weighted_non_max_suppressiony  sS   & z?aI MM*QU"3E	)nq "9Q<0I
 ""1I$Y]3K%i=D $888D#D/K!4%(I "+!2;!#(crc)9:#KB$67$jjl'&055!5<{J*2"3B')4s;7G)G"2&$$%787 )nq : ! r!   )Frh   )r-   r.   r/   __doc__r   rY   rf   rF   r*   r|   r   r   r   r   r   r   r   r   r   r0   r1   s   @r    r8   r8   A   sW    (6<&<D.$L/++': /#b*!X<8!r!   r8   c                 L   | j                  d      }|j                  d      }t        j                  | ddddf   j                  d      j	                  ||d      |ddddf   j                  d      j	                  ||d            }t        j
                  | ddddf   j                  d      j	                  ||d      |ddddf   j                  d      j	                  ||d            }t        j                  ||z
  d      }|dddddf   |dddddf   z  S )al   We resize both tensors to [A,B,2] without new malloc:
    [A,2] -> [A,1,2] -> [A,B,2]
    [B,2] -> [1,B,2] -> [A,B,2]
    Then we compute the area of intersect between box_a and box_b.
    Args:
      box_a: (tensor) bounding boxes, Shape: [A,4].
      box_b: (tensor) bounding boxes, Shape: [B,4].
    Return:
      (tensor) intersection area, Shape: [A,B].
    r   Nr   r	   )min)sizerp   r   r   expandmaxr   )box_abox_bABmax_xymin_xyinters          r    	intersectr     s    	

1A

1AYYuQU|--a0771a@QU|--a0771a@BFYYuQU|--a0771a@QU|--a0771a@BFKK&q1EAq>E!Q'N**r!   c                 H   t        | |      }| dddf   | dddf   z
  | dddf   | dddf   z
  z  j                  d      j                  |      }|dddf   |dddf   z
  |dddf   |dddf   z
  z  j                  d      j                  |      }||z   |z
  }||z  S )u	  Compute the jaccard overlap of two sets of boxes.  The jaccard overlap
    is simply the intersection over union of two boxes.  Here we operate on
    ground truth boxes and default boxes.
    E.g.:
        A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
    Args:
        box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4]
        box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]
    Return:
        jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]
    Nr   r   r+   r	   )r   r   	expand_as)r   r   r   area_aarea_bunions         r    jaccardr     s     eU#EQT{U1a4[(q!tuQT{*,-6Yq\))E:J QT{U1a4[(q!tuQT{*,-6Yq\))E:J VOe#E5=r!   c                 V    t        | j                  d      |      j                  d      S )z?Computes the IOU between a bounding box and set of other boxes.r   )r   r   r   )boxr   s     r    r   r     s#    3==#[199!<<r!   )numpyr   rp   torch.nnr   torch.nn.functional
functionalr%   Moduler   r3   r8   r   r   r    r!   r    <module>r      sW        #+ #+J'bii '*p!		 p!j+**=r!   