
    9iJK                        S SK Jr  S SKJr  S SKrS SKJrJrJ	r	  SSK
Jr  SSKJrJrJrJrJr  S	S
KJrJrJrJrJrJrJr  / SQr " S S\5      rS rS rS rS r  " S S\\\\5      r!g)    )annotations)partialN)Image	ImageDraw	ImageFont   )PINGFANG_FONT   )BaseCVResult	HtmlMixin	JsonMixinMarkdownMixin	XlsxMixin   )format_centered_by_htmlformat_first_line_funcformat_image_plain_func format_image_scaled_by_html_funcformat_text_plain_funcformat_title_funcsimplify_table_func)textformulainline_formuladisplay_formula	algorithm	referencereference_contentcontentabstractparagraph_title	doc_titlevertical_textocrnumberfootnoteheaderheader_imagefooterfooter_image
aside_textc                  :    \ rS rSrSrSS	S jjrS
S jrS
S jrSrg)PaddleOCRVLBlock@   zPaddleOCRVL Block ClassNc                r    Xl         [        [        [        U5      5      U l        X0l        SU l        X@l        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bboxr   imagegroup_id)selfr0   r4   r   r6   s        o/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/paddlex/inference/pipelines/paddleocr_vl/result.py__init__PaddleOCRVLBlock.__init__C   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
#################r0   r4   r   r7   _strs     r8   __str__PaddleOCRVLBlock.__str__R   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     r8   __repr__PaddleOCRVLBlock.__repr__Y   rC   r;   )r4   r   r6   r5   r0   ) NreturnNone)rI   str)	__name__
__module____qualname____firstlineno____doc__r9   rA   rE   __static_attributes__ r;   r8   r-   r-   @   s    !!r;   r-   c                T    U R                  SS5      n SR                  X5      nSU S3$ )z
Merge a formula and its formula number for display.

Args:
    formula (str): The formula string.
    formula_number (str): The formula number string.

Returns:
    str: The merged formula with tag.
z$$rG   z{} \tag*{{{}}})replaceformat)r   formula_numbermerge_formulas      r8   merge_formula_and_numberrX   a   s3     oodB'G%,,WEMb!!r;   c                l   U R                   R                  S5      nUS   R                  S5      nUSS   Vs/ s H  o3R                  S5      PM     nnSnUSSR                  S U 5       5      -   S	-   -  nUS
-  nU H$  nUSSR                  S U 5       5      -   S-   -  nM&     US-  nUS-  nU$ s  snf )N
r   |   z;<table border=1 style='margin: auto; width: max-content;'>
z  <thead><tr>rG   c              3  J   #    U  H  nS UR                  5        S3v   M     g7f) <th style='text-align: center;'>z</th>Nstrip.0cells     r8   	<genexpr>*format_chart2table_func.<locals>.<genexpr>z   s#      
OUt.tzz|nEBv   !#z</tr></thead>
z
  <tbody>
z    <tr>c              3  J   #    U  H  nS UR                  5        S3v   M     g7f) <td style='text-align: center;'>z</td>Nr_   ra   s     r8   rd   re      s$      SV424::<.FSVrf   z</tr>
z  </tbody>
z</table>)r   splitjoin)block
lines_listr'   linerowshtmlrows          r8   format_chart2table_funcrq   q   s    $$T*J]  %F(2127JJsOD7ID
'' 
OU
 
	
 		D 	MDgg SV  		
  	NDJDK+ 8s   B1c                    U R                   nUR                  SS5      nUR                  SS5      nUR                  SS5      nU$ )Nz<table>z:<table border=1 style='margin: auto; width: max-content;'>z<th>r^   z<td>rh   r   rT   )rk   tabel_contents     r8   format_table_center_funcru      sO    MMM!))OM "))&2TUM!))&2TUMr;   c                *   0 S[         _S[         _S[         _S[         _SS _SU _SU _S	U _S
S _SS _SS _SS _SS _S[        [        SS/S SS9_SS _SU_SU_UUUU[        [        SS/S S S9S! U[        [        [        U[        U[        S".E$ )#a  
Build a dictionary mapping block labels to their formatting functions.

Args:
    text_func: Function to format text blocks.
    image_func: Function to format image blocks.
    chart_func: Function to format chart blocks.
    table_func: Function to format table blocks.
    formula_func: Function to format formula blocks.
    seal_func: Function to format seal blocks.

Returns:
    dict: A mapping from block label to handler function.
r!   abstract_titlereference_titlecontent_titler"   c                `    SU R                    3R                  SS5      R                  SS5      $ )Nz# -
rG   rZ    rs   rk   s    r8   <lambda>)build_handle_funcs_dict.<locals>.<lambda>   s,    R#7#?#?r#J#R#R#$
r;   table_titlefigure_titlechart_titlevision_footnotec                Z    U R                   R                  SS5      R                  SS5      $ N

rZ   rs   r}   s    r8   r~   r      s%    )>)>vt)L)T)T&*
r;   r   c                Z    U R                   R                  SS5      R                  SS5      $ r   rs   r}   s    r8   r~   r      s#    emm33FDAII$PVWr;   r$   c                Z    U R                   R                  SS5      R                  SS5      $ r   rs   r}   s    r8   r~   r      s"    U]]2264@HHvVr;   r#   c                Z    U R                   R                  SS5      R                  SS5      $ r   rs   r}   s    r8   r~   r      s%    u}}'<'<VT'J'R'R&(
r;   r   c                Z    U R                   R                  SS5      R                  SS5      $ r   rs   r}   s    r8   r~   r      s%    5==+@+@+N+V+V&,
r;   r    u   摘要c                    SU  S3$ )N## rZ   rR   ls    r8   r~   r      s    Cs"+r;   r|   )	templatesformat_funcspliterr   c                Z    U R                   R                  SS5      R                  SS5      $ )Nr{   z  
rZ   rs   r}   s    r8   r~   r      s%    !6!6uf!E!M!M&"
r;   r5   chartu   参考文献
referencesc                    SU  3$ )Nr   rR   r   s    r8   r~   r      s
    Cs)r;   rZ   c                8    U R                   R                  S5      $ NrZ   )r   r`   r}   s    r8   r~   r      s    5==#6#6t#<r;   )r   r   r   tabler   r   sealr%   r&   r'   r(   r)   r*   r+   )r   r   r   r   	text_func
image_func
chart_func
table_funcformula_func	seal_funcs         r8   build_handle_funcs_dictr      sL   .4,4+4 	,4 	*	4
 	 
4 	y4 		4 	y4 	 
4 	W4 	V4  	 
!4& 	 
'4, 	G",-	
-48 	 
94> 	?4@ 	A4B  '&"%|4+	
 =(*("(",g4 4r;   c                  p   ^  \ 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S	 jjrS
rU =r$ )PaddleOCRVLResult   zL
PaddleOCRVLResult class for holding and formatting OCR/VL parsing results.
c                N  > [         TU ]  U5        [        R                  " U 5        [        R                  " U 5        [        R                  " U 5        [
        R                  " U 5        U S   R                  S/ 5      n[         Vs/ s H  nX2;  d  M
  UP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)	superr9   r   r   r   r   getVISUALIZE_ORDE_LABELSvisualize_order_labels)r7   datar   r0   	__class__s       r8   r9   PaddleOCRVLResult.__init__   s     	4 4 t$4 !%&6!7!;!;$b"

 /'
.2 .'
# '
s   	B"B"c                n   SSK Jn  0 nU S   nUS   (       a*  U S   R                  R                  5        H	  u  pEXRU'   M     U S   S   (       a  U S   R                  S   US'   [        R
                  " U S   S	   S
S
2S
S
2S
S
S24   5      n[        R                  " US5      n[        S[        UR                  5      -  5      S-   n[        R                  " [        R                  USS9n	U S   n
SnU
 H  nUR                  nUR                  nU" US5      nUR!                  XS9  XR"                  ;   d  MD  US   S-   US   US-  -
  4n[        UR                  5      US   -
  U:  a  [        US   US-  -
  5      US   US-  -
  4nUR%                  U['        US-   5      U	SS9  US-  nM     XbS'   U$ )z}
Convert the parsing result to a dictionary of images.

Returns:
    dict: Keys are names, values are numpy arrays (images).
r   )get_show_colorr   use_doc_preprocessordoc_preprocessor_resuse_layout_detectionlayout_det_resres
output_imgNRGBAg;On?zutf-8)encodingparsing_res_listr   F)fillr\   g?red)fontr   layout_order_res)layout_parsing.utilsr   imgitemsr   	fromarrayr   Drawr3   widthr   truetyper	   pathr4   r0   	rectangler   r   rK   )r7   r   res_img_dictr   keyvaluer5   draw	font_sizer   parsing_resultorder_indexrk   r4   r0   
fill_colortext_positions                    r8   _to_imgPaddleOCRVLResult._to_img   s    	:./01"#9:>>DDF
$)S! G !"89-12B-C-G-G-NL)* %; <\ J1aQUSUQU: VW~~eV,EKK 001A5	!!-"4"4i'R01#E::DKKE'u5JNN4N1333!%a1d1g	Q.F Gu{{#d1g-	9DGi#o56Q)q.0%M 		-[1_)=Du	Uq  $ ,1'(r;   c                    0 n[        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)lenrangero   )r7   res_html_dictsno	table_resr   r   s         r8   _to_htmlPaddleOCRVLResult._to_html+  w     t$%&*S&6!789 !1237	"+,=">/0%.^^F%;"	 :
 r;   c                    0 n[        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   xlsx)r7   res_xlsx_dictr   r   r   r   s         r8   _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   (       a  U S   R                   S	   US'   U S   S
   (       a  U S   R                   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_countr   heightr   r   r   r   r   r   r   )block_labelblock_content
block_bbox)rK   r0   r   r4   r   _to_str)r7   argskwargsr   r   r   parsing_ress          r8   r   PaddleOCRVLResult._to_strK  s4    !,/\!,/\!,/\WWhX./!/ !"89+/0F+G+K+KE+RD'( !"89%)*:%;%?%?%FD!" 23  0
  0	  +00!,!4!4)..
  0 	 
 $4   7777
s   .C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   R                  SS5      (       aW  U S	   S
   R                  S   mS nU4S jnU S   R                  SS5      (       a  [        nOUnUnS n	S n
[        UUUU	U
US9nU S   n/ nSn[	        U5       H  u  nnUR
                  nUU R                  ;   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   R                  SS5      (       aH  WR                  UR
                  5      (       a  UUR
                     " U5      US'   OUR                  US'   UR                  U5        M     XS'   U S   S   (       a  U S	   R                  S   US	'   U S   S   (       a  U S   R                  S   US'   [        R                  " U/UQ70 UD6$ )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.
r   r   r   r   r   r   format_block_contentFr   r   r\   c                *    [        [        U 5      5      $ Nr   r   r}   s    r8   r~   ,PaddleOCRVLResult._to_json.<locals>.<lambda>      -D&u-.r;   c                *   > [        [        U TS95      $ N)original_image_widthr   r   rk   r   s    r8   r~   r         .E0)=/r;   use_chart_recognitionc                    S[        U 5      -   $ r   ru   r}   s    r8   r~   r         d5Me5T.Tr;   c                    U R                   $ r   r   r}   s    r8   r~   r     s    r;   r   r   N)r   r   r   block_idblock_orderr6   r   r   r   r   r   )r   shaperq   r   	enumerater0   r   r   r4   r6   appendjsonr   _to_json)r7   r   r   r   r   format_text_funcformat_image_funcformat_chart_funcformat_seal_funcformat_table_funcformat_formula_funchandle_funcs_dictr   parsing_res_list_jsonr   idxr   r0   orderres_dictr   s                       @r8   r  PaddleOCRVLResult._to_jsonn  s    !,/\!,/\!,/\WWhX./!/ !%%&<eDD#'(>#?#M#S#STU#V  ! $%))*A5II$;!$5!0 T"= 7*,,,0*!   23 " )*: ;C%%E333#q *00!,!4!4)..$,7,@,@,LK((RU	H $%))*@%HH$(():):;;0A+BSBS0T#1H_- 1<0C0CH_-!((23 !<4 $9  !"89+/0F+G+L+LU+SD'( !"89%)*:%;%@%@%GD!"!!$8888r;   c           	       ^ U S   S   R                   S   mU(       a
  S nU4S jnO	S n[        nU S   S   (       a  [        OUnU(       a  S	 nOS
 nS nUn[        UUUUUUS9n	U S   R	                  S/ 5       H  n
U	R                  U
S5        M     Sn0 n0 US'   [        U S   5       H  u  pUR                  n
UR                  b"  UR                  S   US   UR                  S   '   U	R	                  U
S5      nU(       ac  U
S:X  d  U
S:X  aW  U[        U S   5      S-
  :w  aB  U S   US-      nUR                  nUS:X  a%  [        UR                  UR                  5      Ul        U(       d  M  UU(       a  SU" U5      -   OU" U5      -  nM     U S   US'   U S   US'   XS'   U S    H  nUS   US   US   '   M     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   r\   c                *    [        [        U 5      5      $ r   r   r}   s    r8   r~   0PaddleOCRVLResult._to_markdown.<locals>.<lambda>  r   r;   c                *   > [        [        U TS95      $ r   r   r   s    r8   r~   r    r   r;   c                    U R                   $ r   r  r}   s    r8   r~   r    s    U]]r;   r   r   c                    S[        U 5      -   $ r   r  r}   s    r8   r~   r    r  r;   c                2    [        SU R                  -   5      $ r   )r   r   r}   s    r8   r~   r    s    .A$BV.Wr;   c                    U R                   $ r   r  r}   s    r8   r~   r    s    EMMr;   r   r   NrG   markdown_imagesr   r   r   r   r   rV   r   r   r   markdown_textsimgs_in_doc)r  r   rq   r   r   popr  r0   r5   r   rX   r   )r7   prettyshow_formula_numberr  r  r  r  r  r  r  r0   markdown_contentmarkdown_infor  rk   handle_func
next_blocknext_block_labelr   r   s                      @r8   _to_markdownPaddleOCRVLResult._to_markdown  s;     $$:;LIOOPQR !  ; 7 $%&=> $" 	  T W9,3&(((,&
 *+//0H"ME!!%. N +-'(#D);$<=JCKKE{{&HMI/0V1DE ,//t<K#//5I3E3t$6781<<!"45cAg>
#-#3#3 #'77$<z'9'9%EM { ' [//$U+ ' >2 '+<&8l#&*<&8l#*:&'&C<?JM+,S[9 ' r;   )r   rH   )rI   zdict[str, np.ndarray])rI   zdict[str, str])TF)rI   dict)rL   rM   rN   rO   rP   r9   r   r   r   r   r  r+  rQ   __classcell__)r   s   @r8   r   r      s6    
**X  !8FT9lV Vr;   r   )"
__future__r   	functoolsr   numpynpPILr   r   r   utils.fontsr	   common.resultr   r   r   r   r   layout_parsing.result_v2r   r   r   r   r   r   r   r   objectr-   rX   rq   ru   r   r   rR   r;   r8   <module>r8     sq    #   + + )    0v B" 8K\uiM ur;   