
    +gd9                        U d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZ d dlZd dlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ e	r
d dl Z!ddl"m#Z# da$eee%                  e&d<   ej'        dk    rdndZ( ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d           ej)        d          gZ*e G d d                       Z+d!ee%         fd"Z,d#d$d!e-fd%Z.d#d$d!e/fd&Z0d'ej1        d!e/fd(Z2d)eee%         ee/         eej1                 ed$         f         d!ee/         fd*Z3dS )+    N)	dataclassfield)BytesIO)TYPE_CHECKINGAnyClassVarDictListOptionalUnion   )config)xopen)
array_cast)is_local_path)first_non_null_valueno_op_if_value_is_nullstring_to_dict   )FeatureType_IMAGE_COMPRESSION_FORMATSlittle<>z|b1|u1z<u2z>u2z<i2z>i2z<u4z>u4z<i4z>i4z<f4z>f4z<f8z>f8c                      e Zd ZU dZdZeed<   dZee	         ed<   dZ
ee	         ed<    ej         ej                     ej                    d          Zee         ed	<    ed d
d
          Ze	ed<   d Zdee	eeej        df         defdZddeddfdZdedee	df         f         fdZdeej        ej        ej         f         dej        fdZ!dej        dej        fdZ"dS )Imagea  Image [`Feature`] to read image data from an image file.

    Input: The Image feature accepts as input:
    - A `str`: Absolute path to the image file (i.e. random access is allowed).
    - A `dict` with the keys:

        - `path`: String with relative path of the image file to the archive file.
        - `bytes`: Bytes of the image file.

      This is useful for archived files with sequential access.

    - An `np.ndarray`: NumPy array representing an image.
    - A `PIL.Image.Image`: PIL image object.

    Args:
        decode (`bool`, defaults to `True`):
            Whether to decode the image data. If `False`,
            returns the underlying dictionary in the format `{"path": image_path, "bytes": image_bytes}`.

    Examples:

    ```py
    >>> from datasets import load_dataset, Image
    >>> ds = load_dataset("beans", split="train")
    >>> ds.features["image"]
    Image(decode=True, id=None)
    >>> ds[0]["image"]
    <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x500 at 0x15E52E7F0>
    >>> ds = ds.cast_column('image', Image(decode=False))
    {'bytes': None,
     'path': '/root/.cache/huggingface/datasets/downloads/extracted/b0a21163f78769a2cf11f58dfc767fb458fc7cea5c05dccc0144a2c0f0bc1292/train/healthy/healthy_train.85.jpg'}
    ```
    TdecodeNidPIL.Image.Imagedtypebytespathpa_typeF)defaultinitrepr_typec                     | j         S N)r%   )selfs    7lib/python3.11/site-packages/datasets/features/image.py__call__zImage.__call__X   s
    |    valuereturnc                    t           j        rddl}nt          d          t	          |t
                    rt          j        |          }t	          |t                    r|ddS t	          |t                    rd|dS t	          |t          j
                  rt          |          S t	          ||j        j                  rt          |          S |                    d          =t          j                            |d                   rd|                    d          dS |                    d          |                    d          +|                    d          |                    d          dS t%          d| d	          )
a   Encode example into a format for Arrow.

        Args:
            value (`str`, `np.ndarray`, `PIL.Image.Image` or `dict`):
                Data passed as input to Image feature.

        Returns:
            `dict` with "path" and "bytes" fields
        r   N4To support encoding images, please install 'Pillow'.r$   r#   r$   r"   r#   zUAn image sample should have one of 'path' or 'bytes' but they are missing or None in .)r   PIL_AVAILABLE	PIL.ImageImportError
isinstancelistnparraystrr#   ndarrayencode_np_arrayr   encode_pil_imagegetosr$   isfile
ValueError)r,   r0   PILs      r-   encode_examplezImage.encode_example[   s     	VTUUUeT"" 	$HUOOEeS!! 	!D111u%% 	 5111rz** 	"5)))sy// 	#E***YYv*rw~~eFm/L/L*!599V+<+<===YYw+uyy/@/@/L"YYw//69J9JKKKphmppp  r/   c                 (   | j         st          d          t          j        rddl}nt          d          |i }|d         |d         }}|	|t          d| d          t          |          r|j        	                    |          }n|
                    d	          d
         }	 t          |t          j                  d         }|                    |          }	n# t          $ r d}	Y nw xY wt          |d|	          5 }
t          |
                                          }ddd           n# 1 swxY w Y   |j        	                    |          }n'|j        	                    t          |                    }|                                 |S )aq  Decode example image file into image data.

        Args:
            value (`str` or `dict`):
                A string with the absolute image file path, a dictionary with
                keys:

                - `path`: String with absolute or relative image file path.
                - `bytes`: The bytes of the image file.
            token_per_repo_id (`dict`, *optional*):
                To access and decode
                image files from private repositories on the Hub, you can pass
                a dictionary repo_id (`str`) -> token (`bool` or `str`).

        Returns:
            `PIL.Image.Image`
        zMDecoding is disabled for this feature. Please use Image(decode=True) instead.r   Nz4To support decoding images, please install 'Pillow'.r$   r#   zCAn image should have one of 'path' or 'bytes' but both are None in r5   z::repo_idrb)use_auth_token)r   RuntimeErrorr   r6   r7   r8   rD   r   r   opensplitr   HUB_DATASETS_URLrA   r   r   readload)r,   r0   token_per_repo_idrE   r$   bytes_image
source_urlrI   rK   fs              r-   decode_examplezImage.decode_example   s   $ { 	pnooo 	VTUUU$ "V}eGnf>| !ogl!o!o!oppp && 3INN400EE!%D!1!1"!5J."0V=T"U"UV_"`):)>)>w)G)G% . . .)-.tT.III 3Q!(!2!23 3 3 3 3 3 3 3 3 3 3 3 3 3 3INN622EEINN76??33E

s$   )5C C.-C."D11D58D5r   c                 N    ddl m} | j        r| n |d           |d          dS )zfIf in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary.r   )Valuebinarystringr"   )featuresrY   r   )r,   rY   s     r-   flattenzImage.flatten   sK    ###### {DD xh 	
r/   storagec                    t           j                            |j                  rrt          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }nt           j        
                    |j                  rrt          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }nt           j                            |j                  r|j                            d          dk    r|                    d          }n8t          j        dgt          |          z  t          j                              }|j                            d          dk    r|                    d          }n8t          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }nt           j                            |j                  rt          j        d |                                D             t          j                              }t          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }t#          || j                  S )a'  Cast an Arrow array to the Image arrow storage type.
        The Arrow types that can be converted to the Image pyarrow storage type are:

        - `pa.string()` - it must contain the "path" data
        - `pa.binary()` - it must contain the image bytes
        - `pa.struct({"bytes": pa.binary()})`
        - `pa.struct({"path": pa.string()})`
        - `pa.struct({"bytes": pa.binary(), "path": pa.string()})`  - order doesn't matter
        - `pa.list(*)` - it must contain the image array data

        Args:
            storage (`Union[pa.StringArray, pa.StructArray, pa.ListArray]`):
                PyArrow array to cast.

        Returns:
            `pa.StructArray`: Array in the Image arrow storage type, that is
                `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
        Ntyper#   r$   maskr   c                 d    g | ]-}|'t          t          j        |                    d         nd .S )Nr#   )r?   r;   r<   ).0arrs     r-   
<listcomp>z&Image.cast_storage.<locals>.<listcomp>   s8    uuuZ]CO#//88QUuuur/   )patypes	is_stringra   r<   lenrZ   StructArrayfrom_arraysis_null	is_binaryr[   	is_structget_field_indexr   is_list	to_pylistr   r%   )r,   r^   bytes_array
path_arrays       r-   cast_storagezImage.cast_storage   s   & 8gl++ 	(D6CLL#8ry{{KKKKn00+w1G'SYIZahapaparar0ssGGX-- 	4&3w<<"7bikkJJJJn00':1FRXHY`g`o`o`q`q0rrGGX-- 	|++G4499%mmG44 hvG'<29;;OOO|++F33q88$]]622

Xtfs7||&;")++NNN
n00+z1JWV\L]dkdsdsdudu0vvGGXgl++ 	(uuahararatatuuuY[[  K 4&3w<<"7bikkJJJJn00j)GV+<;CVCVCXCX 1  G '4<000r/   c                    t           d             t          j        fd|                                D             t          j                              }t          j        d |                    d                                          D             t          j                              }t          j                            ||gddg|	                                          }t          || j                  S )a8  Embed image files into the Arrow array.

        Args:
            storage (`pa.StructArray`):
                PyArrow array to embed.

        Returns:
            `pa.StructArray`: Array in the Image arrow storage type, that is
                `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
        c                 ~    t          | d          5 }|                                }d d d            n# 1 swxY w Y   |S )NrJ   )r   rP   )r$   rV   rS   s      r-   path_to_bytesz*Image.embed_storage.<locals>.path_to_bytes   sv    tT"" "a" " " " " " " " " " " " " " "Ms   266c                 Z    g | ]'}|!|d          |d                   n	|d         nd (S )Nr#   r$    )re   xry   s     r-   rg   z'Image.embed_storage.<locals>.<listcomp>  sU        UVTaQwZ-?qy)))QwZZgk  r/   r`   c                 T    g | ]%}|t           j                            |          nd &S r+   )rB   r$   basename)re   r$   s     r-   rg   z'Image.embed_storage.<locals>.<listcomp>
  s2    pppdt'7RWd###Tpppr/   r$   r#   rb   )r   rh   r<   rs   rZ   r   r[   rl   rm   rn   r   r%   )r,   r^   rt   ru   ry   s       @r-   embed_storagezImage.embed_storage   s    
 	 	 
 		
 h    **,,   
 
 
 Xppgmm\bNcNcNmNmNoNoppp
 
 

 .,,k:-FRXHY`k`s`s`u`u,vv'4<000r/   r+   )#__name__
__module____qualname____doc__r   bool__annotations__r   r   r=   r!   r   rh   structrZ   r[   r%   r   r   r)   r.   r   r#   dictr;   r>   rF   rW   r	   r]   StringArrayrl   	ListArrayrv   r   r{   r/   r-   r   r   -   s           D FDB,E8C=,,,&RYibikk'R'RSSGXc]SSSwU???E3???  %E#udBJHY*Y$Z %_c % % % %N1 1D 1EV 1 1 1 1f
}d33E.FFG 
 
 
 
,1E".".",*V$W ,1\^\j ,1 ,1 ,1 ,1\1R^ 1 1 1 1 1 1 1r/   r   r1   c                  X   t           j        rdd l} nt          d          t          }| j                                         t          t          | j        j	        
                                          t          | j        j        
                                          z            at          S )Nr   r3   )r   r6   r7   r8   r   r   r'   r:   setOPENkeysSAVE)rE   s    r-   list_image_compression_formatsr     s     RPQQQ ")	%)#cin.A.A.C.C*D*Ds39>K^K^K`K`GaGa*a%b%b"%%r/   rT   r    c                     t                      }| j        t                      v r| j        }n| j        dv rdnd}|                     ||           |                                S )zmConvert a PIL Image object to bytes using native compression if possible, otherwise use PNG/TIFF compression.)1LLARGBRGBAPNGTIFF)format)r   r   r   modesavegetvalue)rT   bufferr   s      r-   image_to_bytesr     sd    YYF|57777*(GGGV	JJvfJ%%%??r/   c                 p    t          | d          r| j        dk    r
| j        d dS d t          |           dS )Nfilename r4   )hasattrr   r   )rT   s    r-   r@   r@   )  sF    uj!! >en&:&:666~e'<'<===r/   r<   c                    t           j        rdd l}nt          d          | j        }|j        dk    r|j        nt          }|j        }|j        }d }| j	        dd          rNt          j        d          }|dvrt          d| d| d	          ||urt          j        d
| d| d           nk|t          v r|}n_|dk    rY||z   t          |          z   }t          j        |          }|t          v rt          j        d
| d| d           n|dz  }|dk    Y|t          d| dt                     |j                            |                     |                    }d t'          |          dS )Nr   r3   =r   r   )uizUnsupported array dtype z for image encoding. Only z' is supported for multi-channel arrays.zDowncasting array dtype z to z to be compatible with 'Pillow'r   zCannot convert dtype z- to a valid image dtype. Valid image dtypes: r4   )r   r6   r7   r8   r!   	byteorder_NATIVE_BYTEORDERkinditemsizeshaper;   	TypeErrorwarningswarn_VALID_IMAGE_ARRAY_DTPYESr=   r   	fromarrayastyper   )	r<   rE   r!   dtype_byteorder
dtype_kinddtype_itemsize
dest_dtypedest_dtype_strrT   s	            r-   r?   r?   0  s    RPQQQKE).C)?)?eooEVOJ^NJ {122 %Xe__
Z''5J   
""MkUkk
kkklll	+	+	+

!!,z9C<O<OON.11J666oooJoooppp1$ !! sEssXqss
 
 	
 IZ 8 899E>%#8#8999r/   objsc                    t           j        rddl}nt          d          | rt	          |           \  }}t          |t                    rd | D             S t          |t          j                  r"t          t                    fd| D             S t          ||j        j                  r"t          t                    fd| D             S | S | S )zmEncode a list of objects into a format suitable for creating an extension array of type `ImageExtensionType`.r   Nr3   c                      g | ]}||d dnd S )Nr4   r{   )re   objs     r-   rg   z2objects_to_list_of_image_dicts.<locals>.<listcomp>g  s*    ^^^RUCOS4000^^^r/   c                 &    g | ]} |          S r{   r{   re   r   obj_to_image_dict_funcs     r-   rg   z2objects_to_list_of_image_dicts.<locals>.<listcomp>j  %    @@@C**3//@@@r/   c                 &    g | ]} |          S r{   r{   r   s     r-   rg   z2objects_to_list_of_image_dicts.<locals>.<listcomp>m  r   r/   )r   r6   r7   r8   r   r9   r=   r;   r>   r   r?   r   r@   )r   rE   _r   r   s       @r-   objects_to_list_of_image_dictsr   [  s      RPQQQ %d++3c3 	_^^Y]^^^^c2:&& 	%;O%L%L"@@@@4@@@@SY_-- 	%;<L%M%M"@@@@4@@@@Kr/   )4rB   sysr   dataclassesr   r   ior   typingr   r   r   r	   r
   r   r   numpyr;   pyarrowrh   r   r   #download.streaming_download_managerr   tabler   utils.file_utilsr   utils.py_utilsr   r   r   r7   rE   r\   r   r   r=   r   r   r   r!   r   r   r   r#   r   r   r@   r>   r?   r   r{   r/   r-   <module>r      s6   					 



  ( ( ( ( ( ( ( (       L L L L L L L L L L L L L L L L L L               7 7 7 7 7 7       , , , , , , Y Y Y Y Y Y Y Y Y Y  &%%%%%% 37 HT#Y/ 6 6 6=H44CC#  BHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOOBHUOO $ `1 `1 `1 `1 `1 `1 `1 `1F
&S	 
& 
& 
& 
&+     >- >$ > > > >(:2: (:$ (: (: (: (:V
S	4:tBJ'7>O9PP
Q	$Z     r/   