
    ih                         d dl Z d dlmZmZ d dlZd dlZd dlZd dlm	Z	 d dl
mZmZ d dlmZ  ed      Z G d d	e      Zy)
    N)AnyList)folder_utils)DetectorFacialAreaRegion)Loggerzdetectors.YunetWrapper)modulec                   H    e Zd Zd ZdefdZdej                  dee	   fdZ
y)YuNetClientc                 .    | j                         | _        y )N)build_modelmodel)selfs    V/var/www/html/dev/engine/venv/lib/python3.12/site-packages/deepface/detectors/YuNet.py__init__zYuNetClient.__init__   s    %%'
    returnc                 n   t         j                  j                  d      }t        |      dk\  st	        d|       t        |d         }t        |d         }|dk  s
|dk(  r!|dk  rt	        dt         j                         d	}d
}t        j                         }t        j                  j                  |d| z         du r8t        j                  | d       |d| z   }t        j                  ||d       	 t        j                  |d| z   dd      }|S # t         $ r}	t	        d      |	d}	~	ww xY w)zW
        Build a yunet detector model
        Returns:
            model (Any)
        .   z<OpenCv's version must have major and minor values but it is r            z1YuNet requires opencv-python >= 4.8 but you have zkhttps://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2023mar.onnxz!face_detection_yunet_2023mar.onnxz/.deepface/weights/Fz will be downloaded...)quiet )r   r   zException while calling opencv.FaceDetectorYN_create module.This is an optional dependency.You can install it as pip install opencv-contrib-python.N)cv2__version__splitlen
ValueErrorintr   get_deepface_homeospathisfileloggerinfogdowndownloadFaceDetectorYN_create	Exception)
r   opencv_versionopencv_version_majoropencv_version_minorurl	file_namehomeoutputface_detectorerrs
             r   r   zYuNetClient.build_model   sa    ..s3>"a'N~N^_   #>!#45">!#45!#(<(AFZ]^F^PQTQ`Q`Pabcc |7	--/77>>$#6yk!BBCuLKK9+%;<=1)==FNN3e4		55,YK88"fM   	M 		s   ;D 	D4#D//D4imgc                    t        t        j                  j                  dd            }g }g }|j                  d   |j                  d   }}d}d}|dkD  s|dkD  r_dt        ||      z  }t        j                  |t        ||z        t        ||z        f      }|j                  d   |j                  d   }}d}| j                  j                  ||f       | j                  j                  |       | j                  j                  |      \  }	}||S |D ]  }
	 t        t        t        |
d	d
             \  }}}}}}}}t        |d      }t        |d      }|rvt        ||z        t        ||z        t        ||z        t        ||z        f\  }}}}t        ||z        t        ||z        t        ||z        t        ||z        f\  }}}}t        |
d         }t        |||||||f||f      }|j!                  |        |S )z
        Detect and align face with yunet

        Args:
            img (np.ndarray): pre-loaded image as numpy array

        Returns:
            results (List[FacialAreaRegion]): A list of FacialAreaRegion objects
        yunet_score_thresholdz0.9r   r   Fi  g      @TNr   )xywh
confidenceleft_eye	right_eye)floatr#   environgetshapemaxr   resizer!   r   setInputSizesetScoreThresholddetectlistmapr   append)r   r5   score_thresholdrespfacesheightwidthresizedr_facer9   r:   r;   r<   x_rey_rex_ley_ler=   facial_areas                        r   detect_faceszYuNetClient.detect_faces:   s     

/F NO		!ciil C<53;FE**A**S3uqy>3vz?"CDCIIaL#))A,EFG

0

$$_5::$$S)5=K '	%D 48Cbq8J3K0Q1atT4 Aq	AAq	A QZQUSQZQUK
1aqMqMqMqM	*&dD$ tBxJ*%,K KK$O'	%P r   N)__name__
__module____qualname__r   r   r   npndarrayr   r   rZ    r   r   r   r      s4    ('S 'RF

 Ft4D/E Fr   r   )r#   typingr   r   r   numpyr^   r(   deepface.commonsr   deepface.models.Detectorr   r   deepface.commons.loggerr   r&   r   r`   r   r   <module>rf      s6    	  
   ) ? *	/	0s( sr   