a
    ¡˜ëj“N  ã                   @  sz  d dl m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Zd dlmZ d dlmZ d dlmZ d dlmZ d dlm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"m#Z#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-m.Z. d dl/m0Z0m1Z1 d dl2m3Z3 dde#de$j4j5ddde#f	ddddddddddddddœd d!„Z6e(d"ƒdde#de$j4j5ddde#f	ddd#ddddddddddd$œd%d&„ƒZ7e(d"ƒde#de$j4j5dde#fd'd(ddddd)ddd'd*œ
d+d,„ƒZ8e(d"ƒdFd-d(d.d)d/d0œd1d2„ƒZ9d(d3d4d5œd6d7„Z:d8e j;fd-d9dd:d-d;œd<d=„Z<e j;fd>d?d:dd@œdAdB„Z=e(d"ƒe j;fd-d9d:d-dCœdDdE„ƒZ>dS )Gé    )ÚannotationsN)ÚIOÚTYPE_CHECKINGÚAnyÚListÚOptionalÚcast)ÚImage)ÚImageSequence)ÚElementType)ÚSimpleTableCell)Útesseract_to_paddle_language)ÚOCRLayoutDumper©Ú
valid_text)Ú aggregate_embedded_text_by_blockÚ&bboxes1_is_almost_subregion_of_bboxes2)Ú
env_config)ÚOCR_AGENT_PADDLEÚOCR_AGENT_TESSERACTÚOCRMode)ÚOCRAgent)Úrequires_dependencies)Ú
TextRegionÚTextRegions)ÚDocumentLayoutÚ
PageLayout)ÚLayoutElementÚLayoutElements)Ú!UnstructuredTableTransformerModelFZengéÈ   zbytes | IO[bytes]z'DocumentLayout'zList[List['TextRegion']]ÚboolÚstrÚintzOptional[OCRLayoutDumper]zOptional[str])ÚdataÚ
out_layoutÚextracted_layoutÚis_imageÚinfer_table_structureÚ	ocr_agentÚocr_languagesÚocr_modeÚpdf_image_dpiÚocr_layout_dumperÚpasswordÚtable_ocr_agentÚreturnc                 C  s¤   t | tƒr| n|  ¡ }t ¡ �r}tj |d¡}t|dƒ�}| 	|¡ W d  ƒ n1 sX0    Y  t
||||||||||	|
|d�}W d  ƒ n1 s–0    Y  |S )a  
    Process OCR data from a given data and supplement the output DocumentLayout
    from unstructured_inference with ocr.

    Parameters:
    - data (Union[bytes, BinaryIO]): The input file data,
        which can be either bytes or a BinaryIO object.

    - out_layout (DocumentLayout): The output layout from unstructured-inference.

    - is_image (bool, optional): Indicates if the input data is an image (True) or not (False).
        Defaults to False.

    - infer_table_structure (bool, optional):  If true, extract the table content.

    - ocr_languages (str, optional): The languages for OCR processing. Defaults to "eng" (English).

    - ocr_mode (str, optional): The OCR processing mode, e.g., "entire_page" or "individual_blocks".
        Defaults to "entire_page". If choose "entire_page" OCR, OCR processes the entire image
        page and will be merged with the output layout. If choose "individual_blocks" OCR,
        OCR is performed on individual elements by cropping the image.

    - pdf_image_dpi (int, optional): DPI (dots per inch) for processing PDF images. Defaults to 200.

    - ocr_layout_dumper (OCRLayoutDumper, optional): The OCR layout dumper to save the OCR layout.

    Returns:
        DocumentLayout: The merged layout information obtained after OCR processing.
    Útmp_fileÚwbN)Úfilenamer%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   )Ú
isinstanceÚbytesÚreadÚtempfileÚTemporaryDirectoryÚosÚpathÚjoinÚopenÚwriteÚprocess_file_with_ocr)r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   Z
data_bytesZtmp_dir_pathZtmp_file_pathr1   Zmerged_layouts© r?   úc/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/ocr.pyÚprocess_data_with_ocr$   s(    +
(ô$rA   Zunstructured_inferencezList[TextRegions])r3   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   c                 C  s  ddl m} g }�z�|rÀt | ¡�Œ}|j}tt |¡ƒD ]Z\}}| d¡}||_|t	|ƒk rd|| nd}t
|j| |||||||	|d�	}| |¡ q8| |¡W  d  ƒ W S 1 s´0    Y  nàt ¡ �Æ}tj| ||d|
pÜdd�}ttt |ƒ}t|ƒD ]z\}}|t	|ƒk �r|| nd}t |¡�:}t
|j| |||||||	|d�	}| |¡ W d  ƒ n1 �sf0    Y  � qø| |¡W  d  ƒ W S 1 �s–0    Y  W nZ t�yü } z@tj | ¡�sÐtj | ¡�rÖ|‚ntd	| › d
�ƒ|‚W Y d}~n
d}~0 0 dS )a  
    Process OCR data from a given file and supplement the output DocumentLayout
    from unstructured-inference with ocr.

    Parameters:
    - filename (str): The path to the input file, which can be an image or a PDF.

    - out_layout (DocumentLayout): The output layout from unstructured-inference.

    - extracted_layout (List[TextRegions]): a list of text regions extracted by pdfminer, one for
      each page

    - is_image (bool, optional): Indicates if the input data is an image (True) or not (False).
        Defaults to False.

    - infer_table_structure (bool, optional):  If true, extract the table content.

    - ocr_languages (str, optional): The languages for OCR processing. Defaults to "eng" (English).

    - ocr_mode (str, optional): The OCR processing mode, e.g., "entire_page" or "individual_blocks".
        Defaults to "entire_page". If choose "entire_page" OCR, OCR processes the entire image
        page and will be merged with the output layout. If choose "individual_blocks" OCR,
        OCR is performed on individual elements by cropping the image.

    - pdf_image_dpi (int, optional): DPI (dots per inch) for processing PDF images. Defaults to 200.

    Returns:
        DocumentLayout: The merged layout information obtained after OCR processing.
    r   )r   ZRGBN)	Úpage_layoutÚimager(   r)   r*   r+   Úextracted_regionsr-   r/   TÚ )ZdpiZoutput_folderZ
paths_onlyZuserpwzFile "z" not found!)Ú'unstructured_inference.inference.layoutr   ÚPILImager<   ÚformatÚ	enumerater
   ÚIteratorÚconvertÚlenÚsupplement_page_layout_with_ocrZpagesÚappendZ
from_pagesr7   r8   Ú	pdf2imageZconvert_from_pathr   r   r"   Ú	Exceptionr9   r:   ÚisdirÚisfileÚFileNotFoundError)r3   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r   Zmerged_page_layoutsZimagesZimage_formatÚirC   rD   Zmerged_page_layoutÚtemp_dirZ_image_pathsZimage_pathsZ
image_pathÚer?   r?   r@   r>   h   sh    -
÷,
û÷.0r>   z'PageLayout'zPILImage.ImagezOptional[TextRegions])
rB   rC   r(   r)   r*   r+   rD   r-   r/   r0   c	                 C  sZ  |}	|t krt|ƒ}	tj||	d�}
|tjjkr\|
 |¡}|rJ| | 	¡ ¡ t
| j|d�| _n�|tjjkrät| jjƒD ]l\}}|r‚qttj}| | jj| | | jj| | | jj| | | jj| | f¡}|
 |¡}|| jj|< qtntdƒ‚|�rV|}	|t k�rt|ƒ}	tj||	d�}ddlm} | ¡  |jdu �r>tdƒ‚t| j||j||d�| _| S )	a6  
    Supplement an PageLayout with OCR results depending on OCR mode.
    If mode is "entire_page", we get the OCR layout for the entire image and
    merge it with PageLayout.
    If mode is "individual_blocks", we find the elements from PageLayout
    with no text and add text from OCR to each element.
    )Zocr_agent_moduleÚlanguage)r%   Ú
ocr_layoutz[Invalid OCR mode. Parameter `ocr_mode` must be set to `entire_page` or `individual_blocks`.r   )ÚtablesNz&Unable to load table extraction agent.)ÚelementsrC   Útables_agentr)   rD   )r   r   r   Zget_instancer   Ú	FULL_PAGEÚvalueÚget_layout_from_imageZadd_ocred_pageZas_listÚ merge_out_layout_with_ocr_layoutZelements_arrayZINDIVIDUAL_BLOCKSrI   Útextsr   ZIMAGE_CROP_PADÚcropÚx1Úy1Úx2Úy2Zget_text_from_imageÚ
ValueErrorZunstructured_inference.modelsrY   Z
load_agentr[   ÚRuntimeErrorÚ(supplement_element_with_table_extraction)rB   rC   r(   r)   r*   r+   rD   r-   r/   rW   Z
_ocr_agentrX   rT   ÚtextÚpaddingÚcropped_imageZtext_from_ocrZ_table_ocr_agentrY   r?   r?   r@   rM   Î   s`    
þ
üÿ

ÿ
ÿûrM   r   z#'UnstructuredTableTransformerModel'zList['LayoutElement'])rZ   rC   r[   rD   r0   c                 C  s  ddl m} dd„ | j ¡ D ƒ tj¡}|du r4| S t | j	|k¡d }|  
|¡}tj}	t|jƒD ]š\}
}| |d |	 |d |	 |d |	 |d |	 f¡}t||d	�}|j||d
d�}|dkrÄdn||ƒ}|| j||
 < tjrbdd„ |D ƒ}|| j||
 < qb| S )aH  Supplement the existing layout with table extraction. Any Table elements
    that are extracted will have a metadata fields "text_as_html" where
    the table's text content is rendered into a html string and "table_as_cells"
    with the raw table cells output from table agent if env_config.EXTRACT_TABLE_AS_CELLS is True
    r   )Úcells_to_htmlc                 S  s   i | ]\}}||“qS r?   r?   )Ú.0ÚkÚvr?   r?   r@   Ú
<dictcomp>/  ó    z<supplement_element_with_table_extraction.<locals>.<dictcomp>Né   é   é   )Útable_element_imager)   Úcells)Z
ocr_tokensZresult_formatrE   c                 S  s   g | ]}t  |¡ ¡ ‘qS r?   )r   Zfrom_table_transformer_cellÚto_dict)rm   Úcellr?   r?   r@   Ú
<listcomp>M  s   z<supplement_element_with_table_extraction.<locals>.<listcomp>)Ú$unstructured_inference.models.tablesrl   Zelement_class_id_mapÚitemsÚgetr   ZTABLEÚnpÚwhereZelement_class_idsÚslicer   ZTABLE_IMAGE_CROP_PADrI   Úelement_coordsra   Úget_table_tokensZpredictÚtext_as_htmlZEXTRACT_TABLE_AS_CELLSZtable_as_cells)rZ   rC   r[   r)   rD   rl   Ztable_idZtable_ele_indicesZtable_elementsrj   rT   r€   rk   Útable_tokensZ
tatr_cellsr‚   Zsimple_table_cellsr?   r?   r@   rh      s<    




üÿþÿÿrh   r   zList[dict[str, Any]])ru   r)   r0   c              	   C  s^   |j | d�}g }t|jƒD ]>\}}| |j| |j| |j| |j| g||dddœ¡ q|S )z1Get OCR tokens from either paddleocr or tesseract)rC   r   )Úbboxri   Zspan_numÚline_numZ	block_num)r^   rI   r`   rN   rb   rc   rd   re   )ru   r)   rX   rƒ   rT   ri   r?   r?   r@   r�   U  s     üôÿr�   Tr   Úfloat)r%   rX   Úsupplement_with_ocr_elementsÚsubregion_thresholdr0   c                 C  sz   t | ƒdkst |ƒdkr| S dd„ t| jƒD ƒ}| j t¡| _|D ] }t|  |g¡||d�| j|< qB|rrt| |ƒn| }|S )aœ  
    Merge the out layout with the OCR-detected text regions on page level.

    This function iterates over each out layout element and aggregates the associated text from
    the OCR layout using the specified threshold. The out layout's text attribute is then updated
    with this aggregated text. If `supplement_with_ocr_elements` is `True`, the out layout will be
    supplemented with the OCR layout.
    r   c                 S  s   g | ]\}}t |ƒs|‘qS r?   r   )rm   rT   ri   r?   r?   r@   ry   ˆ  rq   z4merge_out_layout_with_ocr_layout.<locals>.<listcomp>)Ztarget_regionZsource_regionsÚ	threshold)rL   rI   r`   ÚastypeÚobjectr   r   Ú#supplement_layout_with_ocr_elements)r%   rX   r‡   rˆ   Zinvalid_text_indicesÚidxÚfinal_layoutr?   r?   r@   r_   r  s    
ýÿýr_   zList['TextRegion']z'TextRegion')rX   Úregionrˆ   r0   c                 C  sF   g }| D ]*}|j  |j |¡}|r|jr| |j¡ q|rBd |¡S dS )zdExtracts the text aggregated from the regions of the ocr layout that lie within the given
    block.ú rE   )r„   Zis_almost_subregion_ofri   rN   r;   )rX   r�   rˆ   Zextracted_textsZ
ocr_regionZ'ocr_region_is_subregion_of_given_regionr?   r?   r@   Úaggregate_ocr_text_by_block›  s    þ
r‘   )ÚlayoutrX   rˆ   r0   c           	      C  sŒ   ddl m} ddlm} t| ƒdkr:t|ƒdkr4| S |}n*t|j| j|ƒjdd� t	¡ }| 
|¡}t|ƒr„||ƒ}| | |g¡}n| }|S )aµ  
    Supplement the existing layout with additional OCR-derived elements.

    This function takes two lists: one list of pre-existing layout elements (`layout`)
    and another list of OCR-detected text regions (`ocr_layout`). It identifies OCR regions
    that are subregions of the elements in the existing layout and removes them from the
    OCR-derived list. Then, it appends the remaining OCR-derived regions to the existing layout.

    Parameters:
    - layout (LayoutElements): A collection of existing layout elements in array structures
    - ocr_layout (TextRegions): A collection of OCR-derived text regions in array structures

    Returns:
    - List[LayoutElement]: The final combined layout consisting of both the original layout
                           elements and the new OCR-derived elements.

    Note:
    - The function relies on `is_almost_subregion_of()` method to determine if an OCR region
      is a subregion of an existing layout element.
    - It also relies on `build_layout_elements_from_ocr_regions()` to convert OCR regions to
     layout elements.
    - The env_config `OCR_LAYOUT_SUBREGION_THRESHOLD` is used to specify the subregion matching
     threshold.
    r   )r   )Ú&build_layout_elements_from_ocr_regionsrr   )Zaxis)Ú.unstructured_inference.inference.layoutelementr   Z0unstructured.partition.pdf_image.inference_utilsr“   rL   r   r€   ÚsumrŠ   r!   r   Zconcatenate)	r’   rX   rˆ   r   r“   Zocr_regions_to_addÚmaskZocr_elements_to_addrŽ   r?   r?   r@   rŒ   °  s(    
ÿýüÿ	
rŒ   )N)?Ú
__future__r   r9   r7   Útypingr   r   r   r   r   r   Únumpyr}   rO   ZPILr	   rG   r
   Zunstructured.documents.elementsr   Z(unstructured.metrics.table.table_formatsr   Z"unstructured.partition.common.langr   Z5unstructured.partition.pdf_image.analysis.layout_dumpr   Z0unstructured.partition.pdf_image.pdf_image_utilsr   Z4unstructured.partition.pdf_image.pdfminer_processingr   r   Z#unstructured.partition.utils.configr   Z&unstructured.partition.utils.constantsr   r   r   Z5unstructured.partition.utils.ocr_models.ocr_interfacer   Zunstructured.utilsr   Z)unstructured_inference.inference.elementsr   r   rF   r   r   r”   r   r   rz   r   r\   r]   rA   r>   rM   rh   r�   ZOCR_LAYOUT_SUBREGION_THRESHOLDr_   r‘   rŒ   r?   r?   r?   r@   Ú<module>   s€    ô(Dô*e÷$Q û4 ü,ýý