Ë
    ÛÓ^jÝ  ã                   ó¾   — d Z ddlZddlmc mc mZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZmZ  G d
„ ded¬«      Z G d„ de«      ZdgZy)z(Fast Video processor class for InternVL.é    Né   )ÚBatchFeature)ÚOPENAI_CLIP_MEANÚOPENAI_CLIP_STDÚPILImageResamplingÚSizeDict)ÚUnpackÚVideosKwargs)Ú
TensorType)ÚBaseVideoProcessor)ÚVideoMetadataÚgroup_videos_by_shapeÚreorder_videosc                   ó$   — e Zd ZU eez  ez  ed<   y)Ú InternVLVideoProcessorInitKwargsÚinitial_shiftN)Ú__name__Ú
__module__Ú__qualname__ÚboolÚfloatÚintÚ__annotations__© ó    ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/internvl/video_processing_internvl.pyr   r      s   … Ø˜%‘< #Ñ%Ô%r   r   F)Útotalc                   ó.  ‡ — e Zd Zej                  ZeZeZ	dddœZ
dZdZdZdZdZdZeZdee   fˆ fd„Z	 	 	 dded	edz  d
eez  dz  deez  ez  dz  fd„Z	 dded   dedededddedededededeee   z  dz  deee   z  dz  deez  dz  defd„Zˆ xZ S ) ÚInternVLVideoProcessori€  )ÚheightÚwidthTFÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr   )ÚsuperÚ__init__)Úselfr"   Ú	__class__s     €r   r%   zInternVLVideoProcessor.__init__,   s   ø€ Ü‰ÑÑ"˜6Ó"r   NÚmetadataÚ
num_framesÚfpsr   c                 ód  — |�|n| j                   }|�|n| j                  }|j                  }|€6|�4|�|j                  €t	        d«      ‚t        ||j                  z  |z  «      }|du r||z  dz  }||kD  rt	        d|› d|› d�«      ‚t        j                  ||||z  «      j                  «       }|S )a  
        Default sampling function which uniformly samples the desired number of frames between 0 and total number of frames.
        If `fps` is passed along with metadata, `fps` frames per second are sampled uniformty. Arguments `num_frames`
        and `fps` are mutually exclusive.

        Args:
            metadata (`VideoMetadata`):
                Metadata of the video containing information about total duration, fps and total number of frames.
            num_frames (`int`, *optional*):
                Maximum number of frames to sample. Defaults to `self.num_frames`.
            fps (`int` or `float`, *optional*):
                Target frames to sample per second. Defaults to `self.fps`.
            initial_shift (`bool`, `float` or `int`, defaults to `self.initial_shift`):
                The initial shift to apply when sampling frames. If `True`, the shift is set so that frames are sampled from the middle of the video.

        Returns:
            np.ndarray:
                Indices to sample video frames.
        zÈAsked to sample `fps` frames per second but no video metadata was provided which is required when sampling with `fps`. Please pass in `VideoMetadata` object or use a fixed `num_frames` per input videoTé   z(Video can't be sampled. The `num_frames=z` exceeds `total_num_frames=z`. )r)   r   Útotal_num_framesr*   Ú
ValueErrorr   ÚtorchÚarange)r&   r(   r)   r*   r   r"   r-   Úindicess           r   Úsample_framesz$InternVLVideoProcessor.sample_frames/   sè   € ð6 $.Ð#9‘Z¸t¿¹ˆ
Ø)6Ð)B™È×HZÑHZˆØ#×4Ñ4Ðð Ð # /ØÐ 8§<¡<Ð#7Ü ðhóð ô Ð-°·±Ñ<¸sÑBÓCˆJà˜DÑ Ø,¨zÑ9¸AÑ=ˆMàÐ(Ò(ÜØ:¸:¸,ÐFbÐcsÐbtÐtwÐxóð ô —,‘,˜}Ð.>Ð@PÐS]Ñ@]Ó^×bÑbÓdˆØˆr   Úvideosztorch.TensorÚdo_convert_rgbÚ	do_resizeÚsizeÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizeÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚreturn_tensorsÚreturnc           	      óª  — t        |«      \  }}i }|j                  «       D ]3  \  }}|r| j                  |«      }|r| j                  |||¬«      }|||<   Œ5 t	        ||«      }t        |«      \  }}i }|j                  «       D ]4  \  }}|r| j                  ||«      }| j                  |||	|
||«      }|||<   Œ6 t	        ||«      }t        d|i|¬«      S )N)r6   r7   Úpixel_values_videos)ÚdataÚtensor_type)r   ÚitemsÚconvert_to_rgbÚresizer   Úcenter_cropÚrescale_and_normalizer   )r&   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r"   Úgrouped_videosÚgrouped_videos_indexÚresized_videos_groupedÚshapeÚstacked_videosÚresized_videosÚprocessed_videos_groupedÚprocessed_videoss                          r   Ú_preprocessz"InternVLVideoProcessor._preprocessb   s  € ô$ 0EÀVÓ/LÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ó%;ò 	;Ñ!ˆE�>ÙØ!%×!4Ñ!4°^Ó!D�ÙØ!%§¡¨^À$ÐQY Ó!Z�Ø,:Ð" 5Ò)ð	;ô (Ð(>Ð@TÓUˆô 0EÀ^Ó/TÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ó%;ò 	=Ñ!ˆE�>ÙØ!%×!1Ñ!1°.À)Ó!L�à!×7Ñ7Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð	=ô *Ð*BÐDXÓYÐäÐ"7Ð9IÐ!JÐXfÔgÐgr   )NNN)N)!r   r   r   r   ÚBICUBICr7   r   r=   r   r>   r6   r5   r:   r<   r4   r   Údo_sample_framesr   Úvalid_kwargsr	   r%   r   r   r   r   r2   Úlistr   Ústrr   r   rR   Ú__classcell__)r'   s   @r   r   r      sy  ø„ Ø!×)Ñ)€HØ!€JØ€IØ CÑ(€DØ€IØ€JØ€LØ€NØ€MØÐØ3€Lð# Ð(HÑ!Iõ #ð "&Ø"&Ø37ñ1àð1ð ˜$‘Jð1ð �5‰[˜4Ñð	1ð
 ˜e‘| cÑ)¨DÑ0ó1ðB 37ñ+hà�^Ñ$ð+hð ð+hð ð	+hð
 ð+hð Lð+hð ð+hð ð+hð ð+hð ð+hð ð+hð ˜D ™KÑ'¨$Ñ.ð+hð ˜4 ™;Ñ&¨Ñ-ð+hð ˜jÑ(¨4Ñ/ð+hð  
÷!+hr   r   )Ú__doc__r/   Ú$torchvision.transforms.v2.functionalÚ
transformsÚv2Ú
functionalÚtvFÚimage_processing_utilsr   Úimage_utilsr   r   r   r   Úprocessing_utilsr	   r
   Úutilsr   Úvideo_processing_utilsr   Úvideo_utilsr   r   r   r   r   Ú__all__r   r   r   ú<module>rf      sU   ðñ /ã ß 2Ó 2å 2ß ZÓ Zß 4Ý Ý 8ß OÑ Oô& |¸5õ &ônhÐ/ô nhðb $Ð
$�r   