pipe_result.rst 15.1 KB
Newer Older
xu rui's avatar
xu rui committed
1
2


icecraft's avatar
icecraft committed
3
Pipe Result
xu rui's avatar
xu rui committed
4
5
==============

6
7
8
9
10
11
.. admonition:: Tip
    :class: tip

    Please first navigate to :doc:`tutorial/pipeline` to get an initial understanding of how the pipeline works; this will help in understanding the content of this section.


icecraft's avatar
icecraft committed
12
The **PipeResult** class is a container for storing pipeline processing results and implements a series of methods related to these results, such as draw_layout, draw_span.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Checkout :doc:`../api/pipe_operators` for more details about **PipeResult**



Structure Definitions
-------------------------------

**some_pdf_middle.json**

+----------------+--------------------------------------------------------------+
| Field Name     | Description                                                  |
|                |                                                              |
+================+==============================================================+
| pdf_info       | list, each element is a dict representing the parsing result |
|                | of each PDF page, see the table below for details            |
+----------------+--------------------------------------------------------------+
| \_             | ocr \| txt, used to indicate the mode used in this           |
| parse_type     | intermediate parsing state                                   |
|                |                                                              |
+----------------+--------------------------------------------------------------+
| \_version_name | string, indicates the version of magic-pdf used in this      |
|                | parsing                                                      |
|                |                                                              |
+----------------+--------------------------------------------------------------+

**pdf_info**

Field structure description

+-------------------------+------------------------------------------------------------+
| Field                   | Description                                                |
| Name                    |                                                            |
+=========================+============================================================+
| preproc_blocks          | Intermediate result after PDF preprocessing, not yet       |
|                         | segmented                                                  |
+-------------------------+------------------------------------------------------------+
| layout_bboxes           | Layout segmentation results, containing layout direction   |
|                         | (vertical, horizontal), and bbox, sorted by reading order  |
+-------------------------+------------------------------------------------------------+
| page_idx                | Page number, starting from 0                               |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+
| page_size               | Page width and height                                      |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+
| \_layout_tree           | Layout tree structure                                      |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+
| images                  | list, each element is a dict representing an img_block     |
+-------------------------+------------------------------------------------------------+
| tables                  | list, each element is a dict representing a table_block    |
+-------------------------+------------------------------------------------------------+
| interline_equation      | list, each element is a dict representing an               |
|                         | interline_equation_block                                   |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+
| discarded_blocks        | List, block information returned by the model that needs   |
|                         | to be dropped                                              |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+
| para_blocks             | Result after segmenting preproc_blocks                     |
|                         |                                                            |
+-------------------------+------------------------------------------------------------+

In the above table, ``para_blocks`` is an array of dicts, each dict
representing a block structure. A block can support up to one level of
nesting.

**block**

The outer block is referred to as a first-level block, and the fields in
the first-level block include:

+------------------------+-------------------------------------------------------------+
| Field                  | Description                                                 |
| Name                   |                                                             |
+========================+=============================================================+
| type                   | Block type (table|image)                                    |
+------------------------+-------------------------------------------------------------+
| bbox                   | Block bounding box coordinates                              |
+------------------------+-------------------------------------------------------------+
| blocks                 | list, each element is a dict representing a second-level    |
|                        | block                                                       |
+------------------------+-------------------------------------------------------------+

There are only two types of first-level blocks: “table” and “image”. All
other blocks are second-level blocks.

The fields in a second-level block include:

+----------------------+----------------------------------------------------------------+
| Field                | Description                                                    |
| Name                 |                                                                |
+======================+================================================================+
|                      | Block type                                                     |
| type                 |                                                                |
+----------------------+----------------------------------------------------------------+
|                      | Block bounding box coordinates                                 |
| bbox                 |                                                                |
+----------------------+----------------------------------------------------------------+
|                      | list, each element is a dict representing a line, used to      |
| lines                | describe the composition of a line of information              |
+----------------------+----------------------------------------------------------------+

Detailed explanation of second-level block types

================== ======================
type               Description
================== ======================
image_body         Main body of the image
image_caption      Image description text
table_body         Main body of the table
table_caption      Table description text
table_footnote     Table footnote
text               Text block
title              Title block
interline_equation Block formula
================== ======================

**line**

The field format of a line is as follows:

+---------------------+----------------------------------------------------------------+
| Field               | Description                                                    |
| Name                |                                                                |
+=====================+================================================================+
|                     | Bounding box coordinates of the line                           |
| bbox                |                                                                |
+---------------------+----------------------------------------------------------------+
| spans               | list, each element is a dict representing a span, used to      |
|                     | describe the composition of the smallest unit                  |
+---------------------+----------------------------------------------------------------+

**span**

+---------------------+-----------------------------------------------------------+
| Field               | Description                                               |
| Name                |                                                           |
+=====================+===========================================================+
| bbox                | Bounding box coordinates of the span                      |
+---------------------+-----------------------------------------------------------+
| type                | Type of the span                                          |
+---------------------+-----------------------------------------------------------+
| content             | Text spans use content, chart spans use img_path to store |
| \|                  | the actual text or screenshot path information            |
| img_path            |                                                           |
+---------------------+-----------------------------------------------------------+

The types of spans are as follows:

================== ==============
type               Description
================== ==============
image              Image
table              Table
text               Text
inline_equation    Inline formula
interline_equation Block formula
================== ==============

**Summary**

A span is the smallest storage unit for all elements.

The elements stored within para_blocks are block information.

The block structure is as follows:

First-level block (if any) -> Second-level block -> Line -> Span

.. _example-1:

example
^^^^^^^

.. code:: json

   {
       "pdf_info": [
           {
               "preproc_blocks": [
                   {
                       "type": "text",
                       "bbox": [
                           52,
                           61.956024169921875,
                           294,
                           82.99800872802734
                       ],
                       "lines": [
                           {
                               "bbox": [
                                   52,
                                   61.956024169921875,
                                   294,
                                   72.0000228881836
                               ],
                               "spans": [
                                   {
                                       "bbox": [
                                           54.0,
                                           61.956024169921875,
                                           296.2261657714844,
                                           72.0000228881836
                                       ],
                                       "content": "dependent on the service headway and the reliability of the departure ",
                                       "type": "text",
                                       "score": 1.0
                                   }
                               ]
                           }
                       ]
                   }
               ],
               "layout_bboxes": [
                   {
                       "layout_bbox": [
                           52,
                           61,
                           294,
                           731
                       ],
                       "layout_label": "V",
                       "sub_layout": []
                   }
               ],
               "page_idx": 0,
               "page_size": [
                   612.0,
                   792.0
               ],
               "_layout_tree": [],
               "images": [],
               "tables": [],
               "interline_equations": [],
               "discarded_blocks": [],
               "para_blocks": [
                   {
                       "type": "text",
                       "bbox": [
                           52,
                           61.956024169921875,
                           294,
                           82.99800872802734
                       ],
                       "lines": [
                           {
                               "bbox": [
                                   52,
                                   61.956024169921875,
                                   294,
                                   72.0000228881836
                               ],
                               "spans": [
                                   {
                                       "bbox": [
                                           54.0,
                                           61.956024169921875,
                                           296.2261657714844,
                                           72.0000228881836
                                       ],
                                       "content": "dependent on the service headway and the reliability of the departure ",
                                       "type": "text",
                                       "score": 1.0
                                   }
                               ]
                           }
                       ]
                   }
               ]
           }
       ],
       "_parse_type": "txt",
       "_version_name": "0.6.1"
   }


icecraft's avatar
icecraft committed
291
Pipeline Result
292
293
------------------

icecraft's avatar
icecraft committed
294
.. code:: python
295
296

    from magic_pdf.pdf_parse_union_core_v2 import pdf_parse_union
icecraft's avatar
icecraft committed
297
298
    from magic_pdf.operators.pipes import PipeResult
    from magic_pdf.data.dataset import Dataset
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334

    res = pdf_parse_union(*args, **kwargs)
    res['_parse_type'] = PARSE_TYPE_OCR
    res['_version_name'] = __version__
    if 'lang' in kwargs and kwargs['lang'] is not None:
        res['lang'] = kwargs['lang']

    dataset : Dataset = some_dataset   # not real dataset
    pipeResult = PipeResult(res, dataset)



some_pdf_layout.pdf
~~~~~~~~~~~~~~~~~~~

Each page layout consists of one or more boxes. The number at the top
left of each box indicates its sequence number. Additionally, in
``layout.pdf``, different content blocks are highlighted with different
background colors.

.. figure:: ../_static/image/layout_example.png
   :alt: layout example

   layout example

some_pdf_spans.pdf
~~~~~~~~~~~~~~~~~~

All spans on the page are drawn with different colored line frames
according to the span type. This file can be used for quality control,
allowing for quick identification of issues such as missing text or
unrecognized inline formulas.

.. figure:: ../_static/image/spans_example.png
   :alt: spans example

icecraft's avatar
icecraft committed
335
   spans example