a
    ¡˜ëjD  ã                   @   s   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
mZmZmZmZ d dlmZmZmZmZmZmZ eee dœdd	„Zed
œdd„Zdddddddddœeeeee ee eeeeee edœdd„Zdeeee eeee edœdd„ZdS )é    N)ÚBytesIO)ÚPath)ÚOptional)ÚAnalysisDrawerÚFinalLayoutDrawerÚLayoutDrawerÚOCRLayoutDrawerÚODModelLayoutDrawerÚPdfminerLayoutDrawer)ÚExtractedLayoutDumperÚFinalLayoutDumperÚJsonLayoutDumperÚLayoutDumperÚObjectDetectionLayoutDumperÚOCRLayoutDumper)ÚdumperÚreturnc                 C   sr   t | tƒrt|  ¡ d�S t | tƒr0t|  ¡ d�S t | tƒrHt|  ¡ d�S t | tƒr`t	|  ¡ d�S t
d| › �ƒ‚dS )zýFor a given layout dumper, return the corresponding layout drawer instance initialized with
    a dumped layout dict.

    Args:
        dumper: The layout dumper instance

    Returns:
        LayoutDrawer: The corresponding layout drawer instance
    ©Úlayout_dumpzUnknown dumper type: N)Ú
isinstancer   r	   Údumpr   r
   r   r   r   r   Ú
ValueError)r   © r   ún/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/analysis/tools.pyÚ_get_drawer_for_dumper   s    




r   )Úis_imagec                 C   s.   t  ¡ jdd… }| r"d|› d�S d|› d�S )ziGenerate a filename for the analysis artifacts based on the file type.
    Adds a random uuid suffix
    Né   Zimage_z.pngZpdf_z.pdf)ÚuuidÚuuid4Úhex)r   Úsuffixr   r   r   Ú_generate_filename/   s    r!   FTÚpng)ÚfilenameÚfileÚskip_bboxesÚskip_dump_odÚ	draw_gridÚdraw_captionÚresizeÚformat)Úlayout_dumpersr   Úanalyzed_image_output_dir_pathr#   r$   r%   r&   r'   r(   r)   r*   c        
      
   G   sž   |st | ƒ}|s|rdS t|ƒ}|jddd� |sZt||d�}|
D ]}| |¡ qB| ¡  |sšt||| |||||	d�}|
D ]}t|ƒ}| |¡ qz| ¡  dS )a¹  Save the analysis artifacts for a given file. Loads some settings from
    the environment configuration.

    Args:
        layout_dumpers: The layout dumpers to save and use for bboxes rendering
        is_image: Flag for the file type (pdf/image)
        analyzed_image_output_dir_path: The directory to save the analysis artifacts
        filename: The filename of the sources analyzed file (pdf/image).
            Only one of filename or file should be provided.
        file: The file object for the analyzed file.
            Only one of filename or file should be provided.
        draw_grid: Flag for drawing the analysis bboxes on a single image (as grid)
        draw_caption: Flag for drawing the caption above the analyzed page (for e.g. layout source)
        resize: Output image resize value. If not provided, the image will not be resized.
        format: The format for analyzed pages with bboxes drawn on them. Default is 'png'.
    NT©ÚparentsÚexist_ok)r#   Úsave_dir)r#   r$   r   r0   r'   r(   r)   r*   )	r!   r   Úmkdirr   Zadd_layout_dumperÚprocessr   r   Ú
add_drawer)r   r,   r#   r$   r%   r&   r'   r(   r)   r*   r+   Úoutput_pathZjson_layout_dumperZlayout_dumperÚanalysis_drawerÚdrawerr   r   r   Úsave_analysis_artifiacts9   s:    þør7   )r#   r,   Úrenders_output_dir_pathr'   r(   r)   r*   c              	   C   sb  t | ƒj}t | ƒj d¡ }t |ƒd | d }	|	 ¡ s<dS g }
|	 ¡ D ]¨}| ¡ sVqHt|ƒ�}t 	|¡}W d  ƒ n1 s~0    Y  |jdkr¢|
 
t|d�¡ |jdkr¼|
 
t|d�¡ |jdkrÖ|
 
t|d�¡ |jd	krH|
 
t|d�¡ qH|
�r^|�st |ƒd | d
 }nt |ƒ}|jddd� t| ||||||d�}|
D ]}| |¡ �qD| ¡  dS )a‚  Render the bounding boxes for a given layout dimp file.
    To be used for analysis after the partition is performed for
    only dumping the layouts - the bboxes can be rendered later.

    Expects that the analyzed_image_output_dir_path keeps the structure
    that was created by the save_analysis_artifacts function.

    Args:
        filename: The filename of the sources analyzed file (pdf/image)
        analyzed_image_output_dir_path: The directory where the analysis artifacts
          (layout dumps) are saved. It should be the root directory of the structure
          created by the save_analysis_artifacts function.
        renders_output_dir_path: Optional directory to save the rendered bboxes -
          if not provided, it will be saved in the analysis directory.
        draw_grid: Flag for drawing the analysis bboxes on a single image (as grid)
        draw_caption: Flag for drawing the caption above the analyzed page (for e.g. layout source)
        resize: Output image resize value. If not provided, the image will not be resized.
        format: The format for analyzed pages with bboxes drawn on them. Default is 'png'.
    ZpdfZanalysisr   NÚfinalr   Zobject_detectionZocrZpdfminerZbboxesTr-   )r#   r0   r   r'   r(   r)   r*   )r   Ústemr    ÚendswithÚexistsÚiterdirÚis_fileÚopenÚjsonÚloadÚappendr   r	   r   r
   r1   r   r3   r2   )r#   r,   r8   r'   r(   r)   r*   Zfilename_stemr   Zanalysis_dumps_dirZlayout_drawersZanalysis_dump_filenameÚfr   r4   r5   r6   r   r   r   Úrender_bboxes_for_filex   sL    
ÿ
(



ÿù
rD   )NFTNr"   )r@   r   Úior   Úpathlibr   Útypingr   Z<unstructured.partition.pdf_image.analysis.bbox_visualisationr   r   r   r   r	   r
   Z5unstructured.partition.pdf_image.analysis.layout_dumpr   r   r   r   r   r   r   Úboolr!   ÚstrÚfloatr7   rD   r   r   r   r   Ú<module>   sV     
õõB     ùù