
    =pjDV              
      J   S SK Jr  S SKrS SKrS SKJrJr  SSKJ	r	  SSK
JrJr  SSKJr  SS	KJrJrJrJrJrJrJr  SS
KJr  SSKJrJrJrJrJrJrJ r J!r!  SSK"J#r#J$r$  / SQr%\" S5      (       a  S SK&r& " S S\'5      r(\" S5       " S S\\\\\5      5       r) " S S\)5      r*g)    )annotationsN)Image	ImageDraw   )logging)class_requires_depsis_dep_available)SIMFANG_FONT   )BaseCVResult
BaseResult	HtmlMixin	JsonMixinMarkdownMixin	WordMixin	XlsxMixin)MarkdownConverter)build_handle_funcs_dictformat_centered_by_htmlformat_chart2html_tableformat_image_plainformat_image_scaled_by_htmlformat_table_centerformat_text_plainsimplify_table   )draw_box_txt_fineget_minarea_rect)figure_titlevision_footnoteimagecharttableheaderheader_imagefooterfooter_imagefootnote
aside_textzopencv-contrib-pythonc                  F    \ rS rSrSr     S S	S jjrS
S jrS
S jrSrg)PaddleOCRVLBlockA   zPaddleOCRVL Block ClassNc                    Xl         [        [        [        U5      5      U l        X0l        SU l        XPl        X@l        X`l	        Xpl
        g)z
Initialize a PaddleOCRVLBlock object.

Args:
    label (str): Label assigned to the block.
    bbox (list): Bounding box coordinates of the block.
    content (str, optional): Content of the block. Defaults to an empty string.
N)labellistmapintbboxcontentr!   polygon_pointsgroup_idglobal_block_idglobal_group_id)selfr.   r2   r3   r5   r4   r6   r7   s           k/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddlex/inference/pipelines/paddleocr_vl/result.py__init__PaddleOCRVLBlock.__init__D   s>    $ 
S$(	
, ..    c                Z    SU R                    SU R                   SU R                   S3nU$ z.
Return a string representation of the block.
z

#################
label:	z
bbox:	z

content:	z
#################r.   r2   r3   r8   _strs     r9   __str__PaddleOCRVLBlock.__str___   5     1Idii[P\]a]i]i\jj}~r<   c                Z    SU R                    SU R                   SU R                   S3nU$ r>   r?   r@   s     r9   __repr__PaddleOCRVLBlock.__repr__f   rD   r<   )r2   r3   r6   r7   r5   r!   r.   r4   ) NNNNreturnNone)rJ   str)	__name__
__module____qualname____firstlineno____doc__r:   rB   rF   __static_attributes__ r<   r9   r+   r+   A   s/    ! / 
/6r<   r+   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSSS jjrSS jrSrU =r$ )PaddleOCRVLResultn   zL
PaddleOCRVLResult class for holding and formatting OCR/VL parsing results.
c                  > [         TU ]  U5        [        R                  " U 5        [        R                  " U 5        [        R                  " U 5        [
        R                  " U 5        [        R                  " U 5        U S   R                  S/ 5      n[        R                  5       U-    Vs/ s H  o3PM     snU l
        gs  snf )zz
Initializes a new instance of the class with the specified data.

Args:
    data: The input data for the parsing result.
model_settingsmarkdown_ignore_labelsN)superr:   r   r   r   r   r   getSKIP_ORDER_LABELScopyskip_order_labels)r8   datarY   r.   	__class__s       r9   r:   PaddleOCRVLResult.__init__t   s     	4 4 t$4 4 !%&6!7!;!;$b"
  1557:PP"
PeEP"
 "
s   -Cc                D    U S   n[        U[        5      (       a  US   $ U$ )z:Return the page image width, unwrapping list if necessary.widthr   )
isinstancer/   )r8   ws     r9   _page_image_width#PaddleOCRVLResult._page_image_width   s&    M!!T**qt11r<   c           
     R
   0 nU S   nUS   (       a  [        U S   [        5      (       a*  U S   R                  R                  5        H	  u  p4XAU'   M     [        U S   [        5      (       aZ  [        U S   5       HH  u  pV[        U[        5      (       d  M  UR                  R                  5        H  u  p4XAU SU 3'   M     MJ     U S   S   (       a  [        U S   [        5      (       a  U S   R                  S   US'   [        U S   [        5      (       aC  [        U S   5       H1  u  pW[        U[        5      (       d  M  UR                  S   USU 3'   M3     U R                  S	5      (       Ga  [        U S	   [        5      (       Gd  U R                  S5      (       Gap  U S	   S
   nU S	   S   n	U S   S   SS2SS2SSS24   n
U
R                  SS u  p[        R                  " U
5      n[        R                  " XS4[        R                  S9S-  n[        R                  " S5        [        R                   " U5      n["        n[        [%        X5      5       GH!  u  nu  nn [        R&                  " SS5      [        R&                  " SS5      [        R&                  " SS5      4n[        R(                  " U5      n[+        U5      S:  a  UR-                  5        VVs/ s H
  u  nnUU4PM     nnnUR/                  UUSUS9  [1        U5      n[3        S[5        USS2S4   5      [7        USS2S4   5      -
  -  5      n[        R8                  " USS2S4   5      USS2S4'   [        R8                  " USS2S4   5      [7        SU5      -   USS2S4'   OIUR-                  5        VVs/ s H  u  nn[3        U5      [3        U5      4PM     nnnUR/                  UUS9  [        U[:        5      (       a  US   n[=        X4UUUR>                  5      n[        R(                  " U[        R@                  5      RC                  S5      n[D        RF                  " UU/SUS5        [D        RH                  " UU5      nGM$     [        RJ                  " [        R                  " U
5      US5      n[        RL                  " SUS-  U4S5      nURO                  USSX45        URO                  [        R                  " U5      USUS-  U45        UUS'   U$ s  snnf s  snnf !    GM  = f) z}
Convert the parsing result to a dictionary of images.

Returns:
    dict: Keys are names, values are numpy arrays (images).
rX   use_doc_preprocessordoc_preprocessor_res_use_layout_detectionlayout_det_resreslayout_det_res_spotting_res	rec_polys	rec_texts
output_imgNr   r   r   )dtype   r      )outlinerc   fillg      ?      )ry   )rt   rz   r   TRGB)rv   rv   rv   spotting_res_img)(rd   r   imgitemsr/   	enumerater[   shaper   	fromarraynponesuint8randomseedr   Drawr
   ziprandintarraylentolistpolygonr   r1   maxminmeantupler   pathint32reshapecv2	polylinesbitwise_andblendnewpaste)r8   res_img_dictrX   keyvalueidxrj   
layout_resboxestxtsr!   hre   img_left	img_right	draw_leftvis_fontboxtxtcolorxyptsheightbox_ptsimg_right_textimg_shows                              r9   _to_imgPaddleOCRVLResult._to_img   s    ./01$56
CC"&'=">"B"B"H"H"JJC(-% #K$56==1:/02-C ""6
CC*>*B*B*H*H*JJC;@C5#8 +K	2  !"89$/0*==156F1G1K1KE1R-.$/0$77'06F1G'HOC!*j99@Ju@Use%<= (I
 HH^$$tN3T::/00(5E'4D/0>q!TrTzJE;;q#DAu-Hq	:S@IKKN!x0I#H#,S-=#>Zc3q#.q#.q#.E
 ((3-C3x!|25**,?,$!Q1v,?!))#uAE)R.s3!$SCAqD	NSQT^,K%L!M%'WWSAY%7BQBE
%'WWSAY%7#b&/%IABE
@C

"M1CFCF#3"M!))')>!#u--!!f%6vsC%WN((3199*ECMM.3%uaH #	> JI/ $?6 {{5??5#98SIHyyQ
ODHNN8aA\2NN5??951a!eQ7GH/7L+,3 @ #Ns-   'A<T #T4B:T .#TB,T T  T&c                    0 nU R                  S5      (       aS  [        U S   5      S:  aA  [        [        U S   5      5       H&  nU S   U   nUS   nSU 3nUR                  S   X'   M(     U$ )z
Converts the prediction to its corresponding HTML representation.

Returns:
    dict: The str type HTML representation result.
table_res_listr   table_region_idtable_pred)r[   r   rangehtml)r8   res_html_dictsno	table_resr   r   s         r9   _to_htmlPaddleOCRVLResult._to_html        88$%%#d3C.D*E*IS&6!789 !1237	"+,=">/0%.^^F%;"	 :
 r<   c                    0 nU R                  S5      (       aS  [        U S   5      S:  aA  [        [        U S   5      5       H&  nU S   U   nUS   nSU 3nUR                  S   X'   M(     U$ )zq
Converts the prediction HTML to an XLSX file path.

Returns:
    dict: The str type XLSX representation result.
r   r   r   r   r   )r[   r   r   xlsx)r8   res_xlsx_dictr   r   r   r   s         r9   _to_xlsxPaddleOCRVLResult._to_xlsx   r   r<   c                ,   0 nU S   US'   U S   US'   U S   US'   U S   US'   U S   US'   U S   nXCS'   U S   S   (       a6  [        U S   [        5      (       a  U S   R                  S	   US'   OU S   US'   U S   S
   (       a6  [        U S   [        5      (       a  U S   R                  S	   US'   OU S   US'   U S   nU Vs/ s H'  nUR                  UR                  UR
                  S.PM)     nnXSS'   [        R                  " U/UQ70 UD6$ s  snf )a?  
Converts the instance's attributes to a dictionary and then to a string.

Args:
    *args: Additional positional arguments passed to the base class method.
    **kwargs: Additional keyword arguments passed to the base class method.

Returns:
    dict: A dictionary with the instance's attributes converted to strings.

input_path
page_index
page_countrc   r   rX   ri   rj   rn   rl   rm   parsing_res_list)block_labelblock_content
block_bbox)rd   r   rL   r.   r3   r2   r   _to_str)r8   argskwargsr_   rX   r   parsing_ress          r9   r   PaddleOCRVLResult._to_str   s}    !,/\!,/\!,/\WWhX./!/ !"89$56
CC/34J/K/O/OPU/V+,/34J/K+, !"89$/0*==)-.>)?)C)CE)J%&)-.>)?%& 23  0
  0	  +00!,!4!4)..
  0 	 
 $4   7777
s   .Dc           	       ^
^^ U S   R                  SS5      mU S   R                  SS5      mU R                  5       m
U(       a  S nU
U4S jnU
U4S jnOS nU4S	 jnU4S
 jnU S   R                  SS5      (       a  [        OUnU S   R                  SS5      (       d  UnU(       a  S nOS nS n[        UUUUUUS9nU S   R                  S/ 5       H  n	UR	                  U	S5        M     U$ )z6Build label-to-handler mapping for content formatting.rX   use_ocr_for_image_blockFuse_seal_recognitionc                *    [        [        U 5      5      $ N)r   r   blocks    r9   <lambda><PaddleOCRVLResult._build_handle_funcs_dict.<locals>.<lambda>.  s    -D!%(.r<   c                4   > [        [        U TTS9T(       + S9$ N)original_image_widthshow_ocr_content)collapse_newlinesr   r   )r   r   r   s    r9   r   r   1  s$    .E+)=%<
 '>"=/r<   c                4   > [        [        U TTS9T(       + S9$ r   r   )r   r   r   s    r9   r   r   9  s$    -D+)=%9
 ';":.r<   c                    U R                   $ r   r3   r   s    r9   r   r   B  s    U]]r<   c                   > [        U TS9$ N)r   r   )r   r   s    r9   r   r   C  s    .@(?/r<   c                   > [        U TS9$ r   r   )r   r   s    r9   r   r   F  s    -?(<.r<   use_chart_recognitionrl   c                    S[        U 5      -   $ N
)r   r   s    r9   r   r   T  s    d5H5O.Or<   c                2    [        SU R                  -   5      $ r   )r   r3   r   s    r9   r   r   V  s    nTEMM=Q.Rr<   c                    U R                   $ r   r   r   s    r9   r   r   X  s    EMMr<   )	text_func
image_func
chart_func
table_funcformula_func	seal_funcrY   N)r[   rf   r   r   pop)r8   prettyformat_text_funcformat_image_funcformat_seal_funcformat_chart_funcformat_table_funcformat_formula_funchandle_funcs_dictr.   r   r   r   s             @@@r9   _build_handle_funcs_dict*PaddleOCRVLResult._build_handle_funcs_dict%  s2   "&'7"8"<"<%u#
  $$4599:PRWX#557 !   ;!  $%))*A5II $" 	 $%))*@%HH/ O R93&(((,&
 *+//0H"ME!!%. N  r<   c                   UR                  SS5      n0 nU S   US'   U S   US'   U S   US'   U S   US'   U S   US'   U S   nXTS'   U S   R                  S	S5      (       a  U R                  S
S9nU S   n/ nSn	[        U5       GHv  u  pUR                  nXR
                  ;  a  U	nU	S-  n	OSnUR                  UR                  UR                  U
UUR                  b  UR                  OU
S.n[        US5      (       a  UR                  b  UR                  US'   [        US5      (       a  UR                  b  UR                  US'   UR                  b  UR                  US'   U(       a  UR                  b  UR                  US'   U S   R                  S	S5      (       aG  WR                  UR                  5      (       a  XkR                     " U5      US'   OUR                  US'   UR                  U5        GMy     XS'   U R                  S5      (       a9  [        U S   [         5      (       a  U S    Vs/ s H  oPM     snUS'   OU S   US'   U S   S   (       a  [        U S   ["        5      (       a  U S   R$                  S   US'   Oz[        U S   [         5      (       aZ  / nU S    HI  n[        U["        5      (       a   UR                  UR$                  S   5        M8  UR                  U5        MK     UUS'   OU S   US'   U S   S   (       a  [        U S   ["        5      (       a  U S   R$                  S   US'   Oz[        U S   [         5      (       aZ  / nU S    HI  n[        U["        5      (       a   UR                  UR$                  S   5        M8  UR                  U5        MK     UUS'   OU S   US'   [&        R(                  " U/UQ70 UD6$ s  snf )a  
Converts the object's data to a JSON dictionary.

Args:
    *args: Positional arguments passed to the JsonMixin._to_json method.
    **kwargs: Keyword arguments passed to the JsonMixin._to_json method.

Returns:
    dict: A dictionary containing the object's data in JSON format.
keep_imgFr   r   r   rc   r   rX   format_block_contentTr   r   rz   N)r   r   r   block_idblock_orderr5   r6   r7   block_polygon_pointsr!   r   rp   ri   rj   rn   rl   rm   )r   r[   r   r   r.   r^   r3   r2   r5   hasattrr6   r7   r4   r!   appendrd   r/   r   jsonr   _to_json)r8   r   r   	_keep_imgr_   rX   r   r   parsing_res_list_jsonorder_indexr   r   r.   orderres_dictrn   rj   rm   s                     r9   r
  PaddleOCRVLResult._to_jsonf  s     JJz51	!,/\!,/\!,/\WWhX./!/ !%%&<eDD $ = =T = J 23 " )*: ;C%%E222#q *00!,!4!4)..$,7,@,@,LK((RU	H %677//;.9.I.I*+%677//;.9.I.I*+))53>3M3M/0[..:$/$5$5!$%))*@%HH$(():):;;0ABSBS0T#1H_- 1<0C0CH_-!((2S !<T $9 88N##$~.557;N7K'L7K7K'L^$'+N';^$ !"89$56
CC/34J/K/P/PQV/W+,D!78$??')$ 67C!#z22,33CHHUOD,33C8	 8
 0D+,/34J/K+, !"89$/0*==)-.>)?)D)DU)K%&D!12D99!# 01C!#z22&--chhuo>&--c2	 2
 *8%&)-.>)?%&!!$8888= (Ms   7Oc                ~    U R                  US9n[        R                  " U S   UUU S   S9nU S   US'   U S   US'   U$ )a  
Save the parsing result to a Markdown file.

Args:
    pretty (Optional[bool]): whether to pretty markdown by HTML, default by True.
    show_formula_number (bool): whether to show formula numbers.

Returns:
    dict: Markdown information with text and images.
r  r   imgs_in_doc)r   show_formula_numberr  r   r   )r   r   convert)r8   r   r  r   results        r9   _to_markdownPaddleOCRVLResult._to_markdown  sd     !999H"**#$/ 3]+	
  $L1|#L1|r<   c                   SSK Jn  SSKJn  SUR                  SS.SUR                  SS.SUR
                  S	.SUR
                  S	.SUR
                  S	.SUR                  S	.SUR                  S	.SUR
                  S	.S
UR
                  S	.S.	nU R                  5       nU R                  SS5      n[        U[        5      (       a  US   O[        U=(       d    S5      nU" U S   UU R                  S/ 5      S9u  pxUUUU S   US.$ )az  Convert the parsing result to a Word-compatible dict.

Returns:
    dict: {
        "word_blocks": List[Dict],       # Simplified list of content blocks
        "original_image_width": int,   # Pixel width of the source page
        "input_path": str,             # Original input file path
        "images": List[Dict]           # List of {"path": str, "img": PIL.Image}
    }
r   )WD_ALIGN_PARAGRAPHr   )build_word_blocks   T)sizealignindent
   )r  r  	   )	ocrvertical_textr)   spottinginline_formuladisplay_formulareference_contentr3   r(   r   r   r  )extra_style_mapr  r   )word_blocksr   original_image_heightr   images)docx.enum.textr  common.result.converterr  JUSTIFYLEFTCENTERrf   r[   rd   r/   r1   )	r8   r  r  r'  r   
height_valr)  r(  r*  s	            r9   _to_wordPaddleOCRVLResult._to_word  s:    	6@
 +33 +33
 $&0B0G0GH!#.@.E.EF')4F4K4KL(*5G5N5NO+33" !#-?-D-DE!"-?-D-DE)
.  $557XXh*
'
D99JqMs:?QR?S 	 0#$+3
 '$8%:|,
 	
r<   )r^   rI   )rJ   r1   )rJ   zdict[str, np.ndarray])rJ   zdict[str, str])T)TF)rJ   dict)rM   rN   rO   rP   rQ   r:   rf   r   r   r   r   r   r
  r  r1  rR   __classcell__)r`   s   @r9   rU   rU   n   sE    
(2
M^  '8R?!Bf9P0:
 :
r<   rU   c                  ,    \ rS rSrS rS rS rS rSrg)PaddleOCRVLPagesResulti#  c                0    [         R                  " S5        g )Nz@The result of multi-pages don't support to save as image format!r   warningr8   r   r   s      r9   save_to_img"PaddleOCRVLPagesResult.save_to_img$  s    N	
 r<   c                0    [         R                  " S5        g )Nz?The result of multi-pages don't support to save as html format!r8  r:  s      r9   save_to_html#PaddleOCRVLPagesResult.save_to_html*      M	
 r<   c                0    [         R                  " S5        g )Nz?The result of multi-pages don't support to save as xlsx format!r8  r:  s      r9   save_to_xlsx#PaddleOCRVLPagesResult.save_to_xlsx0  r@  r<   c                0    [         R                  " S5        g )Nz?The result of multi-pages don't support to save as word format!r8  r:  s      r9   save_to_word#PaddleOCRVLPagesResult.save_to_word6  r@  r<   rS   N)	rM   rN   rO   rP   r;  r>  rB  rE  rR   rS   r<   r9   r6  r6  #  s    r<   r6  )+
__future__r   r   numpyr   PILr   r   utilsr   
utils.depsr   r	   utils.fontsr
   common.resultr   r   r   r   r   r   r   r,  r   -common.result.converter.markdown_format_funcsr   r   r   r   r   r   r   r   
ocr.resultr   r   r\   r   objectr+   rU   r6  rS   r<   r9   <module>rQ     s    #      @ (   9	 	 	 =  +,,*v *Z ,-q
iM9 q
 .q
h. r<   