o
    îôjdÊ  ã                   @   sp  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlZd dlZd dlmZ dd	lmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# dd
l$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* eƒ r·d dl+Z,d dl-Z,e .e .e,j/¡j0¡e .d¡kr‹e,j1j2Z3ne,j1Z3eƒ r·d dl4mZ5 d dl6m7Z7 e3j8e7j8e3j9e7j9e3j:e7j:e3j;e7j;e3j<e7j<e3j=e7j=iZ>erÀeƒ rÀd dl?Z?e! @eA¡ZBedejCdeDd eDejC eDd f ZEeeDd ddeDd eDd eDeDd  eDeDd  eDeDd  f ZFG dd„ deƒZGG dd„ deƒZHG dd„ deƒZIeG dd„ dƒƒZJeKeLeeMeLeDeK f f ZNdd„ ZOG dd„ deƒZPdd„ ZQdd „ ZRd!eDfd"d#„ZSd$d%„ ZTd&d'„ ZUd(ejCd)eVfd*d+„ZWd‰d-eMd)eDeE fd.d/„ZXd!eeDeE eEf d)eEfd0d1„ZYd!eeDeE eEf d)eEfd2d3„ZZd)eFfd4d5„Z[d)ejCfd6d7„Z\	dŠd(ejCd8eeeMe]eMd9f f  d)eGfd:d;„Z^	dŠd(ejCd<eeeGeLf  d)eMfd=d>„Z_dŠd(ejCd?eGd)e]eMeMf fd@dA„Z`dBe]eMeMf dCeMdDeMd)e]eMeMf fdEdF„ZadGeKeLeeDe]f f d)eVfdHdI„ZbdGeKeLeeDe]f f d)eVfdJdK„ZcdLeeKeLeeDe]f f  d)eVfdMdN„ZddLeeKeLeeDe]f f  d)eVfdOdP„ZedŠd(eeLdf dQeef d)dfdRdS„Zgd‹dTeJfdUdV„ZhdWeLdXefdYdZ„Zi	dŠdWeLdXee fd[d\„ZjdWeLdXefd]d^„ZkdWeLdXefd_d`„ZlejeiekeldaœZm			b	dŒdceeLddf deeeM dfeeM dgeLdXee d)ejnfdhdi„Zo	dŠd!eeDe]eLdf dQeef d)edeDd eDeDd  f fdjdk„Zp												d�dleeV dmeef dneeV doeeefeDef f  dpeeefeDef f  dqeeV dreeM dseeV dteeKeLeMf  dueeV dveeKeLeMf  dwedx fdydz„ZqG d{d|„ d|ƒZrd}eHd~e]eHd9f dLeDeK d)dfdd€„Zsd�eDeL d‚eDeL fdƒd„„Zted…d†�G d‡dˆ„ dˆƒƒZudS )Žé    N)ÚIterable)Úredirect_stdout)Ú	dataclass)ÚBytesIO)ÚTYPE_CHECKINGÚCallableÚOptionalÚUnion)Úversioné   )ÚExplicitEnumÚis_av_availableÚis_cv2_availableÚis_decord_availableÚis_jax_tensorÚis_numpy_arrayÚis_tf_tensorÚis_torch_availableÚis_torch_tensorÚis_torchvision_availableÚis_vision_availableÚis_yt_dlp_availableÚloggingÚrequires_backendsÚto_numpy)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚIMAGENET_STANDARD_MEANÚIMAGENET_STANDARD_STDÚOPENAI_CLIP_MEANÚOPENAI_CLIP_STDz9.1.0)Úio)ÚInterpolationModezPIL.Image.Imageztorch.Tensorz
np.ndarrayc                   @   ó   e Zd ZdZdZdS )ÚChannelDimensionÚchannels_firstÚchannels_lastN)Ú__name__Ú
__module__Ú__qualname__ÚFIRSTÚLAST© r,   r,   úk/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/transformers/image_utils.pyr$   f   ó    r$   c                   @   r#   )ÚAnnotationFormatZcoco_detectionZcoco_panopticN)r'   r(   r)   ÚCOCO_DETECTIONÚCOCO_PANOPTICr,   r,   r,   r-   r/   k   r.   r/   c                   @   s   e Zd ZejjZejjZdS )ÚAnnotionFormatN)r'   r(   r)   r/   r0   Úvaluer1   r,   r,   r,   r-   r2   p   s    r2   c                   @   s.   e Zd ZU eed< eed< eed< eed< dS )ÚVideoMetadataÚtotal_num_framesÚfpsÚdurationÚvideo_backendN)r'   r(   r)   ÚintÚ__annotations__ÚfloatÚstrr,   r,   r,   r-   r4   u   s
   
 r4   c                 C   s   t ƒ o	t| tjjƒS ©N)r   Ú
isinstanceÚPILÚImage©Úimgr,   r,   r-   Úis_pil_image€   s   rC   c                   @   s    e Zd ZdZdZdZdZdZdS )Ú	ImageTypeÚpillowÚtorchÚnumpyÚ
tensorflowÚjaxN)r'   r(   r)   r?   ÚTORCHÚNUMPYÚ
TENSORFLOWÚJAXr,   r,   r,   r-   rD   „   s    rD   c                 C   sX   t | ƒrtjS t| ƒrtjS t| ƒrtjS t| ƒrtjS t	| ƒr#tj
S tdt| ƒ› �ƒ‚)NzUnrecognised image type )rC   rD   r?   r   rJ   r   rK   r   rL   r   rM   Ú
ValueErrorÚtype©Úimager,   r,   r-   Úget_image_typeŒ   s   rR   c                 C   s(   t | ƒpt| ƒpt| ƒpt| ƒpt| ƒS r=   )rC   r   r   r   r   rA   r,   r,   r-   Úis_valid_imageš   s   (rS   Úimagesc                 C   s   | o
t dd„ | D ƒƒS )Nc                 s   ó   � | ]}t |ƒV  qd S r=   )rS   ©Ú.0rQ   r,   r,   r-   Ú	<genexpr>Ÿ   ó   € z*is_valid_list_of_images.<locals>.<genexpr>©Úall©rT   r,   r,   r-   Úis_valid_list_of_imagesž   ó   r]   c                 C   s:   t | ttfƒr| D ]	}t|ƒs dS q	dS t| ƒsdS dS )NFT)r>   ÚlistÚtupleÚvalid_imagesrS   )ZimgsrB   r,   r,   r-   ra   ¢   s   ÿþra   c                 C   s   t | ttfƒrt| d ƒS dS )Nr   F)r>   r_   r`   rS   rA   r,   r,   r-   Ú
is_batched®   s   rb   rQ   Úreturnc                 C   s,   | j tjkrdS t | ¡dkot | ¡dkS )zV
    Checks to see whether the pixel values have already been rescaled to [0, 1].
    Fr   r   )ÚdtypeÚnpÚuint8ÚminÚmaxrP   r,   r,   r-   Úis_scaled_image´   s   ri   é   Úexpected_ndimsc                 C   s†   t | ƒr| S t| ƒr| gS t| ƒr9| j|d krt| ƒ} | S | j|kr(| g} | S td|d › d|› d| j› d�ƒ‚tdt| ƒ› d�ƒ‚)a  
    Ensure that the output is a list of images. If the input is a single image, it is converted to a list of length 1.
    If the input is a batch of images, it is converted to a list of images.

    Args:
        images (`ImageInput`):
            Image of images to turn into a list of images.
        expected_ndims (`int`, *optional*, defaults to 3):
            Expected number of dimensions for a single input image. If the input image has a different number of
            dimensions, an error is raised.
    r   z%Invalid image shape. Expected either z or z dimensions, but got z dimensions.ztInvalid image type. Expected either PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray, but got Ú.)rb   rC   rS   Úndimr_   rN   rO   )rT   rk   r,   r,   r-   Úmake_list_of_images¿   s*   	
øüÿÿÿÿrn   c                 C   sÐ   t | ttfƒr tdd„ | D ƒƒr tdd„ | D ƒƒr dd„ | D ƒS t | ttfƒrHt| ƒrHt| d ƒs8| d jdkr:| S | d jdkrHd	d„ | D ƒS t| ƒrat| ƒsU| jdkrX| gS | jdkrat| ƒS td
| › �ƒ‚)a|  
    Ensure that the output is a flat list of images. If the input is a single image, it is converted to a list of length 1.
    If the input is a nested list of images, it is converted to a flat list of images.
    Args:
        images (`Union[List[ImageInput], ImageInput]`):
            The input image.
    Returns:
        list: A list of images or a 4d array of images.
    c                 s   ó   � | ]
}t |ttfƒV  qd S r=   ©r>   r_   r`   ©rW   Zimages_ir,   r,   r-   rX   õ   ó   € z+make_flat_list_of_images.<locals>.<genexpr>c                 s   rU   r=   ©r]   rq   r,   r,   r-   rX   ö   rY   c                 S   ó   g | ]	}|D ]}|‘qqS r,   r,   ©rW   Zimg_listrB   r,   r,   r-   Ú
<listcomp>ø   ó    z,make_flat_list_of_images.<locals>.<listcomp>r   rj   é   c                 S   rt   r,   r,   ru   r,   r,   r-   rv   þ   rw   z*Could not make a flat list of images from ©	r>   r_   r`   r[   r]   rC   rm   rS   rN   r\   r,   r,   r-   Úmake_flat_list_of_imagesæ   s$   ÿþý
rz   c                 C   sÆ   t | ttfƒrtdd„ | D ƒƒrtdd„ | D ƒƒr| S t | ttfƒrDt| ƒrDt| d ƒs3| d jdkr6| gS | d jdkrDdd„ | D ƒS t| ƒr_t| ƒsQ| jdkrU| ggS | jdkr_t| ƒgS td	ƒ‚)
zð
    Ensure that the output is a nested list of images.
    Args:
        images (`Union[List[ImageInput], ImageInput]`):
            The input image.
    Returns:
        list: A list of list of images or a list of 4d array of images.
    c                 s   ro   r=   rp   rq   r,   r,   r-   rX     rr   z-make_nested_list_of_images.<locals>.<genexpr>c                 s   rU   r=   rs   rq   r,   r,   r-   rX     rY   r   rj   rx   c                 S   ó   g | ]}t |ƒ‘qS r,   ©r_   rV   r,   r,   r-   rv   !  ó    z.make_nested_list_of_images.<locals>.<listcomp>z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.ry   r\   r,   r,   r-   Úmake_nested_list_of_images	  s$   ÿþý

r~   c                 C   s   t | ttfƒr2t | d ttfƒr2t| d d ƒr2t| d d ƒs0| d d jdkr0dd„ | D ƒ} | S t | ttfƒr^t| d ƒr^t| d ƒsL| d jdkrO| gS | d jdkr]dd„ | D ƒS nt| ƒryt| ƒsk| jdkro| ggS | jdkryt| ƒgS td| › �ƒ‚)zÅ
    Ensure that the input is a list of videos.
    Args:
        videos (`VideoInput`):
            Video or videos to turn into a list of videos.
    Returns:
        list: A list of videos.
    r   rx   c                 S   s   g | ]	}d d„ |D ƒ‘qS )c                 S   rt   r,   r,   )rW   Z
batch_listÚvideor,   r,   r-   rv   9  rw   z2make_batched_videos.<locals>.<listcomp>.<listcomp>r,   )rW   Zbatched_videosr,   r,   r-   rv   9  rw   z'make_batched_videos.<locals>.<listcomp>rj   c                 S   r{   r,   r|   )rW   r   r,   r,   r-   rv   A  r}   z"Could not make batched video from )r>   r_   r`   rS   rC   rm   rN   )Zvideosr,   r,   r-   Úmake_batched_videos-  s    0	"ÿ

r€   c                 C   s@   t | ƒstdt| ƒ› �ƒ‚tƒ rt| tjjƒrt | ¡S t	| ƒS )NzInvalid image type: )
rS   rN   rO   r   r>   r?   r@   re   Úarrayr   rA   r,   r,   r-   Úto_numpy_arrayL  s
   
r‚   Únum_channels.c                 C   sÂ   |dur|nd}t |tƒr|fn|}| jdkrd\}}n| jdkr&d\}}ntd| j› �ƒ‚| j| |v rI| j| |v rIt d| j› d�¡ tjS | j| |v rStjS | j| |v r]tj	S td	ƒ‚)
a[  
    Infers the channel dimension format of `image`.

    Args:
        image (`np.ndarray`):
            The image to infer the channel dimension of.
        num_channels (`int` or `Tuple[int, ...]`, *optional*, defaults to `(1, 3)`):
            The number of channels of the image.

    Returns:
        The channel dimension of the image.
    N©r   rj   rj   )r   é   rx   z(Unsupported number of image dimensions: z4The channel dimension is ambiguous. Got image shape z,. Assuming channels are the first dimension.z(Unable to infer channel dimension format)
r>   r9   rm   rN   ÚshapeÚloggerÚwarningr$   r*   r+   )rQ   rƒ   Ú	first_dimÚlast_dimr,   r,   r-   Úinfer_channel_dimension_formatU  s"   



ÿr‹   Úinput_data_formatc                 C   sF   |du rt | ƒ}|tjkr| jd S |tjkr| jd S td|› �ƒ‚)a–  
    Returns the channel dimension axis of the image.

    Args:
        image (`np.ndarray`):
            The image to get the channel dimension axis of.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format of the image. If `None`, will infer the channel dimension from the image.

    Returns:
        The channel dimension axis of the image.
    Nrj   r   úUnsupported data format: )r‹   r$   r*   rm   r+   rN   )rQ   rŒ   r,   r,   r-   Úget_channel_dimension_axisz  s   



rŽ   Úchannel_dimc                 C   sZ   |du rt | ƒ}|tjkr| jd | jd fS |tjkr&| jd | jd fS td|› �ƒ‚)a�  
    Returns the (height, width) dimensions of the image.

    Args:
        image (`np.ndarray`):
            The image to get the dimensions of.
        channel_dim (`ChannelDimension`, *optional*):
            Which dimension the channel dimension is in. If `None`, will infer the channel dimension from the image.

    Returns:
        A tuple of the image's height and width.
    Néþÿÿÿéÿÿÿÿéýÿÿÿr�   )r‹   r$   r*   r†   r+   rN   )rQ   r�   r,   r,   r-   Úget_image_size’  s   

r“   Ú
image_sizeÚ
max_heightÚ	max_widthc           
      C   sB   | \}}|| }|| }t ||ƒ}t|| ƒ}t|| ƒ}	||	fS )aË  
    Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio.
    Important, even if image_height < max_height and image_width < max_width, the image will be resized
    to at least one of the edges be equal to max_height or max_width.

    For example:
        - input_size: (100, 200), max_height: 50, max_width: 50 -> output_size: (25, 50)
        - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400)

    Args:
        image_size (`Tuple[int, int]`):
            The image to resize.
        max_height (`int`):
            The maximum allowed height.
        max_width (`int`):
            The maximum allowed width.
    )rg   r9   )
r”   r•   r–   ÚheightÚwidthÚheight_scaleÚwidth_scaleZ	min_scaleÚ
new_heightÚ	new_widthr,   r,   r-   Ú#get_image_size_for_max_height_widthª  s   
r�   Ú
annotationc                 C   sV   t | tƒr)d| v r)d| v r)t | d ttfƒr)t| d ƒdks't | d d tƒr)dS dS )NÚimage_idÚannotationsr   TF©r>   Údictr_   r`   Úlen©rž   r,   r,   r-   Ú"is_valid_annotation_coco_detectionÉ  s   ÿÿ"r¥   c                 C   s^   t | tƒr-d| v r-d| v r-d| v r-t | d ttfƒr-t| d ƒdks+t | d d tƒr-dS dS )NrŸ   Zsegments_infoÚ	file_namer   TFr¡   r¤   r,   r,   r-   Ú!is_valid_annotation_coco_panopticØ  s   ÿÿ"r§   r    c                 C   ó   t dd„ | D ƒƒS )Nc                 s   rU   r=   )r¥   ©rW   Úannr,   r,   r-   rX   é  rY   z3valid_coco_detection_annotations.<locals>.<genexpr>rZ   ©r    r,   r,   r-   Ú valid_coco_detection_annotationsè  ó   r¬   c                 C   r¨   )Nc                 s   rU   r=   )r§   r©   r,   r,   r-   rX   í  rY   z2valid_coco_panoptic_annotations.<locals>.<genexpr>rZ   r«   r,   r,   r-   Úvalid_coco_panoptic_annotationsì  r­   r®   Útimeoutc              
   C   s   t tdgƒ t| tƒre|  d¡s|  d¡r$tj tt	j
| |d�jƒ¡} nOtj | ¡r1tj | ¡} nB|  d¡r=|  d¡d } zt |  ¡ ¡}tj t|ƒ¡} W n$ tyd } z
td| › d	|› �ƒ‚d
}~ww t| tjjƒro| } ntdƒ‚tj | ¡} |  d¡} | S )a3  
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        timeout (`float`, *optional*):
            The timeout value in seconds for the URL request.

    Returns:
        `PIL.Image.Image`: A PIL Image.
    Úvisionúhttp://úhttps://©r¯   zdata:image/ú,r   z’Incorrect image source. Must be a valid URL starting with `http://` or `https://`, a valid path to an image file, or a base64 encoded string. Got z. Failed with NzuIncorrect format used for image. Should be an url linking to an image, a base64 string, a local path, or a PIL image.ÚRGB)r   Ú
load_imager>   r<   Ú
startswithr?   r@   Úopenr   ÚrequestsÚgetÚcontentÚosÚpathÚisfileÚsplitÚbase64ÚdecodebytesÚencodeÚ	ExceptionrN   Ú	TypeErrorZImageOpsZexif_transposeÚconvert)rQ   r¯   Úb64Úer,   r,   r-   r¶   ð  s2   

ÿ€ÿÿ
r¶   Úmetadatac                 K   s†   | j }| j}|du r(|dur(t|| | ƒ}||kr(td|› d|› d|› d�ƒ‚|dur9tjd||| td�}|S tjd|td�}|S )a`  
    A default sampling function that replicates the logic used in get_uniform_frame_indices,
    while optionally handling `fps` if `num_frames` is not provided.

    Args:
        metadata (`VideoMetadata`):
            `VideoMetadata` object containing metadata about the video, such as "total_num_frames" or "fps".
        num_frames (`int`, *optional*):
            Number of frames to sample uniformly.
        fps (`int`, *optional*):
            Desired frames per second. Takes priority over num_frames if both are provided.

    Returns:
        `np.ndarray`: Array of frame indices to sample.
    Nz When loading the video with fps=z, we computed num_frames=z  which exceeds total_num_frames=z. Check fps or video metadata.r   )rd   )r5   r6   r9   rN   re   Úarange)rÈ   Ú
num_framesr6   Úkwargsr5   Ú	video_fpsÚindicesr,   r,   r-   Údefault_sample_indices_fn  s   ÿÿÿrÎ   Ú
video_pathÚsample_indices_fnc                 K   s  t tdgƒ ddl}| | ¡}t| |j¡ƒ}| |j¡}|r#|| nd}tt|ƒt	|ƒt	|ƒdd�}|dd|i|¤Ž}	d}
g }| 
¡ r}| ¡ \}}|sMn0|
|	v rn|j\}}}| ||j¡}| |d|…d|…d|…f ¡ |rt|
d7 }
|
|kryn| 
¡ sD| ¡  |	|_t |¡|fS )	av  
    Decode a video using the OpenCV backend.

    Args:
        video_path (`str`):
            Path to the video file.
        sample_indices_fn (`Callable`):
            A callable function that will return indices at which the video should be sampled. If the video has to be loaded using
            by a different sampling technique than provided by `num_frames` or `fps` arguments, one should provide their own `sample_indices_fn`.
            If not provided, simple uniform sampling with fps is performed.
            Example:
            def sample_indices_fn(metadata, **kwargs):
                return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int)

    Returns:
        Tuple[`np.array`, `VideoMetadata`]: A tuple containing:
            - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]).
            - `VideoMetadata` object.
    Úcv2r   NÚopencv©r5   r6   r7   r8   rÈ   r   r,   )r   Úread_video_opencvrÑ   ZVideoCapturer9   rº   ZCAP_PROP_FRAME_COUNTZCAP_PROP_FPSr4   r;   ZisOpenedÚreadr†   ZcvtColorZCOLOR_BGR2RGBÚappendÚreleaseÚframes_indicesre   Ústack)rÏ   rÐ   rË   rÑ   r   r5   rÌ   r7   rÈ   rÍ   ÚindexÚframesÚsuccessÚframer—   r˜   Úchannelr,   r,   r-   rÔ   ?  s8   
ÿ õrÔ   c                 K   s–   t tdgƒ ddlm}m} || |dƒd�}| ¡ }t|ƒ}|r$|| nd}tt|ƒt	|ƒt	|ƒdd�}	|dd|	i|¤Ž}
| 
|
¡ ¡ }|
|	_||	fS )	a‚  
    Decode a video using the Decord backend.

    Args:
        video_path (`str`):
            Path to the video file.
        sample_indices_fn (`Callable`, *optional*):
            A callable function that will return indices at which the video should be sampled. If the video has to be loaded using
            by a different sampling technique than provided by `num_frames` or `fps` arguments, one should provide their own `sample_indices_fn`.
            If not provided, simple uniform sampling with fps is performed.
            Example:
            def sample_indices_fn(metadata, **kwargs):
                return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int)

    Returns:
        Tuple[`np.array`, `VideoMetadata`]: A tuple containing:
            - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]).
            - `VideoMetadata` object.
    Údecordr   )ÚVideoReaderÚcpu)ÚuriÚctxrÓ   rÈ   Nr,   )r   Úread_video_decordrß   rà   rá   Zget_avg_fpsr£   r4   r9   r;   Ú	get_batchZasnumpyrØ   )rÏ   rÐ   rË   rà   rá   ÚvrrÌ   r5   r7   rÈ   rÍ   rÛ   r,   r,   r-   rä   x  s   ÿrä   c                 K   sð   t tdgƒ ddl}| | ¡}|jjd j}|jjd j}|r#|| nd}tt	|ƒt
|ƒt
|ƒdd�}|dd|i|¤Ž}	g }
| d¡ |	d }t|jdd�ƒD ]\}}||krY n|dkrf||	v rf|
 |¡ qOt d	d
„ |
D ƒ¡}|	|_||fS )a}  
    Decode the video with PyAV decoder.

    Args:
        video_path (`str`):
            Path to the video file.
        sample_indices_fn (`Callable`, *optional*):
            A callable function that will return indices at which the video should be sampled. If the video has to be loaded using
            by a different sampling technique than provided by `num_frames` or `fps` arguments, one should provide their own `sample_indices_fn`.
            If not provided, simple uniform sampling with fps is performed.
            Example:
            def sample_indices_fn(metadata, **kwargs):
                return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int)

    Returns:
        Tuple[`np.array`, `VideoMetadata`]: A tuple containing:
            - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]).
            - `VideoMetadata` object.
    Úavr   NÚpyavrÓ   rÈ   r‘   )r   c                 S   s   g | ]}|j d d�‘qS )Úrgb24)Úformat)Z
to_ndarray)rW   Úxr,   r,   r-   rv   Ñ  ó    z#read_video_pyav.<locals>.<listcomp>r,   )r   Úread_video_pyavrç   r¸   Ústreamsr   rÛ   Zaverage_rater4   r9   r;   ÚseekÚ	enumerateÚdecoderÖ   re   rÙ   rØ   )rÏ   rÐ   rË   rç   Ú	containerr5   rÌ   r7   rÈ   rÍ   rÛ   Ú	end_indexÚirÝ   r   r,   r,   r-   rí   £  s,   
ÿ

€rí   c                 K   sˆ   t j| ddddd�\}}}|d }| d¡}|r|| nd}tt|ƒt|ƒt|ƒdd	�}	|dd
|	i|¤Ž}
||
  ¡  ¡ }|
|	_||	fS )a„  
    Decode the video with torchvision decoder.

    Args:
        video_path (`str`):
            Path to the video file.
        sample_indices_fn (`Callable`, *optional*):
            A callable function that will return indices at which the video should be sampled. If the video has to be loaded using
            by a different sampling technique than provided by `num_frames` or `fps` arguments, one should provide their own `sample_indices_fn`.
            If not provided, simple uniform sampling with fps is performed.
            Example:
            def sample_indices_fn(metadata, **kwargs):
                return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int)

    Returns:
        Tuple[`np.array`, `VideoMetadata`]: A tuple containing:
            - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]).
            - `VideoMetadata` object.
    g        NÚsecZTHWC)Z	start_ptsZend_ptsZpts_unitÚoutput_formatrÌ   r   ÚtorchvisionrÓ   rÈ   r,   )	Útorchvision_ioÚ
read_videoÚsizer4   r9   r;   Ú
contiguousrG   rØ   )rÏ   rÐ   rË   r   Ú_ÚinforÌ   r5   r7   rÈ   rÍ   r,   r,   r-   Úread_video_torchvisionÖ  s(   û
ürþ   )rß   rÒ   rè   r÷   rÒ   r   Ú
VideoInputrÊ   r6   Úbackendc              	      sö  ˆ durˆdur|du rt dƒ‚|du r‡ ‡fdd„}|}|  d¡s'|  d¡rstƒ s.tdƒ‚ttdgƒ d	d
lm} tƒ }t	|ƒ�! |ƒ �}	|	 
| g¡ W d  ƒ n1 sVw   Y  W d  ƒ n1 sew   Y  | ¡ }
t|
ƒ}n4|  d¡s}|  d¡r†tt | ¡jƒ}n!tj | ¡r�| }nt| ƒs t| ttfƒr£t| d	 ƒr£d}ntdƒ‚|  d¡p°|  d¡}|r»|dv r»t dƒ‚|du rÁ| S tƒ sÈ|dksÝtƒ sÏ|dksÝtƒ sÖ|dksÝtƒ sè|dkrètd|› d|› d�ƒ‚t| }|||fi |¤Ž\} }| |fS )aÍ  
    Loads `video` to a numpy array.

    Args:
        video (`str` or `VideoInput`):
            The video to convert to the numpy array format. Can be a link to video or local path.
        num_frames (`int`, *optional*):
            Number of frames to sample uniformly. If not passed, the whole video is loaded.
        fps (`int`, *optional*):
            Number of frames to sample per second. Should be passed only when `num_frames=None`.
            If not specified and `num_frames==None`, all frames are sampled.
        backend (`str`, *optional*, defaults to `"opencv"`):
            The backend to use when loading the video. Can be any of ["decord", "pyav", "opencv", "torchvision"]. Defaults to "opencv".
        sample_indices_fn (`Callable`, *optional*):
            A callable function that will return indices at which the video should be sampled. If the video has to be loaded using
            by a different sampling technique than provided by `num_frames` or `fps` arguments, one should provide their own `sample_indices_fn`.
            If not provided, simple uniformt sampling with fps is performed, otherwise `sample_indices_fn` has priority over other args.
            The function expects at input the all args along with all kwargs passed to `load_video` and should output valid
            indices at which the video should be sampled. For example:

            Example:
            def sample_indices_fn(metadata, **kwargs):
                return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int)

    Returns:
        Tuple[`np.array`, Dict]: A tuple containing:
            - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]).
            - Metadata dictionary.
    Nzc`num_frames`, `fps`, and `sample_indices_fn` are mutually exclusive arguments, please use only one!c                    s   t | fˆˆ dœ|¤ŽS )N)rÊ   r6   )rÎ   )rÈ   Ú	fn_kwargs©r6   rÊ   r,   r-   Úsample_indices_fn_func=  r^   z*load_video.<locals>.sample_indices_fn_funczhttps://www.youtube.comzhttp://www.youtube.comzETo load a video from YouTube url you have  to install `yt_dlp` first.Úyt_dlpr   )Ú	YoutubeDLr±   r²   zVIncorrect format used for video. Should be an url linking to an video or a local path.)rÒ   r÷   zlIf you are trying to load a video from URL, you can decode the video only with `pyav` or `decord` as backendrß   rè   rÒ   r÷   zYou chose backend=zf for loading the video but the required library is not found in your environment Make sure to install z before loading the video.)rN   r·   r   ÚImportErrorr   Ú
load_videor  r  r   r   ÚdownloadÚgetvaluer¹   rº   r»   r¼   r½   r¾   rS   r>   r_   r`   rÄ   r   r   r   r   ÚVIDEO_DECODERS)r   rÊ   r6   r   rÐ   rË   r  r  ÚbufferÚfZ	bytes_objÚfile_objZvideo_is_urlZvideo_decoderrÈ   r,   r  r-   r    sl   'ÿÿ€ 
"ÿÿÿÿÿÿÿr  c                    sX   t | ttfƒr&t| ƒrt | d ttfƒr‡ fdd„| D ƒS ‡ fdd„| D ƒS t| ˆ d�S )a  Loads images, handling different levels of nesting.

    Args:
      images: A single image, a list of images, or a list of lists of images to load.
      timeout: Timeout for loading images.

    Returns:
      A single image, a list of images, a list of lists of images.
    r   c                    s   g | ]}‡ fd d„|D ƒ‘qS )c                    ó   g | ]}t |ˆ d �‘qS ©r³   ©r¶   rV   r³   r,   r-   rv   €  rì   z*load_images.<locals>.<listcomp>.<listcomp>r,   )rW   Zimage_groupr³   r,   r-   rv   €  s    zload_images.<locals>.<listcomp>c                    r  r  r  rV   r³   r,   r-   rv   ‚  rì   r³   )r>   r_   r`   r£   r¶   )rT   r¯   r,   r³   r-   Úload_imagesr  s
   r  Ú
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdo_padÚsize_divisibilityÚdo_center_cropÚ	crop_sizeÚ	do_resizerú   ÚresampleÚPILImageResamplingc                 C   s|   | r
|du r
t dƒ‚|r|du rt dƒ‚|r"|du s|du r"t dƒ‚|r,|du r,t dƒ‚|	r:|
du s6|du r<t dƒ‚dS dS )a‡  
    Checks validity of typically used arguments in an `ImageProcessor` `preprocess` method.
    Raises `ValueError` if arguments incompatibility is caught.
    Many incompatibilities are model-specific. `do_pad` sometimes needs `size_divisor`,
    sometimes `size_divisibility`, and sometimes `size`. New models and processors added should follow
    existing arguments when possible.

    Nz=`rescale_factor` must be specified if `do_rescale` is `True`.zzDepending on the model, `size_divisibility`, `size_divisor`, `pad_size` or `size` must be specified if `do_pad` is `True`.zP`image_mean` and `image_std` must both be specified if `do_normalize` is `True`.z<`crop_size` must be specified if `do_center_crop` is `True`.zA`size` and `resample` must be specified if `do_resize` is `True`.)rN   )r  r  r  r  r  r  r  r  r  r  rú   r  r,   r,   r-   Úvalidate_preprocess_arguments‡  s   ÿÿr  c                   @   sŒ   e Zd ZdZdd„ Zddd„Zdd„ Zd	ejd
e	e
ef dejfdd„Zd dd„Zdd„ Zd!dd„Zd"dd„Zdd„ Zdd„ Zd#dd„ZdS )$ÚImageFeatureExtractionMixinzD
    Mixin that contain utilities for preparing image features.
    c                 C   s8   t |tjjtjfƒst|ƒstdt|ƒ› d�ƒ‚d S d S )Nz	Got type zS which is not supported, only `PIL.Image.Image`, `np.array` and `torch.Tensor` are.)r>   r?   r@   re   Úndarrayr   rN   rO   ©ÚselfrQ   r,   r,   r-   Ú_ensure_format_supported¶  s
   ÿÿz4ImageFeatureExtractionMixin._ensure_format_supportedNc                 C   sŽ   |   |¡ t|ƒr| ¡ }t|tjƒrE|du r t|jd tjƒ}|jdkr3|j	d dv r3| 
ddd¡}|r9|d }| tj¡}tj |¡S |S )a"  
        Converts `image` to a PIL Image. Optionally rescales it and puts the channel dimension back as the last axis if
        needed.

        Args:
            image (`PIL.Image.Image` or `numpy.ndarray` or `torch.Tensor`):
                The image to convert to the PIL Image format.
            rescale (`bool`, *optional*):
                Whether or not to apply the scaling factor (to make pixel values integers between 0 and 255). Will
                default to `True` if the image type is a floating type, `False` otherwise.
        Nr   rj   r„   r   r…   éÿ   )r#  r   rG   r>   re   r   ÚflatÚfloatingrm   r†   Ú	transposeÚastyperf   r?   r@   Ú	fromarray)r"  rQ   Úrescaler,   r,   r-   Úto_pil_image½  s   
z(ImageFeatureExtractionMixin.to_pil_imagec                 C   s&   |   |¡ t|tjjƒs|S | d¡S )z—
        Converts `PIL.Image.Image` to RGB format.

        Args:
            image (`PIL.Image.Image`):
                The image to convert.
        rµ   )r#  r>   r?   r@   rÅ   r!  r,   r,   r-   Úconvert_rgbÛ  s   

z'ImageFeatureExtractionMixin.convert_rgbrQ   Úscalerc   c                 C   s   |   |¡ || S )z7
        Rescale a numpy image by scale amount
        )r#  )r"  rQ   r-  r,   r,   r-   r*  é  s   
z#ImageFeatureExtractionMixin.rescaleTc                 C   sˆ   |   |¡ t|tjjƒrt |¡}t|ƒr| ¡ }|du r&t|jd tj	ƒn|}|r4|  
| tj¡d¡}|rB|jdkrB| ddd¡}|S )aÓ  
        Converts `image` to a numpy array. Optionally rescales it and puts the channel dimension as the first
        dimension.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to convert to a NumPy array.
            rescale (`bool`, *optional*):
                Whether or not to apply the scaling factor (to make pixel values floats between 0. and 1.). Will
                default to `True` if the image is a PIL Image or an array/tensor of integers, `False` otherwise.
            channel_first (`bool`, *optional*, defaults to `True`):
                Whether or not to permute the dimensions of the image to put the channel dimension first.
        Nr   çp?rj   r…   r   )r#  r>   r?   r@   re   r�   r   rG   r%  Úintegerr*  r(  Úfloat32rm   r'  )r"  rQ   r*  Úchannel_firstr,   r,   r-   r‚   ð  s   

z*ImageFeatureExtractionMixin.to_numpy_arrayc                 C   sD   |   |¡ t|tjjƒr|S t|ƒr| d¡}|S tj|dd�}|S )z½
        Expands 2-dimensional `image` to 3 dimensions.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to expand.
        r   )Úaxis)r#  r>   r?   r@   r   Ú	unsqueezere   Úexpand_dimsr!  r,   r,   r-   r4    s   

ÿz'ImageFeatureExtractionMixin.expand_dimsFc                 C   sh  |   |¡ t|tjjƒr| j|dd�}n|r3t|tjƒr'|  | tj	¡d¡}nt
|ƒr3|  | ¡ d¡}t|tjƒrXt|tjƒsHt |¡ |j¡}t|tjƒsWt |¡ |j¡}n6t
|ƒrŽddl}t||jƒswt|tjƒrr| |¡}n| |¡}t||jƒsŽt|tjƒr‰| |¡}n| |¡}|jdkr®|jd dv r®||dd…ddf  |dd…ddf  S || | S )a  
        Normalizes `image` with `mean` and `std`. Note that this will trigger a conversion of `image` to a NumPy array
        if it's a PIL Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to normalize.
            mean (`List[float]` or `np.ndarray` or `torch.Tensor`):
                The mean (per channel) to use for normalization.
            std (`List[float]` or `np.ndarray` or `torch.Tensor`):
                The standard deviation (per channel) to use for normalization.
            rescale (`bool`, *optional*, defaults to `False`):
                Whether or not to rescale the image to be between 0 and 1. If a PIL image is provided, scaling will
                happen automatically.
        T)r*  r.  r   Nrj   r„   )r#  r>   r?   r@   r‚   re   r   r*  r(  r0  r   r;   r�   rd   rF   ÚTensorÚ
from_numpyÚtensorrm   r†   )r"  rQ   ÚmeanÚstdr*  rF   r,   r,   r-   Ú	normalize$  s6   
€

(z%ImageFeatureExtractionMixin.normalizec                 C   sJ  |dur|nt j}|  |¡ t|tjjƒs|  |¡}t|tƒr#t|ƒ}t|t	ƒs.t
|ƒdkrž|rBt|t	ƒr9||fn|d |d f}n\|j\}}||krO||fn||f\}}	t|t	ƒr\|n|d }
||
krf|S |
t	|
|	 | ƒ}}|dur’||
krƒtd|› d|› �ƒ‚||kr’t	|| | ƒ|}}||krš||fn||f}|j||d�S )a›  
        Resizes `image`. Enforces conversion of input to PIL.Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to resize.
            size (`int` or `Tuple[int, int]`):
                The size to use for resizing the image. If `size` is a sequence like (h, w), output size will be
                matched to this.

                If `size` is an int and `default_to_square` is `True`, then image will be resized to (size, size). If
                `size` is an int and `default_to_square` is `False`, then smaller edge of the image will be matched to
                this number. i.e, if height > width, then image will be rescaled to (size * height / width, size).
            resample (`int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                The filter to user for resampling.
            default_to_square (`bool`, *optional*, defaults to `True`):
                How to convert `size` when it is a single int. If set to `True`, the `size` will be converted to a
                square (`size`,`size`). If set to `False`, will replicate
                [`torchvision.transforms.Resize`](https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize)
                with support for resizing only the smallest edge and providing an optional `max_size`.
            max_size (`int`, *optional*, defaults to `None`):
                The maximum allowed for the longer edge of the resized image: if the longer edge of the image is
                greater than `max_size` after being resized according to `size`, then the image is resized again so
                that the longer edge is equal to `max_size`. As a result, `size` might be overruled, i.e the smaller
                edge may be shorter than `size`. Only used if `default_to_square` is `False`.

        Returns:
            image: A resized `PIL.Image.Image`.
        Nr   r   zmax_size = zN must be strictly greater than the requested size for the smaller edge size = )r  )r  ÚBILINEARr#  r>   r?   r@   r+  r_   r`   r9   r£   rú   rN   Úresize)r"  rQ   rú   r  Údefault_to_squareÚmax_sizer˜   r—   ÚshortÚlongÚrequested_new_shortÚ	new_shortÚnew_longr,   r,   r-   r<  X  s4   


$
ÿÿz"ImageFeatureExtractionMixin.resizec                 C   s€  |   |¡ t|tƒs||f}t|ƒst|tjƒr8|jdkr"|  |¡}|jd dv r0|jdd… n|jdd… }n
|j	d |j	d f}|d |d  d }||d  }|d |d  d }||d  }t|t
jjƒrr| ||||f¡S |jd dv r{dnd}|s—t|tjƒrŒ| ddd¡}t|ƒr—| ddd¡}|dkr¶||d kr¶|dkr¶||d kr¶|d||…||…f S |jdd	… t|d |d ƒt|d |d ƒf }	t|tjƒrÝtj||	d
�}
n	t|ƒræ| |	¡}
|	d	 |d  d }||d  }|	d |d  d }||d  }||
d||…||…f< ||7 }||7 }||7 }||7 }|
dtd|ƒt|
jd	 |ƒ…td|ƒt|
jd |ƒ…f }
|
S )a•  
        Crops `image` to the given size using a center crop. Note that if the image is too small to be cropped to the
        size given, it will be padded (so the returned result has the size asked).

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor` of shape (n_channels, height, width) or (height, width, n_channels)):
                The image to resize.
            size (`int` or `Tuple[int, int]`):
                The size to which crop the image.

        Returns:
            new_image: A center cropped `PIL.Image.Image` or `np.ndarray` or `torch.Tensor` of shape: (n_channels,
            height, width).
        r…   r   r„   r   NTF.r�   )r†   r‘   )r#  r>   r`   r   re   r   rm   r4  r†   rú   r?   r@   Úcropr'  Úpermuterh   Ú
zeros_likeÚ	new_zerosrg   )r"  rQ   rú   Zimage_shapeÚtopÚbottomÚleftÚrightr1  Ú	new_shapeÚ	new_imageÚtop_padÚ
bottom_padÚleft_padÚ	right_padr,   r,   r-   Úcenter_crop›  sP   



,(2
4ÿz'ImageFeatureExtractionMixin.center_cropc                 C   s>   |   |¡ t|tjjƒr|  |¡}|ddd…dd…dd…f S )a   
        Flips the channel order of `image` from RGB to BGR, or vice versa. Note that this will trigger a conversion of
        `image` to a NumPy array if it's a PIL Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image whose color channels to flip. If `np.ndarray` or `torch.Tensor`, the channel dimension should
                be first.
        Nr‘   )r#  r>   r?   r@   r‚   r!  r,   r,   r-   Úflip_channel_orderæ  s   


z.ImageFeatureExtractionMixin.flip_channel_orderr   c                 C   sL   |dur|nt jj}|  |¡ t|t jjƒs|  |¡}|j||||||d�S )aÖ  
        Returns a rotated copy of `image`. This method returns a copy of `image`, rotated the given number of degrees
        counter clockwise around its centre.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to rotate. If `np.ndarray` or `torch.Tensor`, will be converted to `PIL.Image.Image` before
                rotating.

        Returns:
            image: A rotated `PIL.Image.Image`.
        N)r  ÚexpandÚcenterÚ	translateÚ	fillcolor)r?   r@   ÚNEARESTr#  r>   r+  Úrotate)r"  rQ   Úangler  rT  rU  rV  rW  r,   r,   r-   rY  ÷  s   

ÿz"ImageFeatureExtractionMixin.rotater=   )NT)F)NTN)Nr   NNN)r'   r(   r)   Ú__doc__r#  r+  r,  re   r   r	   r;   r9   r*  r‚   r4  r:  r<  rR  rS  rY  r,   r,   r,   r-   r  ±  s    
"
 

4CKr  Úannotation_formatÚsupported_annotation_formatsc                 C   sX   | |vrt dt› d|› �ƒ‚| tju rt|ƒst dƒ‚| tju r(t|ƒs*t dƒ‚d S d S )NzUnsupported annotation format: z must be one of zäInvalid COCO detection annotations. Annotations must a dict (single image) or list of dicts (batch of images) with the following keys: `image_id` and `annotations`, with the latter being a list of annotations in the COCO format.zòInvalid COCO panoptic annotations. Annotations must a dict (single image) or list of dicts (batch of images) with the following keys: `image_id`, `file_name` and `segments_info`, with the latter being a list of annotations in the COCO format.)rN   rê   r/   r0   r¬   r1   r®   )r\  r]  r    r,   r,   r-   Úvalidate_annotations  s   
ÿ
ÿþr^  Úvalid_processor_keysÚcaptured_kwargsc                 C   s:   t |ƒ t | ƒ¡}|rd |¡}t d|› d�¡ d S d S )Nz, zUnused or unrecognized kwargs: rl   )ÚsetÚ
differenceÚjoinr‡   rˆ   )r_  r`  Zunused_keysZunused_key_strr,   r,   r-   Úvalidate_kwargs)  s
   
ýrd  T)Úfrozenc                   @   sz   e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< dZee ed< d	d
„ ZdS )ÚSizeDictz>
    Hashable dictionary to store image size information.
    Nr—   r˜   Úlongest_edgeÚshortest_edger•   r–   c                 C   s$   t | |ƒr
t| |ƒS td|› d�ƒ‚)NzKey z not found in SizeDict.)ÚhasattrÚgetattrÚKeyError)r"  Úkeyr,   r,   r-   Ú__getitem__>  s   

zSizeDict.__getitem__)r'   r(   r)   r[  r—   r   r9   r:   r˜   rg  rh  r•   r–   rm  r,   r,   r,   r-   rf  1  s   
 rf  )rj   r=   )NN)NNrÒ   N)NNNNNNNNNNNN)vrÀ   r¼   Úcollections.abcr   Ú
contextlibr   Údataclassesr   r!   r   Útypingr   r   r   r	   rG   re   r¹   Ú	packagingr
   Úutilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zutils.constantsr   r   r   r   r   r    Ú	PIL.Imager?   ZPIL.ImageOpsÚparseÚ__version__Úbase_versionr@   Ú
Resamplingr  r÷   rø   Ztorchvision.transformsr"   rX  ÚBOXr;  ÚHAMMINGÚBICUBICÚLANCZOSZpil_torch_interpolation_mappingrF   Ú
get_loggerr'   r‡   r   r_   Ú
ImageInputrÿ   r$   r/   r2   r4   r¢   r<   r9   ZAnnotationTyperC   rD   rR   rS   r]   ra   rb   Úboolri   rn   rz   r~   r€   r‚   r`   r‹   rŽ   r“   r�   r¥   r§   r¬   r®   r;   r¶   rÎ   rÔ   rä   rí   rþ   r
  r�   r  r  r  r  r^  rd  rf  r,   r,   r,   r-   Ú<module>   s   D 

ú

ÿ


ùÿ'ÿ
þ#ÿ
þ$
ÿÿÿ
þ&ÿÿÿ
þ"
ÿþý

ü""&&$,#ÿ
þ;þÿ
þ+ÿ
þ3ÿ
þ1ü
û
ÿþýüû
ùeÿÿÿ
þôÿþýüûúùø	÷
öõ
ô*  aÿ
þý
ü