a
    ¡˜ëjÄ>  ã                   @  s*  d dl mZ d dlZd dlZd dlZd dl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mZmZmZmZmZmZmZmZ d dl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#m$Z$ d dl%m&Z& e�rd dl'm(Z( d dl)m*Z*m+Z+ d dl,m-Z- d dlm.Z. dddœdd„Z/dQddddddddœdd„Z0d d!d d"œd#d$„Z1d%d&d%d'œd(d)„Z2dRd+ddddd,ddd-dd.œ
d/d0„Z3d1d2d3œd4d5„Z4ddd6œd7d8„Z5dd9d6œd:d;„Z6ddd6œd<d=„Z7d>d?dddd@œdAdB„Z8dSdCdDdddddEdFœdGdH„Z9dTddJdddKdLœdMdN„Z:ddd6œdOdP„Z;dS )Ué    )ÚannotationsN)Údeepcopy)ÚBytesIO)ÚPathÚPurePath)	ÚIOÚTYPE_CHECKINGÚBinaryIOÚIteratorÚListÚOptionalÚTupleÚUnionÚcast)ÚImage)ÚElementType)Úlogger)Úconvert_to_bytesÚexactly_one)Ú
env_config)Ú
TextRegion)ÚDocumentLayoutÚ
PageLayout)ÚLayoutElement)ÚElementzUnion[Image.Image, np.ndarray]Ústr©ÚimageZoutput_image_pathc                 C  s>   t | tjƒr|  |¡ n"t | tjƒr2t || ¡ ntdƒ‚dS )a  
    Write an image to a specified file path, supporting both PIL Image and numpy ndarray formats.

    Parameters:
    - image (Union[Image.Image, np.ndarray]): The image to be written, which can be in PIL Image
      format or a numpy ndarray format.
    - output_image_path (str): The path to which the image will be written.

    Raises:
    - ValueError: If the provided image type is neither PIL Image nor numpy ndarray.

    Returns:
    - None: The function writes the image to the specified path but does not return any value.
    zUnsupported Image TypeN)Ú
isinstancer   ÚsaveÚnpZndarrayÚcv2ZimwriteÚ
ValueErrorr   © r#   úo/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/pdf_image_utils.pyÚwrite_image   s
    r%   éÈ   Fz Optional[Union[bytes, BinaryIO]]ÚintzOptional[Union[str, PurePath]]ÚboolzOptional[str]z#Union[List[Image.Image], List[str]])ÚfilenameÚfileÚdpiÚoutput_folderÚ	path_onlyÚpasswordÚreturnc                 C  sL   |r|st dƒ‚|dur6t|ƒ}tj|||||d�}ntj| |||d�}|S )z9Get the image renderings of the pdf pages using pdf2imagez4output_folder must be specified if path_only is trueN)r+   r,   Ú
paths_onlyÚuserpw©r+   r,   r0   )r"   r   Ú	pdf2imageÚconvert_from_bytesÚconvert_from_path)r)   r*   r+   r,   r-   r.   Úf_bytesÚimagesr#   r#   r$   Úconvert_pdf_to_image7   s$    
ûür8   z'LayoutElement'zUnion[int, float])ÚelementÚpaddingr/   c                 C  sL   t | ƒ}|j j|8  _|j j|7  _|j j|8  _|j j|7  _|S )z“Increases (or decreases, if padding is negative) the size of the bounding
    boxes of the element by extending the boundary outward (resp. inward))r   ÚbboxÚx1Úx2Úy1Úy2)r9   r:   Zout_elementr#   r#   r$   Úpad_element_bboxesX   s    r@   z!Tuple[float, float, float, float]z+Tuple[Union[int, float], Union[int, float]])r;   r:   r/   c                 C  s@   | \}}}}|\}}||8 }||7 }||8 }||7 }||||fS )zJPads a bounding box (bbox) by a specified horizontal and vertical padding.r#   )r;   r:   r<   r>   r=   r?   Ú	h_paddingÚ	v_paddingr#   r#   r$   Úpad_bboxh   s    rC   Ú zList['Element']zbytes | IO[bytes] | Nonez
str | None)
ÚelementsÚstarting_page_numberÚelement_category_to_saveÚpdf_image_dpir)   r*   Úis_imageÚextract_image_block_to_payloadÚoutput_dir_pathr.   c
           %      C  s®  |s>|p.t jr ttt jƒd ƒntt ¡ d ƒ}tj|dd� t 	¡ ��P}
|rÊ|du r^|g}qìt
|tƒrn|}n| d¡ | ¡ }tj |
d¡}t|dƒ�}| |¡ W d  ƒ n1 s¸0    Y  |g}n"t||||
d|	d�}ttt |ƒ}d}| D �]’}|j|k�rqô|jj}|rô|j�sqô|j}|d \}}|d	 \}}t j}t j}ttttttf t||||f||fƒƒ}|jj�s~J ‚|jj}|| }|d
7 }zÊ|| }t |¡}|  |¡}|j!dk�rÊ| "d¡}|�rt#ƒ } |j$| dd� t% &|  '¡ ¡}!|! (¡ }"|"|j_)d|j_*nR|jt+j,k�rdnd}#|�s,J ‚tj ||#› d|› d|› d�¡}$t-||$ƒ |$|j_.W qô t/t0f�y†   t1j2ddd� Y qô0 qôW d  ƒ n1 �s 0    Y  dS )a”  
    Saves specific elements from a PDF as images either to a directory or embeds them in the
    element's payload.

    This function processes a list of elements partitioned from a PDF file. For each element of
    a specified category, it extracts and saves the image. The images can either be saved to
    a specified directory or embedded into the element's payload as a base64-encoded string.
    ZfiguresT)Úexist_okNr   Útmp_fileÚwb)r,   r-   r.   é   é   ZRGBAÚRGBZJPEG)Úformatz
image/jpegÚtableÚfigureú-ú.jpgz1Image Extraction Error: Skipping the failed image)Úexc_info)3r   ZGLOBAL_WORKING_DIR_ENABLEDr   r   ZGLOBAL_WORKING_PROCESS_DIRÚcwdÚosÚmakedirsÚtempfileÚTemporaryDirectoryr   ÚbytesÚseekÚreadÚpathÚjoinÚopenÚwriter8   r   r   ÚcategoryÚmetadataÚcoordinatesÚpointsZ'EXTRACT_IMAGE_BLOCK_CROP_HORIZONTAL_PADZ%EXTRACT_IMAGE_BLOCK_CROP_VERTICAL_PADr   r'   rC   Úpage_numberr   ZcropÚmodeÚconvertr   r   Úbase64Ú	b64encodeÚgetvalueÚdecodeZimage_base64Zimage_mime_typer   ZTABLEr%   Ú
image_pathr"   ÚIOErrorr   Úwarning)%rE   rF   rG   rH   r)   r*   rI   rJ   rK   r.   Útemp_dirÚimage_pathsZ	file_dataZtmp_file_pathrM   Ú_image_pathsZfigure_numberÚelrf   rg   r<   r>   r=   r?   rA   rB   Zpadded_bboxZmetadata_page_numberZ
page_indexro   r   Zcropped_imageZbufferedZ
img_base64Zimg_base64_strÚbasenameÚoutput_f_pathr#   r#   r$   Úsave_elementsx   sŒ    ÿý

(ú
"ÿ




þ
rx   zOptional[List[str]]z	List[str])Úextract_image_block_typesr/   c                 C  s†   | du rg S t | tƒstdƒ‚dd„ t ¡  ¡ D ƒ}g }| D ]D}| | ¡ | ¡  ¡ ¡}|| ¡ vrvt	 
d|› d�¡ | |¡ q<|S )zBCheck and normalize the provided list of element types to extract.NzjThe extract_image_block_types parameter must be a list of element types as strings, ex. ['Table', 'Image']c                 S  s   i | ]}|  ¡ |“qS r#   )Úlower)Ú.0Úe_typer#   r#   r$   Ú
<dictcomp>÷   ó    z2check_element_types_to_extract.<locals>.<dictcomp>zThe requested type (z") doesn't match any available type)r   ÚlistÚ	TypeErrorr   Úto_dictÚvaluesÚgetrz   Ú
capitalizer   rq   Úappend)ry   Zavailable_element_typesZ$normalized_extract_image_block_typesZel_typeZnormalized_el_typer#   r#   r$   Úcheck_element_types_to_extracté   s     
ÿÿr†   )Útextr/   c                 C  s   | sdS d| vS )z8a helper that determines if the text is valid ascii textFú(cid:r#   ©r‡   r#   r#   r$   Ú
valid_text  s    rŠ   Úfloatc                 C  s6   t | ƒsdS d}t |d| ¡\}}|t|ƒ }|| S )zMGets ratio of unknown 'cid' characters extracted from text to all characters.g        z\(cid\:(\d+)\)rD   )Úis_cid_presentÚreÚsubnÚlen)r‡   Zcid_patternÚ	unmatchedZn_cidÚtotalr#   r#   r$   Ú	cid_ratio  s    r’   c                 C  s"   t | ƒt dƒk rdS |  d¡dkS )z4Checks if a cid code is present in a text selection.z(cid:x)Frˆ   éÿÿÿÿ)r�   Úfindr‰   r#   r#   r$   rŒ     s    rŒ   z'PageLayout'zOptional['PageLayout']©Úinferred_page_layoutÚextracted_page_layoutrK   Úoutput_f_basenamerh   c              
   C  sŽ   d| ddœi}|r |ddœ|d< |  ¡ D ]`\}}| d¡}| d¡}	|j|	d�}
tj ||› d	|› d	|› d
�¡}t|
|ƒ td|› �ƒ q(dS )až  
     Annotates a page image with both inferred and extracted layout elements.

    This function takes the layout elements of a single page, either extracted from or inferred
    for the document, and annotates them on the page image. It creates two separate annotated
    images, one for each set of layout elements: 'inferred' and 'extracted'.
    These annotated images are saved to a specified directory.
    ÚinferredZblue)ÚlayoutÚcolorZgreenZ	extractedrš   r›   )ÚcolorsÚ_rV   zoutput_image_path: N)Úitemsrƒ   ZannotaterY   r`   ra   r%   Úprint)r–   r—   rK   r˜   rh   Z
layout_mapÚlabelZlayout_dataZpage_layoutr›   Úimgrw   r#   r#   r$   Ú#annotate_layout_elements_with_image  s    

ÿ
r¢   z'DocumentLayout'zList['TextRegion']ÚNone)Úinferred_document_layoutÚextracted_layoutr)   rK   rH   rI   r/   c              
   C  sð  ddl m} tj tj |¡¡d }g }�zf|r¶t |¡�j}	|	 d¡}	| 	|	¡ d}
|rn|d|	d�}
|d |
_
| jd }|	|_t| jd |
||dd� W d  ƒ n1 sª0    Y  nØt ¡ �¾}tj|||dd	�}ttt |ƒ}t|ƒD ]„\}}t |¡�`}	|d }d}
|�r$|||	d�}
|| |
_
| j| }|	|_t| j| |
|||d� W d  ƒ qè1 �sb0    Y  qèW d  ƒ n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”  
    Annotates layout elements on images extracted from a PDF or an image file.

    This function processes a given document (PDF or image) and annotates layout elements based
    on the inferred and extracted layout information.
    It handles both PDF documents and standalone image files. For PDFs, it converts each page
    into an image, whereas for image files, it processes the single image.
    r   )r   rQ   NrP   )Únumberr   r•   Tr2   zFile "z" not found!)Ú'unstructured_inference.inference.layoutr   rY   r`   Úsplitextrv   r   rb   rj   r…   rE   Zpagesr   r¢   r[   r\   r3   r5   r   r   r   Ú	enumerateÚ	ExceptionÚisdirÚisfileÚFileNotFoundError)r¤   r¥   r)   rK   rH   rI   r   r˜   r7   r¡   r—   r–   rr   rt   rs   Úiro   rh   Úer#   r#   r$   Úannotate_layout_elements<  sn    

þ

û&
üþ

ûLr°   é
   zOptional[bytes | IO[bytes]]zIterator[Image.Image])r)   r*   Ú
chunk_sizer.   r/   c                 c  s°   t | |d� |d ur,t|ƒ}tj||d�}nd }tj| |d�}|d }td|d |ƒD ]T}t|| d |ƒ}|d urˆtj||||d�}	ntj| |||d�}	|	D ]
}
|
V  qžqVd S )N)r)   r*   )r1   ZPagesrP   )Z
first_pageZ	last_pager1   )	r   r   r3   Zpdfinfo_from_bytesZpdfinfo_from_pathÚrangeÚminr4   r5   )r)   r*   r²   r.   r6   ÚinfoZtotal_pagesZ
start_pageZend_pageZchunk_imagesr   r#   r#   r$   Úconvert_pdf_to_images�  s0    üür¶   c                 C  s,   |   dd¡  dd¡} d dd„ | D ƒ¡}|S )z%Removes control characters from text.ú	ú Ú
rD   c                 s  s$   | ]}t  |¡d  dkr|V  qdS )r   ÚCN)Úunicodedatard   )r{   Úcr#   r#   r$   Ú	<genexpr>º  r~   z,remove_control_characters.<locals>.<genexpr>)Úreplacera   )r‡   Zout_textr#   r#   r$   Úremove_control_characters´  s    r¿   )Nr&   NFN)rD   NFFNN)F)rD   Nr±   N)<Ú
__future__r   rk   rY   r�   r[   r»   Úcopyr   Úior   Úpathlibr   r   Útypingr   r   r	   r
   r   r   r   r   r   r!   Únumpyr    r3   ZPILr   Zunstructured.documents.elementsr   Zunstructured.loggerr   Z$unstructured.partition.common.commonr   r   Z#unstructured.partition.utils.configr   Z)unstructured_inference.inference.elementsr   r§   r   r   Z.unstructured_inference.inference.layoutelementr   r   r%   r8   r@   rC   rx   r†   rŠ   r’   rŒ   r¢   r°   r¶   r¿   r#   r#   r#   r$   Ú<module>   sf   ,     ú!      ö"q
& úT    ü%