
    ihz                        d dl mZmZmZ d dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZmZmZmZmZmZmZ d dlmZ  ed      Zd	ed
efdZ	 dd	edej6                  deded
ee
   f
dZdeeeeef   dedeeef   d
eeeeef   fdZ y)    )AnyListTupleN)	detection)DetectorDetectedFaceFacialAreaRegion)		FastMtCnn	MediaPipeMtCnnOpenCvDlib
RetinaFaceSsdYoloYuNet)Loggerz%deepface/detectors/DetectorWrapper.py)moduledetector_backendreturnc           
         t         j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t         j"                  d	}dt%               vri at)        t&        j+                               }| |vr:|j-                  |       }|r |       }|t&        | <   t&        |    S t/        d| z         t&        |    S )z
    Build a face detector model
    Args:
        detector_backend (str): backend detector name
    Returns:
        built detector (Any)
    )	opencvmtcnnssddlib
retinaface	mediapipeyolov8yunet	fastmtcnnface_detector_objz"invalid detector_backend passed - )r   OpenCvClientr   MtCnnClientr   	SsdClientr   
DlibClientr   RetinaFaceClientr   MediaPipeClientr   
YoloClientr   YuNetClientr
   FastMtCnnClientglobalsr!   listkeysget
ValueError)r   backendsbuilt_modelsface_detectors       `/var/www/html/dev/engine/venv/lib/python3.12/site-packages/deepface/detectors/DetectorWrapper.pybuild_modelr4      s     %%""}} 11..//""..
H ')+)..01L|+ %56)OM2?./ -.. ADTTUU-..    imgalignexpand_percentagec                 V   t        |       }|dk  rt        j                  d| d       d}|j                  |      }g }|D ]  }|j                  }|j
                  }	|j                  }
|j                  }|j                  }|j                  }|j                  }|dkD  r|
t        |
|z  dz        z   }|t        ||z  dz        z   }t        d|t        ||
z
  dz        z
        }t        d|	t        ||z
  dz        z
        }	t        |j                  d   |z
  |      }
t        |j                  d   |	z
  |      }|t        |	      t        |	|z         t        |      t        ||
z         f   }|du rt        j                   |||	      \  }}t#        ||	||
z   |	|z   f||j                  d   |j                  d   f
      \  }}}}|t        |      t        |      t        |      t        |      f   }t%        |t'        ||	||
|||      |      }|j)                  |        |S )a  
    Detect face(s) from a given image
    Args:
        detector_backend (str): detector name

        img (np.ndarray): pre-loaded image

        align (bool): enable or disable alignment after detection

        expand_percentage (int): expand detected facial area with a percentage (default is 0).

    Returns:
        results (List[DetectedFace]): A list of DetectedFace objects
            where each object contains:

        - img (np.ndarray): The detected face as a NumPy array.

        - facial_area (FacialAreaRegion): The facial area region represented as x, y, w, h

        - confidence (float): The confidence score associated with the detected face.
    r   z7Expand percentage cannot be negative but you set it to z.Overwritten it to 0.)r6   d         T)r6   left_eye	right_eye)facial_areaanglesize)xyhw
confidencer=   r>   )r6   r?   rF   )r4   loggerwarndetect_facesrB   rC   rE   rD   r=   r>   rF   intmaxminshaper   
align_facerotate_facial_arear   r	   append)r   r6   r7   r8   r2   facial_areasresultsr?   rB   rC   rE   rD   r=   r>   rF   
expanded_w
expanded_hdetected_facealigned_imgr@   
rotated_x1
rotated_y1
rotated_x2
rotated_y2results                            r3   rI   rI   ;   sn   0 **:;M 1EFWEX Y# #	
  !--#-6LG# ,MMMMMMMM''))	 ++
q  S%6!6!<==JS%6!6!<==JAq3
Q!3445AAq3
Q!3445ACIIaL1$j1ACIIaL1$j1A CFSQZ/Q#a!e*1DDE D=!*!5!5(i"K >P1q5!a%0iilCIIaL1>:J
J

 (J#j/1J#j/123M (qAzHXa "
 	vY,Z Nr5   r?   r@   rA   c                    |dk\  rdnd}t        |      dz  }|dk(  r| S |t        j                  z  dz  }| d   | d   z   dz  |d   dz  z
  }| d   | d   z   dz  |d   dz  z
  }|t        j                  |      z  ||z  t        j                  |      z  z   }| |z  t        j                  |      z  |t        j                  |      z  z   }||d   dz  z   }||d   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        |	      t        |
      t        |      fS )a  
    Rotate the facial area around its center.
    Inspried from the work of @UmutDeniz26 - github.com/serengil/retinaface/pull/80

    Args:
        facial_area (tuple of int): Representing the (x1, y1, x2, y2) of the facial area.
            x2 is equal to x1 + w1, and y2 is equal to y1 + h1
        angle (float): Angle of rotation in degrees. Its sign determines the direction of rotation.
                       Note that angles > 360 degrees are normalized to the range [0, 360).
        size (tuple of int): Tuple representing the size of the image (width, height).

    Returns:
        rotated_coordinates (tuple of int): Representing the new coordinates
            (x1, y1, x2, y2) or (x1, y1, x1+w1, y1+h1) of the rotated facial area.
    r   r<   ih     r;      )absnppicossinrJ   )r?   r@   rA   	directionrB   rC   x_newy_newx1y1x2y2s               r3   rO   rO      s   2 aZRIJEz BEEMCE 
Q+a.	(A-Q!;A	Q+a.	(A-Q!;A uIu ==EBNRVVE]*Q->>E DGaKEDGaKE 
+a.;q>1Q6	6B	+a.;q>1Q6	6B	+a.;q>1Q6	6B	+a.;q>1Q6	6BGSWc"gs2w//r5   )Tr   )!typingr   r   r   numpyra   deepface.modulesr   deepface.models.Detectorr   r   r	   deepface.detectorsr
   r   r   r   r   r   r   r   r   deepface.commons.loggerr   rG   strr4   ndarrayboolrJ   rI   floatrO    r5   r3   <module>rw      s    # #  & M M
 
 
 +	>	?#/# #/# #/N Z[SS "

S37SSVS	,Sl30sCc)*3030 S/30 3S#	30r5   