/usr/local/lib/python3.6/site-packages/datasets/features/__pycache__
NameSizeModeActions
audio.cpython-36.pyc114730644editdlrm
features.cpython-36.pyc659380644editdlrm
image.cpython-36.pyc106000644editdlrm
translation.cpython-36.pyc51690644editdlrm
__init__.cpython-36.pyc5970644editdlrm
Edit: /usr/local/lib/python3.6/site-packages/datasets/features/__pycache__/image.cpython-36.pyc (10600B)
3 <%Eg/@s@UddlZddlmZmZddlmZddlmZmZm Z m Z m Z m Z m Z ddlZddlZddlmZddlmZddlmZdd lmZdd lmZmZmZerddlZd d l m!Z!da"e e e#"eGd ddZ$e e#dddZ%de&dddZ'de(dddZ)e e e#e e(e ej*e dfe e(dddZ+dS)N) dataclassfield)BytesIO) TYPE_CHECKINGAnyClassVarDictListOptionalUnion)config)xopen) array_cast) is_local_path)first_non_null_valueno_op_if_value_is_nullstring_to_dict) FeatureTypec@seZdZUdZdZedZeedZ e e e j e j e jdZe eeddddZedd Zeeeejdfed d d Zdedd d dZedeedffdddZee je je jfe jdddZde jee jdddZ dS)ImageaImage feature to read image data from an image file. Input: The Image feature accepts as input: - A :obj:`str`: Absolute path to the image file (i.e. random access is allowed). - A :obj:`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 :obj:`np.ndarray`: NumPy array representing an image. - A :obj:`PIL.Image.Image`: PIL image object. Args: decode (:obj:`bool`, default ``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"] >>> ds = ds.cast_column('image', Image(decode=False)) {'bytes': None, 'path': '/root/.cache/huggingface/datasets/downloads/extracted/b0a21163f78769a2cf11f58dfc767fb458fc7cea5c05dccc0144a2c0f0bc1292/train/healthy/healthy_train.85.jpg'} ``` TNzPIL.Image.Image)bytespathF)defaultinitreprcCs|jS)N)pa_type)selfrA/usr/local/lib/python3.6/site-packages/datasets/features/image.py__call__CszImage.__call__)valuereturncCstjrddl}ntdt|tr,|ddSt|tjrZ|jj |j tj }dt |dSt||jjrpt |S|jddk rtjj|drd|jddS|jddk s|jddk r|jd|jddStd|d dS) a Encode example into a format for Arrow. Args: value (:obj:`str`, :obj:`np.ndarray`, :obj:`PIL.Image.Image` or :obj:`dict`): Data passed as input to Image feature. Returns: :obj:`dict` with "path" and "bytes" fields rNz4To support encoding images, please install 'Pillow'.)rrr)rrrzUAn image sample should have one of 'path' or 'bytes' but they are missing or None in .)r PIL_AVAILABLE PIL.Image ImportError isinstancestrnpndarrayr fromarrayastypeuint8image_to_bytesencode_pil_imagegetosrisfile ValueError)rr!PILimagerrrencode_exampleFs     zImage.encode_examplec Cs|jstdtjrddl}ntd|dkr2i}|d|d}}|dkr|dkrftd|dnt|r||jj |}nx|j d d}yt |tj d }|j |} Wntk rd} YnXt|d | d } t| j}WdQRX|jj |}n|jj t|}|j|S)aWDecode example image file into image data. Args: value (obj:`str` or :obj:`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 (:obj:`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: :obj:`PIL.Image.Image` zMDecoding is disabled for this feature. Please use Image(decode=True) instead.rNz4To support decoding images, please install 'Pillow'.rrzCAn image should have one of 'path' or 'bytes' but both are None in r#z::rrepo_idrb)use_auth_token)decode RuntimeErrorr r$r%r&r3rropensplitrZHUB_DATASETS_URLr0rrreadload) rr!Ztoken_per_repo_idr4rbytes_r5 source_urlr7r9frrrdecode_examplehs2  zImage.decode_exampler)r"cCs(ddlm}|jr|S|d|ddS)zfIf in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary.r)Valuebinarystring)rr)featuresrEr;)rrErrrflattens  z Image.flatten)storager"cs~tjrddlntdtjj|jrdtjdgt |tj d}tj j ||gddg|j d}ntjj|jr|jjddkr|jd}ntjdgt |tj d}|jjddkr|jd}ntjdgt |tjd}tj j ||gddg|j d}nntjj|jrrtjfdd |jDtj d}tjdgt |tjd}tj j ||gddg|j d}t||jS) aCast 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.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()}) rNz4To support encoding images, please install 'Pillow'.)typerr)maskcs2g|]*}|dk r*tjjtj|tjndqS)N)r.rr+r)arrayr-).0Zarr)r4rr sz&Image.cast_storage..)r r$r%r&patypes is_stringrKrMlenrF StructArray from_arraysis_nullZ is_structZget_field_indexrrGZis_list to_pylistrr)rrJ bytes_array path_arrayr)r4r cast_storages. "      zImage.cast_storage)rJ drop_pathsr"cstddtjfdd|jDtjd}|rPtjdgt|tjdn|jd}tjj ||gddg|j d }t ||j S) avEmbed image files into the Arrow array. Args: storage (pa.StructArray): PyArrow array to embed. drop_paths (bool, default ``True``): If True, the paths are set to None. Returns: pa.StructArray: Array in the Image arrow storage type, that is pa.struct({"bytes": pa.binary(), "path": pa.string()}) c Ss"t|d}|j}WdQRX|S)Nr8)rr?)rrCrArrr path_to_bytess z*Image.embed_storage..path_to_bytescs8g|]0}|dk r0|ddkr(|dq2|dndqS)Nrrr)rNx)r\rrrOsz'Image.embed_storage..)rKNrr)rL) rrPrMrWrFrSrGrrTrUrVrr)rrJr[rXrYr)r\r embed_storages    *zImage.embed_storage)N)T)!__name__ __module__ __qualname____doc__r;boolidr r(ZdtyperrPstructrFrGrrr_typer r dictr)r*r6rDrrIZ StringArrayrTZ ListArrayrZr^rrrrrs !   "1 "1r)r"cCsRtjrddl}ntdtdkrN|jjtt|jj j t|jj j @atS)Nrz4To support encoding images, please install 'Pillow'.) r r$r%r&_IMAGE_COMPRESSION_FORMATSrrlistsetZOPENkeysZSAVE)r4rrrlist_image_compression_formatss  $rlzPIL.Image.Image)r5r"cCs2t}|jtkr|jnd}|j||d|jS)zhConvert a PIL Image object to bytes using native compression if possible, otherwise use PNG compression.ZPNG)format)rrmrlsavegetvalue)r5bufferrmrrrr.sr.cCs2t|dr |jdkr |jddSdt|dSdS)Nfilename)rr)hasattrrqr.)r5rrrr/ s r/)objsr"cstjrddlntd|rt|\}}t|tr@dd|DSt|tjr^fdd|DSt|j j rt t fdd|DS|Sn|SdS)zrEncode a list of objects into a format suitable for creating an extension array of type :obj:`ImageExtensionType`.rNz4To support encoding images, please install 'Pillow'.cSs"g|]}|dk r|ddndqS)N)rrr)rNobjrrrrOsz2objects_to_list_of_image_dicts..cs6g|].}|dk r.dtjj|jtjdndqS)N)rr)r.rr+r,r)r-)rNru)r4rrrO scsg|] }|qSrr)rNru)obj_to_image_dict_funcrrrO's) r r$r%r&rr'r(r)r*rrr/)rt_rur)r4rvrobjects_to_list_of_image_dictss     rx),r1Z dataclassesrriortypingrrrrr r r Znumpyr)ZpyarrowrPrrr Z#download.streaming_download_managerrtablerZutils.file_utilsrZutils.py_utilsrrrr%r4rHrrhr(rrlrr.rgr/r*rxrrrrs, $     ]