
    *#h;                        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 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jR                  dk(  rdndZ* ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d       ejV                  d      gZ,e G d  d!             Z-d"ee'   fd#Z.d$d%d"e/fd&Z0d$d%d"e1fd'Z2d(ejf                  d"e1fd)Z4d*eee'   ee1   eejf                     ed%   f   d"ee1   fd+Z5y),    N)	dataclassfield)BytesIO)TYPE_CHECKINGAnyClassVarDictListOptionalUnion   )config)DownloadConfig)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j2                  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"y)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    T/var/www/html/sandstorm/venv/lib/python3.12/site-packages/datasets/features/image.py__call__zImage.__call__Y   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 |j                  d      6t        j                   j#                  |d         rd|j                  d      dS |j                  d      |j                  d      #|j                  d      |j                  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-   r1   PILs      r.   encode_examplezImage.encode_example\   s    TUUeT"HHUOEeS!!D11u% 511rzz*"5))syy/#E**YYv*rww~~eFm/L!599V+<==YYw+uyy/@/L"YYw/69JKKghmgnnop r0   c                 @   | j                   st        d      t        j                  rddl}nt        d      |i }|d   |d   }}||t        d| d      t        |      r|j                  j                  |      }n|j                  d	      d
   }|j                  t        j                        rt        j                  nt        j                  }	 t        ||      d   }	|j!                  |	      }
t#        |
      }t%        |d|      5 }t'        |j)                               }ddd       |j                  j                  |      }n$|j                  j                  t'        |            }|j+                          |S # t        $ r d}
Y w xY w# 1 sw Y   lxY w)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 r6   z::repo_id)tokenrb)download_config)r   RuntimeErrorr   r7   r8   r9   rE   r   r   opensplit
startswithHF_ENDPOINTHUB_DATASETS_URLHUB_DATASETS_HFFS_URLr   rB   r   r   r   readload)r-   r1   token_per_repo_idrF   r%   bytes_image
source_urlpatternrJ   rK   rM   fs                r.   decode_examplezImage.decode_example   s{   $ {{nooTUU$ "V}eGnf>| #fglfmmn!opp &IINN40E!%D!1"!5J &001C1CD //#99 
%"0W"Ei"P 1 5 5g > '55&AOtT?K 3q!(!23IINN62EIINN76?3E

 & % $%3 3s    F FFFFr   c                 L    ddl m} | j                  r| S  |d       |d      dS )zfIf in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary.r   )Valuebinarystringr#   )featuresr_   r   )r-   r_   s     r.   flattenzImage.flatten   s2    # {{ 	
 xh	
r0   storagec           
         t         j                  j                  |j                        rlt        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }nt         j                  j                  |j                        rlt        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }n&t         j                  j                  |j                        r|j                  j                  d      dk\  r|j                  d      }n6t        j                  dgt        |      z  t        j                               }|j                  j                  d      dk\  r|j                  d      }n6t        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }nt         j                  j                  |j                        rt        j                  |j!                         D cg c]'  }|!t#        t%        j                  |            d   nd) c}t        j                               }t        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }t'        || j(                        S c c}w )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   )patypes	is_stringrg   r=   lenr`   StructArrayfrom_arraysis_null	is_binaryra   	is_structget_field_indexr   is_list	to_pylistr@   r<   r   r&   )r-   rd   bytes_array
path_arrayarrs        r.   cast_storagezImage.cast_storage   st   & 88gll+((D6CL#8ryy{KKnn00+w1G'SYIZahapapar0sGXX-4&3w<"7biikJJnn00':1FRXHY`g`o`o`q0rGXX-||++G49%mmG4 hhvG'<299;O||++F3q8$]]62
XXtfs7|&;"))+N
nn00+z1JWV\L]dkdsdsdu0vGXXgll+((ahararatuZ]CO#/8QUUuYY[K 4&3w<"7biikJJnn00j)GV+<;CVCVCX 1 G '4<<00 vs    ,M'c           	      d   t         d        }t        j                  |j                         D cg c]  }||d    ||d         n|d   nd c}t        j                               }t        j                  |j                  d      j                         D cg c]%  }|t        j                  j                  |      nd' c}t        j                               }t        j                  j                  ||gddg|j                               }t        || j                        S c c}w c c}w )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                 j    t        | d      5 }|j                         }d d d        |S # 1 sw Y   S xY w)NrL   )r   rU   )r%   r\   rX   s      r.   path_to_bytesz*Image.embed_storage.<locals>.path_to_bytes  s3    tT" "a"M"Ms   (2Nr$   r%   rf   rh   )r   rj   r=   ru   r`   r   rC   r%   basenamera   rn   ro   rp   r   r&   )r-   rd   r|   xrv   r%   rw   s          r.   embed_storagezImage.embed_storage   s    
 	 
 	
 hh !**, UVTaQwZ-?qy)QwZgkk 
 XXNUmm\bNcNmNmNopdt'7RWWd#TAp

 ..,,k:-FRXHY`k`s`s`u,v'4<<00 qs    D(*D-r,   )#__name__
__module____qualname____doc__r   bool__annotations__r    r   r>   r"   r   rj   structr`   ra   r&   r   r   r*   r/   r   r$   dictr<   r?   rG   r]   r	   rc   StringArrayrn   	ListArrayry   r    r0   r.   r   r   .   s#    D FDB,E8C=,&RYYibiik'RSGXc]SwU?E3?%E#udBJJHY*Y$Z %_c %N7D 7EV 7r
}d33E.FFG 
,1E".."..",,*V$W ,1\^\j\j ,1\1R^^ 1 1r0   r   r2   c                  V   t         j                  rdd l} nt        d      t        ~| j
                  j                          t        t        | j
                  j                  j                               t        | j
                  j                  j                               z        at        S )Nr   r4   )r   r7   r8   r9   r   r   r(   r;   setOPENkeysSAVE)rF   s    r.   list_image_compression_formatsr     sr    PQQ ")		%)#ciinn.A.A.C*Ds399>>K^K^K`Ga*a%b"%%r0   rY   r!   c                     t               }| j                  t               v r| j                  }n| j                  dv rdnd}| j	                  ||       |j                         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)rY   bufferr   s      r.   image_to_bytesr   %  sR    YF||577**(GGV	JJvfJ%??r0   c                 r    t        | d      r| j                  dk7  r| j                  d dS d t        |       dS )Nfilename r5   )hasattrr   r   )rY   s    r.   rA   rA   0  s6    uj!enn&:66~e'<==r0   r=   c                    t         j                  rdd l}nt        d      | j                  }|j
                  dk7  r|j
                  nt        }|j                  }|j                  }d }| j                  dd  rLt        j                  d      }|dvrt        d| d| d	      ||urt        j                  d
| d| d       nf|t        v r|}n[|dk\  rV||z   t        |      z   }t        j                  |      }|t        v rt        j                  d
| d| d       n|dz  }|dk\  rV|t        d| dt               |j                   j#                  | j%                  |            }d t'        |      dS )Nr   r4   =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: r5   )r   r7   r8   r9   r"   	byteorder_NATIVE_BYTEORDERkinditemsizeshaper<   	TypeErrorwarningswarn_VALID_IMAGE_ARRAY_DTPYESr>   r   	fromarrayastyper   )	r=   rF   r"   dtype_byteorder
dtype_kinddtype_itemsize
dest_dtypedest_dtype_strrY   s	            r.   r@   r@   7  s   PQQKKE).C)?eooEVOJ^^NJ {{12XXe_
Z'*5'1KJ<W~  
"MM4UG4
|Kjkl	+	+
!,z9C<OON.1J66 8tJ<Onop1$ ! #E7*WXqWrs
 	
 IIZ 89E>%#899r0   objsc                    t         j                  rddl}nt        d      | rt	        |       \  }}t        |t              r| D cg c]  }||ddnd c}S t        |t        j                        r%t        t              }| D cg c]
  } ||       c}S t        ||j                  j                        r%t        t              }| D cg c]
  } ||       c}S | S | S c c}w c c}w c c}w )zmEncode a list of objects into a format suitable for creating an extension array of type `ImageExtensionType`.r   Nr4   r5   )r   r7   r8   r9   r   r:   r>   r<   r?   r   r@   r   rA   )r   rF   _objobj_to_image_dict_funcs        r.   objects_to_list_of_image_dictsr   b  s     PQQ%d+3c3Y]^RUCOS40M^^c2::&%;O%L";?@C*3/@@SYY__-%;<L%M";?@C*3/@@K _ A As   C C%
C*)6rC   sysr   dataclassesr   r   ior   typingr   r   r   r	   r
   r   r   numpyr<   pyarrowrj   r   r   download.download_configr   #download.streaming_download_managerr   tabler   utils.file_utilsr   utils.py_utilsr   r   r   r8   rF   rb   r   r   r>   r   r   r   r"   r   r   r   r$   r   r   rA   r?   r@   r   r   r0   r.   <module>r      s   	 
  (  L L L    5 7  , Y Y % 37 HT#Y/ 6==H4C#  BHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUOBHHUO $ f1 f1 f1R
&S	 
&+  >- >$ >(:2:: (:$ (:V
S	4:tBJJ'7>O9PP
Q	$Zr0   