U
    ¥A{iˆ  ã                   @   sì   d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dl	Z	d dl
Z
d dlZd dlZdd„ Zddd„Zdd	d
„Zdd„ Zdd„ Ze ¡ e d¡e d¡ejdddd�ejdddd�ejddedd�dd„ ƒƒƒƒƒƒZedkrèeƒ  dS )é    )Úprint_functionNc                 C   sœ   g }g }t | ƒD ]‚}tj tj |¡¡d }t |¡}t |¡}t|ƒdkr\t	 
d |¡¡ t|ƒdkrzt	 
d |¡¡ q| |¡ | |d ¡ q||fS )Nr   é   zIWARNING: More than one face found in {}. Only considering the first face.z-WARNING: No faces found in {}. Ignoring file.)Úimage_files_in_folderÚosÚpathÚsplitextÚbasenameÚface_recognitionÚload_image_fileÚface_encodingsÚlenÚclickÚechoÚformatÚappend)Úknown_people_folderÚknown_namesÚknown_face_encodingsÚfiler   ÚimgÚ	encodings© r   úI/tmp/pip-unpacked-wheel-pbnq7y3j/face_recognition/face_recognition_cli.pyÚscan_known_people   s    


r   Fc                 C   s,   |rt d | ||¡ƒ nt d | |¡ƒ d S )Nz{},{},{}z{},{})Úprintr   )ÚfilenameÚnameÚdistanceÚshow_distancer   r   r   Úprint_result#   s    r   ç333333ã?c                    s´   t  ˆ ¡}t|jƒdkr>tj |¡}| dtjj¡ t	 
|¡}t  |¡}|D ]P}t  ||¡}	t|	|kƒ}
d|
krŽ‡ ‡fdd„t|
||	ƒD ƒ qLtˆ dd ˆƒ qL|s°tˆ dd ˆƒ d S )Né@  )r!   r!   Tc                    s$   g | ]\}}}|rt ˆ ||ˆƒ‘qS r   )r   )Ú.0Zis_matchr   r   ©Úimage_to_checkr   r   r   Ú
<listcomp>:   s      ztest_image.<locals>.<listcomp>Zunknown_personZno_persons_found)r	   r
   ÚmaxÚshapeÚPILZImageZ	fromarrayZ	thumbnailZLANCZOSÚnpÚarrayr   Zface_distanceÚlistÚzipr   )r$   r   r   Ú	tolerancer   Zunknown_imageZpil_imgZunknown_encodingsZunknown_encodingZ	distancesÚresultr   r#   r   Ú
test_image*   s    


r/   c                    s   ‡ fdd„t  ˆ ¡D ƒS )Nc                    s,   g | ]$}t jd |t jd�rtj ˆ |¡‘qS )z.*\.(jpg|jpeg|png))Úflags)ÚreÚmatchÚIr   r   Újoin)r"   Úf©Úfolderr   r   r%   D   s      z)image_files_in_folder.<locals>.<listcomp>)r   Úlistdirr6   r   r6   r   r   C   s    r   c           
      C   sp   |dkrd }n|}t }dt  ¡ kr,t  d¡}|j|d�}t| t |¡t |¡t |¡t |¡ƒ}	| t|	¡ d S )NéÿÿÿÿZ
forkserver)Ú	processes)	ÚmultiprocessingZget_all_start_methodsÚget_contextZPoolr,   Ú	itertoolsÚrepeatÚstarmapr/   )
Zimages_to_checkr   r   Znumber_of_cpusr-   r   r:   ÚcontextÚpoolZfunction_parametersr   r   r   Úprocess_images_in_process_poolG   s    
ûrB   r   r$   z--cpusr   zmnumber of CPU cores to use in parallel (can speed up processing lots of images). -1 means "use all in system")ÚdefaultÚhelpz--tolerancezkTolerance for face comparisons. Default is 0.6. Lower this if you get multiple matches for the same person.z--show-distancez<Output face distance. Useful for tweaking tolerance setting.)rC   ÚtyperD   c                    sŠ   t | ƒ\‰‰ tjdk r,|dkr,t d¡ d}tj |¡rv|dkr^‡ ‡‡‡fdd„t|ƒD ƒ q†t	t|ƒˆˆ |ˆˆƒ nt
|ˆˆ ˆˆƒ d S )N)é   é   r   zmWARNING: Multi-processing support requires Python 3.4 or greater. Falling back to single-threaded processing!c                    s   g | ]}t |ˆˆ ˆˆƒ‘qS r   )r/   )r"   Z
image_file©r   r   r   r-   r   r   r%   o   s     zmain.<locals>.<listcomp>)r   ÚsysÚversion_infor   r   r   r   Úisdirr   rB   r/   )r   r$   Zcpusr-   r   r   rH   r   Úmain_   s    
rL   Ú__main__)F)r    F)Ú
__future__r   r   r   r1   Zface_recognition.apiÚapir	   r;   r=   rI   Z	PIL.Imager(   Znumpyr)   r   r   r/   r   rB   ÚcommandÚargumentÚoptionÚboolrL   Ú__name__r   r   r   r   Ú<module>   s.   

