o
    پi'                     @   sB  d dl Z d dlZd dlmZ d dlmZ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Zd dlmZ eeejjedkr[ejjjejjjejjjejjjejjjdZnejjejjejjejjejjdZdeejj ejjB de	jfd	d
Zde	jdejfddZ de	jejB de	jejB fddZ!	d!de"ejjB deejjgejjf dB dejjfddZ#	d!de"deeejj geejj f dB deejj fddZ$		d"dejje	jB ejB de%de%dB de%dB de&e%e%f f
ddZ'		d#dejje	jB ejB de%de%de"de"dejje	jB ejB fdd Z(dS )$    N)Callable)unquoteurlparse)versionz9.1.0)linearbilinearbicubiclanczosnearestimagesreturnc                 C   s0   t | ts| g} dd | D } tj| dd}|S )a4  
    Convert a PIL image or a list of PIL images to NumPy arrays.

    Args:
        images (`PIL.Image.Image` or `List[PIL.Image.Image]`):
            The PIL image or list of images to convert to NumPy format.

    Returns:
        `np.ndarray`:
            A NumPy array representation of the images.
    c                 S   s"   g | ]}t |t jd  qS )g     o@)nparrayastypefloat32).0image r   e/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/models/vision_utils.py
<listcomp>2   s   " z pil_to_numpy.<locals>.<listcomp>r   )axis)
isinstancelistr   stack)r   
images_arrr   r   r   pil_to_numpy$   s
   
r   c                 C   s,   | j dkr	| d } t| dddd} | S )z
    Convert a NumPy image to a PyTorch tensor.

    Args:
        images (`np.ndarray`):
            The NumPy image array to convert to PyTorch format.

    Returns:
        `torch.Tensor`:
            A PyTorch tensor representation of the images.
       ).Nr         )ndimtorch
from_numpy	transposer   r   r   r   numpy_to_pt8   s   
r$   c                 C   s   d|  d S )z
    Normalize an image array to [-1,1].

    Args:
        images (`np.ndarray` or `torch.Tensor`):
            The image array to normalize.

    Returns:
        `np.ndarray` or `torch.Tensor`:
            The normalized image array.
    g       @g      ?r   r#   r   r   r   	normalizeK   s   r%   r   convert_methodc                 C   s   t | tr1| ds| drtjtj| ddj} n#t	j
| r)tj| } ntd|  dt | tjjr;| } ntdtj| } |durO|| } | S | d	} | S )
au  
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], *optional*):
            A conversion method to apply to the image after loading it. When set to `None` the image will be converted
            "RGB".
    http://https://TstreamIIncorrect path or URL. URLs must start with `http://` or `https://`, and  is not a valid path.zgIncorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image.NRGB)r   str
startswithPILImageopenrequestsgetrawospathisfile
ValueErrorImageOpsexif_transposeconvert)r   r&   r   r   r   
load_image[   s&   


r=   videoc                 C   s  |  dp	|  d}tj| }d}|s|std|  d|rwtj| dd}|jdkr4td	|j t| }tj	t
|j}tj|d
 pJd}tj|dd}	|	j}
|jdd}|D ]}|	| q^W d   n1 spw   Y  |
} g }| drtj| }z	 ||  || d
  q ty   Y n9w ztjj  W n ty   tddw t| }|D ]}|tj | qW d   n1 sw   Y  |rt!|
 |dur||}|S )a  
    Loads `video` to a list of PIL Image.
    Args:
        video (`str`):
            A URL or Path to a video to convert to a list of PIL Image format.
        convert_method (Callable[[List[PIL.Image.Image]], List[PIL.Image.Image]], *optional*):
            A conversion method to apply to the video after loading it. When set to `None` the images will be converted
            to "RGB".
    Returns:
        `List[PIL.Image.Image]`:
            The video as a list of PIL images.
    r'   r(   Fr+   r,   Tr)      z'Failed to download video. Status code: r   z.mp4)suffixdeletei    )
chunk_sizeNz.gifzf`Unable to find an ffmpeg installation on your machine. Please install via `pip install imageio-ffmpeg)"r/   r6   r7   r8   r9   r3   r4   status_coder   basenamer   splitexttempfileNamedTemporaryFilenameiter_contentwriteendswithr0   r1   r2   appendcopyseektellEOFErrorimageiopluginsffmpegget_exeAttributeError
get_reader	fromarrayremove)r>   r&   is_urlis_filewas_tempfile_createdresponse
parsed_url	file_namer@   	temp_file
video_path
video_datachunk
pil_imagesgifreaderframer   r   r   
load_video   sl   




rg   vae_scale_factorheightwidthc                    s   |du r t | tjjr| j}nt | tjr| jd }n| jd }|du r@t | tjjr/| j}nt | tjr;| jd }n| jd } fdd||fD \}}||fS )a  
    Returns the height and width of the image, downscaled to the next integer multiple of `vae_scale_factor`.

    Args:
        image (`Union[PIL.Image.Image, np.ndarray, torch.Tensor]`):
            The image input, which can be a PIL image, NumPy array, or PyTorch tensor. If it is a NumPy array, it
            should have shape `[batch, height, width]` or `[batch, height, width, channels]`. If it is a PyTorch
            tensor, it should have shape `[batch, channels, height, width]`.
        height (`Optional[int]`, *optional*, defaults to `None`):
            The height of the preprocessed image. If `None`, the height of the `image` input will be used.
        width (`Optional[int]`, *optional*, defaults to `None`):
            The width of the preprocessed image. If `None`, the width of the `image` input will be used.

    Returns:
        `Tuple[int, int]`:
            A tuple containing the height and width, both resized to the nearest integer multiple of
            `vae_scale_factor`.
    Nr   r   r   c                 3   s    | ]	}||   V  qd S Nr   )r   xrh   r   r   	<genexpr>   s    
z+get_default_height_width.<locals>.<genexpr>)r   r0   r1   ri   r    Tensorshaperj   )r   rh   ri   rj   r   rm   r   get_default_height_width   s    



rq   defaultr	   resize_moderesamplec                 C   sh   |dkrt | tjjstd| t | tjjsJ |dkr,| j||ft| d} | S td| d)a  
    Resize image.

    Args:
        image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`):
            The image input, can be a PIL image, numpy array or pytorch tensor.
        height (`int`):
            The height to resize to.
        width (`int`):
            The width to resize to.
        resize_mode (`str`, *optional*, defaults to `default`):
            The resize mode to use, can be one of `default` or `fill`. If `default`, will resize the image to fit
            within the specified width and height, and it may not maintaining the original aspect ratio. If `fill`,
            will resize the image to fit within the specified width and height, maintaining the aspect ratio, and
            then center the image within the dimensions, filling empty with data from image. If `crop`, will resize
            the image to fit within the specified width and height, maintaining the aspect ratio, and then center
            the image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only
            supported for PIL image input.

    Returns:
        `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`:
            The resized image.
    rr   z2Only PIL image input is supported for resize_mode )rt   zresize_mode z is not supported)r   r0   r1   r9   resizePIL_INTERPOLATION)r   ri   rj   rs   rt   r   r   r   ru     s   ru   rk   )NN)rr   r	   ))r6   rF   collections.abcr   urllib.parser   r   rQ   numpyr   	PIL.Imager0   PIL.ImageOpsr3   r    	packagingr   parse__version__base_versionr1   
ResamplingBILINEARBICUBICLANCZOSNEARESTrv   LINEARr   ndarrayr   ro   r$   r%   r.   r=   rg   inttuplerq   ru   r   r   r   r   <module>   s   	$	"

- 

Q

4