
    |h+%                     0   d dl Z d dlmZmZ d dlZd dlmZ deej                  e	f   deej                  e	f   de
fdZdej                  d	ed
ededeej                  e
ef   f
dZdeeeeef   de
dedeeef   deeeeef   f
dZd Zd Zd Zd Zd Zy)    N)UnionTuple)Imagesource_representationtest_representationreturnc                     t        | t              rt        j                  |       } t        |t              rt        j                  |      }| |z
  }t        j                  t        j
                  ||            }t        j                  |      }|S )z
    Find euclidean distance between 2 vectors
    Args:
        source_representation (numpy array or list)
        test_representation (numpy array or list)
    Returns
        distance
    )
isinstancelistnparraysummultiplysqrt)r   r   euclidean_distances      ]/var/www/html/test/engine/venv/lib/python3.12/site-packages/retinaface/commons/postprocess.pyfind_euclidean_distancer   
   sx     '. ")> ?%t, hh':;.1DD,>@R ST!34    imgleft_eye	right_eyenosec                    |\  }}|\  }}||kD  r||f}d}	n||f}d}	t        t        j                  |      t        j                  |            }
t        t        j                  |      t        j                  |            }t        t        j                  |      t        j                  |            }|dk7  r|dk7  r||z  ||z  z   |
|
z  z
  d|z  |z  z  }t        dt	        d|            }t        j
                  |      }|dz  t        j                  z  }|	dk(  rd|z
  }t        j                  |       } t        j                  | j                  |	|z              } nd	}| ||	fS )
a  
    Alignma given face with respect to the left and right eye coordinates.
    Left eye is the eye appearing on the left (right eye of the person). Left top point is (0, 0)
    Args:
        img (numpy array): given image
        left_eye (tuple): left eye coordinates.
            Left eye is appearing on the left of image (right eye of the person)
        right_eye (tuple): right eye coordinates.
            Right eye is appearing on the right of image (left eye of the person)
        nose (tuple): coordinates of nose
       r            ?g         Z           )r   r   r   minmaxarccosmathpir   	fromarrayrotate)r   r   r   r   
left_eye_x
left_eye_yright_eye_xright_eye_y	point_3rd	directionabccos_aangles                  r   alignment_procedurer3   !   sW    &J
(K K *-		-		
 	  2BHHY4GHA 3RXXi5HIA 3RXXh5GHA 	Av!q&QQQ&1q5195 CT5)*		% '
 ?JEooc"hhszz)e"345 y  r   facial_arear2   r-   sizec                    |t         j                  z  dz  }|\  }}| d   | d   z   dz  |dz  z
  }| d   | d   z   dz  |dz  z
  }|t        j                  |      z  ||z  t        j                  |      z  z   }| |z  t        j                  |      z  |t        j                  |      z  z   }	||dz  z   }|	|dz  z   }	|| d   | d   z
  dz  z
  }
|	| d   | d   z
  dz  z
  }|| d   | d   z
  dz  z   }|	| d   | d   z
  dz  z   }t	        t        |
      d      }
t	        t        |      d      }t        t        |      |      }t        t        |      |      }|
|||fS )a  
    Rotate the facial area around its center.

    Args:
        facial_area (tuple of int): Representing the (x1, y1, x2, y2) of the facial area.
        angle (float): Angle of rotation in degrees.
        direction (int): Direction of rotation (-1 for clockwise, 1 for counterclockwise).
        size (tuple of int): Tuple representing the size of the image (width, height).

    Returns:
        rotated_facial_area (tuple of int): Representing the new coordinates
            (x1, y1, x2, y2) of the rotated facial area.
    r   r   r   r      )r   r%   cossinr"   intr!   )r4   r2   r-   r5   heightweightxyx_newy_newx1y1x2y2s                 r   rotate_facial_arearE   b   s   " BEEMCENFF 
Q+a.	(A-
:A	Q+a.	(A-
:A uIu ==EBNRVVE]*Q->>E FQJEFQJE 
+a.;q>1Q6	6B	+a.;q>1Q6	6B	+a.;q>1Q6	6B	+a.;q>1Q6	6B 
SWaB	SWaB	SWf	B	SWf	BBr   c                 >   | j                   d   dk(  r$t        j                  d|j                   d   f      S | j                  t        d      } | dddf   | dddf   z
  dz   }| dddf   | dddf   z
  dz   }| dddf   d	|dz
  z  z   }| dddf   d	|dz
  z  z   }|ddddf   }|ddddf   }|ddddf   }|dddd
f   }	||ddt        j
                  f   z  |ddt        j
                  f   z   }
||ddt        j
                  f   z  |ddt        j
                  f   z   }t        j                  |      |ddt        j
                  f   z  }t        j                  |	      |ddt        j
                  f   z  }t        j                  |j                         }|
d	|dz
  z  z
  |ddddf<   |d	|dz
  z  z
  |ddddf<   |
d	|dz
  z  z   |ddddf<   |d	|dz
  z  z   |dddd
f<   |j                   d   d
kD  r|ddd
df   |ddd
df<   |S )z
    This function is copied from the following code snippet:
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/master/retinaface.py
    r   r   FcopyNr   r   r7         ?   )shaper   zerosastypefloatnewaxisexp)boxes
box_deltaswidthsheightsctr_xctr_ydxdydwdh
pred_ctr_x
pred_ctr_ypred_wpred_h
pred_boxess                  r   	bbox_predr`      sY   
 {{1~xxJ,,Q/011LLUL+E1a4[5A;&,FAqDkE!Q$K'#-G!Q$K##..E!Q$K#3//E	AqsF	B	AqsF	B	AqsF	B	AqsF	BfQ

]++eArzzM.BBJgam,,uQ

]/CCJVVBZ&BJJ//FVVBZ'!RZZ-00F***+J#cVc\&::Jq!A#v#cVc\&::Jq!A#v#cVc\&::Jq!A#v#cVc\&::Jq!A#vQ&q!"u-
1ab5r   c                    | j                   d   dk(  r$t        j                  d|j                   d   f      S | j                  t        d      } | dddf   | dddf   z
  dz   }| dddf   | dddf   z
  dz   }| dddf   d	|dz
  z  z   }| dddf   d	|dz
  z  z   }|j                         }t        d
      D ]2  }|dd|df   |z  |z   |dd|df<   |dd|df   |z  |z   |dd|df<   4 |S )z
    This function copied from the following code snippet
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/master/retinaface.py
    r   r   FrG   Nr   r   r7   rI      )rK   r   rL   rM   rN   rH   range)rQ   landmark_deltasrS   rT   rU   rV   predis           r   landmark_predrg      s+   
 {{1~xxO11!4566LLUL+E1a4[5A;&,FAqDkE!Q$K'#-G!Q$K##..E!Q$K#3//E!D1X C'1a069EAQ1W'1a07:UBQ1WC Kr   c           	      &   t        j                  t        j                  | dddddf   |d   dz
        d      | dddddf<   t        j                  t        j                  | dddddf   |d   dz
        d      | dddddf<   t        j                  t        j                  | dddddf   |d   dz
        d      | dddddf<   t        j                  t        j                  | dddddf   |d   dz
        d      | dddddf<   | S )z
    This function copied from rcnn module of retinaface-tf2 project
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/master/rcnn/processing/bbox_transform.py
    Nr   rJ   r   r   r7   )r   maximumminimum)rQ   im_shapes     r   
clip_boxesrl      s    ZZ

5ADqD>8A;? KQOE!QTT'NZZ

5ADqD>8A;? KQOE!QTT'NZZ

5ADqD>8A;? KQOE!QTT'NZZ

5ADqD>8A;? KQOE!QTT'NLr   c           	      h   |j                   d   }t        j                  t        j                  d|      t        j                  ddt        j                  t        j                  f   | d|df      }t        j                  t        j                  d|       ddt        j                  t        j                  t        j                  f   d||df      }t        j
                  ||||gd      |z  t        j                  |t        j                  t        j                  ddddf   | |ddf      z   }|S )z
    This function is mainly based on the following code snippet
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/master/rcnn/cython/anchors.pyx
    r   Nr   r   )axis)rK   r   tilearangerO   concatenate)r;   widthstridebase_anchorsAc_0_2c_1_3all_anchorss           r   anchors_planery      s    
 	1AGGBIIa'

Arzz2::(MNQWYZ\]_`PabEGGBIIa(BJJ

BJJ)NORSUZ\]_`QabE..%u!=BG&PSUSZSZRZZQ12VUAq4IT K r   c                    | dddf   }| dddf   }| dddf   }| dddf   }| dddf   }||z
  dz   ||z
  dz   z  }|j                         ddd   }| j                  d   }	t        j                  |	t              }
g }t        |	      D ]  }||   }|
|   dk(  r|j                  |       ||   }||   }||   }||   }||   }t        |dz   |	      D ]  }||   }|
|   dk(  rt        |||         }t        |||         }t        |||         }t        |||         }t        d	||z
  dz         }t        d	||z
  dz         }||z  }||||   z   |z
  z  }||k\  sd|
|<     |S )
z
    This function is mainly based on the following code snippet
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/master/rcnn/cython/cpu_nms.pyx
    Fast R-CNN by Ross Girshick
    Nr   r   r   r7   rJ   r   )dtyper    )	argsortrK   r   rL   r:   rc   appendr"   r!   )dets	thresholdrA   rB   rC   rD   scoresareasorderndets
suppressedkeep_irf   ix1iy1ix2iy2iarea_jjxx1yy1xx2yy2whinterovrs                                r   cpu_nmsr      s    
adB	adB	adB	adB!Q$ZF"Wq[R"Wq[)ENNTrT"EJJqME5-JDEl ""Ia=AAeeeeaQ& 	"Bb	A!}!c2a5/Cc2a5/Cc2a5/Cc2a5/CCsQ'ACsQ'AEE558+e34Ci !
1	""2 Kr   )r$   typingr   r   numpyr   PILr   ndarrayr   rN   r   tupler:   r3   rE   r`   rg   rl   ry   r    r   r   <module>r      s        T!12INrzz[_O_I`
.>!	>!$>!16>!>C>!
2::uc!">!B-sCc)*-38-EH-PUVY[^V^P_-
3S#-`%P& ,r   