o
    'j<                  	   @   s  d dl Z d dlZe je jeZeje ejd e je j	ed de j
d< d dlZd dlZd dlZd dlZd dlm  mZ d dlmZ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  e Z!dd Z"G dd de#Z$dd Z%e&dkre%e   dS dS )    Nz../..Zauto_growthZFLAGS_allocator_strategy)create_operators	transform)build_post_process)
get_logger)get_image_file_listcheck_and_read)draw_rectangle)
parse_argsc                 C   s   dd| j ii}dd| j | j gii}d| jdvrg dng d| jdvr'g d	ng dd
ddi}dd i}ddddgii}| jdvrJ|||||g}|S |||||g}|S )NZResizeTableImagemax_lenZPaddingTableImagesizeZNormalizeImageZTableMaster)gZd;O?gy&1?g?)      ?r   r   )g
ףp=
?gv/?gCl?z1./255.Zhwc)ZstdmeanscaleorderZ
ToCHWImageZKeepKeysZ	keep_keysimageshape)Ztable_max_lentable_algorithm)argsZ	resize_opZpad_opZnormalize_opZ	to_chw_opZkeep_keys_oppre_process_list r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddleocr/ppstructure/table/predict_structure.pybuild_pre_process_list'   s2   





r   c                   @   s   e Zd Zdd Zdd ZdS )TableStructurerc                 C   s   || _ |j| _t|}|jdvrd|j|jd}n	d|jd|jd}t|| _t|| _	t
|dt\| _| _| _| _|jrgdd l}t }t
 }|jd|jd	d
d | j|d |jr[|nd g ddtd| _d S d S )Nr   ZTableLabelDecode)namecharacter_dict_pathmerge_no_span_structureZTableMasterLabelDecodepad)r   r   Z	box_shaper   tabler      Zdynamic)Zpreprocess_timeZinference_timeZpostprocess_time)Z
model_nameZmodel_precisionZ
batch_sizeZ
data_shapeZ	save_pathZinference_configZpidsZprocess_nameZgpu_idsZ	time_keysZwarmuplogger)r   use_onnxr   r   Ztable_char_dict_pathr   r   preprocess_opr   postprocess_oputilityZcreate_predictorr    	predictorinput_tensoroutput_tensorsconfig	benchmarkauto_logosgetpidZget_infer_gpuidZ
AutoLogger	precisionZuse_gpuautolog)selfr   r   Zpostprocess_paramsr*   pidZgpu_idr   r   r   __init__F   sH   


zTableStructurer.__init__c                 C   s  t   }| jjr| jj  | }d|i}t|| j}|d }|d u r&dS t	j
|dd}| }| jjr;| jj  | jrOi }||| jj< | j| j|}n&| j| | j  g }| jD ]}| }|| q_| jjru| jj  i }	|d |	d< |d |	d< t	j
|d dd}
| |	|
g}|d	 d }|d
 d }|d }g d| g d }t   | }| jjr| jjjdd ||f|fS )Nr   r   )Nr   )Zaxisr   Zstructure_probsZ	loc_predsZstructure_batch_listZbbox_batch_list)z<html>z<body>z<table>)z</table>z</body>z</html>T)stamp)timer   r)   r.   timesstartcopyr   r"   npZexpand_dimsr3   r!   r&   r   r%   runr'   Zcopy_from_cpuZcopy_to_cpuappendr#   end)r/   imgZ	starttimeZori_imdataZ
input_dictZoutputsZoutput_tensoroutputpredsZ
shape_listZpost_resultstructure_str_list	bbox_listelapser   r   r   __call__q   sT   

zTableStructurer.__call__N)__name__
__module____qualname__r1   rC   r   r   r   r   r   E   s    +r   c                 C   s  t | j}t| }d}d}tj| jdd ttj| jdddd}|D ]}t	|\}}}	|s5t
|}|d u rBtd| q%||\}
}|
\}}t| }td	|| |d
|| t|dkryt|d dkryt||}nt||}tj| jtj|}t
|| td| |dkr||7 }|d7 }td|| q%W d    n1 sw   Y  | jr|j  d S d S )Nr   T)exist_okz	infer.txtwzutf-8)modeencodingzerror in loading image:{}zresult: {}, {}zresult: {}, {}
   zsave vis result to {}r   zPredict time of {}: {})r   Z	image_dirr   r+   makedirsr>   openpathjoinr   cv2Zimreadr    infoformatjsondumpstolistwritelenr   r$   Z
draw_boxesbasenameZimwriter)   r.   report)r   Zimage_file_listZtable_structurercount
total_timeZf_wZ
image_filer<   flag_Zstructure_resrB   r@   rA   Zbbox_list_strZimg_save_pathr   r   r   main   sV   





r^   __main__)'r+   sysrN   dirnameabspath__file____dir__r:   insertrO   environrP   numpyr8   r4   rS   Ztools.infer.utilityZinferr$   Z
ppocr.datar   r   Zppocr.postprocessr   Zppocr.utils.loggingr   Zppocr.utils.utilityr   r   Zppocr.utils.visualr   Zppstructure.utilityr	   r    r   objectr   r^   rD   r   r   r   r   <module>   s0    
\(