Ë
    ÃÓ^jz'  ã                  ó  — d Z ddlmZ ddlZddlmZmZ ddlZddlZ	ddl
mZmZmZmZmZ ddlmZ erddlmZ dd„Zdd	„Z	 	 	 	 	 	 	 	 	 	 dd
„Zdd„Zdd„Zdd„Zeedd„«       «       Zej6                  f	 	 	 	 	 	 	 dd„Zy)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                óÚ   — |t        | «      k\  ry|j                  t        t        | «      «      |«      }t        | «      D ��cg c]  \  }}||vsŒ|‘Œ }}}dj	                  |«      S c c}}w )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          úw/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/albumentations/augmentations/text/functional.pyÚdelete_random_wordsr      sh   € ð  ”C˜“JÒØà!×(Ñ(¬¬s°5«zÓ):¸IÓFÐÜ'0°Ó'7×X™)˜#˜t¸3ÐFWÒ;W’ÐX€IÑXØ�8‰8�IÓÐùó Ys   ÁA'ÁA'c                ó$  — |dk(  st        | «      t        k  rdj                  | «      S | j                  «       } t	        |«      D ]:  }|j                  t	        t        | «      «      d«      \  }}| |   | |   c| |<   | |<   Œ< dj                  | «      S )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Ò*Ø�x‰x˜‹Ðà�J‰J‹L€Eä�9Óò <ˆØ×%Ñ%¤e¬C°«JÓ&7¸Ó;‰
ˆˆdØ#(¨¡;°°d±Ð ˆˆd‰�U˜4’[ð<ð �8‰8�E‹?Ðó    c                óÄ   — |€d}t        |«      D ]>  }|j                  dt        | «      «      }| j                  ||j	                  |«      «       Œ@ dj                  | «      S )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   sb   € ð, ÐØ:ˆ	ä�>Ó"ò 7ˆØ×Ñ ¤3 u£:Ó.ˆØ�‰�S˜)×*Ñ*¨9Ó5Õ6ð7ð �8‰8�E‹?Ðr&   c                óÀ  — 	 ddl m} | j                  t        k(  r |j
                  | «      S | j                  t        k(  r0| j                  d   dk(  r |j
                  | dd…dd…df   d¬«      S | j                  t        k(  r(| j                  d   t        k(  r |j
                  | «      S t        d	| j                  › �«      ‚# t        $ r t        d«      t        ‚w xY w)
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Ýð ‡z�zÔ,Ò,Øˆu�‰˜uÓ%Ð%Ø‡z�zÔ1Ò1°e·k±kÀ!±nÈÒ6IØˆu�‰˜u¢Qª¨1 W™~°CÔ8Ð8Ø‡z�zÔ1Ò1°e·k±kÀ!±nÔHXÒ6XØˆu�‰˜uÓ%Ð%ä
Ð/°·±¨}Ð=Ó
>Ð>øô ò FÜÐ3Ó4¼+ÐEðFús   ‚C ÃCc                óè  — 	 ddl m} |j                  | «      }|D ]·  }|d   }|d   }|d   }|d   }| j                  dk(  rVt        |t        «      rht        |«      d	k\  rt        t        |d
d	 «      d	z  «      }n?t        |«      dk(  r1t        |d   «      }n"t        |t        «      rt        d„ |D «       «      }|d
d }	|j                  |	|||¬«       Œ¹ | S # t        $ r t        d«      t        ‚w xY w)zDraw text on a PIL image.r   )Ú	ImageDrawr5   Úbbox_coordsÚtextÚfontÚ
font_colorr7   é   Nr6   c              3  ó2   K  — | ]  }t        |«      –— Œ y ­w)N)Úint)Ú.0Úcs     r   ú	<genexpr>z)draw_text_on_pil_image.<locals>.<genexpr>š   s   è ø€ Ò:¨!œs 1ŸvÑ:ùs   ‚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_imagerX   �   s  € ðFÝ!ð �>‰>˜)Ó$€DØ!ò >ˆØ˜}Ñ-ˆØ˜ÑˆØ˜ÑˆØ˜lÑ+ˆ
ð �>‰>˜SÒ ä˜*¤eÔ,Ü�z“? aÒ'ä!$¤S¨°B°Q¨Ó%8¸1Ñ%<Ó!=‘JÜ˜“_¨Ò)Ü!$ Z°¡]Ó!3‘Jä˜
¤EÔ*ÜÑ:¨zÔ:Ó:ˆJà˜r �?ˆØ�	‰	�(˜D t°*ˆ	Õ=ð)>ð* Ðøô3 ò FÜÐ3Ó4¼+ÐEðFús   ‚C ÃC1c           	     ó`  — 	 ddl m}m} t	        | j
                  d   «      D �cg c]  } |j                  | dd…dd…|f   «      ‘Œ  }}|D �cg c]  }|j                  |«      ‘Œ }}|D ]Û  }|d   }	|d   }
|d   }|d	   }t        |«      | j
                  d   k  r5t        t        |«      dg| j
                  d   t        |«      z
  z  z   «      }n-t        |«      | j
                  d   kD  r|d| j
                  d    }|D �cg c]  }t        |«      ‘Œ }}|	dd }t        |«      D ]  \  }}|j                  ||
|||   ¬
«       Œ ŒÝ t        j                  |D �cg c]  }t        j                   |«      ‘Œ c}d¬«      S # t        $ r t        d«      t        ‚w xY wc c}w c c}w c c}w c c}w )zADraw text on a multi-channel image with more than three channels.r   )r   rB   r5   r    NrC   rD   rE   rF   rM   )Úaxis)r9   r   rB   r:   r   r=   r<   rO   r   rQ   ÚlistrI   r   rD   ÚnpÚstackÚarray)r?   rT   r   rB   ÚiÚchannelsÚchannelÚ
pil_imagesrV   rC   rD   rE   rF   rK   rW   Ú
channel_idrS   s                    r   Ú draw_text_on_multi_channel_imagerd   ¡   s·  € ðFß(ô :?¸u¿{¹{È1¹~Ó9NÖO°A��—‘ ¢aª¨A g¡Õ/ÐO€HÐOØ9AÖB¨g�)—.‘. Õ)ÐB€JÐBà!ò SˆØ˜}Ñ-ˆØ˜ÑˆØ˜ÑˆØ˜lÑ+ˆ
ô ˆz‹?˜UŸ[™[¨™^Ò+äœt JÓ/°1°#¸¿¹ÀQ¹Ì#ÈjË/Ñ9YÑ2ZÑZÓ[‰JÜ�‹_˜uŸ{™{¨1™~Ò-à#Ð$4 e§k¡k°!¡nÐ5ˆJð '1Ö1 ”c˜!•fÐ1ˆ
Ð1à˜r �?ˆô &/¨zÓ%:ò 	SÑ!ˆJ˜	à�N‰N˜8 T°¸:ÀjÑ;QˆNÕRñ	Sð+Sô2 �8‰8°hÖ?¨7”R—X‘X˜gÕ&Ò?ÀaÔHÐHøô? ò FÜÐ3Ó4¼+ÐEðFüò PùÚBùò$ 2ùò @s"   ‚E? ¤#FÁF!ÄF&ÅF+Å?Fc                ó,  — |rt        | |«      } t        | j                  «      t        k(  s3t        | j                  «      t        k(  rC| j                  d   dt
        hv r,t        | «      }t        ||«      }t        j                  |«      S t        | |«      S )a5  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@   rX   r\   r^   rd   )r?   rT   Úclear_bgrS   s       r   Úrender_textrh   Ç   s   € ñ. Ü'¨¨}Ó=ˆä
ˆ5�;‰;ÓÔ2Ò2ÜˆE�K‰KÓÔ8Ò8¸U¿[¹[È¹^ÐPQÔScÐOdÑ=dä(¨Ó/ˆ	Ü*¨9°mÓDˆ	Ü�x‰x˜	Ó"Ð"ä+¨E°=ÓAÐAr&   c                ó$  — | j                  «       }t        j                  | j                  d   | j                  d   ft        j                  ¬«      }|D ]"  }|d   \  }}}}	d|||	…||…f<   d|||	…||…f<   Œ$ t        j                  ||d|¬«      S )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?   rT   ÚmethodÚresult_imageÚmaskrV   Úx_minÚy_minÚx_maxÚy_maxs
             r   rf   rf   ë   s£   € ð0 —:‘:“<€LÜ�8‰8�U—[‘[ ‘^ U§[¡[°¡^Ð4¼B¿H¹HÔE€Dà!ò -ˆØ%-¨mÑ%<Ñ"ˆˆu�e˜Uð 23ˆ�U˜5�[ %¨ +Ð-Ñ.ð *-ˆˆU�5ˆ[˜% ˜+Ð%Ò&ð-ô �;‰;�| T¸À&ÔIÐIr&   )r   ú	list[str]r   rI   r   úrandom.RandomÚreturnÚstr)
r   ry   r1   rI   r2   ztuple[str, ...] | Noner   rz   r{   r|   )r?   ú
np.ndarrayr{   r   )rS   r   rT   úlist[dict[str, Any]]r{   r   )r?   r}   rT   r~   r{   r}   )r?   r}   rT   r~   rg   Úboolr{   r}   )r?   r}   rT   r~   rr   rI   r{   r}   )Ú__doc__Ú
__future__r   ÚrandomÚtypingr   r   rp   Únumpyr\   Úalbucorer   r   r   r	   r
   Ú$albumentations.core.type_definitionsr   r9   r   r   r%   r3   r@   rX   rd   rh   ÚINPAINT_TELEArf   © r&   r   ú<module>r‰      sÙ   ðñõ #ã ß %ã 
Û ÷õ õ 6ñ Ýóó0ð6Øðàðð &ðð ð	ð
 	óó>?ó"ó@#IðL 
ØòBó ó 
ðBðJ ×#Ñ#ð%JØð%Jà'ð%Jð ð%Jð ô	%Jr&   