U
    -e;                     @   s  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	rd dl"Z#ddl$m%Z% da&eee'  e(d< ej)dkrdndZ*e+de+de+de+de+de+de+de+de+de+de+de+de+de+dgZ,eG d d! d!Z-ee' d"d#d$Z.d%e/d&d'd(Z0d%e1d&d)d*Z2ej3e1d+d,d-Z4eee' ee1 eej3 ed% f ee1 d.d/d0Z5dS )1    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                   @   s  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e e dZee ed	< ed d
d
dZe	ed< dd Zee	eeejdf edddZdeddddZedee	df f dddZeejejej f ejdddZ!ejejdd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                 C   s   | j S N)r$   )self r+   X/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/datasets/features/image.py__call__Y   s    zImage.__call__)valuereturnc                 C   s   t jrddl}ntdt|tr,t|}t|tr@|ddS t|t	rTd|dS t|tj
rht|S t||jjr~t|S |ddk	rtj|d rd|ddS |ddk	s|ddk	r|d|ddS td| d	dS )
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*   r.   PILr+   r+   r,   encode_example\   s(    








zImage.encode_examplec              	   C   s4  | j stdtjrddl}ntd|dkr2i }|d |d  }}|dkr|dkrhtd| dnt|r~|j	|}n|
d	d
 }|tjrtjntj}zt||d }	||	}
W n tk
r   d}
Y nX t|
d}t|d|d}t| }W 5 Q R X |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 r2   z::repo_id)tokenrb)download_config)r   RuntimeErrorr   r3   r4   r5   rA   r   r   opensplit
startswithZHF_ENDPOINTZHUB_DATASETS_URLZHUB_DATASETS_HFFS_URLr   r>   r   r   r   readload)r*   r.   Ztoken_per_repo_idrB   r#   bytes_image
source_urlpatternrE   rF   rH   fr+   r+   r,   decode_example   s<    




zImage.decode_exampler   r/   c                 C   s(   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!   )featuresrV   r   )r*   rV   r+   r+   r,   flatten   s    zImage.flatten)storager/   c                 C   s  t j|jrLt jdgt| t  d}t jj||gddg|	 d}nVt j
|jrt jdgt| t  d}t jj||gddg|	 d}n
t j|jr8|jddkr|d}nt jdgt| t  d}|jddkr|d}nt jdgt| t  d}t jj||gddg|	 d}njt j|jrt jdd | D t  d}t jdgt| t  d}t jj||gddg|	 d}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                 S   s*   g | ]"}|d k	r"t t|d nd qS )Nr"   )r<   r8   r9   ).0Zarrr+   r+   r,   
<listcomp>   s     z&Image.cast_storage.<locals>.<listcomp>)patypes	is_stringr]   r9   lenrW   StructArrayfrom_arraysis_nullZ	is_binaryrX   Z	is_structZget_field_indexr   Zis_list	to_pylistr   r$   r*   r[   Zbytes_arrayZ
path_arrayr+   r+   r,   cast_storage   s4    ""   zImage.cast_storagec                    s   t dd  tj fdd| D t d}tjdd |d D t d}tjj||gddg|	 d	}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              	   S   s"   t | d}| }W 5 Q R X |S )NrG   )r   rM   )r#   rS   rO   r+   r+   r,   path_to_bytes  s    z*Image.embed_storage.<locals>.path_to_bytesc                    s8   g | ]0}|d k	r0|d d kr( |d q2|d nd qS )Nr"   r#   r+   )r`   xrl   r+   r,   ra   
  s   z'Image.embed_storage.<locals>.<listcomp>r\   c                 S   s$   g | ]}|d k	rt j|nd qS r)   )r?   r#   basename)r`   r#   r+   r+   r,   ra     s     r#   r"   r^   )r   rb   r9   ri   rW   r   rX   rf   rg   rh   r   r$   rj   r+   rn   r,   embed_storage   s    

zImage.embed_storage)N)#__name__
__module____qualname____doc__r   bool__annotations__r   r
   r:   r    r   rb   structrW   rX   r$   r   r   r(   r-   r   r"   dictr8   r;   rC   rT   r   rZ   ZStringArrayrf   Z	ListArrayrk   rp   r+   r+   r+   r,   r   .   s   
"$ '9".r   rU   c                  C   sR   t jrdd l} ntdtd krN| j  tt| jj	
 t| jj
 @ atS )Nr   r0   )r   r3   r4   r5   r   r   r&   r7   setZOPENkeysZSAVE)rB   r+   r+   r,   list_image_compression_formats  s    

$r{   r   )rP   r/   c                 C   sB   t  }| jt kr| j}n| jdkr(dnd}| j||d | S )zmConvert a PIL Image object to bytes using native compression if possible, otherwise use PNG/TIFF compression.)1LZLARGBZRGBAZPNGZTIFF)format)r   r   r{   modesavegetvalue)rP   bufferr   r+   r+   r,   image_to_bytes%  s    r   c                 C   s2   t | dr | jdkr | jd dS d t| dS d S )Nfilename r1   )hasattrr   r   )rP   r+   r+   r,   r=   0  s    r=   )r9   r/   c           	      C   s6  t jrdd l}ntd| j}|jdkr.|jnt}|j}|j}d }| j	dd  rt
d}|dkrxtd| d| d	||k	rtd
| d| d n^|tkr|}nP|dkr|| t| }t
|}|tkrtd
| d| d qq|d }q|d krtd| dt |j| |}d t|dS )Nr   r0   =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: r1   )r   r3   r4   r5   r    	byteorder_NATIVE_BYTEORDERkinditemsizeshaper8   	TypeErrorwarningswarn_VALID_IMAGE_ARRAY_DTPYESr:   r   Z	fromarrayZastyper   )	r9   rB   r    Zdtype_byteorderZ
dtype_kindZdtype_itemsizeZ
dest_dtypeZdest_dtype_strrP   r+   r+   r,   r<   7  s>    




r<   )objsr/   c                    s   t jrddl}ntd| rt| \}}t|tr@dd | D S t|tjrft	t
  fdd| D S t||jjrt	t  fdd| D S | S n| S dS )zmEncode a list of objects into a format suitable for creating an extension array of type `ImageExtensionType`.r   Nr0   c                 S   s"   g | ]}|d k	r|d dnd qS )Nr1   r+   r`   objr+   r+   r,   ra   n  s     z2objects_to_list_of_image_dicts.<locals>.<listcomp>c                    s   g | ]} |qS r+   r+   r   Zobj_to_image_dict_funcr+   r,   ra   q  s     c                    s   g | ]} |qS r+   r+   r   r   r+   r,   ra   t  s     )r   r3   r4   r5   r   r6   r:   r8   r;   r   r<   r   r=   )r   rB   _r   r+   r   r,   objects_to_list_of_image_dictsb  s    

r   )6r?   sysr   dataclassesr   r   ior   typingr   r   r   r   r	   r
   r   numpyr8   Zpyarrowrb   r   r   Zdownload.download_configr   Z#download.streaming_download_managerr   tabler   Zutils.file_utilsr   Zutils.py_utilsr   r   r   r4   rB   rY   r   r   r:   rv   r   r   r    r   r   r{   r"   r   rx   r=   r;   r<   r   r+   r+   r+   r,   <module>   sX    $ j, 