o
    vic                     @   s\   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eddfddZ	dd Z
dd
dZdS )    N)Imageseedreturnc                 C   sR   t |  t j|  tj|  t|  dt jj_	dt jj_
t| tjd< dS )z
    Set random seed for all random number generators for reproducibility.

    Args:
        seed: The random seed to use
    TFZPYTHONHASHSEEDN)torchmanual_seedcudamanual_seed_allnprandomr   backendscudnndeterministic	benchmarkstrosenviron)r    r   ?/data/cameron/vidgen/cosmos-policy/cosmos_policy/utils/utils.pyset_seed_everywhere   s   



r   c                 C   s   t | g| S )a3  
    Duplicates a NumPy array multiple times along a new first axis.

    Args:
        arr (numpy.ndarray): The input array to duplicate
        total_num_copies (int): Total number of copies to have in the end

    Returns:
        numpy.ndarray: A new array with shape (total_num_copies, *arr.shape)
    )r	   stack)arrtotal_num_copiesr   r   r   duplicate_array)   s   r   _   c                 C   sj   | j tjkr
td| jdks| jd dkrtdt| }t	 }|j
|d|d tj| tjdS )zEncode image as JPEG bytes.z,Image array must be uint8 for JPEG encoding.      z Image must have shape (H, W, 3).JPEG)formatquality)dtype)r   r	   uint8
ValueErrorndimshaper   	fromarrayioBytesIOsave
frombuffergetvalue)imager   imgbufferr   r   r   jpeg_encode_image8   s   
r-   )r   )r%   r   r
   numpyr	   r   PILr   intr   r   r-   r   r   r   r   <module>   s   