a
    ¡˜ëjŠ  ã                   @  s¤   d dl mZ d dl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 d d	lmZ er�d d
lmZmZ d dlmZ G dd„ deƒZdS )é    )Úannotations)ÚTYPE_CHECKINGÚAnyN)ÚImage)ÚElementType)ÚloggerÚtrace_logger)ÚSource)ÚOCRAgent)Úrequires_dependencies)Ú
TextRegionÚTextRegions)ÚLayoutElementsc                   @  s†   e Zd ZdZdddœdd„Zddœdd„Zd	dd
œdd„Zdd„ Zd	dd
œdd„Ze	dƒd	dd
œdd„ƒZ
e	dƒdddœdd„ƒZdS )ÚOCRAgentPaddlez)OCR service implementation for PaddleOCR.ÚenÚstr)Úlanguagec                 C  s   |   |¡| _d S )N)Ú
load_agentÚagent)Úselfr   © r   úq/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/utils/ocr_models/paddle_ocr.pyÚ__init__   s    zOCRAgentPaddle.__init__c              
   C  s–   ddl }ddlm} | ¡  |jj ¡ dk}|rDt d|› d�¡ nt d|› d�¡ z|d||ddd	�}W n$ t	y�   |d||ddd	�}Y n0 |S )
zSLoads the PaddleOCR agent as a global variable to ensure that we only load it once.r   N)Ú	PaddleOCRz$Loading paddle with GPU on language=z...z$Loading paddle with CPU on language=TF)Zuse_angle_clsZuse_gpuÚlangZenable_mkldnnZshow_log)
ÚpaddleZunstructured_paddleocrr   Zdisable_signal_handlerZdeviceZcudaZdevice_countr   ÚinfoÚAttributeError)r   r   r   r   Zgpu_availableZ
paddle_ocrr   r   r   r      s0    û
ûzOCRAgentPaddle.load_agentzPILImage.Image)ÚimageÚreturnc                 C  s   |   |¡}d |j¡S )Nz

)Úget_layout_from_imageÚjoinÚtexts©r   r   Úocr_regionsr   r   r   Úget_text_from_image>   s    
z"OCRAgentPaddle.get_text_from_imagec                 C  s   dS )NFr   )r   r   r   r   Úis_text_sortedB   s    zOCRAgentPaddle.is_text_sortedr   c                 C  s.   t  d¡ | jjt |¡dd�}|  |¡}|S )zEGet the OCR regions from image as a list of text regions with paddle.z)Processing entire page OCR with paddle...T)Úcls)r   Zdetailr   ZocrÚnpÚarrayÚ
parse_data)r   r   Úocr_datar$   r   r   r   r    E   s    

z$OCRAgentPaddle.get_layout_from_imageZunstructured_inferencer   c                 C  s.   |   |¡}t|j|jt |jj¡dtjid�S )Nr   )Úelement_coordsr"   Zelement_class_idsZelement_class_id_map)	r    r   r,   r"   r(   ÚzerosÚshaper   ZUNCATEGORIZED_TEXTr#   r   r   r   Úget_layout_elements_from_imageR   s    
üz-OCRAgentPaddle.get_layout_elements_from_imagez	list[Any])r+   r   c              
   C  sæ   ddl m} ddlm} g }tt|ƒƒD ]²}|| }|s:q(|D ]š}tdd„ |d D ƒƒ}tdd„ |d D ƒƒ}	tdd„ |d D ƒƒ}
tdd„ |d D ƒƒ}|d	 d }|s¬q>| ¡ }|r>|||	|
||t	j
d
�}| |¡ q>q(| |¡S )aù  Parse the OCR result data to extract a list of TextRegion objects from paddle.

        The function processes the OCR result dictionary, looking for bounding
        box information and associated text to create instances of the TextRegion
        class, which are then appended to a list.

        Parameters:
        - ocr_data (list): A list containing the OCR result data

        Returns:
        - TextRegions:
            TextRegions object, containing data from all text regions in numpy arrays; each row
            represents a detected text region within the OCR-ed image.

        Note:
        - An empty string or a None value for the 'text' key in the input
          dictionary will result in its associated bounding box being ignored.
        r   )r   )Úbuild_text_region_from_coordsc                 S  s   g | ]}|d  ‘qS ©r   r   ©Ú.0Úir   r   r   Ú
<listcomp>�   ó    z-OCRAgentPaddle.parse_data.<locals>.<listcomp>c                 S  s   g | ]}|d  ‘qS ©é   r   r2   r   r   r   r5   ‚   r6   c                 S  s   g | ]}|d  ‘qS r1   r   r2   r   r   r   r5   ƒ   r6   c                 S  s   g | ]}|d  ‘qS r7   r   r2   r   r   r   r5   „   r6   r8   )ÚtextÚsource)Ú)unstructured_inference.inference.elementsr   Z0unstructured.partition.pdf_image.inference_utilsr0   ÚrangeÚlenÚminÚmaxÚstripr	   Z
OCR_PADDLEÚappendÚ	from_list)r   r+   r   r0   Ztext_regionsÚidxÚresÚlineÚx1Úy1Zx2Úy2r9   Zcleaned_textZtext_regionr   r   r   r*   a   s,    ÿzOCRAgentPaddle.parse_dataN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r&   r    r   r/   r*   r   r   r   r   r      s   %r   )Ú
__future__r   Útypingr   r   Únumpyr(   ZPILr   ZPILImageZunstructured.documents.elementsr   Zunstructured.loggerr   r   Z&unstructured.partition.utils.constantsr	   Z5unstructured.partition.utils.ocr_models.ocr_interfacer
   Zunstructured.utilsr   r;   r   r   Z.unstructured_inference.inference.layoutelementr   r   r   r   r   r   Ú<module>   s   