
    >|h)                          d dl mZmZmZmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZmZ 	 	 	 	 	 	 ddeeej                   f   deeef   ded	ed
edededeeeef      fdZy)    )AnyDictListUnionN)tqdm)modeling	detection)GenderRaceEmotionimg_pathactionsenforce_detectiondetector_backendalignexpand_percentagesilentreturnc           	      F   t        |t              r|f}t        |d      r|st        d      t	        |      }|D ]  }|dvst        dt        |       d       g }t        j                  | d|d|||      }	|	D ]  }
|
d	   }|
d
   }|
d   }|j                  d   dkD  s&|j                  d   dkD  s9i }t        t        dt        |            dt        |      dkD  r|nd      }|D ]   }||   }|j                  d|        |dk(  rt        j                  d      j                  |      }|j!                         }i |d<   t#        t$        j&                        D ]  \  }}d||   z  |z  }||d   |<    t$        j&                  t)        j*                  |         |d<   n]|dk(  r4t        j                  d      j                  |      }t-        |      |d<   n$|dk(  rt        j                  d      j                  |      }i |d<   t#        t.        j&                        D ]  \  }}d||   z  }||d   |<    t.        j&                  t)        j*                  |         |d<   n|dk(  rt        j                  d      j                  |      }|j!                         }i |d<   t#        t0        j&                        D ]  \  }}d||   z  |z  }||d   |<    t0        j&                  t)        j*                  |         |d<   ||d<   ||d<   # |j3                  |        |S ) a&  
    Analyze facial attributes such as age, gender, emotion, and race in the provided image.

    Args:
        img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
            or a base64 encoded image. If the source image contains multiple faces, the result will
            include information for each detected face.

        actions (tuple): Attributes to analyze. The default is ('age', 'gender', 'emotion', 'race').
            You can exclude some of these attributes from the analysis if needed.

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

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

        distance_metric (string): Metric for measuring similarity. Options: 'cosine',
            'euclidean', 'euclidean_l2' (default is cosine).

        align (boolean): Perform alignment based on the eye positions (default is True).

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

        silent (boolean): Suppress or allow some log messages for a quieter analysis process
            (default is False).

    Returns:
        results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary represents
           the analysis results for a detected face.

           Each dictionary in the list contains the following keys:

           - 'region' (dict): Represents the rectangular region of the detected face in the image.
               - 'x': x-coordinate of the top-left corner of the face.
               - 'y': y-coordinate of the top-left corner of the face.
               - 'w': Width of the detected face region.
               - 'h': Height of the detected face region.

           - 'age' (float): Estimated age of the detected face.

           - 'face_confidence' (float): Confidence score for the detected face.
                Indicates the reliability of the face detection.

           - 'dominant_gender' (str): The dominant gender in the detected face.
                Either "Man" or "Woman."

           - 'gender' (dict): Confidence scores for each gender category.
               - 'Man': Confidence score for the male gender.
               - 'Woman': Confidence score for the female gender.

           - 'dominant_emotion' (str): The dominant emotion in the detected face.
                Possible values include "sad," "angry," "surprise," "fear," "happy,"
                "disgust," and "neutral."

           - 'emotion' (dict): Confidence scores for each emotion category.
               - 'sad': Confidence score for sadness.
               - 'angry': Confidence score for anger.
               - 'surprise': Confidence score for surprise.
               - 'fear': Confidence score for fear.
               - 'happy': Confidence score for happiness.
               - 'disgust': Confidence score for disgust.
               - 'neutral': Confidence score for neutrality.

           - 'dominant_race' (str): The dominant race in the detected face.
                Possible values include "indian," "asian," "latino hispanic,"
                "black," "middle eastern," and "white."

           - 'race' (dict): Confidence scores for each race category.
               - 'indian': Confidence score for Indian ethnicity.
               - 'asian': Confidence score for Asian ethnicity.
               - 'latino hispanic': Confidence score for Latino/Hispanic ethnicity.
               - 'black': Confidence score for Black ethnicity.
               - 'middle eastern': Confidence score for Middle Eastern ethnicity.
               - 'white': Confidence score for White ethnicity.
    __getitem__z$`actions` must be a list of strings.emotionagegenderracezInvalid action passed (z9)). Valid actions are `emotion`, `age`, `gender`, `race`.)   r   F)r   target_sizer   	grayscaler   r   r   facefacial_area
confidencer      zFinding actionsT)descdisablezAction: r   r   d   dominant_emotionr   Ager   r
   dominant_genderr   r   dominant_raceregionface_confidence)
isinstancestrhasattr
ValueErrorlistreprr	   extract_facesshaper   rangelenset_descriptionr   build_modelpredictsum	enumerater   labelsnpargmaxintr
   r   append)r   r   r   r   r   r   r   actionresp_objectsimg_objsimg_objimg_content
img_regionimg_confidenceobjpbarindexemotion_predictionssum_of_predictionsiemotion_labelemotion_predictionapparent_agegender_predictionsgender_labelgender_predictionrace_predictions
race_labelrace_predictions                                Z/var/www/html/test/engine/venv/lib/python3.12/site-packages/deepface/modules/demography.pyanalyzerW      s   n '3* 7M*'?@@7mG  ==)$v, 8H H  L&&)++H  :%fo]+
 .Q!#(9(9!(<q(@CaW&&"%g,"2D
  ,8 $$xx%89Y&*2*>*>y*I*Q*QR]*^')<)@)@)B&%'C	N,5gnn,E K(=-03Fq3I-IL^-^*8JI}5K /6nnRYYGZ=[.\C*+u_#+#7#7#>#F#F{#SL!$\!2CJx')1)=)=h)G)O)OP[)\&$&CM+4V]]+C H<,/2DQ2G,G)6GHl3H .4]]299EW;X-YC)*v%'/';';F'C'K'KK'X$)9)=)=)?&"$CK)24;;)? B:*-0@0C*CFX*X2AFJ/B ,0;;ryyAQ7R+SC( !+H)7%&Y,8\ $u:%x     )r   TopencvTr   F)typingr   r   r   r   numpyr<   r   deepface.modulesr   r	   deepface.extendedmodelsr
   r   r   r-   ndarraytupler0   boolr>   rW    rX   rV   <module>rb      s    ) )   1 9 9
 #G"$pCO$p5$;p p 	p
 p p p 
$sCx.prX   