Ë
    ©sgT  ã                   ó¬   — d Z ddlmZmZ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y)z"Image processor class for Idefics.é    )ÚCallableÚDictÚListÚ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{Îå Ú?)g‡Bô91Ñ?g•wÝt.¹Ð?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       úg/var/www/html/venv/lib/python3.12/site-packages/transformers/models/idefics/image_processing_idefics.pyÚconvert_to_rgbr#   &   sa   € ð ‡zzUÒØˆà—‘˜vÓ&€JÜ—‘˜6 :§?¡?°OÓD€JÜ×+Ñ+¨J¸
ÓC€OØ%×-Ñ-¨eÓ4€OØÐó    c                   ó  ‡ — 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	df
ˆ fd
„Z
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deeeef      d	efd„Zˆ xZS )ÚIdeficsImageProcessora1  
    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.
    Úpixel_valuesNr	   Ú
image_sizeÚ
image_meanÚ	image_stdÚimage_num_channelsÚreturnc                 ó\   •— t        ‰|   di |¤Ž || _        || _        || _        || _        y )N© )ÚsuperÚ__init__r(   r+   r)   r*   )Úselfr(   r)   r*   r+   ÚkwargsÚ	__class__s         €r"   r0   zIdeficsImageProcessor.__init__H   s2   ø€ ô 	‰ÑÑ"˜6Ò"à$ˆŒØ"4ˆÔØ$ˆŒØ"ˆr$   ÚimagesÚ	transformÚreturn_tensorsc                 ó~  — ||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¬«      ‘Œ }}|D cg c]  }| j)                  |||¬«      ‘Œ }}|D cg c]  }t+        |t,        j.                  «      ‘Œ }}t1        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

        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)Úresamplegp?)r   Úscale)ÚmeanÚstdr'   )ÚdataÚtensor_type)r(   r+   r)   r*   Ú
isinstanceÚlistÚlenr   r   Ú
ValueErrorr   ÚImportErrorÚtorchÚstackr#   r   r   r   ÚBICUBICÚrescaleÚ	normalizer   r   ÚFIRSTr   )r1   r4   r+   r(   r)   r*   r5   r6   r2   r   rC   Úxr   s                r"   Ú
preprocessz IdeficsImageProcessor.preprocessW   sÃ  € ðL $.Ð#9‘Z¸t¿¹ˆ
Ø3EÐ3QÑ/ÐW[×WnÑWnÐØ#-Ð#9‘Z¸t¿¹ˆ
Ø!*Ð!6‘I¸D¿N¹Nˆ	Ø˜JÐ'ˆäfœdÔ#¬¨F«°qÒ(8ØˆIä$ VÓ,ˆä˜FÔ#Üð:óð ð Ð Ü%Ô'Ü!Ð"RÓSÐSÛà,2Ö3 q‘i •lÐ3ˆFÐ3Ø—;‘;˜vÓ&Ð&ð .4Ö4¨”. Õ#Ð4ˆÐ4à-3Ö4¨”. Õ#Ð4ˆÐ4ØPVÖWÈ1”&˜˜DÔ+=×+EÑ+EÖFÐWˆÐWØHNÖO¸u$—,‘, U°',Õ:ÐOˆÐOØMSÖTÈ$—.‘. ¨¸.ÕCÐTˆÐTØRXÖYÈQÔ-¨aÔ1A×1GÑ1GÕHÐYˆÐYÜ N°FÐ#;ÈÔXÐYgÑhˆàˆùò 4ùò 5ùâ4ùÚWùÚOùÚTùÚYs*   Â%FÃF!Ã#F&Ã;#F+Ä$F0ÅF5Å%!F:)éà   NNr	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesÚintr   r   Úfloatr   r0   r   ÚPYTORCHr   r   Ústrr   rJ   Ú__classcell__)r3   s   @r"   r&   r&   3   s;  ø„ ñð$ (Ð(Ðð Ø:>Ø9=Ø,-ñ#àð#ð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ð#ð ˜E %¨¨e©Ð"4Ñ5Ñ6ð	#ð
 % S™Mð#ð 
õ#ð$ -.Ø/3Ø:>Ø9=Ø"Ø;E×;MÑ;MñQàðQð % S™MðQð ˜T # s (™^Ñ,ð	Qð
 ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ðQð ˜E %¨¨e©Ð"4Ñ5Ñ6ðQð ðQð !  s¨J Ñ!7Ñ8ðQð 
÷Qr$   r&   N)rO   Útypingr   r   r   r   r   ÚPILr   Úimage_processing_utilsr
   r   Úimage_transformsr   r   Úimage_utilsr   r   r   r   r   r   Úutilsr   r   ÚIDEFICS_STANDARD_MEANÚIDEFICS_STANDARD_STDr#   r&   r.   r$   r"   ú<module>r^      sJ   ðñ )ç 8Õ 8å ç Fß C÷÷ ÷ 4ò <Ð Ú;Ð ò
ôuÐ.õ ur$   