
     `i^                        d Z ddlZddlmZmZ ddlZddlmZm	Z	 ddl
mZmZ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 ddlmZmZ dd	lmZ  ej         e!          Z"	 	 	 	 dde#de#de#de#de#de#de#fdZ$ G d de          Z%dgZ&dS )z#Image processor class for GLM-4.1V.    N)OptionalUnion   )BaseImageProcessorBatchFeature)convert_to_rgbresizeto_channel_dimension_format)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingget_image_sizeinfer_channel_dimension_formatis_scaled_imagemake_flat_list_of_imagesto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypelogging)
VideoInput       1     
num_framesheightwidthtemporal_factorfactor
min_pixels
max_pixelsc                 x   | |k     rt          d|  d|           ||k     s||k     rt          d| d| d|           t          ||          t          ||          z  dk    r1t          dt          ||          t          ||          z             t          ||z            |z  }t          ||z            |z  }t          | |z            |z  }	|	|z  |z  |k    rtt	          j        | |z  |z  |z            }
t          |t	          j        ||
z  |z            |z            }t          |t	          j        ||
z  |z            |z            }nc|	|z  |z  |k     rWt	          j        || |z  |z  z            }
t	          j        ||
z  |z            |z  }t	          j        ||
z  |z            |z  }||fS )Nzt:z% must be larger than temporal_factor:zheight:z
 or width:z must be larger than factor:   z4absolute aspect ratio must be smaller than 200, got )
ValueErrormaxminroundmathsqrtfloorceil)r   r   r    r!   r"   r#   r$   h_barw_bart_barbetas              /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/glm4v/image_processing_glm4v.pysmart_resizer4   1   s    O##`j````aaa%&..`6``U``X^``aaa	VU		c&%00	03	6	6l3vuCUCUX[\bdiXjXjCjll
 
 	
 &6/""V+E%&.!!F*E*.///AEu}uz))y*v-5CDDFDJv}v'=>>GHHFDJut|f'<==FGG			+	+yzF':U'BCDD	&4-&011F:	%$,/0069%<    c            &       .    e Zd ZdZddgZddej        dddddddddfd	ed
ee	e
ef                  dededeeef         dedeeeee         f                  deeeee         f                  dededededdf fdZddddddddddddej        dfdeeef         d	ee         d
ee	e
ef                  dee         dee         dee         dee         deeeee         f                  deeeee         f                  dee         dee         dee         dee         dee         deee
ef                  fdZddddddddddddddej        dfdedee         d	ee         d
ee	e
ef                  dee         dee         dee         dee         deeeee         f                  deeeee         f                  dee         dee         dee         dee         deee
ef                  dee         deee
ef                  f"dZd!dedefd Z xZS )"Glm4vImageProcessora
  
    Constructs a GLM-4V image processor that dynamically resizes images based on the original images.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image's (height, width) dimensions.
        size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 112 * 112, "longest_edge": 28 * 28 * 15000}`):
            Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter
            in the `preprocess` method. Available options are:
                - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                    Do NOT keep the aspect ratio.
                - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                    the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                    less or equal to `longest_edge`.
                - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                    aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                    `max_width`.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`):
            Resampling filter to use when resizing the image.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image by the specified scale `rescale_factor`.
        rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
            Scale factor to use if rescaling the image.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image.
        image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`):
            Mean to use if normalizing the image. This is a float or list of floats for each channel in the image.
        image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`):
            Standard deviation to use if normalizing the image. This is a float or list of floats for each channel in the image.
        do_convert_rgb (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to RGB.
        patch_size (`int`, *optional*, defaults to 14):
            The spatial 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.
    pixel_valuesimage_grid_thwTNgp?   r   	do_resizesizeresample
do_rescalerescale_factordo_normalize
image_mean	image_stddo_convert_rgb
patch_sizetemporal_patch_size
merge_sizereturnc                 F    t                      j        di | |d|vsd|vrt          d          |ddd}|| _        || _        || _        || _        || _        || _        ||nt          | _
        ||nt          | _        |
| _        || _        || _        |	| _        d S )Nshortest_edgelongest_edge:size must contain 'shortest_edge' and 'longest_edge' keys.r   q rI   rJ    )super__init__r'   r<   r;   r=   r>   r?   r@   r   rA   r   rB   rD   rE   rF   rC   )selfr;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   kwargs	__class__s                 r3   rP   zGlm4vImageProcessor.__init__|   s      	""6"""!<!<VZ@Z@ZYZZZ\%.PPD	" $,((2(>**DT&/&;$#6 $,r5   imagesdata_formatinput_data_formatc                    t          |          }|rd |D             }d |D             }|r/t          |d                   rt                              d           |t	          |d                   }t          |d         |          \  }}||}}g }|D ]}|r=t          |||||
|z  |d         |d         	          \  }}t          |||f||
          }|r|                     |||          }|r| 	                    |||	|          }t          |||          }|                    |           t          j        |          }|t          j        k    r|                    dddd          }|j        d         |z  dk    rVt          j        |d         t          j                 ||j        d         |z  z
  d          }t          j        ||gd          }|j        d         }|j        d         |z  }||
z  ||
z  }}|                    |||||z  ||
||z  ||
	  	        }|                    ddddddddd	  	        }|                    ||z  |z  ||z  |
z  |
z            }||||ffS )a~  
        Preprocess an image or batch of images. Copy of the `preprocess` method from `CLIPImageProcessor`.

        Args:
            images (`ImageInput`):
                Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`.
            vision_info (`List[Dict]`, *optional*):
                Optional list of dictionaries containing additional information about vision inputs.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`Dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present.
            resample (`PILImageResampling`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the `PILImageResampling` enums.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Scale factor to use if rescaling the image.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`):
                Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
            image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`):
                Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
            patch_size (`int`, *optional*, defaults to `self.patch_size`):
                The spatial patch size of the vision encoder.
            temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
                The temporal patch size of the vision encoder.
            merge_size (`int`, *optional*, defaults to `self.merge_size`):
                The merge size of the vision encoder to llm encoder.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            data_format (`ChannelDimension`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.   - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        c                 ,    g | ]}t          |          S rN   )r   .0images     r3   
<listcomp>z3Glm4vImageProcessor._preprocess.<locals>.<listcomp>   s     @@@nU++@@@r5   c                 ,    g | ]}t          |          S rN   )r   rY   s     r3   r\   z3Glm4vImageProcessor._preprocess.<locals>.<listcomp>   s     <<<E.''<<<r5   r   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.N)channel_dimrI   rJ   )r   r   r    r!   r"   r#   r$   )r<   r=   rV   )scalerV   )r[   meanstdrV   )input_channel_dimr      r   )axis               )r   r   loggerwarning_oncer   r   r4   r	   rescale	normalizer
   appendnparrayr   LAST	transposeshaperepeatnewaxisconcatenatereshape)rQ   rT   r;   r<   r=   r>   r?   r@   rA   rB   rD   rE   rF   rC   rU   rV   r   r    resized_heightresized_widthprocessed_imagesr[   patchesrepeatschannelgrid_tgrid_hgrid_wflatten_patchess                                r3   _preprocesszGlm4vImageProcessor._preprocess   s/   z *&11 	A@@@@@F =<V<<< 	/&)44 	s   $ >vay I I&vay>OPPP(. 	+ 	+E 0<2!$7%
2#O4#N31 1 1-  ?(fw    gU.Teff jiSd '   0{VghhhE##E****(+,,*///''1a33G=11Q66iBJ')<a@PSf@f)gno  G ngw%7a@@@G-"q!%88':5}
7R//j j 

 

 ##Aq!Q1aA>>!//VOf$g0C&Cj&PS]&]
 
  888r5   videosreturn_tensorsc                 `   ||n| j         }|d|vsd|vrt          d          |ddd}||n| j        }||n| j        }||n| j        }||n| j        }||n| j        }|	|	n| j        }	|
|
n| j        }
||n| j	        }||n| j
        }||n| j        }||n| j        }|$|                     |          }t          |          }|t          |          st          d          t!          |||	|
|||	           i }|g g }}|D ]S}|                     ||||||||	|
||||||
          \  }}|                    |           |                    |           Tt)          j        |          }t)          j        |          }|                    ||d           t/          ||          S )a  
        Args:
            images (`ImageInput`):
                Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
                passing in images with pixel values between 0 and 1, set `do_rescale=False`.
            videos (`VideoInput`):
                Video to preprocess. Expects a single or batch of videos with pixel values ranging from 0 to 255. If
                passing in videos with pixel values between 0 and 1, set `do_rescale=False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`Dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with
                the longest edge resized to keep the input aspect ratio.
            resample (`int`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
                has an effect if `do_resize` is set to `True`.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`):
                Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
            image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`):
                Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
                `True`.
                The max pixels of the image to resize the image.
            patch_size (`int`, *optional*, defaults to `self.patch_size`):
                The spatial patch size of the vision encoder.
            temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
                The temporal patch size of the vision encoder.
            merge_size (`int`, *optional*, defaults to `self.merge_size`):
                The merge size of the vision encoder to llm encoder.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.

        NrI   rJ   rK   r   rL   rM   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r?   r@   rA   rB   r;   r<   r=   )r;   r<   r=   r>   r?   r@   rA   rB   rD   rE   rF   rU   rC   rV   )r8   r9   )datatensor_type)r<   r'   r;   r=   r>   r?   r@   rA   rB   rD   rE   rF   rC   fetch_imagesr   r   r   r   extendro   rp   rq   updater   )rQ   rT   r   r;   r<   r=   r>   r?   r@   rA   rB   rD   rE   rF   rC   r   rU   rV   r   r8   vision_grid_thwsr[   r|   r9   s                           r3   
preprocesszGlm4vImageProcessor.preprocess(  st   Z 'ttTY!<!<VZ@Z@ZYZZZ\%.PPD!*!6IIDN	'388#-#9ZZt
+9+E4K^'3'?||TEV#-#9ZZt
!*!6IIDN	#-#9ZZt
5H5T11Z^Zr#-#9ZZt
+9+E4K^&&v..F-f55Fl6&:&::  
 	&)%!	
 	
 	
 	
 -/*L 8 8*.*:*:'%)#1!-)')(;) +#1&7 +; + +'" ##G,,, ''77778L11L!x(899KKIYZZ[[[>BBBBr5   r   r    c           	      &   |                     d| j                  }|                     d| j                  }|                     dddd          }||z  }t          | j        ||||d         |d         | j        	          \  }}	||z  |	|z  }}
|
|z  S )
a  
        A utility that returns number of image patches for a given image size.

        Args:
            height (`int`):
                Height of the input image.
            width (`int`):
                Width of the input image.
            images_kwargs (`dict`, *optional*)
                Any kwargs to override defaults of the image processor.
        Returns:
            `int`: Number of image patches per image.
        rD   rF   r<   r   rL   rM   rI   rJ   )r   r   r    r"   r#   r$   r!   )getrD   rF   r4   rE   )rQ   r   r    images_kwargsrD   rF   r<   r"   ry   rz   r   r   s               r3   get_number_of_image_patchesz/Glm4vImageProcessor.get_number_of_image_patches  s     #&&|T_EE
"&&|T_EE
  9Ve)f)fggj((4/O,N+ 4)
 )
 )
% (:5}
7Rr5   )N)__name__
__module____qualname____doc__model_input_namesr   BICUBICboolr   dictstrintr   floatlistrP   r   FIRSTr   r   r   r   r   r   __classcell__)rS   s   @r3   r7   r7   R   sw       % %N ()9: )-'9'A,3!:>9=##$"- "-"- tCH~&"- %	"-
 "- c5j)"- "- U5$u+#567"- E%e"456"- "- "- !"- "- 
"- "- "- "- "- "-N %))-15%)*.'+:>9=$(-1$()-2B2HDH!F9 F9j*,-F9 D>F9 tCH~&	F9
 -.F9 TNF9 !F9 tnF9 U5$u+#567F9 E%e"456F9 SMF9 &c]F9 SMF9 !F9 ./F9  $E#/?*?$@A!F9 F9 F9 F9V (,$()-15%)*.'+:>9=$(-1$()-;?2B2HDH%NC NCNC $NC D>	NC
 tCH~&NC -.NC TNNC !NC tnNC U5$u+#567NC E%e"456NC SMNC &c]NC SMNC !NC  !sJ!78!NC" ./#NC$ $E#/?*?$@A%NC NC NC NC` # c        r5   r7   )r   r   r   r   )'r   r+   typingr   r   numpyrp   image_processing_utilsr   r   image_transformsr   r	   r
   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   utilsr   r   video_utilsr   
get_loggerr   rk   r   r4   r7   __all__rN   r5   r3   <module>r      s   * )  " " " " " " " "     F F F F F F F F         
                            ) ( ( ( ( ( ( ( % % % % % % 
	H	%	% 0   	
      BC C C C C, C C CL !
!r5   