o
    0j0                     @   s   d dl Z d dlZd dlmZmZ d dlZddlmZm	Z	 ddl
mZ e	dr*d dlZddejd	ee d
dfddZejedG dd dZejG dd dZejG dd deZejG dd dZdS )    N)ListOptional   )class_requires_depsis_dep_available   )	benchmarkzopencv-contrib-pythonimgindexreturnc                 C   s   | j dk rtdt| j dt| jd t| jd }}|dks,|dks,| jdkrD|dur5d| nd}td	t| j | ddS )
zMRaise ValueError if *img* has zero height or width (invalid for recognition).   zFText recognition expects images with at least 2 dimensions; got shape .r      Nz at input index  zKText recognition requires images with positive height and width; got shape )ndim
ValueErrortupleshapeintsize)r	   r
   hwsuffix r   u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/text_recognition/processors.pyvalidate_text_rec_image_array   s"   
r   c                       sJ   e Zd ZdZg ddf fdd	Zdd Zdd	 Zd
d Zdd Z  Z	S )OCRReisizeNormImgz&for ocr image resize and normalization)r   0   i@  Nc                    s    t    || _|| _d| _d S )Ni  )super__init__rec_image_shapeinput_shapemax_imgW)selfr    r!   	__class__r   r   r   2   s   

zOCRReisizeNormImg.__init__c                 C   s  | j \}}}||jd ksJ t|| }|| jkr*t|| j|f}| j}| j}n,|jdd \}}	|	t| }
t||
 |krE|}n	tt||
 }t|||f}|	d}|
dd }|d8 }|d }tj|||ftjd}||ddddd|f< |S )	zresize and normalize the imgr   Nfloat32r   r   r            ?dtyper   )r    r   r   r"   cv2resizefloatmathceilastype	transposenpZzerosr&   )r#   r	   max_wh_ratioimgCimgHimgWresized_imageZ	resized_wr   r   ratioZ
padding_imr   r   r   resize_norm_img8   s(   

z!OCRReisizeNormImg.resize_norm_imgc                    sL   t |D ]
\}}t||d q jdu r fdd|D S  fdd|D S )apply)r
   Nc                       g | ]}  |qS r   )r-   .0r	   r#   r   r   
<listcomp>V       z.OCRReisizeNormImg.__call__.<locals>.<listcomp>c                    r<   r   )staticResizer=   r?   r   r   r@   X   rA   )	enumerater   r!   )r#   imgsir	   r   r?   r   __call__Q   s
   
zOCRReisizeNormImg.__call__c           	      C   sL   | j \}}}|| }|jd d \}}|d | }t||}| ||}|S )Nr   g      ?)r    r   maxr:   )	r#   r	   r5   r6   r7   r4   r   r   wh_ratior   r   r   r-   Z   s   
zOCRReisizeNormImg.resizec                 C   sF   | j \}}}t|t|t|f}|dd }|d8 }|d }|S )Nr'   r(   r)   )r!   r,   r-   r   r2   )r#   r	   r5   r6   r7   r8   r   r   r   rB   c   s   zOCRReisizeNormImg.staticResize)
__name__
__module____qualname____doc__r   r:   rF   r-   rB   __classcell__r   r   r$   r   r   -   s    		r   c                       sZ   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Z			dddZdd Z	dd Z
  ZS )BaseRecLabelDecode)Convert between text-label and text-indexNTc                    sl   t    d| _|d urt|ntd}|r|d | |}i | _t|D ]	\}}|| j|< q'|| _d S )NFZ$0123456789abcdefghijklmnopqrstuvwxyz )	r   r   reverselistappendadd_special_chardictrC   	character)r#   Zcharacter_struse_space_charcharacter_listrE   charr$   r   r   r   p   s   



zBaseRecLabelDecode.__init__c                 C   sr   g }d}|D ]}t td|s!|dkr|| || d}q||7 }q|dkr/|| d|ddd S )pred_reverser   z[a-zA-Z0-9 :*./%+-]N)boolresearchrS   join)r#   predZpred_reZ	c_currentcr   r   r   rZ      s   



zBaseRecLabelDecode.pred_reversec                 C   s   |S )rT   r   r#   rX   r   r   r   rT      s   z#BaseRecLabelDecode.add_special_charc                 C   s^  d}g }g }g }g }g }t |dkd }	t|D ]{\}
}d|  kr'dkr,n nd}nttd|r7d}nd	}|d
krW|dkrW|
d t|k rWttd||
d  rWd}|dkra|dkrad}|du rg|}||krt|dkr|| || || g }g }|}|| |t|	|
  qt|dkr|| || || |||fS )u  
        Group the decoded characters and record the corresponding decoded positions.

        Args:
            text: the decoded text
            selection: the bool array that identifies which columns of features are decoded as non-separated characters
        Returns:
            word_list: list of the grouped words
            word_col_list: list of decoding positions corresponding to each character in the grouped word
            state_list: list of marker to identify the type of grouping words, including two types of grouping words:
                        - 'cn': continuous chinese characters (e.g., 你好啊)
                        - 'en&num': continuous english characters (e.g., hello), number (e.g., 123, 1.123), or mixed of them connected by '-' (e.g., VGG-16)
                        The remaining characters in text are treated as separators between groups (e.g., space, '(', ')', etc.).
        NTr   u   一u   鿿Zcnz[a-zA-Z0-9]zen&numsymbolr   r   z[0-9]-)	r3   whererC   r\   r]   r^   lenrS   r   )r#   text	selectionstateZword_contentZword_col_content	word_listword_col_list
state_listZ	valid_colZc_irY   Zc_stater   r   r   get_word_info   sL   







z BaseRecLabelDecode.get_word_infoFc              	      sF  g }   }t|}t|D ]}tjt|| td}	|r1|| dd || dd k|	dd< |D ]
}
|	|| |
kM }	q3 fdd|| |	 D }|durV|| |	 }ndgt|	 }t|dkrfdg}d|} jrs |}|r 	||	\}}}|
|t| t|| |||gf q|
|t| f q|S )	z#convert text-index into text-label.r*   r   Nr[   c                    s   g | ]} j | qS r   )rV   )r>   Ztext_idr?   r   r   r@      s    
z-BaseRecLabelDecode.decode.<locals>.<listcomp>r   r   )get_ignored_tokensrf   ranger3   Zonesr\   r_   rQ   rZ   rm   rS   meantolist)r#   Z
text_indexZ	text_probis_remove_duplicatereturn_word_boxZresult_listZignored_tokensZ
batch_sizeZ	batch_idxrh   Zignored_tokenZ	char_listZ	conf_listrg   rj   rk   rl   r   r?   r   decode   sH   (





zBaseRecLabelDecode.decodec                 C   s   dgS )rn   r   r   r?   r   r   r   rn   
  s   z%BaseRecLabelDecode.get_ignored_tokensc           	      C   s   t |}t|tst|tr|d }|jdd}|jdd}| j||dd}g }g }|D ]}||d  ||d  q-||fS )r;   r[   ZaxisT)rr   r   r   )	r3   array
isinstancer   rR   argmaxrG   rt   rS   )	r#   r`   preds	preds_idx
preds_probrg   textsscorestr   r   r   rF     s   
zBaseRecLabelDecode.__call__NT)NFF)rI   rJ   rK   rL   r   rZ   rT   rm   rt   rn   rF   rM   r   r   r$   r   rN   l   s    B
5rN   c                       s4   e Zd ZdZd fdd	ZdddZd	d
 Z  ZS )CTCLabelDecoderO   NTc                    s   t  j||d d S )N)rW   )r   r   )r#   rX   rW   r$   r   r   r   "  s   zCTCLabelDecode.__init__Fc                 K   s   t |d }|jdd}|jdd}| j||d|d}|r?t|D ]\}}	|d | }
|d }|	d d |
|  |	d d< q"g }g }|D ]}|t|dkrS|d n|d |d f ||d	  qE||fS )
r;   r   r[   ru   T)rr   rs   Zwh_ratio_listr4   r   r   )r3   rv   rx   rG   rt   rC   rS   rf   )r#   r`   rs   kwargsry   rz   r{   rg   Zrec_idxZrecrH   r4   r|   r}   r~   r   r   r   rF   %  s(   *zCTCLabelDecode.__call__c                 C   s   dg| }|S )rT   blankr   rb   r   r   r   rT   <  s   
zCTCLabelDecode.add_special_charr   )F)rI   rJ   rK   rL   r   rF   rT   rM   r   r   r$   r   r     s
    
r   c                   @   sL   e Zd ZdZdeej deej fddZdeej deej fddZdS )	ToBatchz;A class for batching and padding images to a uniform width.rD   r   c           
      C   s^   t dd |D }g }|D ]}|j\}}}|| }tj|ddd|ffddd}	||	 q|S )zPad images to the maximum width in the batch.

        Args:
            imgs (list of np.ndarrays): List of images to pad.

        Returns:
            list of np.ndarrays: List of padded images.
        c                 s   s    | ]}|j d  V  qdS )r   N)r   r=   r   r   r   	<genexpr>O  s    z%ToBatch.__pad_imgs.<locals>.<genexpr>)r   r   r   Zconstant)modeZconstant_values)rG   r   r3   padrS   )
r#   rD   	max_widthZpadded_imgsr	   _heightwidthZ	pad_widthZ
padded_imgr   r   r   Z
__pad_imgsF  s   	zToBatch.__pad_imgsc                 C   s&   |  |}tj|ddjtjddgS )zCall method to pad images and stack them into a batch.

        Args:
            imgs (list of np.ndarrays): List of images to process.

        Returns:
            list of np.ndarrays: List containing a stacked tensor of the padded images.
        r   ru   F)r+   copy)_ToBatch__pad_imgsr3   stackr1   r&   )r#   rD   r   r   r   rF   ]  s   
	zToBatch.__call__N)	rI   rJ   rK   rL   r   r3   ndarrayr   rF   r   r   r   r   r   B  s    "r   )N)r/   r]   typingr   r   numpyr3   Z
utils.depsr   r   Zutils.benchmarkr   r,   r   r   r   Ztimeitr   rN   r   r   r   r   r   r   <module>   s&   = 2#