
    >|h                        d dl mZmZmZmZmZmZ d dlZd dl	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  ed	
      Z ej0                         Zedk(  rd dlmZ nedk(  rd dlmZ 	 	 	 	 	 	 	 ddeeej<                  f   deeeef      dede de dede deeeef      fdZ!dej<                  dee"e#f   dee"e#f   deej<                  e$f   fdZ%y)    )AnyDictListTupleUnionOptionalN)Image)preprocessing)DetectedFaceFacialAreaRegion)DetectorWrapper)package_utils)Loggerzdeepface/modules/detection.py)module   )image   img_pathtarget_sizedetector_backendenforce_detectionalignexpand_percentage	grayscalereturnc                 (   g }t        j                  |       \  }	}
|	t        d|
       t        dd|	j                  d   |	j                  d   d      }|dk(  rt        |	|d      g}nt        j                  ||	||      }t        |      dk(  r |d	u r|
t        d
|
 d      t        d      t        |      dk(  r|du rt        |	|d      g}|D ]  }|j                  }|j                  }|j                  d   dk(  s|j                  d   dk(  rA|d	u r$t        j                  |t        j                        }|8|d   |j                  d   z  }|d   |j                  d   z  }t        ||      }t        |j                  d   |z        t        |j                  d   |z        f}t        j                   ||      }|d   |j                  d   z
  }|d   |j                  d   z
  }|du r1t#        j$                  ||dz  ||dz  z
  f|dz  ||dz  z
  fdfd      }n/t#        j$                  ||dz  ||dz  z
  f|dz  ||dz  z
  ffd      }|j                  dd |k7  rt        j                   ||      }t'        j(                  |      }t#        j*                  |d      }|dz  }|d	u rt        |j                        dk(  r|d   }|j-                  |d	u r|dddddddf   n|t        |j.                        t        |j0                        t        |j2                        t        |j4                        |j6                  |j8                  dt;        |j<                  d      d        t        |      dk(  r|d	k(  rt        d|
 d      |S )aF  
    Extract faces from a given image

    Args:
        img_path (str or np.ndarray): Path to the first image. Accepts exact image path
            as a string, numpy array (BGR), or base64 encoded images.

        target_size (tuple): final shape of facial image. black pixels will be
            added to resize the image.

        detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
            'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv)

        enforce_detection (boolean): If no face is detected in an image, raise an exception.
            Default is True. Set to False to avoid the exception for low-resolution images.

        align (bool): Flag to enable face alignment (default is True).

        expand_percentage (int): expand detected facial area with a percentage

        grayscale (boolean): Flag to convert the image to grayscale before
            processing (default is False).

        human_readable (bool): Flag to make the image human readable. 3D RGB for human readable
            or 4D BGR for ML models (default is False).

    Returns:
        results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary contains:

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

        - "facial_area" (Dict[str, Any]): The detected face's regions as a dictionary containing:
            - keys 'x', 'y', 'w', 'h' with int values
            - keys 'left_eye', 'right_eye' with a tuple of 2 ints as values

        - "confidence" (float): The confidence score associated with the detected face.
    NzException while loading r   r   )xywh
confidenceskip)imgfacial_arear!   )r   r#   r   r   TzFace could not be detected in ze.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.zFace could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.Fr   )r   r   constant)axis      )r   r   r   r    left_eye	right_eye)facer$   r!   z&Exception while extracting faces from z0.Consider to set enforce_detection arg to False.)r
   
load_image
ValueErrorr   shaper   r   detect_faceslenr#   r$   cv2cvtColorCOLOR_BGR2GRAYminintresizenppadr   img_to_arrayexpand_dimsappendr   r   r   r    r*   r+   roundr!   )r   r   r   r   r   r   r   human_readable	resp_objsr#   img_namebase_region	face_objsface_objcurrent_imgcurrent_regionfactor_0factor_1factordsizediff_0diff_1
img_pixelss                          Y/var/www/html/test/engine/venv/lib/python3.12/site-packages/deepface/modules/detection.pyextract_facesrN      s   ` I ",,X6MC
{3H:>??"Q!syy|syy|XYZK6!!c{qQR	#00-/	
	 9~0D80
 ;G G  G 
 9~0E9!c{qQR	 G
ll!--Q1$(9(9!(<(A,,{C4F4FGK ""1~(9(9!(<<H"1~(9(9!(<<H8,F K%%a(612K%%a(612E **[%8K ^k&7&7&::F ^k&7&7&::FE! ff1fv{&:;1fv{&:;
  !ff1fv{&:;1fv{&:;    1%4!jjkB ''4
^^JQ7
c
T!c**:*:&;q&@#AJ2@D2H
1a2:.j^--.^--.^--.^--. . 7 7!/!9!9  $N$=$=qA	
uG
R 9~0D84XJ ?> >
 	

     r#   r*   r+   c           
      `   ||| dfS | j                   d   dk(  s| j                   d   dk(  r| dfS t        t        j                  t        j                  |d   |d   z
  |d   |d   z
                    }t        j
                  t        j                  |       j                  |            } | |fS )a  
    Align a given image horizantally with respect to their left and right eye locations
    Args:
        img (np.ndarray): pre-loaded image with detected face
        left_eye (list or tuple): coordinates of left eye with respect to the you
        right_eye(list or tuple): coordinates of right eye with respect to the you
    Returns:
        img (np.ndarray): aligned facial image
    r   r   )	r/   floatr8   degreesarctan2arrayr	   	fromarrayrotate)r#   r*   r+   angles       rM   
align_facerX      s     9,Av yy|qCIIaLA-Av"**RZZ	!x{(BIaLS[\]S^D^_`aE
((5??3'..u5
6C:rO   ))   rY   opencvTTr   FF)&typingr   r   r   r   r   r   numpyr8   r2   PILr	   deepface.modulesr
   deepface.models.Detectorr   r   deepface.detectorsr   deepface.commonsr   deepface.commons.loggerr   loggerget_tf_major_versiontf_major_versionkeras.preprocessingr   tensorflow.keras.preprocessingstrndarrayr6   boolrN   listtuplerQ   rX    rO   rM   <module>rn      sA   : :  
  + C . * *	6	7
 6=557 q)4
 .8$"dCO$d%S/*d d 	d
 d d d 
$sCx.dN	D%K  T5[! 2::u	rO   