
    rh$                         d Z ddlmZmZmZ ddl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mZmZmZ ddlmZmZ g d	Zg d
Zd Z G d de      ZdgZy)z"Image processor class for Idefics.    )CallableOptionalUnion)Image   )BaseImageProcessorBatchFeature)resizeto_channel_dimension_format)ChannelDimension
ImageInputPILImageResamplingmake_list_of_imagesto_numpy_arrayvalid_images)
TensorTypeis_torch_available)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?c                     | j                   dk(  r| S | j                  d      }t        j                  d|j                  d      }t        j
                  ||      }|j                  d      }|S )NRGBRGBA)   r   r   )modeconvertr   newsizealpha_composite)image
image_rgba
backgroundr   s       /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/models/idefics/image_processing_idefics.pyconvert_to_rgbr!   &   sa     zzUv&J6:??ODJ++J
CO%--e4O    c                   J    e Zd ZdZdgZ	 	 	 	 	 	 ddedeeee	e   f      deeee	e   f      dee   d	e
d
eeef   ddf fdZdddddddej                  fdedee   deeeef      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eef      defdZ xZS )IdeficsImageProcessora  
    Constructs a Idefics image processor.

    Args:
        image_size (`int`, *optional*, defaults to 224):
            Resize to image size
        image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`):
            Mean to use if normalizing the image. This is a float or list of floats the length of the number of
            channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be
            overridden by the `image_mean` parameter in the `preprocess` method.
        image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`):
            Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
            number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method.
            Can be overridden by the `image_std` parameter in the `preprocess` method.
        image_num_channels (`int`, *optional*, defaults to 3):
            Number of image channels.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
            the `preprocess` method.
        rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
            Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
            method.
    pixel_valuesNr   
image_size
image_mean	image_stdimage_num_channels
do_rescalerescale_factorreturnc                     t        |   di | || _        || _        ||nt        | _        ||nt        | _        || _        || _	        y )N )
super__init__r&   r)   IDEFICS_STANDARD_MEANr'   IDEFICS_STANDARD_STDr(   r*   r+   )	selfr&   r'   r(   r)   r*   r+   kwargs	__class__s	           r    r0   zIdeficsImageProcessor.__init__N   sQ     	"6"$"4(2(>*DY&/&;AU$,r"   images	transformreturn_tensorsc
                    ||n| j                   }||n| j                  }||n| j                  }||n| j                  }||n| j                  }||n| j
                  }||f}t        |t              rt        |      dk(  rg S t        |      }t        |      st        d      |?t               st        d      ddl}|D cg c]
  } ||       }}|j                  |      S |D cg c]  }t!        |       }}|D cg c]  }t#        |       }}|D cg c]  }t%        ||t&        j(                          }}|D cg c]  }| j+                  ||       }}|D cg c]  }| j-                  |||       }}|D cg c]  }t/        |t0        j2                         }}t5        d|i|		      d   }|S c c}w c c}w c c}w c c}w c c}w c c}w c c}w )
a|  
        Preprocess a batch of images.

        Args:
            images (`ImageInput`):
                A list of images to preprocess.
            image_size (`int`, *optional*, defaults to `self.image_size`):
                Resize to image size
            image_num_channels (`int`, *optional*, defaults to `self.image_num_channels`):
                Number of image channels.
            image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`):
                Mean to use if normalizing the image. This is a float or list of floats the length of the number of
                channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can
                be overridden by the `image_mean` parameter in the `preprocess` method.
            image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`):
                Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
                number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess`
                method. Can be overridden by the `image_std` parameter in the `preprocess` method.
            transform (`Callable`, *optional*, defaults to `None`):
                A custom transform function that accepts a single image can be passed for training. For example,
                `torchvision.Compose` can be used to compose multiple transforms. If `None` - an inference mode is
                assumed - and then a preset of inference-specific transforms will be applied to the images
            do_rescale (`bool`, *optional*, defaults to `True`):
                Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
                the `preprocess` method.
            rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
                Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
                method.

        Returns:
            a PyTorch tensor of the processed images

        Nr   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.z.To pass in `transform` torch must be installed)resample)r   scale)meanstdr%   )datatensor_type)r&   r)   r'   r(   r*   r+   
isinstancelistlenr   r   
ValueErrorr   ImportErrortorchstackr!   r   r
   r   BICUBICrescale	normalizer   r   FIRSTr	   )r3   r6   r)   r&   r'   r(   r7   r*   r+   r8   r4   r   rE   xr   s                  r    
preprocessz IdeficsImageProcessor.preprocessa   s   \ $.#9Zt
3E3Q/W[WnWn#-#9Zt
!*!6IDNN	#-#9Zt
+9+E4K^K^J'fd#Fq(8I$V,F#:   %'!"RSS,23qil3F3;;v&& .44.#44-34.#44PVW1&D+=+E+EFWWOUVe$,,U.,AVVMST$...CTTRXYQ-a1A1G1GHYYNF#;XYgh 4 54WVTYs*   F<+GG#GG$G!G)   NNr   Tgp?)__name__
__module____qualname____doc__model_input_namesintr   r   floatrA   boolr0   r   PYTORCHr   dictstrr   rL   __classcell__)r5   s   @r    r$   r$   3   s   0 (( :>9=,-,3-- U5$u+#567- E%e"456	-
 %SM- - c5j)- 
-, -./3:>9=(,%)*.;E;M;M[[ %SM[ T#s(^,	[
 U5$u+#567[ E%e"456[ H%[ TN[ ![ !sJ!78[ 
[r"   r$   N)rQ   typingr   r   r   PILr   image_processing_utilsr   r	   image_transformsr
   r   image_utilsr   r   r   r   r   r   utilsr   r   r1   r2   r!   r$   __all__r.   r"   r    <module>ra      sU    ) , ,  F C  4 < ; 
I. IX #
#r"   