o
    el                  	   @   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Zd dlZd dlm  mZ d dlmZ d dlmZ d dlmZmZ e ZG dd	 d	eZd
d Ze dkr}ee!  dS dS )    Nz../..Zauto_growthZFLAGS_allocator_strategy)build_post_process)
get_logger)get_image_file_listcheck_and_readc                   @   s$   e Zd Zdd Zdd Zdd ZdS )TextClassifierc                 C   sf   dd |j dD | _ |j| _|j| _d|jd}t|| _t|dt	\| _
| _| _}|j| _d S )Nc                 S   s   g | ]}t |qS  )int).0vr   r   QD:\Projects\ConvertPro\env\Lib\site-packages\paddleocr/tools/infer/predict_cls.py
<listcomp>(   s    z+TextClassifier.__init__.<locals>.<listcomp>,ZClsPostProcess)name
label_listcls)cls_image_shapesplitcls_batch_num
cls_threshr   r   postprocess_oputilityZcreate_predictorlogger	predictorinput_tensoroutput_tensorsuse_onnx)selfargsZpostprocess_params_r   r   r   __init__'   s   
zTextClassifier.__init__c                 C   s   | j \}}}|jd }|jd }|t| }t|| |kr"|}n	tt|| }t|||f}	|	d}	| j d dkrM|	d }	|	t	j
d d f }	n|	dd }	|	d8 }	|	d }	t	j|||ft	jd}
|	|
d d d d d|f< |
S )Nr      float32   )   r   r    g      ?)Zdtype)r   shapefloatmathceilr   cv2resizeZastypenpnewaxisZ	transposeZzerosr!   )r   imgZimgCZimgHZimgWhwratioZ	resized_wZresized_imageZ
padding_imr   r   r   resize_norm_img4   s$   


zTextClassifier.resize_norm_imgc                 C   s  t |}t|}g }|D ]}||jd t|jd   qtt|}ddgg| }| j	}d}t
d||D ]}	t||	| }
g }d}t }t
|	|
D ]}|||  jdd \}}|d | }t||}qOt
|	|
D ]}| |||  }|tjd d f }|| qot|}|  }| jri }||| jj< | j| j|}|d }n| j| | j  | jd  }| j  | |}|t | 7 }t
t|D ]/}|| \}}||g|||	|  < d|v r|| jkrt|||	|   d|||	|  < qq9|||fS )Nr    r    g        r#   g      ?Z180)copydeepcopylenappendr$   r%   r*   Zargsortarrayr   rangemintimemaxr0   r+   Zconcatenater   r   r   r   runr   Zcopy_from_cpuZcopy_to_cpuZtry_shrink_memoryr   r   r(   rotate)r   img_listZimg_numZ
width_listr,   indicescls_resZ	batch_numZelapseZ
beg_img_noZ
end_img_noZnorm_img_batchZmax_wh_ratioZ	starttimeinor-   r.   Zwh_ratioZnorm_imgZ
input_dictZoutputsZprob_outZ
cls_resultZrnolabelZscorer   r   r   __call__J   sZ   
 





zTextClassifier.__call__N)__name__
__module____qualname__r   r0   rB   r   r   r   r   r   &   s    r   c              
   C   s   t | j}t| }g }g }|D ]'}t|\}}}|st|}|d u r,td| q|	| |	| qz	||\}}	}
W n! t
ya } ztt  t| t  W Y d }~nd }~ww tt|D ]}td|| |	|  qhd S )Nzerror in loading image:{}zPredicts of {}:{})r   Z	image_dirr   r   r(   Zimreadr   infoformatr5   	Exception	traceback
format_excexitr7   r4   )r   Zimage_file_listZtext_classifierZvalid_image_file_listr=   Z
image_filer,   flagr   r?   Zpredict_timeEr@   r   r   r   main}   s4   



rN   __main__)"ossyspathdirnameabspath__file____dir__r5   insertjoinenvironr(   r2   numpyr*   r&   r9   rI   Ztools.infer.utilityZinferr   Zppocr.postprocessr   Zppocr.utils.loggingr   Zppocr.utils.utilityr   r   r   objectr   rN   rC   
parse_argsr   r   r   r   <module>   s,    
W