
    >|h=                        d dl Z d dlmZmZ d dlZd dlmZ d dlZd dl	Z	d dl
Z
deeej                  f   deej                  ef   fdZdedej                  fdZd	edej                  fd
Zddej                  dedej                  fdZy)    N)UnionTuple)Pathimgreturnc                 h   t        | t        j                        r| dfS t        | t              rt	        |       } t        | t              st        dt        |              | j                  d      rt        |       dfS | j                         j                  d      s| j                         j                  d      rt        |       | fS t        j                  j                  |       durt        d	|  d
      | j                         du rt        d|        t        j                   |       }|| fS )z
    Load image from path, url, base64 or numpy array.
    Args:
        img: a path, url, base64 or numpy array.
    Returns:
        image (numpy array): the loaded image in BGR format
        image name (str): image name itself
    znumpy arrayz)img must be numpy array or str but it is zdata:image/zbase64 encoded stringzhttp://zhttps://)urlTzConfirm that z existsFz3Input image must not have non-english characters - )
isinstancenpndarrayr   str
ValueErrortype
startswithload_base64lowerload_image_from_webospathisfileisasciicv2imread)r   img_obj_bgrs     ]/var/www/html/test/engine/venv/lib/python3.12/site-packages/deepface/modules/preprocessing.py
load_imager      s    #rzz"M!!#t#hc3DT#YKPQQ ~~m$3!888 yy{i(CIIK,B,B:,N"s+S00 
ww~~c$&=W566
 {{}NseTUU**S/K    r	   c                 *   t        j                  | dd      }|j                          t        j                  t        |j                  j                               t        j                        }t        j                  |t        j                        }|S )z
    Loading an image from web
    Args:
        url: link for the image
    Returns:
        img (np.ndarray): equivalent to pre-loaded image from opencv (BGR format)
    T<   )streamtimeout)dtype)requestsgetraise_for_statusr   asarray	bytearrayrawreaduint8r   imdecodeIMREAD_COLOR)r	   responseimage_arrayimages       r   r   r   7   sc     ||Cb9H**Yx||'8'8':;288LKLLc&6&67ELr   uric                    | j                  d      }t        |      dk  rt        d      | j                  d      s0| j                  d      s| j                  d      st        d|       |d   }t	        j
                  t        j                  |      t        j                        }t        j                  |t        j                        }|S )	zLoad image from base64 string.

    Args:
        uri: a base64 string.

    Returns:
        numpy array: the loaded image.
    ,   z%format error in base64 encoded stringzdata:image/jpegzdata:image/jpgzdata:image/pngz/input image can be jpg, jpeg or png, but it is    )splitlenr   r   r   
fromstringbase64	b64decoder*   r   r+   r,   )r0   encoded_data_partsencoded_datanparrimg_bgrs        r   r   r   F   s     3
"@AA 	()>>*+>>*+JK]J^_``%a(LMM&**<8"((CEll5#"2"23GNr   normalizationc                    |dk(  r| S | dz  } |dk(  r	 | S |dk(  r*| j                         | j                         }}| |z
  |z  } | S |dk(  r| dz  } | dz  } | S |dk(  r)| d	xx   d
z  cc<   | dxx   dz  cc<   | dxx   dz  cc<   | S |dk(  r)| d	xx   dz  cc<   | dxx   dz  cc<   | dxx   dz  cc<   | S |dk(  r| dz  } | dz  } | S t        d|       )a  Normalize input image.

    Args:
        img (numpy array): the input image.
        normalization (str, optional): the normalization technique. Defaults to "base",
        for no normalization.

    Returns:
        numpy array: the normalized image.
    base   r(   FacenetFacenet2018g     _@r4   VGGFace).r   gtfW@).r4   g^)0Z@).r3   gj+%`@VGGFace2g]mV@gx$(~Y@g,Cb`@ArcFace   z#unimplemented normalization type - )meanstdr   )r   r>   rH   rI   s       r   normalize_inputrJ   d   s6    

 3JC@ J= 
)	#HHJ	cTzS 8 J5 
-	'uq. J+ 
)	#FwFxFx" J 
*	$FwFxFx J 
)	# 	us
 J >}oNOOr   )r@   )r   typingr   r   r8   pathlibr   numpyr   r   r#   r   r   r   r   r   rJ    r   r   <module>rO      s    	     
 (E#rzz/* (uRZZ_/E (VS RZZ S RZZ <7 7C 7RZZ 7r   