
    >|h                         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l	m
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)    )ListN)OpenCv)folder_utils)DetectorFacialAreaRegion)Loggerzdetectors.SsdWrapper)modulec                   H    e Zd Zd ZdefdZdej                  dee	   fdZ
y)	SsdClientc                 .    | j                         | _        y )N)build_modelmodel)selfs    U/var/www/html/test/engine/venv/lib/python3.12/site-packages/deepface/detectors/Ssd.py__init__zSsdClient.__init__   s    %%'
    returnc                 V   t        j                         }t        j                  j	                  |dz         dk7  r4t
        j                  d       d}|dz   }t        j                  ||d       t        j                  j	                  |dz         dk7  r4t
        j                  d       d	}|dz   }t        j                  ||d       	 t        j                  j                  |dz   |dz         }i }||d<   t        j                         |d<   |S # t        $ r}t        d
      |d}~ww xY w)zV
        Build a ssd detector model
        Returns:
            model (dict)
        z"/.deepface/weights/deploy.prototxtTz%deploy.prototxt will be downloaded...zThttps://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxtF)quietz;/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodelz>res10_300x300_ssd_iter_140000.caffemodel will be downloaded...zyhttps://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodelzException while calling opencv.dnn module.This is an optional dependency.You can install it as pip install opencv-contrib-python.Nface_detectoropencv_module)r   get_deepface_homeospathisfileloggerinfogdowndownloadcv2dnnreadNetFromCaffe	Exception
ValueErrorr   OpenCvClient)r   homeurloutputr   errdetectors          r   r   zSsdClient.build_model   s,    --/ 77>>$!EEF$NKK?@hC@@FNN3e4 GGNN4"__` KKXY NCYYFNN3e4
	GG44;;TTM $1!$*$7$7$9!  	M 		s   &D 	D(D##D(imgc           
         | j                   d   }g }d}g d}d}|j                  }t        j                  ||      }|d   |d   z  }	|d   |d   z  }
t        j                  j                  |      }| j                   d   }|j                  |       |j                         }t        j                  |d   d   |	      }||d
   dk(     }||d   dk\     }|d   dz  j                  t              |d<   |d   dz  j                  t              |d<   |d   dz  j                  t              |d<   |d   dz  j                  t              |d<   |j                  d   dkD  r|j                         D ]  \  }}|d   }|d   }|d   }|d   }|d   }t        ||	z        }t        ||
z        }t        ||	z        t        ||	z        z
  }t        ||
z        t        ||
z        z
  }|t        |      t        ||z         t        |      t        ||z         f   }|j                  |      \  }}t        |||||||      }|j                  |        |S )z
        Detect and align face with ssd

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

        Returns:
            results (List[FacialAreaRegion]): A list of FacialAreaRegion objects
        r   N)img_idis_face
confidencelefttoprightbottom),  r4      r   )imager   )columnsr.   r/   g?r0   r4   r3   r2   r1   )xywhleft_eye	right_eyer/   )r   shaper    resizer!   blobFromImagesetInputforwardpd	DataFrameastypeintiterrows	find_eyesr   append)r   r+   r   respdetected_face
ssd_labelstarget_sizeoriginal_sizecurrent_imgaspect_ratio_xaspect_ratio_y	imageBlobr   
detectionsdetections_df_instancer0   r2   r3   r1   r/   r8   r9   r:   r;   r<   r=   facial_areas                                r   detect_faceszSsdClient.detect_facesI   s    .2ZZ-HZ
 		jjk2&q)KN:&q)KN:GG)))<	

?3y)"**,
Z]1%5zJ%mI&>!&CD%mL&AT&IJ!.v!6!< D DS If#0#:S#@"H"H"Mh"/"83">!F!Fs!Kg -e 4s :BB3Geq!A%,557 )8' )!(+uo%l3
~-.n,-./#d^6K2LL/03s^7K3LL #CFSQZ$7Q#a!e*9L$L M&3&=&=m&L#).%') K(5)8 r   N)__name__
__module____qualname__r   dictr   npndarrayr   r   rX    r   r   r   r      s4    (2T 2hI

 It4D/E Ir   r   )typingr   r   r   r    pandasrC   numpyr]   deepface.detectorsr   deepface.commonsr   deepface.models.Detectorr   r   deepface.commons.loggerr   r   r   r_   r   r   <module>rg      s<     	  
   % ) ? *	-	.
A Ar   