
    j]!                         d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZmZ ddlmZmZmZ ddlmZmZ dd	lmZ d
gZ G d d
          ZdS )    )Callable)AnyN)DocumentBuilder)extract_cropsextract_rcropsremove_image_paddingrotate_image   )estimate_orientationrectify_cropsrectify_loc_preds)crop_orientation_predictorpage_orientation_predictor)OrientationPredictor_OCRPredictorc                      e Zd ZU dZedz  ed<   edz  ed<   	 	 	 	 	 d!deded	ed
edededdfdZde	e
j                 de	eeef                  fdZde	e
j                 de	e
j                 dee	eeef                  e	e         f         fdZ	 	 d"de	e
j                 de	e
j                 de	eeef                  dz  de	e         dz  de	e
j                 f
dZe	 	 d#de	e
j                 de	e
j                 dedede	e	e
j                          f
d            Ze	 	 d#de	e
j                 de	e
j                 dededee	e	e
j                          e	e
j                 f         f
d            Zde	e	e
j                          de	e
j                 dee	e	e
j                          e	e
j                 e	eeef                  f         fdZede	e
j                 de	eeef                  de	eeef                  dee	e
j                 e	e	eeef                           e	e	eeef                           f         fd            Zdeddfd ZdS )$r   a  Implements an object able to localize and identify text elements in a set of documents

    Args:
        assume_straight_pages: if True, speeds up the inference by assuming you only pass straight pages
            without rotated textual elements.
        straighten_pages: if True, estimates the page general orientation based on the median line orientation.
            Then, rotates page before passing it to the deep learning modules. The final predictions will be remapped
            accordingly. Doing so will improve performances for documents with page-uniform rotations.
        preserve_aspect_ratio: if True, resize preserving the aspect ratio (with padding)
        symmetric_pad: if True and preserve_aspect_ratio is True, pas the image symmetrically.
        detect_orientation: if True, the estimated general page orientation will be added to the predictions for each
            page. Doing so will slightly deteriorate the overall latency.
        **kwargs: keyword args of `DocumentBuilder`
    Nr   r   TFassume_straight_pagesstraighten_pagespreserve_aspect_ratiosymmetric_paddetect_orientationkwargsreturnc                 ^   || _         || _        |                    dd          | _        |                    dd          | _        |rd nt          d| j                  | _        |s|s|st          d| j                  nd | _        t          di || _        || _	        || _
        g | _        d S )Ndisable_page_orientationFdisable_crop_orientationT)
pretraineddisabled )r   r   pop_page_orientation_disabled_crop_orientation_disabledr   r   r   doc_builderr   r   hooks)selfr   r   r   r   r   r   s          ^/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/predictor/base.py__init__z_OCRPredictor.__init__(   s     &;" 0*0**5OQV*W*W'*0**5OQV*W*W' %gDD+tdFefff 	' "%5=R&$A`aaaa 	'
 +44V44%:"*%'


    pagesc                     t          |                     |                    \  }}}d t          ||          D             }|S )Nc                 H    g | ]\  }}t          ||          D ]	\  }}||f
 S r   zip.0page_classes
page_probsorientationprobs        r&   
<listcomp>z<_OCRPredictor._general_page_orientations.<locals>.<listcomp>J   V     
 
 
(j%(z%B%B
 
 "T $
 
 
 
r(   )r-   r   )r%   r)   _classesprobspage_orientationss         r&   _general_page_orientationsz(_OCRPredictor._general_page_orientationsD   sW       ? ? F FGG7E
 
,/,?,?
 
 

 ! r(   seg_mapsc                 h    |                      |          }d t          ||          D             }||fS )Nc                 4    g | ]\  }}t          ||          S r   r   r/   seq_mapgeneral_orientations      r&   r4   z3_OCRPredictor._get_orientations.<locals>.<listcomp>U   s8     $
 $
 $
,, !*=>>$
 $
 $
r(   r:   r-   )r%   r)   r;   general_pages_orientationsorigin_page_orientationss        r&   _get_orientationsz_OCRPredictor._get_orientationsQ   sP     &*%D%DU%K%K"$
 $
03H>X0Y0Y$
 $
 $
  *+CCCr(   rC   origin_pages_orientationsc                     |r|n|                      |          }|r|nd t          ||          D             }d t          ||          D             S )Nc                 4    g | ]\  }}t          ||          S r   r>   r?   s      r&   r4   z3_OCRPredictor._straighten_pages.<locals>.<listcomp>h   s8       0G0 %W.ABB  r(   c                     g | ]>\  }}t          t          |||j        d          |j        d         k                        ?S )r      )expand)r   r	   shape)r/   pageangles      r&   r4   z3_OCRPredictor._straighten_pages.<locals>.<listcomp>m   sZ     
 
 
 e !dE$*Q-SWS]^_S`B`!a!a!abb
 
 
r(   rB   )r%   r)   r;   rC   rF   s        r&   _straighten_pagesz_OCRPredictor._straighten_pages[   s     +Ep&&$JiJijoJpJp 	#
 )%% 47B\4]4]   	"
 
  #5*CDD
 
 
 	
r(   	loc_predsassume_horizontalc                 z    |rd t          | |          D             }nfdt          | |          D             }|S )Nc           	      L    g | ]!\  }}t          ||d d d df                   "S )N   )r   )r/   rM   _boxess      r&   r4   z1_OCRPredictor._generate_crops.<locals>.<listcomp>{   s6    bbbLD&]42A277bbbr(   c           	      R    g | ]#\  }}t          ||d d d df                   $S )NrT   )rQ   )r   )r/   rM   rU   rQ   s      r&   r4   z1_OCRPredictor._generate_crops.<locals>.<listcomp>}   sM        D& tVAAArrE]FWXXX  r(   r,   )r)   rP   r   rQ   cropss      ` r&   _generate_cropsz_OCRPredictor._generate_cropss   sg     ! 	bbCPUW`LaLabbbEE   $'y$9$9  E r(   c                     t                               | |||          }d |D             }d t          ||          D             }d t          ||          D             }||fS )Nc                 &    g | ]}d  |D             S )c                 J    g | ] }t          d  |j        D                       !S )c              3   "   K   | ]
}|d k    V  dS )r   Nr   )r/   ss     r&   	<genexpr>zE_OCRPredictor._prepare_crops.<locals>.<listcomp>.<listcomp>.<genexpr>   s&      22!A222222r(   )allrL   )r/   crops     r&   r4   z;_OCRPredictor._prepare_crops.<locals>.<listcomp>.<listcomp>   s1    JJJtC22tz22222JJJr(   r   )r/   
page_cropss     r&   r4   z0_OCRPredictor._prepare_crops.<locals>.<listcomp>   s'    ccczJJzJJJcccr(   c                 H    g | ]\  }}d  t          ||          D              S )c                     g | ]	\  }}||
S r   r   )r/   r`   _kepts      r&   r4   z;_OCRPredictor._prepare_crops.<locals>.<listcomp>.<listcomp>   s!    IIIkdE5ITIIIr(   r,   )r/   ra   	page_kepts      r&   r4   z0_OCRPredictor._prepare_crops.<locals>.<listcomp>   sE     
 
 
%
I JISY%?%?III
 
 
r(   c                 $    g | ]\  }}||         S r   r   )r/   rU   rd   s      r&   r4   z0_OCRPredictor._prepare_crops.<locals>.<listcomp>   s     PPP}vuVE]PPPr(   )r   rX   r-   )r)   rP   r   rQ   rW   is_kepts         r&   _prepare_cropsz_OCRPredictor._prepare_crops   s     --eY@UWhii dc]bccc
 
),UG)<)<
 
 
 QPIw8O8OPPP	ir(   rW   c                      t           fd|D              \  }}}d t          ||          D             }d t          ||          D             }d t          ||          D             }|||fS )Nc                 :    g | ]}                     |          S r   )r   )r/   ra   r%   s     r&   r4   z0_OCRPredictor._rectify_crops.<locals>.<listcomp>   s(    ,q,q,q]gT-L-LZ-X-X,q,q,qr(   c                 4    g | ]\  }}t          ||          S r   )r   )r/   ra   r2   s      r&   r4   z0_OCRPredictor._rectify_crops.<locals>.<listcomp>   s'    uuuAX[mJ<<uuur(   c                 ^    g | ]*\  }}t          |          d k    rt          ||          n|+S )r   )lenr   )r/   page_loc_predsr2   s      r&   r4   z0_OCRPredictor._rectify_crops.<locals>.<listcomp>   sQ     
 
 
+ ?B.>Q>QTU>U>Unk:::[i
 
 
r(   c                 H    g | ]\  }}t          ||          D ]	\  }}||f
 S r   r,   r.   s        r&   r4   z0_OCRPredictor._rectify_crops.<locals>.<listcomp>   r5   r(   r,   )	r%   rW   rP   orientationsr7   r8   
rect_cropsrect_loc_predscrop_orientationss	   `        r&   _rectify_cropsz_OCRPredictor._rectify_crops   s     (+,q,q,q,qkp,q,q,q'r$guuu\_`egs\t\tuuu

 
/29l/K/K
 
 


 
,/,?,?
 
 

 >+<<<r(   
word_predsrs   c                    g }g }t          |           dk    rmd}| D ]h}|                    ||||j        d         z                       |                    ||||j        d         z                       ||j        d         z  }i| ||fS )Nr   )rm   appendrL   )rP   ru   rs   
text_predscrop_orientation_preds_idx
page_boxess          r&   _process_predictionsz"_OCRPredictor._process_predictions   s     
!#y>>AD' , ,
!!*TD:;KA;N4N-N"OPPP&--.?tjN^_`NaGa@a.bccc
(++*&<<<r(   hookc                 :    | j                             |           dS )zAdd a hook to the predictor

        Args:
            hook: a callable that takes as input the `loc_preds` and returns the modified `loc_preds`
        N)r$   rw   )r%   r}   s     r&   add_hookz_OCRPredictor.add_hook   s      	
$r(   )TFTTF)NN)FF)__name__
__module____qualname____doc__r   __annotations__boolr   r'   listnpndarraytupleintfloatr:   rE   rO   staticmethodrX   rh   rt   strdictr|   r   r   r   r(   r&   r   r      s          !5t ;;;; 4t ;;;; '+!&&*"#(( (#( (  $	(
 ( !( ( 
( ( ( (8!BJ! 
eCJ	 ! ! ! !D"*%D15bj1AD	tE#u*%&S	1	2D D D D FJ6:
 
BJ
 rz"
 %)sEz):$;d$B	

 $(9t#3
 
bj	
 
 
 
0  ',"'	 BJ
#  $  	
 
d2:	   \  ',"'	   BJ 
#   $   	 
 
tD$%tBJ'77	8      \ $=D$%= 
#= 
tD$%tBJ'7eCJ>O9PP	Q	= = = =( =
#=sEz*+=  S#X/= 
tBJd5e+<&=!>T$sTWx.EY@ZZ	[	= = = \=" X  $            r(   )collections.abcr   typingr   numpyr   doctr.models.builderr   doctr.utils.geometryr   r   r   r	   _utilsr   r   r   classificationr   r   classification.predictorr   __all__r   r   r(   r&   <module>r      s    % $ $ $ $ $           0 0 0 0 0 0 b b b b b b b b b b b b K K K K K K K K K K S S S S S S S S ; ; ; ; ; ;
m  m  m  m  m  m  m  m  m  m r(   