
    i                        S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  \" 5         \ R                  R                  \ R                  R                  \5      5      r\ R                  R                  \5      r\ R                  R                  \SS5      r\\R                  ;  a  \R                  R#                  \5        S SKJr  S SKJr  S SKJr  \ R                  R                  \SSSS	5      r\ R                  R                  \S
5      r\" \ R6                  R9                  SS5      5      r \" S 5      r\" 5       r\ " S5        S r#S r$S r%g! \! a  r"SrSr\ " S\" 35         Sr"C"N$Sr"C"ff = f)    N)load_dotenvpluginsz Silent-Face-Anti-Spoofing-master)AntiSpoofPredict)	CropImage)parse_model_name	resourcesanti_spoof_modelsz4_0_0_300x300_MultiFTNet.pthSPOOF_THRESHOLDgffffff?z6[OK] Spoof detection service initialized successfully.z6[ERROR] Failed to initialize spoof detection service: c                 6    U R                  SS5      u  p[        R                  " U5      n[        R                  " U[        R
                  5      n[        R                  " U[        R                  5      $ ! [         a  n[        SU 35         SnAgSnAff = f)z2Internal function to decode a base64 image string.,   z%[ERROR] Error during image decoding: N)splitbase64	b64decodenp
frombufferuint8cv2imdecodeIMREAD_COLOR	Exceptionprint)base64_stringheaderencodedimage_bytesnparres         8/var/www/fran/franai/services/spoof_detection_service.py_decode_imager    '   sy    '--c15&&w/k2884||E3#3#344 5aS9:s   A3A6 6
B BBc           
      `   [         b  [        c  [        S5        g [         R                  U 5      nUc  [        S5        g[        R
                  R                  [        5      n[        U5      u  p4pVXUXCSS.nUc  SUS	'   [        R                  " S0 UD6n[         R                  U[        5      n	[        R                  " U	5      n
U	S
   U
   nU
S
:H  =(       a	    U[        :  n[        SU
 SUS S[         SU(       a  SOS 35        X4$ ! [         a  n[        SU 35         SnAgSnAff = f)z3Internal function to run the spoof detection model.Nu0   ⚠️ Spoof detection service is not available.)T        u%   ⚠️ Spoof check: No face detected.)Fr"   T)org_imgbboxscaleout_wout_hcropFr(   r   zSpoof check details: Label=z, Score=.2fz, Threshold=z	, Result=PassFailz&[ERROR] Error during spoof detection:  )_model_test_image_cropperr   get_bboxospathbasename
MODEL_FILEr   r(   predictr   argmaxr
   r   )image_array
image_bbox
model_nameh_inputw_input
model_typer%   paramimg
predictionlabelscoreis_liver   s                 r   _is_realrB   2   sC   n4@A ))+6
9:WW%%j1
.>z.J+* #
 =%%-!!*E* ((j9
		*%1e$ 1*8!8+E7(5+\RaQbbkv}lr  DJ  lK  L  	M~ 6qc:;s   #D  C
D 
D-D((D-c                 6   [        U [        5      (       d  U /n Sn[        U 5       H]  u  p#[        U5      nUc  SSSUS-    34s  $ [	        U5      u  pVUnU(       a  M8  SUS-    SUS S	[
         3n[        S
U 35        SXg4s  $    [        SUS 35        SUS4$ )z
Public function to perform a full liveness check on a list of base64 image strings.
It will return True only if ALL images in the list pass the liveness check.

Returns:
    tuple: (is_live: bool, score: float, error_message: str or None)
r"   NFz,Invalid image data for spoof check in image r   zLiveness check failed on image z!: Spoof attempt detected. Score: r)   z, Threshold: z LIVENESS FAIL: z* LIVENESS PASS (all images): Final Score: T)
isinstancelist	enumerater    rB   r
   r   )base64_image_listfinal_scoreibase64_image_stringdecoded_imagerA   r@   messages           r   check_livenessrM   W   s     '.../K"+,=">%&9: #!MaPQcUSSS!-0w7!u<]^cdg]hhu  wF  vG  HG$WI./%(( #? 
6{36G
HId""    )&r0   sysnumpyr   r   timer   dotenvr   r1   dirnameabspath__file__current_dirproject_rootjoinplugin_pathappendsrc.anti_spoof_predictr   src.generate_patchesr   src.utilityr   	MODEL_DIRr3   floatenvirongetr
   r-   r.   r   r   r   r    rB   rM   r,   rN   r   <module>rb      s@   	 
  
     ggoobggooh78ww{+ggll<4VWchhHHOOK  4 * ( GGLLy2TVacvw	WW\\)%CD


'8$?@H"1%K[N	
BC	#J#k  HKN	B1#
FGGHs   -E E-E((E-