ó
    †ñ:i½  ã                   óp   • S SK r S SKJr  S SKrS SKJr  S SKrS SK	r	S r
S rS rS rS rS rS	 rSS
 jrg)é    N)Úwrapc                 ó  • Sn[         R                  R                  U 5      (       aN  [         R                  R                  U 5      (       d*  [         R                  " U 5      (       d  Sn[        S5        U$ Sn[        S5        U$ )zwFunction to check if folder at given path exists and is not empty.

Returns True if folder is empty or does not exist.
FTzO'test_images' folder is empty. Please place images to be tested in this folder.zpThere is no 'test_images' folder under current directory. Please create one and place images to be tested there.)ÚosÚpathÚexistsÚisfileÚlistdirÚprint)r   Úemptys     ÚS/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/shap/utils/image.pyÚis_emptyr      sk   € ð
 €EÜ	‡w�w‡~�~�d×Ñ¤B§G¡G§N¡N°4×$8Ñ$8ä�zŠz˜$×ÑØˆEÜÐcÔdð €Lð	 ˆÜØ~ô	
ð €Ló    c                 ó|  • [         R                  R                  U 5      (       dG  [         R                  R                  U 5      (       d  [         R                  " U 5        g[        S5        g[         R                  " U 5      (       a6  [         R                  " U 5       H  n[         R                  " X-   5        M     gg)zQFunction to create a new directory with given path or empty if it already exists.z Please give a valid folder path.N)r   r   r   r   Úmakedirsr
   r	   Úremove)r   Úfiles     r   Úmake_dirr      st   € ä�7‰7�>‰>˜$×ÑÜ�w‰w�~‰~˜d×#Ñ#ä�KŠK˜ÕäÐ4Õ5ô �:Š:�d×ÑäŸ
š
 4Ö(�Ü—	’	˜$™+Ö&ò )ð r   c                 óê   • [         R                  R                  5       u  pSn/ SQn[        U5       H>  u  pVXT;   d  M  [        R
                  R                  X S35      n[        Xg5        US-  nM@     g)zLFunction to add sample images from imagenet50 SHAP data in the given folder.é   )é   é   é   é,   ú.jpgN)ÚshapÚdatasetsÚ
imagenet50Ú	enumerater   r   ÚjoinÚ
save_image)r   ÚXÚ_ÚcounterÚindexes_listÚiÚimageÚpath_to_images           r   Úadd_sample_imagesr(   .   s`   € ä�=‰=×#Ñ#Ó%�D€AØ€GÚ#€LÜ˜a–L‰ˆØÕÜŸG™GŸL™L¨°¸$Ð/?Ó@ˆMÜ�uÔ,Ø�q‰LŠGò	 !r   c                 óÂ   • [         R                  " U 5      n[         R                  " U[         R                  5      n[        R
                  " U5      R                  S5      $ )zPFunction to load image at given path and return numpy array of RGB float values.Úfloat)Úcv2ÚimreadÚcvtColorÚCOLOR_BGR2RGBÚnpÚarrayÚastype)r'   r&   s     r   Ú
load_imager2   :   s?   € ä�JŠJ�}Ó%€EÜ�LŠL˜¤× 1Ñ 1Ó2€EÜ�8Š8�E‹?×!Ñ! 'Ó*Ð*r   c                 ó6   • SnU R                  U5      (       a  gg)z“Function to check if a file has valid image extensions and return True if it does.
Note: Azure Cognitive Services only accepts below file formats.
)ú.pngr   z.jpegz.gifz.bmpz.jfifTN)Úendswith)r'   Úvalid_extensionss     r   Úcheck_valid_imager7   A   s%   € ð JÐØ×ÑÐ.×/Ñ/Øð 0r   c                 ób   • [         R                  " U 5      S-  n[        R                  " X5        g)zOFunction to save image(RGB values array) at given path (filename and location).g     ào@N)r/   r0   ÚpltÚimsave)r0   r'   r&   s      r   r    r    J   s"   € ô �HŠH�U‹O˜eÑ#€EÜ‡J‚Jˆ}Õ$r   c                 ó¢  • [        U 5      nSn[        R                  R                  U 5      u  pEUR                  S5      S   nSnSnUR                  S   UR                  S   :X  a  UR                  S   S:”  a  Xw4n	OÄUR                  S   UR                  S   :”  aA  UR                  S   S:”  a.  U[        UR                  S   U-  UR                  S   -  5      4n	OcUR                  S   UR                  S   :”  aA  UR                  S   S:”  a.  [        UR                  S   U-  UR                  S   -  5      U4n	OSnU(       a…  [        R                  " UW	S   U	S   4S9n[        R                  R                  XS	-   5      n[        S
UR                  5        [        X#5        [        R                  " U5      R                  S5      nX#4$ )ar  Function to resize given image retaining original aspect ratio and save in given directory 'reshaped_dir'.
Returns numpy array of resized image and path where resized file is saved.

Note
----
Azure COGS CV has size limit of < 4MB and min size of 50x50 for images.
Hence, large image files are being reshaped in code below to increase speed of SHAP explanations and run Azure COGS for image captions.
If image (pixel_size, pixel_size) is greater than 500 for either of the dimensions:
1 - image is resized to have max. 500 pixel size for the dimension > 500
2 - other dimension is resized retaining the original aspect ratio

NÚ.r   iô  Tr   F)Údsizer4   zReshaped image size:r*   )r2   r   r   ÚsplitÚshapeÚintr+   Úresizer   r
   r    r/   r0   r1   )
r'   Úreshaped_dirr&   Úreshaped_pathr"   ÚtailÚ	file_nameÚ
max_pixelsÚreshapeÚnew_dims
             r   Úresize_imagerI   Q   sŠ  € ô �}Ó%€Eð €MÜ�g‰g�m‰m˜MÓ*�G€AØ—
‘
˜3“ Ñ"€IØ€JØ€Gà‡{�{�1�~˜Ÿ™ Q™Ó'¨E¯K©K¸©N¸SÓ,@ØÐ*‰Ø	�‰�Q‰˜%Ÿ+™+ a™.Ó	(¨U¯[©[¸©^¸cÓ-AØœs 5§;¡;¨q¡>°JÑ#>ÀÇÁÈQÁÑ#OÓPÐQ‰Ø	�‰�Q‰˜%Ÿ+™+ a™.Ó	(¨U¯[©[¸©^¸cÓ-AÜ�u—{‘{ 1‘~¨
Ñ2°U·[±[À±^ÑCÓDÀjÐQ‰àˆö ä—
’
˜5¨°©°W¸Q±ZÐ(@ÑAˆÜŸ™Ÿ™ \¸vÑ3EÓFˆÜÐ$ e§k¡kÔ2Ü�5Ô(Ü—’˜“×&Ñ& wÓ/ˆàÐÐr   c                 óD  • / nU H2  n[        U5      nUR                  UR                  [        5      5        M4     [        R
                  " US9nSn[        [        U5      5       H¸  n	US-  nX‚S-   :X  a  [        R
                  " US9nSnUR                  SX(5        [        R                  " XI   5        [        R                  " S5        [        U 5      [        U5      :¼  d  M€  [        R                  " SR                  [        [        X	   5      SS95      5        Mº     g)	z=Function to display grid of images and their titles/captions.)Úfigsizer   r   ÚoffÚ
é(   )ÚwidthN)r2   Úappendr1   r@   r9   ÚfigureÚrangeÚlenÚadd_subplotÚimshowÚaxisÚtitler   r   Ústr)
Úlist_of_captionsÚlist_of_imagesÚmax_columnsrK   Úmasked_imagesÚfilenamer&   ÚfigÚcolumnr%   s
             r   Údisplay_grid_plotr`   |   sã   € ð €MÛ"ˆÜ˜8Ó$ˆØ×Ñ˜UŸ\™\¬#Ó.Ö/ñ #ô
 �*Š*˜WÑ
%€CØ€FÜ”3�}Ó%Ö&ˆØ�!‰ˆà 1‘_Ó$Ü—*’* WÑ-ˆCØˆFØ�‰˜˜;Ô/Ü�
Š
�=Ñ#Ô$Ü�Š�ŒÜÐÓ ¤C¨Ó$6Õ6Ü�IŠI�d—i‘i¤¤SÐ)9Ñ)<Ó%=ÀRÑ HÓIÖJò 'r   )é   )é   rb   )r   Útextwrapr   r+   Úmatplotlib.pyplotÚpyplotr9   Únumpyr/   r   r   r   r(   r2   r7   r    rI   r`   © r   r   Ú<module>rh      sA   ðÛ 	Ý ã 
Ý Û ã òò&'ò 	ò+òò%ò( õVKr   