o
    >h(                     @   s.  d dl Zd dlZd dlZd dlmZ zd dlZW n ey,   ed ed e	  Y nw de_
e Ze ZeeZe ZeeZe ZeeZe ZeeZdd Zdd	 Zd
d Zdd Zd'ddZ d(ddZ!d(ddZ"d)ddZ#d)ddZ$d*ddZ%d*dd Z&d+d"d#Z'd,d%d&Z(dS )-    N)	ImageFilez\Please install `face_recognition_models` with this command before using `face_recognition`:
zCpip install git+https://github.com/ageitgey/face_recognition_modelsTc                 C   s   |   |  |  |  fS )z
    Convert a dlib 'rect' object to a plain tuple in (top, right, bottom, left) order

    :param rect: a dlib 'rect' object
    :return: a plain tuple representation of the rect in (top, right, bottom, left) order
    )toprightbottomleft)rect r   Z/home/www/facesmatcher.com/frenv_dlib/lib/python3.10/site-packages/face_recognition/api.py_rect_to_css    s   r
   c                 C   s    t | d | d | d | d S )z
    Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object

    :param css:  plain tuple representation of the rect in (top, right, bottom, left) order
    :return: a dlib `rect` object
       r         )dlibZ	rectangle)cssr   r   r	   _css_to_rect*   s    r   c                 C   s<   t | d dt| d |d t| d |d t | d dfS )aX  
    Make sure a tuple in (top, right, bottom, left) order is within the bounds of the image.

    :param css:  plain tuple representation of the rect in (top, right, bottom, left) order
    :param image_shape: numpy shape of the image array
    :return: a trimmed plain tuple representation of the rect in (top, right, bottom, left) order
    r   r   r   r   )maxmin)r   Zimage_shaper   r   r	   _trim_css_to_bounds4   s   <r   c                 C   s*   t | dkrtdS tjj| | ddS )a  
    Given a list of face encodings, compare them to a known face encoding and get a euclidean distance
    for each comparison face. The distance tells you how similar the faces are.

    :param faces: List of face encodings to compare
    :param face_to_compare: A face encoding to compare against
    :return: A numpy ndarray with the distance for each face in the same order as the 'faces' array
    r   r   )Zaxis)lennpemptyZlinalgZnorm)face_encodingsZface_to_comparer   r   r	   face_distance?   s   	
r   RGBc                 C   s$   t j| }|r||}t|S )a#  
    Loads an image file (.jpg, .png, etc) into a numpy array

    :param file: image file name or file object to load
    :param mode: format to convert the image to. Only 'RGB' (8-bit RGB, 3 channels) and 'L' (black and white) are supported.
    :return: image contents as numpy array
    )PILZImageopenconvertr   array)filemodeZimr   r   r	   load_image_fileN   s   

r    r   hogc                 C   s   |dkr	t | |S t| |S )a  
    Returns an array of bounding boxes of human faces in a image

    :param img: An image (as a numpy array)
    :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
    :param model: Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate
                  deep-learning model which is GPU/CUDA accelerated (if available). The default is "hog".
    :return: A list of dlib 'rect' objects of found face locations
    cnn)cnn_face_detectorface_detectorimgnumber_of_times_to_upsamplemodelr   r   r	   _raw_face_locations\   s   


r)   c                    s<   |dkr fddt  |dD S  fddt  ||D S )a7  
    Returns an array of bounding boxes of human faces in a image

    :param img: An image (as a numpy array)
    :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
    :param model: Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate
                  deep-learning model which is GPU/CUDA accelerated (if available). The default is "hog".
    :return: A list of tuples of found face locations in css (top, right, bottom, left) order
    r"   c                    s   g | ]}t t|j jqS r   r   r
   r   shape.0Zfacer&   r   r	   
<listcomp>w       z"face_locations.<locals>.<listcomp>c                    s   g | ]
}t t| jqS r   )r   r
   r+   r,   r.   r   r	   r/   y   s    )r)   r%   r   r.   r	   face_locationsl   s   
r1      c                 C   s   t | ||dS )aa  
    Returns an 2d array of dlib rects of human faces in a image using the cnn face detector

    :param img: A list of images (each as a numpy array)
    :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
    :return: A list of dlib 'rect' objects of found face locations
    )
batch_size)r#   )imagesr'   r3   r   r   r	   _raw_face_locations_batched|   s   r5   c                    s&    fdd}t  ||}tt||S )a  
    Returns an 2d array of bounding boxes of human faces in a image using the cnn face detector
    If you are using a GPU, this can give you much faster results since the GPU
    can process batches of images at once. If you aren't using a GPU, you don't need this function.

    :param images: A list of images (each as a numpy array)
    :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
    :param batch_size: How many images to include in each GPU processing batch.
    :return: A list of tuples of found face locations in css (top, right, bottom, left) order
    c                    s    fdd| D S )Nc                    s"   g | ]}t t|j d  jqS )r   r*   r,   r4   r   r	   r/      s   " zObatch_face_locations.<locals>.convert_cnn_detections_to_css.<locals>.<listcomp>r   )Z
detectionsr6   r   r	   convert_cnn_detections_to_css   s   z;batch_face_locations.<locals>.convert_cnn_detections_to_css)r5   listmap)r4   r'   r3   r7   Zraw_detections_batchedr   r6   r	   batch_face_locations   s   r:   largec                    sD   |d u r	t  }ndd |D }t|dkrt fdd|D S )Nc                 S   s   g | ]}t |qS r   )r   r-   Zface_locationr   r   r	   r/      s    z'_raw_face_landmarks.<locals>.<listcomp>smallc                    s   g | ]} |qS r   r   r<   
face_imageZpose_predictorr   r	   r/      s    )r)   pose_predictor_68_pointpose_predictor_5_point)r?   r1   r(   r   r>   r	   _raw_face_landmarks   s   
rB   c                 C   sN   t | ||}dd |D }|dkrdd |D S |dkr#dd |D S td)a  
    Given an image, returns a dict of face feature locations (eyes, nose, etc) for each face in the image

    :param face_image: image to search
    :param face_locations: Optionally provide a list of face locations to check.
    :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster.
    :return: A list of dicts of face feature locations (eyes, nose, etc)
    c                 S   s   g | ]}d d |  D qS )c                 S   s   g | ]}|j |jfqS r   )xy)r-   pr   r   r	   r/      s    z-face_landmarks.<locals>.<listcomp>.<listcomp>)parts)r-   Zlandmarkr   r   r	   r/      r0   z"face_landmarks.<locals>.<listcomp>r;   c                 S   s   g | ]i}|d d |dd |dd |dd |dd |dd |dd |dd |d	 g |d
 g |d g |d g |d g |dd |d g |d g |d g |d g |d g |d	 g d	qS )r               $   *   0   7   @   ?   >   =   <   6   C   B   A   )	ZchinZleft_eyebrowZright_eyebrowZnose_bridgenose_tipleft_eye	right_eyeZtop_lipZ
bottom_lipr   r-   Zpointsr   r   r	   r/      s    







<F
r=   c                 S   s.   g | ]}|d  g|dd  |dd dqS )   r   r   )rX   rY   rZ   r   r[   r   r   r	   r/      s    


zFInvalid landmarks model type. Supported models are ['small', 'large'].)rB   
ValueError)r?   r1   r(   Z	landmarksZlandmarks_as_tuplesr   r   r	   face_landmarks   s   	
r^   r=   c                    s    t  ||} fdd|D S )aj  
    Given an image, return the 128-dimension face encoding for each face in the image.

    :param face_image: The image that contains one or more faces
    :param known_face_locations: Optional - the bounding boxes of each face if you already know them.
    :param num_jitters: How many times to re-sample the face when calculating encoding. Higher is more accurate, but slower (i.e. 100 is 100x slower)
    :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster.
    :return: A list of 128-dimensional face encodings (one for each face in the image)
    c              	      s    g | ]}t t |qS r   )r   r   face_encoderZcompute_face_descriptor)r-   Zraw_landmark_setr?   num_jittersr   r	   r/      s     z"face_encodings.<locals>.<listcomp>)rB   )r?   Zknown_face_locationsra   r(   Zraw_landmarksr   r`   r	   r      s   
r   333333?c                 C   s   t t| ||kS )a  
    Compare a list of face encodings against a candidate encoding to see if they match.

    :param known_face_encodings: A list of known face encodings
    :param face_encoding_to_check: A single face encoding to compare against the list
    :param tolerance: How much distance between faces to consider it a match. Lower is more strict. 0.6 is typical best performance.
    :return: A list of True/False values indicating which known_face_encodings match the face encoding to check
    )r8   r   )Zknown_face_encodingsZface_encoding_to_checkZ	tolerancer   r   r	   compare_faces   s   	rc   )r   )r   r!   )r   r2   )Nr;   )Nr   r=   )rb   ))Z	PIL.Imager   r   numpyr   r   Zface_recognition_models	ExceptionprintquitZLOAD_TRUNCATED_IMAGESZget_frontal_face_detectorr$   Zpose_predictor_model_locationZpredictor_68_point_modelZshape_predictorr@   Z(pose_predictor_five_point_model_locationZpredictor_5_point_modelrA   Z cnn_face_detector_model_locationZcnn_face_detection_modelZcnn_face_detection_model_v1r#   Zface_recognition_model_locationZface_recognition_modelZface_recognition_model_v1r_   r
   r   r   r   r    r)   r1   r5   r:   rB   r^   r   rc   r   r   r   r	   <module>   sD   














#