
    jjg                        d dl mZ d dlmZ  e             d dlmZ d dlmZ	 d dlm
Z
 d dlZd dlZd dlmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZ 	 d dlmZmZ n# e$ r Y nw xY wg dZ G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z# G d de          Z$ G d de          Z% G d de          Z& G d  d!e&          Z'dS )"    )Any)defuse_stdlib)ElementTree)Element)
SubElementN)requires_package)BoundingBox)resolve_enclosing_bboxresolve_enclosing_rbbox)synthesize_kie_pagesynthesize_page)NestedObject)visualize_kie_pagevisualize_page)	r   WordArtefactLine
PredictionBlockPageKIEPageDocumentc                       e Zd ZU dZg Zee         ed<   g Zee         ed<   de	ddfdZ
deee	f         fdZed	eee	f         fd
            ZdefdZdS )r   zVImplements an abstract document element with exporting and text rendering capabilities_children_names_exported_keyskwargsreturnNc                     |                                 D ]>\  }}|| j        v rt          | ||            t          | j        j         d| d          d S )Nz+ object does not have any attribute named '')itemsr   setattrKeyError	__class____name__)selfr   kvs       T/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/io/elements.py__init__zElement.__init__&   ss    LLNN 	l 	lDAqD(((a####$."9jjfgjjjkkk		l 	l    c                       fd j         D             } j        D ]S}|dv r0d t           |                                          D             ||<   6d t           |          D             ||<   T|S )z,Exports the object into a nested dict formatc                 2    i | ]}|t          |          S  )getattr).0r&   r%   s     r(   
<dictcomp>z"Element.export.<locals>.<dictcomp>/   s%    HHHqq'$**HHHr*   predictionsc                 .    i | ]\  }}|d  |D             S )c                 6    g | ]}|                                 S r-   export)r/   items     r(   
<listcomp>z-Element.export.<locals>.<dictcomp>.<listcomp>3   s     444$444r*   r-   )r/   r&   cs      r(   r0   z"Element.export.<locals>.<dictcomp>2   s:     . . .9=AA44!444. . .r*   c                 6    g | ]}|                                 S r-   r5   )r/   r9   s     r(   r8   z"Element.export.<locals>.<listcomp>6   s     -_-_-_Qahhjj-_-_-_r*   )r   r   r.   r    )r%   export_dictchildren_names   `  r(   r6   zElement.export-   s    HHHHD4GHHH!1 	` 	`M//. .AH}A]A]AcAcAeAe. . .M** .`-_'$P]B^B^-_-_-_M**r*   	save_dictc                     t           NNotImplementedErrorclsr=   r   s      r(   	from_dictzElement.from_dict:   s    !!r*   c                     t           r?   r@   r%   s    r(   renderzElement.render>   s    !!r*   )r$   
__module____qualname____doc__r   liststr__annotations__r   r   r)   dictr6   classmethodrD   rG   r-   r*   r(   r   r       s         ``!#OT#Y### "NDI"""l l l l l lS#X     "$sCx. " " " ["" " " " " " "r*   r   c                        e Zd ZU dZg dZee         ed<   g Zee         ed<   dede	de
ej        z  de	d	eeef         d
df fdZd
efdZd
efdZedeeef         fd            Z xZS )r   a  Implements a word element

    Args:
        value: the text string of the word
        confidence: the confidence associated with the text prediction
        geometry: bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to
        the page's size
        objectness_score: the objectness score of the detection
        crop_orientation: the general orientation of the crop in degrees and its confidence
    )value
confidencegeometryobjectness_scorecrop_orientationr   r   rQ   rR   rS   rT   rU   r   Nc                     t                                                       || _        || _        || _        || _        || _        d S r?   )superr)   rQ   rR   rS   rT   rU   )r%   rQ   rR   rS   rT   rU   r#   s         r(   r)   zWord.__init__Q   sF     	
$  0 0r*   c                     | j         S $Renders the full text of the elementrQ   rF   s    r(   rG   zWord.render`   
    zr*   c                 (    d| j          d| j        dS )Nvalue='', confidence=.2rQ   rR   rF   s    r(   
extra_reprzWord.extra_reprd   s    GGG4?GGGGr*   r=   c                 :    fd| j         D             } | di |S )Nc                 "    i | ]}||         S r-   r-   r/   r&   r=   s     r(   r0   z"Word.from_dict.<locals>.<dictcomp>i       >>>a!Yq\>>>r*   r-   r   rB   s    ` r(   rD   zWord.from_dictg   1    >>>>3+=>>>s}}V}}r*   )r$   rH   rI   rJ   r   rK   rL   rM   r   floatr	   npndarrayrN   r   r)   rG   rb   rO   rD   __classcell__r#   s   @r(   r   r   B   s-        	 	 !l k kNDIkkk!#OT#Y###11 1 
*	1
  1 sCx.1 
1 1 1 1 1 1    HC H H H H $sCx.    [    r*   r   c                        e Zd ZU dZg dZee         ed<   g Zee         ed<   dede	de
dd	f fd
ZdefdZdefdZedeeef         fd            Z xZS )r   a'  Implements a non-textual element

    Args:
        artefact_type: the type of artefact
        confidence: the confidence of the type prediction
        geometry: bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to
            the page's size.
    )rS   typerR   r   r   artefact_typerR   rS   r   Nc                 r    t                                                       || _        || _        || _        d S r?   )rW   r)   rS   ro   rR   )r%   rp   rR   rS   r#   s       r(   r)   zArtefact.__init__z   s2     !	$r*   c                 <    d| j                                          dS )rZ   [])ro   upperrF   s    r(   rG   zArtefact.render   s    '49??$$''''r*   c                 (    d| j          d| j        dS )Nztype='r_   r`   )ro   rR   rF   s    r(   rb   zArtefact.extra_repr   s    E	EEEEEEr*   r=   c                 :    fd| j         D             } | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z&Artefact.from_dict.<locals>.<dictcomp>   rf   r*   r-   rg   rB   s    ` r(   rD   zArtefact.from_dict   rh   r*   )r$   rH   rI   rJ   r   rK   rL   rM   r   ri   r	   r)   rG   rb   rO   rN   r   rD   rl   rm   s   @r(   r   r   m   s          !C B BNDIBBB!#OT#Y###%c %u % %X\ % % % % % %( ( ( ( (FC F F F F $sCx.    [    r*   r   c            	            e Zd ZU dZddgZee         ed<   dgZee         ed<   g Z	ee
         ed<   	 	 ddee
         deej        z  dz  dedz  ddf fd	Zdefd
Zedeeef         fd            Z xZS )r   am  Implements a line element as a collection of words

    Args:
        words: list of word elements
        geometry: bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to
            the page's size. If not specified, it will be resolved by default to the smallest bounding box enclosing
            all words in it.
    rS   rT   r   wordsr   Nr   c                 H   |+t          t          j        d |D                                 }|At          |d         j                  dk    rt
          nt          } |d |D                       }t                                          |           || _        || _	        d S )Nc                     g | ]	}|j         
S r-   )rT   r/   ws     r(   r8   z!Line.__init__.<locals>.<listcomp>   s    -P-P-PQa.@-P-P-Pr*   r      c                     g | ]	}|j         
S r-   rS   r}   s     r(   r8   z!Line.__init__.<locals>.<listcomp>   s    )D)D)D!*)D)D)Dr*   )rz   )
ri   rj   meanlenrS   r   r
   rW   r)   rT   )r%   rz   rS   rT   box_resolution_fnr#   s        r(   r)   zLine.__init__   s     #$RW-P-P%-P-P-P%Q%QRR;>uQx?P;Q;QUV;V;V 7 7\r(()D)De)D)D)DEEHu%%%  0r*   c                 J    d                     d | j        D                       S )rZ    c              3   >   K   | ]}|                                 V  d S r?   rG   r}   s     r(   	<genexpr>zLine.render.<locals>.<genexpr>   s*      77q

777777r*   )joinrz   rF   s    r(   rG   zLine.render   s%    xx77DJ777777r*   r=   c                     fd| j         D             }|                    dd d         D             i            | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z"Line.from_dict.<locals>.<dictcomp>   rf   r*   rz   c                 B    g | ]}t                               |          S r-   )r   rD   r/   _dicts     r(   r8   z"Line.from_dict.<locals>.<listcomp>   $    LLLdnnU++LLLr*   r-   r   updaterB   s    ` r(   rD   zLine.from_dict   sg    >>>>3+=>>>LL79KLLL
 	 	 	 s}}V}}r*   NN)r$   rH   rI   rJ   r   rK   rL   rM   r   rz   r   r	   rj   rk   ri   r)   rG   rO   rN   r   rD   rl   rm   s   @r(   r   r      s           ",-? @NDI@@@")OT#Y***E4:
 59)-	1 1Dz1 
*T11  $,	1
 
1 1 1 1 1 1&8 8 8 8 8 $sCx.    [    r*   r   c                   *    e Zd ZdZdefdZdefdZdS )r   zImplements a prediction elementr   c                     | j         S rY   r[   rF   s    r(   rG   zPrediction.render   r\   r*   c                 8    d| j          d| j        dd| j         S )Nr^   r_   r`   z, bounding_box=)rQ   rR   rS   rF   s    r(   rb   zPrediction.extra_repr   s+    eee4?eeeVZVceeer*   N)r$   rH   rI   rJ   rL   rG   rb   r-   r*   r(   r   r      sW        ))    fC f f f f f fr*   r   c                   &    e Zd ZU dZddgZee         ed<   ddgZee         ed<   g Z	ee
         ed<   g Zee         ed<   g g ddfdee
         dee         deej        z  dz  dedz  d	df
 fd
Zdded	efdZedeeef         fd            Z xZS )r   a  Implements a block element as a collection of lines and artefacts

    Args:
        lines: list of line elements
        artefacts: list of artefacts
        geometry: bounding box of the word in format ((xmin, ymin), (xmax, ymax)) where coordinates are relative to
            the page's size. If not specified, it will be resolved by default to the smallest bounding box enclosing
            all lines and artefacts in it.
    rS   rT   r   lines	artefactsr   Nr   c                 z   |+t          t          j        d |D                                 }|Yd |D             }d |D             }t          |d         j        t          j                  rt          nt          } |||z             }t                      	                    ||           || _        || _
        d S )Nc                 0    g | ]}|j         D ]	}|j        
S r-   )rz   rT   )r/   liner~   s      r(   r8   z"Block.__init__.<locals>.<listcomp>   s-    -g-g-gT\`\f-g-gWXa.@-g-g-g-gr*   c                 0    g | ]}|j         D ]	}|j        
S r-   )rz   rS   )r/   r   words      r(   r8   z"Block.__init__.<locals>.<listcomp>   s)    QQQDdjQQd$-QQQQr*   c                     g | ]	}|j         
S r-   r   )r/   artefacts     r(   r8   z"Block.__init__.<locals>.<listcomp>   s    JJJHh/JJJr*   r   r   r   )ri   rj   r   
isinstancerS   rk   r   r
   rW   r)   rT   )	r%   r   r   rS   rT   
line_boxesartefact_boxesr   r#   s	           r(   r)   zBlock.__init__   s     #$RW-g-gU-g-g-g%h%hiiQQEQQQJJJ	JJJN+5eAh6G+T+Tp''Zp  )(n)DEEHu	:::  0r*   

line_breakc                 J    |                     d | j        D                       S )rZ   c              3   >   K   | ]}|                                 V  d S r?   r   )r/   r   s     r(   r   zBlock.render.<locals>.<genexpr>   s*      DDt{{}}DDDDDDr*   )r   r   )r%   r   s     r(   rG   zBlock.render   s%    DDDDDDDDr*   r=   c                     fd| j         D             }|                    d d         D             d d         D             d            | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z#Block.from_dict.<locals>.<dictcomp>   rf   r*   c                 B    g | ]}t                               |          S r-   )r   rD   r   s     r(   r8   z#Block.from_dict.<locals>.<listcomp>   r   r*   r   c                 B    g | ]}t                               |          S r-   )r   rD   r   s     r(   r8   z#Block.from_dict.<locals>.<listcomp>   s&    XXX(,,U33XXXr*   r   r   r-   r   rB   s    ` r(   rD   zBlock.from_dict   s    >>>>3+=>>>LL79KLLLXX;AWXXX
 
 	 	 	 s}}V}}r*   )r   )r$   rH   rI   rJ   r   rK   rL   rM   r   r   r   r   r   r	   rj   rk   ri   r)   rG   rO   rN   r   rD   rl   rm   s   @r(   r   r      s^          ",-? @NDI@@@");!7OT#Y777E4: "ItH~""" $&48)-1 1Dz1 >1 
*T1	1
  $,1 
1 1 1 1 1 1.E E E E E E E $sCx.    [    r*   r   c                       e Zd ZU dZg dZee         ed<   dgZee         ed<   g Z	ee
         ed<   	 	 ddej        dee
         ded	eeef         d
eeef         dz  deeef         dz  ddf fdZddedefdZdefdZddededdfdZdej        fdZd dedeeej        f         fdZedeeef         fd            Z xZS )!r   a  Implements a page element as a collection of blocks

    Args:
        page: image encoded as a numpy array in uint8
        blocks: list of block elements
        page_idx: the index of the page in the input raw document
        dimensions: the page size in pixels in format (height, width)
        orientation: a dictionary with the value of the rotation angle in degress and confidence of the prediction
        language: a dictionary with the language value and confidence of the prediction
    page_idx
dimensionsorientationlanguager   blocksr   Npager   r   r   r   r   c                 *   t                                          |           || _        || _        || _        t          |t                    r|nt          d d           | _        t          |t                    r|nt          d d           | _        d S )N)r   ra   	rW   r)   r   r   r   r   rN   r   r   )r%   r   r   r   r   r   r   r#   s          r(   r)   zPage.__init__  s     	'''	 $*4[$*G*Gn;;TX\imMnMnMn$.x$>$>eDt`dDeDeDer*   

block_breakc                 J    |                     d | j        D                       S )rZ   c              3   >   K   | ]}|                                 V  d S r?   r   )r/   bs     r(   r   zPage.render.<locals>.<genexpr>  s*      @@q

@@@@@@r*   )r   r   )r%   r   s     r(   rG   zPage.render  s'    @@DK@@@@@@r*   c                     d| j          S Nzdimensions=r   rF   s    r(   rb   zPage.extra_repr      .T_...r*   TFinteractivepreserve_aspect_ratioc                     t          dd           t          dd           ddlm} t          |                                 | j        ||            |j        di | dS )a.  Overlay the result on a given image

        Args:
            interactive: whether the display should be interactive
            preserve_aspect_ratio: pass True if you passed True to the predictor
            **kwargs: additional keyword arguments passed to the matplotlib.pyplot.show method
        
matplotlib4`.show()` requires matplotlib & mplcursors installed
mplcursorsr   Nr   r   r-   )r   matplotlib.pyplotpyplotr   r6   r   showr%   r   r   r   plts        r(   r   z	Page.show"  sx     	']^^^']^^^''''''t{{}}di[`uvvvv6r*   c                 @    t          |                                 fi |S )zSynthesize the page from the predictions

        Args:
            **kwargs: keyword arguments passed to the `synthesize_page` method

        Returns:
            synthesized page
        )r   r6   r%   r   s     r(   
synthesizezPage.synthesize1  s"     t{{}}77777r*   docTR - XML export (hOCR)
file_titlec                    | j         }d}d}d}| j        \  }}d| j                                        v r| j        nd}t	          ddt          |          d          }	t          |	d          }
|t          |
d	          _        t          |
d
ddd           t          |
d
ddt          j	         d           t          |
d
ddd           t          |	d          }t          |ddd|dz    d| d| dd          }| j
        D ]}t          |j                  dk    rt          d          |j        \  \  }}\  }}t          |ddd| dt          t          ||z                       dt          t          ||z                       d t          t          ||z                       dt          t          ||z                       d          }t          |d!d"d#| dt          t          ||z                       dt          t          ||z                       d t          t          ||z                       dt          t          ||z                       d          }|dz  }|j        D ]}|j        \  \  }}\  }}t          |d$d%d&| dt          t          ||z                       dt          t          ||z                       d't          t          ||z                       dt          t          ||z                       d(	d          }|dz  }|j        D ]}|j        \  \  }}\  }}|j        }t          |d$d)d*| dt          t          ||z                       dt          t          ||z                       d+t          t          ||z                       dt          t          ||z                       d,t          t          |d-z                       
d          }|j        |_        |dz  }t)          j        |	d.d/0          t)          j        |	          fS )1  Export the page as XML (hOCR-format)
        convention: https://github.com/kba/hocr-spec/blob/master/1.2/spec.md

        Args:
            file_title: the title of the XML file

        Returns:
            a tuple of the XML byte string, and its ElementTree
           r   enhtmlhttp://www.w3.org/1999/xhtmlxmlnszxml:langattribheadtitlemetaContent-Typetext/html; charset=utf-8z
http-equivcontent
ocr-systempython-doctr namer   ocr-capabilities-ocr_page ocr_carea ocr_par ocr_line ocrx_wordbodydivocr_pagepage_image; bbox 0 0 r   ; ppageno 0classidr      AXML export is only available for straight bounding boxes for now.	ocr_careablock_bbox z                     pocr_parpar_spanocr_lineline_                         O;                         baseline 0 0; x_size 0; x_descenders 0; x_ascenders 0	ocrx_wordword_z                             z&;                             x_wconf d   utf-8xmlencodingmethod)r   r   r   keys	ETElementrL   r   textdoctr__version__r   r   rS   	TypeErrorintroundr   rz   rR   rQ   ETtostringr   )r%   r   p_idxblock_count
line_count
word_countheightwidthr   	page_hocrr   r   page_divblockxminyminxmaxymax	block_div	paragraphr   	line_spanr   confword_divs                            r(   export_as_xmlzPage.export_as_xml<  s    

$.$-2D2D2F2F$F$F4==Df7Ucfgocpcp-q-qrrr	)V,,)3
4!!&4~Rl(m(mnnnn(5XUEV5X5XYY	
 	
 	
 	

 	.;jkk	
 	
 	
 	
 )V,,#)eai))GEGGFGGG 
 
 
 [ 8	$ 8	$E5>""a'' cddd).&LT4,4"(0;00LSte|)<)<%=%= L LE$QW-DXDX@Y@Y L Lte|,,--L L03E$-4H4H0I0IL L 	 	 	I #&...LSte|)<)<%=%= L LE$QW-DXDX@Y@Y L Lte|,,--L L03E$-4H4H0I0IL L 	 	 	I 1K $ $-1]*tltT&!+2j22"OU4%<-@-@)A)A "O "OCdU[mH\H\D]D] "O "OU4%<0011"O "O47dVm8L8L4M4M"O "O "O 
 
 
	 a
 J $ $D15.LT4,4?D)!%0"6*"6"6&>Ste|1D1D-E-E &> &>ERVY_R_L`L`HaHa &> &> te|!4!455&> &>8;E$-<P<P8Q8Q&> &>%(tcz):):%;%;&> &>   
  
  
 H %)JHM!OJJ!$$B IFFFW`HaHabbr*   r=   c                     fd| j         D             }|                    dd d         D             i            | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z"Page.from_dict.<locals>.<dictcomp>  rf   r*   r   c                 B    g | ]}t                               |          S r-   )r   rD   )r/   
block_dicts     r(   r8   z"Page.from_dict.<locals>.<listcomp>  s$    !d!d!d*%//*"="=!d!d!dr*   r-   r   rB   s    ` r(   rD   zPage.from_dict  s^    >>>>3+=>>>x!d!dPYZbPc!d!d!defffs}}V}}r*   r   r   TFr   )r$   rH   rI   rJ   r   rK   rL   rM   r   r   r   rj   rk   r  tuplerN   r   r)   rG   rb   boolr   r   bytesr  r   r"  rO   rD   rl   rm   s   @r(   r   r      s         	 	 !V U UNDIUUU"*OT#Y+++FDK .2*.f fjf Uf 	f
 #s(Of #s(^d*f sCx.4'f 
f f f f f f A A# A3 A A A A/C / / / /  D _c    	8bj 	8 	8 	8 	8fc fc fceTY[][iTiNj fc fc fc fcP $sCx.    [    r*   r   c                       e Zd ZU dZg dZee         ed<   dgZee         ed<   i Z	e
eee         f         ed<   	 	 ddej        de
eee         f         ded	eeef         d
e
eef         dz  de
eef         dz  ddf fdZddedefdZdefdZddededdfdZdej        fdZd dedeeej        f         fdZede
eef         fd            Z xZS )!r   a*  Implements a KIE page element as a collection of predictions

    Args:
        predictions: Dictionary with list of block elements for each detection class
        page: image encoded as a numpy array in uint8
        page_idx: the index of the page in the input raw document
        dimensions: the page size in pixels in format (height, width)
        orientation: a dictionary with the value of the rotation angle in degress and confidence of the prediction
        language: a dictionary with the language value and confidence of the prediction
    r   r   r2   r   Nr   r   r   r   r   r   c                 *   t                                          |           || _        || _        || _        t          |t                    r|nt          d d           | _        t          |t                    r|nt          d d           | _        d S )Nr1   ra   r   )r%   r   r2   r   r   r   r   r#   s          r(   r)   zKIEPage.__init__  s     	[111	 $*4[$*G*Gn;;TX\imMnMnMn$.x$>$>eDt`dDeDeDer*   r   prediction_breakc                 n    |                     d | j                                        D                       S )rZ   c              3   X   K   | ]%\  }}|D ]}| d |                                  V  &dS )z: Nr   )r/   
class_namer2   r   s       r(   r   z!KIEPage.render.<locals>.<genexpr>  sb       %
 %
.Ej+kv%
 %
fgz))QXXZZ))%
 %
 %
 %
 %
 %
 %
r*   )r   r2   r    )r%   r/  s     r(   rG   zKIEPage.render  sE    $$ %
 %
IMIYI_I_IaIa%
 %
 %
 
 
 	
r*   c                     d| j          S r   r   rF   s    r(   rb   zKIEPage.extra_repr  r   r*   TFr   r   c                     t          dd           t          dd           ddlm} t          |                                 | j        ||            |j        di | dS )a#  Overlay the result on a given image

        Args:
            interactive: whether the display should be interactive
            preserve_aspect_ratio: pass True if you passed True to the predictor
            **kwargs: keyword arguments passed to the matplotlib.pyplot.show method
        r   r   r   r   Nr   r-   )r   r   r   r   r6   r   r   r   s        r(   r   zKIEPage.show  s     	']^^^']^^^''''''KKMM49+Uj	
 	
 	
 	
 	6r*   c                 @    t          |                                 fi |S )zSynthesize the page from the predictions

        Args:
            **kwargs: keyword arguments passed to the `synthesize_kie_page` method

        Returns:
            synthesized page
        )r   r6   r   s     r(   r   zKIEPage.synthesize  s"     #4;;==;;F;;;r*   r   r   c                 T   | j         }d}| j        \  }}d| j                                        v r| j        nd}t	          ddt          |          d          }t          |d          }|t          |d	          _        t          |d
ddd           t          |d
ddt          j	         d           t          |d
ddd           t          |d          }	t          |	ddd|dz    d| d| dd           | j
                                        D ]\  }
}|D ]}t          |j                  dk    rt          d          |j        \  \  }}\  }}t          |	dd|
 d| dt          t!          ||z                       dt          t!          ||z                       d t          t!          ||z                       dt          t!          ||z                       d          }t          |d!d"|
 d#| dt          t!          ||z                       dt          t!          ||z                       d t          t!          ||z                       dt          t!          ||z                       d          }t          |d$d%|
 d&| dt          t!          ||z                       dt          t!          ||z                       d t          t!          ||z                       dt          t!          ||z                       d'	d          }t          |d$d(|
 d)| dt          t!          ||z                       dt          t!          ||z                       d t          t!          ||z                       dt          t!          ||z                       d*t          t!          |j        d+z                       
d          }|j        |_        |dz  }ߐt'          j        |d,d-.          t'          j        |          fS )/r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _prediction_r   r   r   r   _par_r   r   _line_r   r   _word_z";                         x_wconf r   r  r  r  )r   r   r   r  r  rL   r   r  r	  r
  r2   r    r   rS   r  r  r  rR   rQ   r  r  r   )r%   r   r  prediction_countr  r  r   r  r   r   r2  r2   
predictionr  r  r  r  prediction_divpar_divr  r!  s                        r(   r"  zKIEPage.export_as_xml  su     !$.$-2D2D2F2F$F$F4==Df7Ucfgocpcp-q-qrrr	)V,,)3
4!!&4~Rl(m(mnnnn(5XUEV5X5XYY	
 	
 	
 	

 	.;jkk	
 	
 	
 	
 )V,,#)eai))GEGGFGGG 	
 	
 	
 	
 (,'7'='='?'? 2	& 2	&#J) 1& 1&
z*++q00#$ghhh-7-@*tltT!+!,!+KK9IKK"PU4%<-@-@)A)A "P "PCdU[mH\H\D]D] "P "PU4%<0011"P "P47dVm8L8L4M4M"P "P 	" 	" 	" %"!*!+DD2BDD"PU4%<-@-@)A)A "P "PCdU[mH\H\D]D] "P "PU4%<0011"P "P47dVm8L8L4M4M"P "P 	 	 	 '!+!+EE3CEE"OU4%<-@-@)A)A "O "OCdU[mH\H\D]D] "O "OU4%<0011"O "O47dVm8L8L4M4M"O "O "O 
 
 
	 &!,!+EE3CEE"KU4%<-@-@)A)A "K "KCdU[mH\H\D]D] "K "KU4%<0011"K "K47dVm8L8L4M4M"K "K!$U:+@3+F%G%G!H!H"K "K 
 
 
 !+ 0 A%  c1&f {9wuEEEr~V_G`G```r*   r=   c                     fd| j         D             }|                    dd d         D             i            | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z%KIEPage.from_dict.<locals>.<dictcomp>R  rf   r*   r2   c                 B    g | ]}t                               |          S r-   )r   rD   )r/   predictions_dicts     r(   r8   z%KIEPage.from_dict.<locals>.<listcomp>T  s(    tttGWJ001ABBtttr*   r-   r   rB   s    ` r(   rD   zKIEPage.from_dictP  si    >>>>3+=>>>tt[der[sttt
 	 	 	 s}}V}}r*   r   r'  r(  r)  )r$   rH   rI   rJ   r   rK   rL   rM   r   r2   rN   r   rj   rk   r  r*  r   r)   rG   rb   r+  r   r   r,  r  r   r"  rO   rD   rl   rm   s   @r(   r   r     s/        	 	 !V U UNDIUUU"/OT#Y000/1Kc4
++,111 .2*.f fjf #tJ//0f 	f
 #s(Of #s(^d*f sCx.4'f 
f f f f f f 
 
s 
 
 
 
 
/C / / / /  D _c    "	<bj 	< 	< 	< 	<^a ^a ^aeTY[][iTiNj ^a ^a ^a ^a@ $sCx.    [    r*   r   c                   
    e Zd ZU dZdgZee         ed<   g Zee	         ed<   dee	         ddf fdZ
ddedefd	Zdd
Zdeej                 fdZdeeeej        f                  fdZedeeef         fd            Z xZS )r   kImplements a document element as a collection of pages

    Args:
        pages: list of page elements
    pagesr   r   Nc                 L    t                                          |           d S NrE  rW   r)   r%   rE  r#   s     r(   r)   zDocument.__init__c  &     	u%%%%%r*   




page_breakc                 J    |                     d | j        D                       S )rZ   c              3   >   K   | ]}|                                 V  d S r?   r   )r/   r   s     r(   r   z"Document.render.<locals>.<genexpr>k  s*      >>aqxxzz>>>>>>r*   )r   rE  )r%   rM  s     r(   rG   zDocument.renderi  s%    >>4:>>>>>>r*   c                 4    | j         D ]} |j        di | dS )z#Overlay the result on a given imageNr-   )rE  r   )r%   r   results      r(   r   zDocument.showm  s8    j 	" 	"FFK!!&!!!!	" 	"r*   c                 *    fd| j         D             S )zSynthesize all pages from their predictions

        Args:
            **kwargs: keyword arguments passed to the `Page.synthesize` method

        Returns:
            list of synthesized pages
        c                 *    g | ]} |j         d i S r-   )r   r/   r   r   s     r(   r8   z'Document.synthesize.<locals>.<listcomp>{  s+    AAAd))&))AAAr*   rH  r   s    `r(   r   zDocument.synthesizer  s"     BAAAdjAAAAr*   c                 *    fd| j         D             S )zExport the document as XML (hOCR-format)

        Args:
            **kwargs: additional keyword arguments passed to the Page.export_as_xml method

        Returns:
            list of tuple of (bytes, ElementTree)
        c                 *    g | ]} |j         d i S rT  )r"  rU  s     r(   r8   z*Document.export_as_xml.<locals>.<listcomp>  s-    DDD"",,V,,DDDr*   rH  r   s    `r(   r"  zDocument.export_as_xml}  s"     EDDDDDDDr*   r=   c                     fd| j         D             }|                    dd d         D             i            | di |S )Nc                 "    i | ]}||         S r-   r-   re   s     r(   r0   z&Document.from_dict.<locals>.<dictcomp>  rf   r*   rE  c                 B    g | ]}t                               |          S r-   )r   rD   )r/   	page_dicts     r(   r8   z&Document.from_dict.<locals>.<listcomp>  s$     _ _ _y	!:!: _ _ _r*   r-   r   rB   s    ` r(   rD   zDocument.from_dict  s]    >>>>3+=>>>w _ _IV]L^ _ _ _`aaas}}V}}r*   )rL  )r   N)r$   rH   rI   rJ   r   rK   rL   rM   rE  r   r)   rG   r   rj   rk   r   r*  r,  r  r   r"  rO   rN   r   rD   rl   rm   s   @r(   r   r   Y  sM          #*OT#Y***E4:&Dz& 
& & & & & &? ? ?c ? ? ? ?" " " "
	Bd2:&6 	B 	B 	B 	B	EeE2>4I.J)K 	E 	E 	E 	E $sCx.    [    r*   r   c                   p     e Zd ZU dZdgZee         ed<   g Zee	         ed<   dee	         ddf fdZ
 xZS )KIEDocumentrD  rE  r   r   Nc                 L    t                                          |           d S rG  rI  rJ  s     r(   r)   zKIEDocument.__init__  rK  r*   )r$   rH   rI   rJ   r   rK   rL   rM   rE  r   r)   rl   rm   s   @r(   r]  r]    s           #*OT#Y***E4=&G}& 
& & & & & & & & & &r*   r]  )(typingr   
defusedxmlr   	xml.etreer   r  xml.etree.ElementTreer   r  r   numpyrj   r	  doctr.file_utilsr   doctr.utils.common_typesr	   doctr.utils.geometryr
   r   doctr.utils.reconstitutionr   r   doctr.utils.reprr   doctr.utils.visualizationr   r   ModuleNotFoundError__all__r   r   r   r   r   r   r   r   r]  r-   r*   r(   <module>rl     s         $ $ $ $ $ $  ' ' ' ' ' ' 6 6 6 6 6 6 , , , , , ,      - - - - - - 0 0 0 0 0 0 P P P P P P P P K K K K K K K K ) ) ) ) ) )	LLLLLLLLL 	 	 	D	 h
g
g" " " " "l " " "D( ( ( ( (7 ( ( (V    w   @+ + + + +7 + + +\f f f f f f f f2 2 2 2 2G 2 2 2jm m m m m7 m m m`k k k k kg k k k\3 3 3 3 3w 3 3 3l& & & & &( & & & & &s   A A%$A%