pipe_result.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. Pipe Result
  2. ==============
  3. .. admonition:: Tip
  4. :class: tip
  5. 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.
  6. 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.
  7. Checkout :doc:`../api/pipe_operators` for more details about **PipeResult**
  8. Structure Definitions
  9. -------------------------------
  10. **some_pdf_middle.json**
  11. +----------------+--------------------------------------------------------------+
  12. | Field Name | Description |
  13. | | |
  14. +================+==============================================================+
  15. | pdf_info | list, each element is a dict representing the parsing result |
  16. | | of each PDF page, see the table below for details |
  17. +----------------+--------------------------------------------------------------+
  18. | \_ | ocr \| txt, used to indicate the mode used in this |
  19. | parse_type | intermediate parsing state |
  20. | | |
  21. +----------------+--------------------------------------------------------------+
  22. | \_version_name | string, indicates the version of magic-pdf used in this |
  23. | | parsing |
  24. | | |
  25. +----------------+--------------------------------------------------------------+
  26. **pdf_info**
  27. Field structure description
  28. +-------------------------+------------------------------------------------------------+
  29. | Field | Description |
  30. | Name | |
  31. +=========================+============================================================+
  32. | preproc_blocks | Intermediate result after PDF preprocessing, not yet |
  33. | | segmented |
  34. +-------------------------+------------------------------------------------------------+
  35. | layout_bboxes | Layout segmentation results, containing layout direction |
  36. | | (vertical, horizontal), and bbox, sorted by reading order |
  37. +-------------------------+------------------------------------------------------------+
  38. | page_idx | Page number, starting from 0 |
  39. | | |
  40. +-------------------------+------------------------------------------------------------+
  41. | page_size | Page width and height |
  42. | | |
  43. +-------------------------+------------------------------------------------------------+
  44. | \_layout_tree | Layout tree structure |
  45. | | |
  46. +-------------------------+------------------------------------------------------------+
  47. | images | list, each element is a dict representing an img_block |
  48. +-------------------------+------------------------------------------------------------+
  49. | tables | list, each element is a dict representing a table_block |
  50. +-------------------------+------------------------------------------------------------+
  51. | interline_equation | list, each element is a dict representing an |
  52. | | interline_equation_block |
  53. | | |
  54. +-------------------------+------------------------------------------------------------+
  55. | discarded_blocks | List, block information returned by the model that needs |
  56. | | to be dropped |
  57. | | |
  58. +-------------------------+------------------------------------------------------------+
  59. | para_blocks | Result after segmenting preproc_blocks |
  60. | | |
  61. +-------------------------+------------------------------------------------------------+
  62. In the above table, ``para_blocks`` is an array of dicts, each dict
  63. representing a block structure. A block can support up to one level of
  64. nesting.
  65. **block**
  66. The outer block is referred to as a first-level block, and the fields in
  67. the first-level block include:
  68. +------------------------+-------------------------------------------------------------+
  69. | Field | Description |
  70. | Name | |
  71. +========================+=============================================================+
  72. | type | Block type (table|image) |
  73. +------------------------+-------------------------------------------------------------+
  74. | bbox | Block bounding box coordinates |
  75. +------------------------+-------------------------------------------------------------+
  76. | blocks | list, each element is a dict representing a second-level |
  77. | | block |
  78. +------------------------+-------------------------------------------------------------+
  79. There are only two types of first-level blocks: “table” and “image”. All
  80. other blocks are second-level blocks.
  81. The fields in a second-level block include:
  82. +----------------------+----------------------------------------------------------------+
  83. | Field | Description |
  84. | Name | |
  85. +======================+================================================================+
  86. | | Block type |
  87. | type | |
  88. +----------------------+----------------------------------------------------------------+
  89. | | Block bounding box coordinates |
  90. | bbox | |
  91. +----------------------+----------------------------------------------------------------+
  92. | | list, each element is a dict representing a line, used to |
  93. | lines | describe the composition of a line of information |
  94. +----------------------+----------------------------------------------------------------+
  95. Detailed explanation of second-level block types
  96. ================== ======================
  97. type Description
  98. ================== ======================
  99. image_body Main body of the image
  100. image_caption Image description text
  101. table_body Main body of the table
  102. table_caption Table description text
  103. table_footnote Table footnote
  104. text Text block
  105. title Title block
  106. interline_equation Block formula
  107. ================== ======================
  108. **line**
  109. The field format of a line is as follows:
  110. +---------------------+----------------------------------------------------------------+
  111. | Field | Description |
  112. | Name | |
  113. +=====================+================================================================+
  114. | | Bounding box coordinates of the line |
  115. | bbox | |
  116. +---------------------+----------------------------------------------------------------+
  117. | spans | list, each element is a dict representing a span, used to |
  118. | | describe the composition of the smallest unit |
  119. +---------------------+----------------------------------------------------------------+
  120. **span**
  121. +---------------------+-----------------------------------------------------------+
  122. | Field | Description |
  123. | Name | |
  124. +=====================+===========================================================+
  125. | bbox | Bounding box coordinates of the span |
  126. +---------------------+-----------------------------------------------------------+
  127. | type | Type of the span |
  128. +---------------------+-----------------------------------------------------------+
  129. | content | Text spans use content, chart spans use img_path to store |
  130. | \| | the actual text or screenshot path information |
  131. | img_path | |
  132. +---------------------+-----------------------------------------------------------+
  133. The types of spans are as follows:
  134. ================== ==============
  135. type Description
  136. ================== ==============
  137. image Image
  138. table Table
  139. text Text
  140. inline_equation Inline formula
  141. interline_equation Block formula
  142. ================== ==============
  143. **Summary**
  144. A span is the smallest storage unit for all elements.
  145. The elements stored within para_blocks are block information.
  146. The block structure is as follows:
  147. First-level block (if any) -> Second-level block -> Line -> Span
  148. .. _example-1:
  149. example
  150. ^^^^^^^
  151. .. code:: json
  152. {
  153. "pdf_info": [
  154. {
  155. "preproc_blocks": [
  156. {
  157. "type": "text",
  158. "bbox": [
  159. 52,
  160. 61.956024169921875,
  161. 294,
  162. 82.99800872802734
  163. ],
  164. "lines": [
  165. {
  166. "bbox": [
  167. 52,
  168. 61.956024169921875,
  169. 294,
  170. 72.0000228881836
  171. ],
  172. "spans": [
  173. {
  174. "bbox": [
  175. 54.0,
  176. 61.956024169921875,
  177. 296.2261657714844,
  178. 72.0000228881836
  179. ],
  180. "content": "dependent on the service headway and the reliability of the departure ",
  181. "type": "text",
  182. "score": 1.0
  183. }
  184. ]
  185. }
  186. ]
  187. }
  188. ],
  189. "layout_bboxes": [
  190. {
  191. "layout_bbox": [
  192. 52,
  193. 61,
  194. 294,
  195. 731
  196. ],
  197. "layout_label": "V",
  198. "sub_layout": []
  199. }
  200. ],
  201. "page_idx": 0,
  202. "page_size": [
  203. 612.0,
  204. 792.0
  205. ],
  206. "_layout_tree": [],
  207. "images": [],
  208. "tables": [],
  209. "interline_equations": [],
  210. "discarded_blocks": [],
  211. "para_blocks": [
  212. {
  213. "type": "text",
  214. "bbox": [
  215. 52,
  216. 61.956024169921875,
  217. 294,
  218. 82.99800872802734
  219. ],
  220. "lines": [
  221. {
  222. "bbox": [
  223. 52,
  224. 61.956024169921875,
  225. 294,
  226. 72.0000228881836
  227. ],
  228. "spans": [
  229. {
  230. "bbox": [
  231. 54.0,
  232. 61.956024169921875,
  233. 296.2261657714844,
  234. 72.0000228881836
  235. ],
  236. "content": "dependent on the service headway and the reliability of the departure ",
  237. "type": "text",
  238. "score": 1.0
  239. }
  240. ]
  241. }
  242. ]
  243. }
  244. ]
  245. }
  246. ],
  247. "_parse_type": "txt",
  248. "_version_name": "0.6.1"
  249. }
  250. Pipeline Result
  251. ------------------
  252. .. code:: python
  253. from magic_pdf.pdf_parse_union_core_v2 import pdf_parse_union
  254. from magic_pdf.operators.pipes import PipeResult
  255. from magic_pdf.data.dataset import Dataset
  256. res = pdf_parse_union(*args, **kwargs)
  257. res['_parse_type'] = PARSE_TYPE_OCR
  258. res['_version_name'] = __version__
  259. if 'lang' in kwargs and kwargs['lang'] is not None:
  260. res['lang'] = kwargs['lang']
  261. dataset : Dataset = some_dataset # not real dataset
  262. pipeResult = PipeResult(res, dataset)
  263. some_pdf_layout.pdf
  264. ~~~~~~~~~~~~~~~~~~~
  265. Each page layout consists of one or more boxes. The number at the top
  266. left of each box indicates its sequence number. Additionally, in
  267. ``layout.pdf``, different content blocks are highlighted with different
  268. background colors.
  269. .. figure:: ../_static/image/layout_example.png
  270. :alt: layout example
  271. layout example
  272. some_pdf_spans.pdf
  273. ~~~~~~~~~~~~~~~~~~
  274. All spans on the page are drawn with different colored line frames
  275. according to the span type. This file can be used for quality control,
  276. allowing for quick identification of issues such as missing text or
  277. unrecognized inline formulas.
  278. .. figure:: ../_static/image/spans_example.png
  279. :alt: spans example
  280. spans example