
    |h                        d dl Z d dlZd dlm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j                  fdZdej                  fdZde
dej                  fd	Zd
ej                  dededefdZd
ej                  dedefdZy)    N)Path)Unionimg_urireturnc                    t        | t        j                        r| j                         }nt        | t              r| j                  d      rt        |       }nt        | t              r| j                  d      rt        |       }nt        | t        t        f      r_t        | t              rt	        |       } t        j                  j                  |       st        d|  d      t        j                  |       }nt        d|  d      t        |j                         dk7  s"t        j"                  |j                         d	k(  rt        d
      |S )z
    Load the given image
    Args:
        img_path (str or numpy array): exact image path, pre-loaded numpy array (BGR format)
            , base64 encoded string and urls are welcome
    Returns:
        image itself
    zdata:image/http)urlzInput image file path (z) does not exist.zInvalid image input - zS.Exact paths, pre-loaded numpy arrays, base64 encoded strings and urls are welcome.   r   z:Input image needs to have 3 channels at must not be empty.)
isinstancenpndarraycopystr
startswithload_base64_imgload_image_from_webr   ospathisfile
ValueErrorcv2imreadlenshapeprod)r   imgs     \/var/www/html/test/engine/venv/lib/python3.12/site-packages/retinaface/commons/preprocess.py	get_imager   
   s    '2::&lln 
GS	!g&8&8&Gg& 
GS	!g&8&8&@!g. 
Gc4[	)gt$'lGww~~g&6wi?PQRR jj! $WI ., ,
 	
 399~bggcii0A5UVVJ    c                     | j                  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.
    ,   )	splitr   
fromstringbase64	b64decodeuint8r   imdecodeIMREAD_COLOR)uriencoded_datanparrimg_bgrs       r   r   r   8   sP     99S>!$LMM&**<8"((CEll5#"2"23GNr   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readr'   r   r(   r)   )r	   responseimage_arrayimages       r   r   r   H   sc     ||Cb9H**Yx||'8'8':;288LKLLc&6&67ELr   r   scalesallow_upscalingc                 D   | j                   dd \  }}|d   }|d   }||kD  r||}}n||}}|t        |      z  }	|st        d|	      }	t        j                  |	|z        |kD  r|t        |      z  }	|	dk7  r)t        j                  | dd|	|	t
        j                        } | |	fS )aS  
    This function is modified from the following code snippet:
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/5f68ce8130889384cb8aca937a270cea4ef2d020/retinaface.py#L49-L74

    Args:
        img (numpy array): given image
        scales (list)
        allow_upscaling (bool)
    Returns
        resized image, im_scale
    r      r"         ?N)fxfyinterpolation)r   floatminr   roundr   resizeINTER_LINEAR)
r   r>   r?   img_himg_wtarget_sizemax_sizeim_size_minim_size_maxim_scales
             r   resize_imagerR   W   s     99Qq>LE5)KayHu}#(%[#(%[U;//HsH%	xx;&'(2eK003jjdDX(RURbRbc=r   c                 \   t        j                  g dt         j                        }t        j                  g dt         j                        }t        d      }ddg}t	        | ||      \  } }| j                  t         j                        } t        j                  d| j                  d   | j                  d   | j                  d	   ft         j                        }t        d
      D ]1  }| ddddd	|z
  f   |z  |d	|z
     z
  |d	|z
     z  |ddddd|f<   3 || j                  dd	 |fS )aC  
    This function is modified from the following code snippet:
    https://github.com/StanislasBertrand/RetinaFace-tf2/blob/5f68ce8130889384cb8aca937a270cea4ef2d020/retinaface.py#L76-L96
    Args:
        img (numpy array): given image
        allow_upscaling (bool)
    Returns:
        tensor, image shape, im_scale
    )        rT   rT   r2   )rB   rB   rB   rB   i   i  r"   r   rA   r
   N)	r   arrayfloat32rF   rR   astypezerosr   range)	r   r?   pixel_means
pixel_stdspixel_scaler>   rQ   	im_tensoris	            r   preprocess_imager_   y   s    ((?"**=K/<J*KD\F fo>MC
**RZZ
 C!SYYq\399Q<1FbjjYI 1X 
!$Q1q5[!1K!?+aRSeBT!TXbEY
 !
	!Q1*

 cii!nh..r   )r   r%   pathlibr   typingr   r4   numpyr   r   r   r   r   r   r   listbooltuplerR   r_    r   r   <module>rg      s    	      
+uS"**_- +"** +\BJJ  S RZZ bjj $  % D/"** /t / /r   