
    ^h(                        d dl Zd dlZd dlZd dlmZ 	 d dlZ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j0                         Z ej4                  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(y# e$ r  ed        ed        e	        Y w xY w)    N)	ImageFilez\Please install `face_recognition_models` with this command before using `face_recognition`:
zCpip install git+https://github.com/ageitgey/face_recognition_modelsTc                 ~    | j                         | j                         | j                         | j                         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    R/var/www/html/facial_api/venv/lib/python3.12/site-packages/face_recognition/api.py_rect_to_cssr       s+     88:tzz|T[[]DIIK??    c                 J    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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   sL     s1vq>3s1v{1~6CFKPQN8SUXY\]^Y_abUcccr   c                     t        |       dk(  rt        j                  d      S t        j                  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#   ?   s;     >axx}99>>.?:>CCr   c                     t         j                  j                  |       }|r|j                  |      }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   s5     
	BZZ88B<r   c                 <    |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_locationsr5   \   s'     ~ &ABBS"=>>r   c                    |dk(  rBt        | |d      D cg c]+  }t        t        |j                        | j                        - c}S t        | ||      D cg c]!  }t        t        |      | j                        # c}S c c}w c c}w )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/   )r5   r   r   r	   shape)r2   r3   r4   faces       r
   face_locationsr9   l   s     ~Tghk  nI  KP  UQ  RD#L$;SYYG  R  	RObcf  iD  FK  PL  Mt#L$6		B  M  	M R Ms   0B &B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)r0   )imagesr3   r;   s      r
   _raw_face_locations_batchedr=   |   s     V%@ZXXr   c                 R      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                     | D cg c].  }t        t        |j                        d   j                        0 c}S c c}w )Nr   )r   r   r	   r7   )
detectionsr8   r<   s     r
   convert_cnn_detections_to_cssz;batch_face_locations.<locals>.convert_cnn_detections_to_css   s1    ZdeRV#L$;VAY__Meees   3<)r=   listmap)r<   r3   r;   rA   raw_detections_batcheds   `    r
   batch_face_locationsrE      s0    f 9A\^hi13IJKKr   c                     |t        |       }n|D cg c]  }t        |       }}t        }|dk(  rt        }|D cg c]  } || |       c}S c c}w c c}w )Nsmall)r5   r   pose_predictor_68_pointpose_predictor_5_point)
face_imager9   r4   face_locationpose_predictors        r
   _raw_face_landmarksrM      s`    ,Z8KYZ-,}5ZZ,N/KYZ-N:}5ZZ [ [s
   AAc                 6   t        | ||      }|D cg c]6  }|j                         D cg c]  }|j                  |j                  f c}8 }}}|dk(  r|D cg c]v  }|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	x c}S |dk(  r|D cg c]  }|d   g|dd |dd d c}S t	        d      c c}w c c}}w c c}w c c}w )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)
    larger               $   *   0   7   @   ?   >   =   <   6   C   B   A   )	chinleft_eyebrowright_eyebrownose_bridgenose_tipleft_eye	right_eyetop_lip
bottom_liprG      r   )re   rf   rg   zFInvalid landmarks model type. Supported models are ['small', 'large'].)rM   partsxy
ValueError)rJ   r9   r4   	landmarkslandmarkplandmarks_as_tuplespointss           r
   face_landmarksrt      s    $JFIR[\h0@A1QSS!##JA\\  ,
-  1RL"2bM#Br]!"R=r"r"2b}r
|3vbzlBfRj\QU[\^U_T``djkmdncoo B-6":,6&*EPRTX^_aXbWccgmnpgqfrrv|}  wA  vB  B

 
- 
	- 
'	
 ,	-   q!
 - 	- abb- B\
--s#   DD	DA;D!DDc           
          t        | ||      }|D cg c],  }t        j                  t        j	                  | ||            . c}S c c}w )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)
    )rM   r   r)   face_encodercompute_face_descriptor)rJ   known_face_locationsnum_jittersr4   raw_landmarksraw_landmark_sets         r
   r!   r!      sR     (
4H%PM L  MjzBHH\99*FVXcde  M  M  Ms   1Ac                 2    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
    )rB   r#   )known_face_encodingsface_encoding_to_check	tolerances      r
   compare_facesr      s     24JKyXYYr   )RGB)r   hog)r      )NrO   )Nr   rG   )g333333?))	PIL.Imager%   r   numpyr   r   face_recognition_models	ExceptionprintquitLOAD_TRUNCATED_IMAGESget_frontal_face_detectorr1   pose_predictor_model_locationpredictor_68_point_modelshape_predictorrH   (pose_predictor_five_point_model_locationpredictor_5_point_modelrI    cnn_face_detector_model_locationcnn_face_detection_modelcnn_face_detection_model_v1r0   face_recognition_model_locationface_recognition_modelface_recognition_model_v1rv   r   r   r   r#   r-   r5   r9   r=   rE   rM   rt   r!   r    r   r
   <module>r      sA      " #'	 ...0P2PPR .$../GH Z1ZZ\ ---.EF S2SSU 4D445MN P0PPR -t--.DE@:dD? M YL&[ cFM	Z_  	
ij	
OPFs   C- -DD