
    &`i                         d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
Zd dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ erd dlZ ej        e          ZdZd	Z G d
 de          Z G d de          ZdS )    N)TYPE_CHECKINGIteratorListOptionalTupleUnion)DelegatingBlockBuilder)_check_import)BlockBlockMetadata)FileBasedDatasource)DefaultFileMetadataProvider   g      ?c                        e Zd ZdZdZg dZdZ	 	 ddeee	e         f         de
eeef                  de
e         f fd	Zd
ddedee         fdZd Zde
e         fdZdefdZ xZS )ImageDatasourcez'A datasource that lets you read images.T)pngjpgjpegtiftiffbmpgif   Npathssizemodec                     t                      j        |fi | t          | dd           |3t          |          dk    r t	          dt          |           d          |+|d         dk     s|d         dk     rt	          d	| d
          || _        || _        |                    dd           }t          |t                    r5| 
                                | _        |                    | j                   d S t          | _        d S )NPILPillow)modulepackage   zFExpected `size` to contain two integers for height and width, but got z integers instead.r   r   z6Expected `size` to contain positive integers, but got z	 instead.meta_provider)super__init__r
   len
ValueErrorr   r   get
isinstanceImageFileMetadataProvider_estimate_files_encoding_ratio_encoding_ratio_set_encoding_ratio%IMAGE_ENCODING_RATIO_ESTIMATE_DEFAULT)selfr   r   r   file_based_datasource_kwargsr#   	__class__s         /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/_internal/datasource/image_datasource.pyr%   zImageDatasource.__init__%   s5    	??">???d5(;;;;D		Q9t999 9 9  
 a1Q!XXXX   		488$OOm%>?? 	I#'#F#F#H#HD --d.BCCCCC#HD       fzpyarrow.NativeFilepathreturnc              #     K   ddl m}m} |                                }	 |                    t          j        |                    }n!# |$ r}t          d| d          |d }~ww xY w| j        R|j        t          t          | j                            k    r(| j        \  }}	|                    |	|f|j                  }| j        *|j        | j        k    r|                    | j                  }t                      }
t!          j        |          }d|i}|
                    |           |
                                }|V  d S )Nr   )ImageUnidentifiedImageErrorz&PIL couldn't load image file at path 'z'.)resampleimage)r   r8   r9   readallopenioBytesIOr'   r   tuplereversedresizeBILINEARr   convertr	   npasarrayaddbuild)r/   r4   r5   r8   r9   datar;   eheightwidthbuilderarrayitemblocks                 r2   _read_streamzImageDatasource._read_streamE   sW     
 	65555555yy{{	WJJrz$//00EE% 	W 	W 	WNdNNNOOUVV	W 9 UZ5$)9L9L3M3M%M%M IMFELL%5>LJJE9 UZ49%<%<MM$),,E(**
5!!Ds   'A A&A!!A&c                     dS )Nr    )r/   s    r2   _rows_per_filezImageDatasource._rows_per_filea   s    qr3   c                 V    d}|                                  D ]	}|||z  }
|| j        z  S )Nr   )_file_sizesr,   )r/   
total_size	file_sizes      r2   estimate_inmemory_data_sizez+ImageDatasource.estimate_inmemory_data_sized   sC    
))++ 	( 	(I $i'
D000r3   c           
      \   t          j                    }t          t          d t	          |                                 |                                                               }t          |          }|dk    r!t          	                    d           t          S | j        | j        | j        dv rd}nB| j        dv rd}n6| j        d	v rd
}n*t          	                    d| j         d           t          S | j        \  }}||z  |z  }||z  }t          d |D                       }	||	z  }
nt          }
t          j                    |z
  }|dk    r,t          	                    dt          |d           d           t                              d|
 d           t!          |
t"                    S )z5Return an estimate of the image files encoding ratio.c                     | d         dk    S )Nr   r   rS   )ps    r2   <lambda>z@ImageDatasource._estimate_files_encoding_ratio.<locals>.<lambda>r   s    QqTAX r3   r   zYAll input image files are empty. Use on-disk file size to estimate images in-memory size.N)1LPr   )RGBYCbCrLABHSV   )RGBACMYKIF   zFound unknown image mode: .c              3   &   K   | ]}|d          V  dS )r   NrS   ).0r\   s     r2   	<genexpr>zAImageDatasource._estimate_files_encoding_ratio.<locals>.<genexpr>   s&      !H!H1!A$!H!H!H!H!H!Hr3      z!Image input size estimation took r"   z	 seconds.z0Estimated image encoding ratio from sampling is )timeperf_counterlistfilterzip_pathsrV   r&   loggerwarningr.   r   r   sumrounddebugmax)IMAGE_ENCODING_RATIO_ESTIMATE_LOWER_BOUND)r/   
start_timenon_empty_path_and_size	num_files	dimensionrK   rL   single_image_sizetotal_estimated_sizetotal_file_sizeratiosampling_durations               r2   r+   z.ImageDatasource._estimate_files_encoding_ratiom   s   &((
"&%%s4;;==$:J:J:L:L'M'MNN#
 #
 /00	>>NNK   989 TY%:
 yO++		<<<		888		HDIHHHIII<< IMFE & :#4y#@ !!H!H0G!H!H!HHHO(?:EE :E -//*<q  NN:*A..: : :   	PPPPQQQ5CDDDr3   )NN)__name__
__module____qualname____doc___WRITE_FILE_PER_ROW_FILE_EXTENSIONS_NUM_THREADS_PER_TASKr   strr   r   r   intr%   r   r   rQ   rT   rY   floatr+   __classcell__r1   s   @r2   r   r      s3       11JJJ
 +/"	I IS$s)^$I uS#X'I sm	I I I I I I@  
%	   8  1Xc] 1 1 1 1-E -E -E -E -E -E -E -E -Er3   r   c                   l     e Zd ZdefdZdee         dee         deee                  def fdZ	 xZ
S )r*   encoding_ratioc                     || _         dS )zJSet image file encoding ratio, to provide accurate size in bytes metadata.N)r,   )r/   r   s     r2   r-   z-ImageFileMetadataProvider._set_encoding_ratio   s    -r3   r   rows_per_file
file_sizesr6   c                    t                                          |||          }|j        !t          |j        | j        z            |_        |S )N)r   r   )r$   _get_block_metadata
size_bytesr   r,   )r/   r   r   r   metadatar1   s        r2   r   z-ImageFileMetadataProvider._get_block_metadata   sU     77..: / 
 
 *"%h&9D<P&P"Q"QHr3   )r   r   r   r   r-   r   r   r   r   r   r   r   s   @r2   r*   r*      s        .# . . . .Cy  }	
 #' 
         r3   r*   )r>   loggingrp   typingr   r   r   r   r   r   numpyrE   +ray.data._internal.delegating_block_builderr	   ray.data._internal.utilr
   ray.data.blockr   r   )ray.data.datasource.file_based_datasourcer   &ray.data.datasource.file_meta_providerr   pyarrow	getLoggerr   rv   r.   r|   r   r*   rS   r3   r2   <module>r      sb   				   H H H H H H H H H H H H H H H H     N N N N N N 1 1 1 1 1 1 / / / / / / / / I I I I I I N N N N N N NNN 
	8	$	$
 )* % -0 )}E }E }E }E }E) }E }E }E@     ;     r3   