
    ^j6,                         d dl Z d dlZd dlZd dlmZ ddlmZ ddl	m
Z
mZmZmZmZmZ ddlmZmZ ddlmZmZ ddlmZmZ dd	lmZmZmZ d
dlmZ  G d ded      Z eded       G d de             Z dgZ!y)    N)
functional   )BatchFeature)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimensionPILImageResamplingSizeDictget_image_size)UnpackVideosKwargs)
TensorTypeadd_start_docstrings)BASE_VIDEO_PROCESSOR_DOCSTRINGBaseVideoProcessor)VideoMetadatagroup_videos_by_shapereorder_videos   )smart_resizec                   T    e Zd ZU eeef   ed<   eed<   eed<   eed<   eed<   eed<   y)GlmgaVideoProcessorInitKwargsmax_image_size
patch_sizetemporal_patch_size
merge_sizepatch_expand_factor
max_framesN)__name__
__module____qualname__dictstrint__annotations__     {/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/glmga/video_processing_glmga.pyr   r   +   s+    cN"OOOr'   r   F)totalzfConstructs a fast GLM-4V image processor that dynamically resizes videos based on the original videos.aj  
        patch_size (`int`, *optional*, defaults to 14):
            The spacial patch size of the vision encoder.
        temporal_patch_size (`int`, *optional*, defaults to 2):
            The temporal patch size of the vision encoder.
        merge_size (`int`, *optional*, defaults to 2):
            The merge size of the vision encoder to llm encoder.
    c                        e Zd Zej                  ZdddZddiZeZ	e
ZdZdZdZdZdZdZdZdZdZeZd	ZdZd
dgZdZdZdee   f fdZdef fdZ	 d(de de!e"z  dz  fdZ#dddej                  ddddddddddfde$e%jL                     de'de'de(dz  ddde'de"de'd e"e$e"   z  dz  d!e"e$e"   z  dz  d"e!dz  d#e!dz  d$e!dz  d%e!dz  d&e)e*z  dz  fd'Z+ xZ,S ))GlmgaVideoProcessori 1  i6)shortest_edgelongest_edger-   T      i,     pixel_values_videosvideo_grid_thwr   i  kwargsc                 $    t        |   di | y )Nr&   )super__init__)selfr3   	__class__s     r(   r6   zGlmgaVideoProcessor.__init__W   s    "6"r'   returnc                     t        |   di |}|j                  d| j                        }|j                  r|j
                  st        d      |S )z
        Update kwargs that need further processing before being validated
        Can be overridden by subclasses to customize the processing of kwargs.
        sizez:size must contain 'shortest_edge' and 'longest_edge' keys.r&   )r5   _standardize_kwargsgetr;   r,   r-   
ValueError)r7   r3   r;   r8   s      r(   r<   z'GlmgaVideoProcessor._standardize_kwargsZ   sM    
 ,6v6zz&$)),!!):):YZZr'   Nmetadatafpsc                    |t        |dd      t        d      |j                  }|dz
  }|j                  xs t	        ||j
                  z        dz   }||n| j
                  }t        ||z        }t        || j                        }d|j
                  z  }	t        |      D 
cg c]  }
|
|	z  	 }}
||k  r2t        |      D cg c]  }t        j                  ||z  |z         }}nBg }d}d|z  }t        |      D ]+  }||   |k\  s||z  }|j                  |       |||z
  k\  s+ n t        |      |k  rVt        |      dk(  rdt        |dz
  d      }}n
|d   |d   }}t        j                   |||t              j#                         }n<t        |      |kD  r.t        j                   d|dz
  |t              j#                         }t%               g }}|D ])  }||vs|j'                  |       |j                  |       + t        |      dz  r|j                  |d          t        j(                  |      S c c}
w c c}w )a  
        Args:
            metadata (`VideoMetadata`):
                Metadata of the video containing information about total duration, fps and total number of frames.
            fps (`int` or `float`, *optional*):
                Target frames to sample per second. Defaults to `self.fps`.
        Returns:
            np.ndarray:
                Indices to sample video frames.
        Nr@   zAsked to sample frames per second but no video metadata was provided which is required when sampling in Glmga. Please pass in `VideoMetadata` object or set `do_sample_frames=False`r   r   )dtype)getattrr>   total_num_framesdurationroundr@   r$   minr   rangemathfloorappendlenmaxnplinspacetolistsetaddarray)r7   r?   r@   r3   total_framesmax_frame_idxrF   
target_fps	extract_tduration_per_framei
timestamps_iframe_indicescurrent_secondinv_fpsframe_indexstartendseenuniqidxs                         r(   sample_framesz!GlmgaVideoProcessor.sample_framese   sP     wx=EX 
  00$q($$Omhll.J(Ka(OOS
:-.		4??3	-6;L6IJa,,J
J)#QVW`Qab2TZZ\(9I(EFbMbMN*nG$\2 k*n<"g-N!((5%G);; }	)=!Q&L1$4a 8s*1-}R/@sKKsISIPPRM)+KK<!+;YcRYY[MUBd  	!C$C 	!
 t9q=KKR!xx~C K cs   %H="Igp?videosdo_convert_rgb	do_resizer;   resamplez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stdr   r   r   r   return_tensorsc                    t        |      \  }}i }|j                         D ]  \  }}|r| j                  |      }|j                  \  }}}}}|||}}}|rzt	        ||||||z  |z  |j
                  |j                        \  }}|j                  ||z  |||      }| j                  |t        ||      |      }|j                  |||||      }|||<    t        ||      } t        |       \  }}i }!i }"|j                         D ]  \  }}t        |d   t        j                        \  }}| j                  |||||	|
      }|}#|#j                  d   |z  dk7  r:|#d d dd f   j                  d|dz
  ddd      }$t!        j"                  |#|$gd      }#|#j                  d d	 \  }%}&}'|&|z  }&||z  ||z  })}(|#j                  |%|&||'|(|z  |||)|z  ||
      }#|#j%                  ddd
dddd	ddd
      }#|#j'                  |%|&|(z  |)z  |'|z  |z  |z        }*|*|!|<   |&|(|)gg|%z  |"|<     t        |!|      }+t        |"|      }"t!        j"                  |+d      },t!        j(                  |"      }-|,|-d}.t+        |.|      S )N)
num_framesheightwidthtemporal_factorfactor
min_pixels
max_pixels)rs   rt   )r;   rj   r   )channel_dimr   rB   )dimr               r/      	   )r1   r2   )datatensor_type)r   itemsconvert_to_rgbshaper   r,   r-   viewresizer
   r   r   r   FIRSTrescale_and_normalizerepeattorchcatpermutereshapetensorr   )/r7   rg   rh   ri   r;   rj   rk   rl   rm   rn   ro   r   r   r   r   rp   r3   grouped_videosgrouped_videos_indexresized_videos_groupedr   stacked_videosBTCHWrr   rs   rt   resized_heightresized_widthresized_videosprocessed_videos_groupedprocessed_gridspatchesrepeats
batch_sizegrid_tchannelgrid_hgrid_wflatten_patchesprocessed_videosr1   r2   r   s/                                                  r(   _preprocesszGlmgaVideoProcessor._preprocess   s7   & 0EV/L,,!#%3%9%9%; 	;!E>!%!4!4^!D*00MAq!Q()1aJ0<)!$7%
25HH#11#001- "0!4!4QUAq!!D!%"!}M% "- "
 "0!4!4Q1nm!\,:"5)-	;. ((>@TU 0E^/T,,#% %3%9%9%; %	M!E>,:>!;LZjZpZp,q)NM "77
NL*V_N %G }}Q"55:!!RS&/004G!4KQPQSTU))Wg$6A>*1--*;'J22F+z9=J;VFFll#*$*$G ooaAq!Q1aCG%oo&(--
:ZGO />$U+'-vv&>%?*%LOE"K%	MN **BDXY(:NO#ii(8a@o6#6,

 >BBr'   )N)-r   r    r!   r	   BICUBICrj   r;   r   r   rn   r   ro   ri   rk   rm   rh   do_sample_framesr   r   max_durationr   r   valid_kwargsrr   r@   model_input_namesr   r   r   r6   r"   r<   r   r$   floatrf   listr   Tensorboolr
   r#   r   r   __classcell__)r8   s   @r(   r+   r+   4   s    "))H&8KLD$&9:N!JIIJLNJLJ0LJ
C.0@AJ#(E!F #	t 	 #'AA 5[4AL  $ $N`NhNh )!1504*.!%*.!%26!dCU\\"dC dC 	dC
 odC LdC dC dC dC DK'$.dC 4;&-dC !4ZdC $JdC !4ZdC $JdC  j(4/!dCr'   r+   )"rJ   numpyrO   r   torchvision.transforms.v2r   tvFimage_processing_utilsr   image_utilsr   r   r   r	   r
   r   processing_utilsr   r   utilsr   r   video_processing_utilsr   r   video_utilsr   r   r   image_processing_glmgar   r   r+   __all__r&   r'   r(   <module>r      s   *    7 2  5 5 X O O 0L  l"LC, LCLC^ !
!r'   