
    hz'                       S r SSKJr  SSKrSSKJrJr  SSKrSSKr	SSK
JrJrJrJrJr  SSKJr  \(       a  SSKJr  SS jrSS	 jr          SS
 jrSS jrSS jrSS jr\\SS j5       5       r\R6                  4       SS jjrg)a  Functional implementations for text manipulation and rendering.

This module provides utility functions for manipulating text in strings and
rendering text onto images. Includes functions for word manipulation, text drawing,
and handling text regions in images.
    )annotationsN)TYPE_CHECKINGAny)MONO_CHANNEL_DIMENSIONSNUM_MULTI_CHANNEL_DIMENSIONSNUM_RGB_CHANNELSpreserve_channel_dimuint8_io)PAIRImagec                    U[        U 5      :  a  gUR                  [        [        U 5      5      U5      n[        U 5       VVs/ s H  u  pEXC;  d  M  UPM     nnnSR	                  U5      $ s  snnf )a  Delete a specified number of random words from a list.

This function randomly removes words from the input list and joins the remaining
words with spaces to form a new string.

Args:
    words (list[str]): List of words to process.
    num_words (int): Number of words to delete.
    py_random (random.Random): Random number generator for reproducibility.

Returns:
    str: New string with specified words removed. Returns empty string if
         num_words is greater than or equal to the length of words.

  )lensamplerange	enumeratejoin)words	num_words	py_randomindices_to_deleteidxword	new_wordss          f/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/text/functional.pydelete_random_wordsr      sf      CJ!((s5z):IF'0'7X'7)#3;W'7IX88I Ys   A,A,c                "   US:X  d  [        U 5      [        :  a  SR                  U 5      $ U R                  5       n [	        U5       H6  nUR                  [	        [        U 5      5      S5      u  pEX   X   sX'   X'   M8     SR                  U 5      $ )a  Swap random pairs of words in a list of words.

This function randomly selects pairs of words and swaps their positions
a specified number of times.

Args:
    words (list[str]): List of words to process.
    num_words (int): Number of swaps to perform.
    py_random (random.Random): Random number generator for reproducibility.

Returns:
    str: New string with words swapped. If num_words is 0 or the list has fewer
         than 2 words, returns the original string.

r   r      )r   r   r   copyr   r   )r   r   r   _idx1idx2s         r   swap_random_wordsr%   6   s      A~Ud*xxJJLE9%%eCJ&7;
#(; U[  88E?    c                    Uc  Sn[        U5       H>  nUR                  S[        U 5      5      nU R                  XSR	                  U5      5        M@     SR                  U 5      $ )a  Insert random stopwords into a list of words.

This function randomly inserts stopwords at random positions in the
list of words a specified number of times.

Args:
    words (list[str]): List of words to process.
    num_insertions (int): Number of stopwords to insert.
    stopwords (tuple[str, ...] | None): Tuple of stopwords to choose from.
        If None, default stopwords will be used.
    py_random (random.Random): Random number generator for reproducibility.

Returns:
    str: New string with stopwords inserted.

)andtheisinatofr   r   )r   randintr   insertchoicer   )r   num_insertions	stopwordsr   r"   r   s         r   insert_random_stopwordsr3   Q   s[    , :	>"3u:.S**956 # 88E?r&   c                    SSK Jn  U R                  [        :X  a  UR
                  " U 5      $ U R                  [        :X  a.  U R                  S   S:X  a  UR
                  " U SS2SS2S4   SS9$ U R                  [        :X  a)  U R                  S   [        :X  a  UR
                  " U 5      $ [        S	U R                   35      e! [         a    [        S5      [        ef = f)
z+Convert a NumPy array image to a PIL image.r   r   Pillow is not installedr       NL)modezUnsupported image shape: )
PILr   ImportErrorndimr   	fromarrayr   shaper   	TypeError)imager   s     r   convert_image_to_pilr@   p   s    F zz,,u%%zz11ekk!n6IuQ1W~C88zz11ekk!nHX6Xu%%
/}=
>>  F34+EFs   C C c                    SSK Jn  UR                  U 5      nU H  nUS   nUS   nUS   nUS   nU R                  S:X  a]  [        U[        5      (       aG  [        U5      S	:  a  [        [        US
S	 5      S	-  5      nOE[        U5      S:X  a  [        US   5      nO'[        U[        5      (       a  [        S U 5       5      nUS
S n	UR                  XXxS9  M     U $ ! [         a    [        S5      [        ef = f)zDraw text on a PIL image.r   )	ImageDrawr5   bbox_coordstextfont
font_colorr7      Nr6   c              3  8   #    U  H  n[        U5      v   M     g 7f)N)int).0cs     r   	<genexpr>)draw_text_on_pil_image.<locals>.<genexpr>   s     :z!s1vvzs   r    rE   fill)r9   rB   r:   Drawr8   
isinstancetupler   rI   sumrD   )
	pil_imagemetadata_listrB   drawmetadatarC   rD   rE   rF   positions
             r   draw_text_on_pil_imagerY      s
   F! >>)$D!}-l+
 >>S *e,,z?a'!$SBQ%81%<!=J_)!$Z]!3J
E**:z::Jr?		(t	=) "* 3  F34+EFs   C! !C<c           	     v    SSK JnJn  [	        U R
                  S   5       Vs/ s H  oBR                  " U SS2SS2U4   5      PM      nnU Vs/ s H  ocR                  U5      PM     nnU H  nUS   n	US   n
US   nUS	   n[        U5      U R
                  S   :  a5  [        [        U5      S/U R
                  S   [        U5      -
  -  -   5      nO.[        U5      U R
                  S   :  a  USU R
                  S    nU Vs/ s H  n[        U5      PM     nnU	SS n[        U5       H  u  nnUR                  XXU   S
9  M     M     [        R                  " U Vs/ s H  n[        R                   " U5      PM     snSS9$ ! [         a    [        S5      [        ef = fs  snf s  snf s  snf s  snf )zADraw text on a multi-channel image with more than three channels.r   )r   rB   r5   r    NrC   rD   rE   rF   rN   )axis)r9   r   rB   r:   r   r=   r<   rP   r   rR   listrI   r   rD   npstackarray)r?   rU   r   rB   ichannelschannel
pil_imagesrW   rC   rD   rE   rF   rK   rX   
channel_idrT   s                    r    draw_text_on_multi_channel_imagere      s   F( :?u{{1~9NO9NAaAg/9NHO9ABg..)JB!}-l+
 z?U[[^+tJ/1#Q#j/9Y2ZZ[J_u{{1~-#$4ekk!n5J '11jc!fj
1r? &/z%:!J	NN8j;QNR &;+ "2 88h?h7RXXg&h?aHH?  F34+EF PB$ 2 @s"   F	 %F'F,F1# F6	F$c                :   U(       a  [        X5      n [        U R                  5      [        :X  d6  [        U R                  5      [        :X  aE  U R                  S   S[
        1;   a,  [        U 5      n[        X15      n[        R                  " U5      $ [        X5      $ )a  Render text onto an image based on provided metadata.

This function draws text on an image using metadata that specifies text content,
position, font, and color. It can optionally clear the background before rendering.
The function handles different image types (grayscale, RGB, multi-channel).

Args:
    image (np.ndarray): Image to draw text on.
    metadata_list (list[dict[str, Any]]): List of metadata dictionaries containing:
        - bbox_coords: Bounding box coordinates (x_min, y_min, x_max, y_max)
        - text: Text string to render
        - font: PIL ImageFont object
        - font_color: Color for the text
    clear_bg (bool): Whether to clear (inpaint) the background under the text.

Returns:
    np.ndarray: Image with text rendered on it.

r    r6   )inpaint_text_backgroundr   r=   r   r   r   r@   rY   r]   r_   re   )r?   rU   clear_bgrT   s       r   render_textri      sy    . '=
5;;22EKK88U[[^PQScOd=d(/	*9D	xx	""+EAAr&   c                   U R                  5       n[        R                  " U R                  S   U R                  S   4[        R                  S9nU H  nUS   u  pgpSX7U	2Xh24'   SXGU	2Xh24'   M     [
        R                  " X4SUS9$ )a  Inpaint (clear) regions in an image where text will be rendered.

This function creates a clean background for text by inpainting rectangular
regions specified in the metadata. It removes any existing content in those
regions to provide a clean slate for rendering text.

Args:
    image (np.ndarray): Image to inpaint.
    metadata_list (list[dict[str, Any]]): List of metadata dictionaries containing:
        - bbox_coords: Bounding box coordinates (x_min, y_min, x_max, y_max)
    method (int, optional): Inpainting method to use. Defaults to cv2.INPAINT_TELEA.
        Options include:
        - cv2.INPAINT_TELEA: Fast Marching Method
        - cv2.INPAINT_NS: Navier-Stokes method

Returns:
    np.ndarray: Image with specified regions inpainted.

r   r6   )dtyperC      rG   )inpaintRadiusflags)r!   r]   zerosr=   uint8cv2inpaint)
r?   rU   methodresult_imagemaskrW   x_miny_minx_maxy_maxs
             r   rg   rg      s    0 ::<L88U[[^U[[^4BHHED!%-m%<"e 235[%+-. *-5[%+%& " ;;|&IIr&   )r   	list[str]r   rI   r   random.Randomreturnstr)
r   rz   r1   rI   r2   ztuple[str, ...] | Noner   r{   r|   r}   )r?   
np.ndarrayr|   r   )rT   r   rU   list[dict[str, Any]]r|   r   )r?   r~   rU   r   r|   r~   )r?   r~   rU   r   rh   boolr|   r~   )r?   r~   rU   r   rs   rI   r|   r~   )__doc__
__future__r   randomtypingr   r   rq   numpyr]   albucorer   r   r   r	   r
   $albumentations.core.type_definitionsr   r9   r   r   r%   r3   r@   rY   re   ri   INPAINT_TELEArg    r&   r   <module>r      s    #  % 
   6 06 & 	
 	>?"@#IL 
B  
BJ ##%J%J'%J %J 	%Jr&   