
    I/Yh(                        d dl Zd dlZd dlZd dlmZ 	 d dlZn+# e$ r#  ed            ed            e	             Y nw xY wde_
         ej                    Z ej                    Z ej        e          Z ej                    Z ej        e          Z ej                    Z ej        e          Z ej                    Z ej        e          Zd Zd Zd Zd	 ZddZ ddZ!ddZ"ddZ#ddZ$ddZ%ddZ&ddZ'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                     |                                  |                                 |                                 |                                 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)rects    \D:\DownLoads\facial_attendance_system-main\venv311\Lib\site-packages\face_recognition/api.py_rect_to_cssr       s3     88::tzz||T[[]]DIIKK??    c                 `    t          j        | 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         )dlib	rectangle)csss    r
   _css_to_rectr   *   s*     >#a&#a&#a&#a&999r   c                     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   image_shapes     r
   _trim_css_to_boundsr   4   sW     s1vq>>3s1v{1~66CFKPQN8S8SUXY\]^Y_abUcUcccr   c                     t          |           dk    rt          j        d          S t          j                            | |z
  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   )axis)lennpemptylinalgnorm)face_encodingsface_to_compares     r
   face_distancer#   ?   sB     >ax}}9>>.?:>CCCr   RGBc                     t           j                            |           }|r|                    |          }t	          j        |          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
    )PILImageopenconvertr   array)filemodeims      r
   load_image_filer.   N   s>     
		B ZZ8B<<r   r   hogc                 N    |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models      r
   _raw_face_locationsr8   \   s/     ~~ &ABBBS"=>>>r   c                      |dk    r fdt           |d          D             S  f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
    r1   c                 ^    g | ])}t          t          |j                  j                  *S  r   r   r	   shape.0facer5   s     r
   
<listcomp>z"face_locations.<locals>.<listcomp>w   sB      R  R  RD#L$;$;SYGG  R  R  Rr   c                 T    g | ]$}t          t          |          j                  %S r;   )r   r   r=   r>   s     r
   rA   z"face_locations.<locals>.<listcomp>y   s@      M  M  Mt#L$6$6	BB  M  M  Mr   )r8   r4   s   `  r
   face_locationsrC   l   s     ~~ R  R  R  RTghk  nI  KP  UQ  UQ  R  R  R  	R M  M  M  MObcf  iD  FK  PL  PL  M  M  M  	Mr      c                 &    t          | ||          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)r2   )imagesr6   rF   s      r
   _raw_face_locations_batchedrH   |   s     V%@ZXXXXr   c                 j      f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                      fd| D             S )Nc                 j    g | ]/}t          t          |j                  d          j                  0S )r   r<   )r?   r@   rG   s     r
   rA   zObatch_face_locations.<locals>.convert_cnn_detections_to_css.<locals>.<listcomp>   s5    eeeRV#L$;$;VAY_MMeeer   r;   )
detectionsrG   s    r
   convert_cnn_detections_to_cssz;batch_face_locations.<locals>.convert_cnn_detections_to_css   s    eeeeZdeeeer   )rH   listmap)rG   r6   rF   rM   raw_detections_batcheds   `    r
   batch_face_locationsrQ      sQ    f f f f f 9A\^hii13IJJKKKr   largec                      |t                     }nd |D             }t          |dk    rt           fd|D             S )Nc                 ,    g | ]}t          |          S r;   )r   )r?   face_locations     r
   rA   z'_raw_face_landmarks.<locals>.<listcomp>   s     ZZZ-,}55ZZZr   smallc                 (    g | ]} |          S r;   r;   )r?   rU   
face_imagepose_predictors     r
   rA   z'_raw_face_landmarks.<locals>.<listcomp>   s%    ZZZ-NN:}55ZZZr   )r8   pose_predictor_68_pointpose_predictor_5_point)rX   rC   r7   rY   s   `  @r
   _raw_face_landmarksr\      s^    ,Z88ZZ>ZZZ,N/ZZZZZ>ZZZZr   c                     t          | ||          }d |D             }|dk    rd |D             S |dk    r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                 J    g | ] }d  |                                 D             !S )c                 *    g | ]}|j         |j        fS r;   )xy)r?   ps     r
   rA   z-face_landmarks.<locals>.<listcomp>.<listcomp>   s     AAA1QS!#JAAAr   )parts)r?   landmarks     r
   rA   z"face_landmarks.<locals>.<listcomp>   s1    \\\hAA0@0@AAA\\\r   rR   c                    g | ]}|d d         |dd         |dd         |dd         |dd         |dd         |dd         |dd         |d	         gz   |d
         gz   |d         gz   |d         gz   |d         gz   |dd         |d         gz   |d         gz   |d         gz   |d         gz   |d         gz   |d	         gz   d	S )r               $   *   0   7   @   ?   >   =   <   6   C   B   A   )	chinleft_eyebrowright_eyebrownose_bridgenose_tipleft_eye	right_eyetop_lip
bottom_lipr;   r?   pointss     r
   rA   z"face_landmarks.<locals>.<listcomp>   s-    
- 
- 
-  1R4L"2b5M#BrE]!"R%=r"ur"u2be}r
|3vbzlBfRj\QU[\^U_T``djkmdncoo B-6":,6&*EPRTX^_aXbWccgmnpgqfrrv|}  wA  vB  B

 

 
- 
- 
-r   rV   c                 H    g | ]}|d          g|dd          |dd         d S )   r   r   )r{   r|   r}   r;   r   s     r
   rA   z"face_landmarks.<locals>.<listcomp>   sP     - - -   qs!
 
 - - -r   zFInvalid landmarks model type. Supported models are ['small', 'large'].)r\   
ValueError)rX   rC   r7   	landmarkslandmarks_as_tupless        r
   face_landmarksr      s     $JFFI\\R[\\\ 
- 
- ,
- 
- 
- 
	- 
'		- - ,	- - - 	- abbbr   rV   c                 F     t           ||          } f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           	      l    g | ]0}t          j        t                              |                    1S r;   )r   r*   face_encodercompute_face_descriptor)r?   raw_landmark_setrX   num_jitterss     r
   rA   z"face_encodings.<locals>.<listcomp>   sJ      M  M  MjzBH\99*FVXcddee  M  M  Mr   )r\   )rX   known_face_locationsr   r7   raw_landmarkss   ` `  r
   r!   r!      sU     (
4H%PPM M  M  M  M  M  L  M  M  M  Mr   333333?c                 D    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
    )rN   r#   )known_face_encodingsface_encoding_to_check	tolerances      r
   compare_facesr      s#     24JKKyXYYYr   )r$   )r   r/   )r   rD   )NrR   )Nr   rV   )r   ))	PIL.Imager&   r   numpyr   r   face_recognition_models	ExceptionprintquitLOAD_TRUNCATED_IMAGESget_frontal_face_detectorr3   pose_predictor_model_locationpredictor_68_point_modelshape_predictorrZ   (pose_predictor_five_point_model_locationpredictor_5_point_modelr[    cnn_face_detector_model_locationcnn_face_detection_modelcnn_face_detection_model_v1r2   face_recognition_model_locationface_recognition_modelface_recognition_model_v1r   r   r   r   r#   r.   r8   rC   rH   rQ   r\   r   r!   r   r;   r   r
   <module>r      sm                 """""   	E
ijjj	E
OPPPDFFFFF
 #'	 ..00P2PRR .$./GHH Z1Z\\ --.EFF S2SUU 4D45MNN P0PRR -t-.DEE@ @ @: : :d d dD D D   ? ? ? ? M M M M Y Y Y YL L L L&[ [ [ [ c  c  c  cFM M M M	Z 	Z 	Z 	Z 	Z 	Zs    %A A