Table structure reconstruction and text extraction method and system based on computer vision

By using methods based on computer vision to identify and reconstruct the table structure in PDF, combined with optical character recognition technology to process non-text layers, the problem of insufficient table extraction accuracy and versatility in the existing technology is solved, and high accuracy and strong performance are achieved. Universal table extraction effect.

CN114004204BActive Publication Date: 2024-09-13SHANGHAI JIAOTONG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111263283.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-28
Publication Date
2024-09-13
Estimated Expiration
2041-10-28

AI Technical Summary

Technical Problem

When extracting tables from PDF, the existing technology has low accuracy and low versatility, especially the inability to effectively identify image tables, resulting in poor recognition and poor performance of general tables.

Method used

Using a method based on computer vision, the neural network is used to identify and locate the outer frame of the table, analyze the text layer to obtain the text spacing, reconstruct the inner frame structure of the table, and combine it with optical character recognition technology to process tables without text layers to generate editable table files. .

Benefits of technology

It realizes the automatic extraction of all tables in PDF, without the need to manually set the outer frame of the table. It is compatible with picture tables, improves the recognition accuracy and versatility, and can extract large amounts of table data in batches without supervision.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114004204B
    Figure CN114004204B
Patent Text Reader

Abstract

The present invention provides a table structure reconstruction and text extraction method and system based on computer vision, including: step 1: identifying and locating a table in a PDF document through a neural network to obtain an outer frame area where the table is located; step 2: parsing the text layer in the PDF document to obtain the text spacing in the PDF document; step 3: reconstructing the table inner frame structure in the table area through computer vision according to the framed table area and the text spacing; step 4: extracting text information from the same position in the PDF document according to the table inner frame structure; step 5: generating an editable table file according to the table inner frame structure and the corresponding text information. The present invention recognizes the outer frame of the table through a neural network, and can automatically extract all tables in the PDF document without manually giving the table outer frame area, and can batch extract tables in a large amount of PDF data without supervision.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of document reconstruction, and in particular to a table structure reconstruction and text extraction method and system based on computer vision. Background Art

[0002] PDF (Portable Document Format) is a format for saving, displaying and printing documents. It was developed by Adobe and is widely used in various fields such as economy, finance, education, scientific research and academia. However, since the design goal of PDF format is only to better display and more accurately print, for structured data such as tables, the relationship between each text is not preserved. With the continuous development of deep learning, more original data is urgently needed for support. In addition, document reconstruction itself is also an important task for the publishing industry. Tabular data, as a highly structured data, has great information value. How to quickly and accurately extract various tables in various PDFs is an important basic work and prerequisite for more advanced tasks. However, the current table extraction technology has problems such as low extraction accuracy, low versatility and low performance.

[0003] Patent document CN106897690A (application number: 201710095978.4) discloses a PDF table extraction method, comprising the following steps: step A, parsing the PDF document to obtain image data, first line data and character data; step B, using an image recognition algorithm to process the image data obtained in step A, and obtaining second line data corresponding to the table data from the image data with table data; step C, using a graphic algorithm to process the first line data obtained in step A and the second line data obtained in step B respectively, to obtain table frame data with table row data and column data; step D, using a clustering algorithm to cluster the character data obtained in step A, to obtain text data with a string set; step E, obtaining the corresponding table cell through the table row data and column data in the table frame data obtained in step C, matching the table cell with the string set in the text data obtained in step D, and obtaining the table data in the PDF document. This method has poor recognition of general tables and cannot recognize general tables.

[0004] Patent document CN110516208A (application number: 201910738531.3) discloses a system for table extraction for PDF documents, including a table feature extraction module, a table positioning module, and a table internal structure analysis module; a method for table extraction for PDF documents, including: S1, table feature extraction; S2, table positioning; S3, table internal structure analysis; the final table is divided into a two-dimensional grid structure, and for the obtained two-dimensional grid table, the position and size of each cell can be known, and the table is output in HTML format. The present invention takes into account special table formats such as default lines and background colors to distinguish cells, and can extract PDF table data with high accuracy. The invention does not take into account the situation where the PDF is an image table, and its versatility is general.

[0005] Patent document CN105988979A (application number: 201510083646.5) provides a table extraction method and device based on PDF files. After obtaining the text information of each word and the line information of each line in the PDF file through parsing, the horizontal lines extracted from the same page of the PDF file are sorted according to the line position information, and it is determined whether two adjacent horizontal lines are in the same table of the page. According to the line information, the horizontal lines in the same table of the page are drawn in a table, and in the drawn table, the vertical lines extracted from the page are filled according to the line information. Finally, in the drawn table, according to the text information of each word, the text character information in the text information is filled in the position corresponding to the text position information in the cell formed by the horizontal lines and the vertical lines. Since the information of the horizontal lines and the vertical lines of the table is taken into account, the accuracy of extracting the table from the PDF file is improved. This invention also does not consider the situation where the PDF is a picture table, and its general applicability is general.

[0006] Patent document CN109635268A (application number: CN201811630768.1) discloses a method for extracting table information from a PDF file, including: reading a PDF file; parsing the properties of a PDF file; finding and organizing the set of all horizontal and vertical lines in a page; judging whether the set of horizontal and vertical lines on the current page can form a complete table border, if so, processing it as a table with a border, otherwise processing it as a table without a border; obtaining the meta information of the rows and columns of the table and the cells; judging whether it is a cross-page table, if it is a cross-page table, merging the cross-page table; if it is not a cross-page table, directly storing the table; storing the row and column information of the table, as well as the page and the position in the page, etc. The invention does not consider the situation where the PDF is an image table, and its general applicability is general. Summary of the invention

[0007] In view of the defects in the prior art, the object of the present invention is to provide a table structure reconstruction and text extraction method and system based on computer vision.

[0008] The table structure reconstruction and text extraction method based on computer vision provided by the present invention includes:

[0009] Step 1: Use a neural network to identify and locate the table in the PDF document and obtain the outer frame area where the table is located;

[0010] Step 2: Parse the text layer in the PDF document to obtain the text spacing in the PDF document;

[0011] Step 3: Reconstruct the frame structure of the table in the table area through computer vision according to the framed table area and the text spacing;

[0012] Step 4: Extract text information from the same position in the PDF document based on the frame structure in the table;

[0013] Step 5: Generate an editable table file based on the frame structure and corresponding text information within the table.

[0014] Preferably, the step 1 comprises:

[0015] Step 1.1: Train and configure the table detection neural network;

[0016] Step 1.2: Convert each page of the PDF document containing a table target into an image, input each image into the table detection neural network, and if a table target exists, return the page number of the PDF document where the table target is located and the relative position of the table frame on the page of the PDF document.

[0017] Preferably, the step 2 comprises:

[0018] Step 2.1: Determine whether the PDF page where the table is located contains a text layer;

[0019] Step 2.2: If there is no text layer, embed the page into a text layer using optical character recognition technology, and the position of the embedded text is at the corresponding position of the text in the image;

[0020] Step 2.3: Count the sizes of all the text in the PDF document and use the average width of the text as the estimated value of the text spacing.

[0021] Preferably, step 3 comprises:

[0022] Step 3.1: Extract the table as an image based on the table frame area and the PDF page where the table is located;

[0023] Step 3.2: Preprocessing the captured image, including threshold processing and morphological processing, to remove noise points other than text and frame lines in the table;

[0024] Step 3.3: Detect vertical lines on the table. If the vertical line pixels exceed the preset value, it means that the table contains frame lines and execute step 3.4. Otherwise, execute step 3.5.

[0025] Step 3.4: Reconstruct the table structure of the table with frame lines, extract all vertical and horizontal lines of the table, obtain the intersection point set of the vertical and horizontal lines, and after removing redundant points, form the inner frame intersection point set of the table; based on the obtained intersection point set, determine whether the adjacent points form the inner frame line of the table, and if so, connect the two points to form an edge; form the table structure of the frame line table based on the points and edges;

[0026] Step 3.5: Preprocess the image, remove the horizontal and vertical lines in the image whose length exceeds the preset threshold, and perform threshold processing on the image so that the pixel value of the blank position is 0 and the pixel value of the pixel containing text is 255; perform row scanning on the image, if the sum of the pixel values ​​of a row scanned is 0, then the row is the horizontal inner frame line of the table, and the position of the inner frame line of the table is the middle position of all rows with the sum of continuous pixel values ​​of 0, and the inside of two adjacent horizontal inner frame lines is a row in the table; perform vertical scanning between two adjacent horizontal inner frame lines, and sum the pixel values ​​of each column. If the sum of the vertical pixels that exceeds the number of text spacing is 0, then mark the area scanned by the vertical line as a blank area, otherwise mark it as a text area, so as to obtain the coordinate set of each row with or without text area; merge the rows that are interconnected and can be completely separated by a vertical line from top to bottom and from left to right The blank areas that pass through are formed into blank blocks, and the height of each blank block is recorded, and all blank blocks with a height of 1 line are removed; all vertical lines that pass through the blank blocks are traversed, and the total height of all blank blocks passed by this vertical line is recorded; finally, the vertical line with the largest total height passing through the blank blocks is used as a vertical inner frame line of the table, and the blank blocks passed by this line are set as passed through; select the vertical line with the largest total height that can pass through other blank blocks that have not been passed through as another vertical frame line of the table, and record the blank blocks it passes through as passed through, and finally continue to obtain vertical lines until all blank blocks are passed through; establish the minimum cell of the table based on the obtained horizontal inner frame lines and vertical inner frame lines, scan whether the vertical line of each cell passes through the area with text, if so, delete the small section of the vertical line, and merge the left and right cells of the table, and finally form the table inner frame line structure after the merged cells.

[0027] Preferably, the step 4 comprises: obtaining the rectangular frame coordinates of each cell in the PDF document according to the reconstructed frame line structure of the table, extracting the text information in the same position area from the PDF document containing the text layer, and obtaining the content of the table cell by removing spaces and adjusting;

[0028] The step 5 includes: creating an Excel table according to all the table inner borders and the contents of the corresponding table cells, and saving the merged cell information.

[0029] The table structure reconstruction and text extraction system based on computer vision provided by the present invention includes:

[0030] Module M1: Identify and locate the table in the PDF document through a neural network to obtain the outer frame area where the table is located;

[0031] Module M2: parse the text layer in the PDF document to obtain the text spacing in the PDF document;

[0032] Module M3: Reconstruct the frame structure of the table in the table area through computer vision according to the framed table area and the text spacing;

[0033] Module M4: Extract text information from the same position in the PDF document based on the frame structure in the table;

[0034] Module M5: Generate an editable table file based on the frame structure and corresponding text information within the table.

[0035] Preferably, the module M1 comprises:

[0036] Module M1.1: Train and configure the table detection neural network;

[0037] Module M1.2: Convert each page of a PDF document containing a table target into an image, input each image into a table detection neural network, and if a table target exists, return the page number of the PDF document where the table target is located and the relative position of the table frame on the page of the PDF document.

[0038] Preferably, the module M2 comprises:

[0039] Module M2.1: Determine whether the PDF page where the table is located contains a text layer;

[0040] Module M2.2: If there is no text layer, embed the page into the text layer using optical character recognition technology, with the position of the embedded text being the corresponding position of the text in the image;

[0041] Module M2.3: Count the sizes of all text in the PDF document and use the average text width as an estimate of the text spacing.

[0042] Preferably, the module M3 includes:

[0043] Module M3.1: Extract the table as an image based on the table frame area and the PDF page where the table is located;

[0044] Module M3.2: Preprocessing the captured image, including threshold processing and morphological processing, to remove noise points other than text and frame lines in the table;

[0045] Module M3.3: Detect vertical lines in the table. If the vertical line pixels exceed the preset value, it indicates that the table contains frame lines and calls module M3.4. Otherwise, calls module M3.5.

[0046] Module M3.4: Reconstruct the table structure of the table with frame lines, extract all the vertical and horizontal lines of the table, obtain the intersection point set of the vertical and horizontal lines, and after removing redundant points, form the inner frame intersection point set of the table; based on the obtained intersection point set, determine whether the adjacent points form the inner frame line of the table, and if so, connect the two points to form an edge; form the table structure of the frame line table based on the points and edges;

[0047] Module M3.5: Preprocess the image, remove the horizontal and vertical lines in the image whose length exceeds the preset threshold, and perform threshold processing on the image so that the pixel value of the blank position is 0 and the pixel value of the text is 255; perform row scanning on the image, if the sum of the pixel values ​​of a row is 0, then the row is the horizontal inner frame line of the table, and the position of the inner frame line of the table is the middle position of all rows with the sum of continuous pixel values ​​of 0, and the inside of two adjacent horizontal inner frame lines is a row in the table; perform vertical scanning between two adjacent horizontal inner frame lines, sum the pixel values ​​of each column, if there is a continuous vertical pixel sum of 0 exceeding the number of text spacing, then mark the area scanned by the vertical line as a blank area, otherwise mark it as a text area, so as to obtain the coordinate set of each row with or without text area; merge the rows that are interconnected and can be completely separated by a vertical line from top to bottom and from left to right The blank areas that are not penetrated are formed into blank blocks, and the height of each blank block is recorded, and all blank blocks with a height of 1 line are removed; all vertical lines that penetrate the blank blocks are traversed, and the total height of all blank blocks passed by this vertical line is recorded; finally, the vertical line with the largest total height passing through the blank blocks is used as a vertical inner frame line of the table, and the blank blocks passed by this line are set as passed through; the vertical line with the largest total height that can pass through other blank blocks that have not been passed through is selected as another vertical frame line of the table, and the blank blocks passed by it are recorded as passed through, and finally vertical lines are obtained continuously until all blank blocks are passed through; the minimum cell of the table is established according to the obtained horizontal inner frame lines and vertical inner frame lines, and the vertical line of each cell is scanned to see if it passes through the area with text. If so, the small vertical line is deleted, and the left and right cells of the table are merged to finally form the inner frame line structure of the table after the cells are merged.

[0048] Preferably, the module M4 comprises: obtaining the rectangular frame coordinates of each cell in the PDF document according to the reconstructed frame line structure of the table, extracting the text information in the same position area from the PDF document containing the text layer, and obtaining the content of the table cell by removing spaces and adjusting;

[0049] The module M5 includes: creating an Excel table according to all the table inner borders and the contents of the corresponding table cells, and saving the merged cell information.

[0050] Compared with the prior art, the present invention has the following beneficial effects:

[0051] (1) The present invention recognizes the outer frame of a table through a neural network, and does not need to manually set the outer frame area of ​​the table, so that all tables in the PDF can be automatically extracted, and tables in a large amount of PDF data can be batch extracted without human supervision;

[0052] (2) The present invention adds a text layer to a table without a text layer by using OCR technology, which is compatible with the recognition of tables in PDF of image type and has stronger versatility;

[0053] (3) The present invention divides tables into tables with frame lines and tables without frame lines, and has different recognition algorithms for the two, so that the accuracy of table structure reconstruction is higher;

[0054] (4) The present invention calculates the text spacing by parsing the PDF data to assist in reconstructing the frame structure within the table, making the table structure reconstruction more accurate;

[0055] (5) The present invention achieves a balance between recognition speed and accuracy by reasonably setting the size of the image cut out from the PDF for processing during inner frame line recognition. BRIEF DESCRIPTION OF THE DRAWINGS

[0056] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:

[0057] Figure 1 is a flow chart of the method of the present invention;

[0058] Figure 2 A sample image of the table frame in a certain page of the PDF is used for the table detection network detection;

[0059] Figure 3 This is a table example extracted from the original PDF;

[0060] Figure 4 It is a horizontal frame graph in a framed table;

[0061] Figure 5It is a vertical frame line graph in a frame line table;

[0062] Figure 6 It is a schematic diagram of the intersection of the horizontal axis frame line;

[0063] Figure 7 Schematic diagram of inner frame structure lines generated for a framed table;

[0064] Figure 8 To determine that it is a table without frame lines, the frame lines are removed and the table image is obtained after strengthening processing;

[0065] Fig. 9 This is an example diagram of the horizontal frame lines and area collection obtained after M3.5.3;

[0066] Fig.10 This is an example diagram of all longitudinal inner frame lines obtained through M3.5.5;

[0067] Fig.11 This is an example of the frame structure in the table obtained after deleting the vertical lines in the merged cells in M3.5.6;

[0068] Fig.12 For a framed table, all characters within the cell area are classified as the content map of the table cell;

[0069] Fig.13 For a borderless table, all characters within the cell area are classified as the content map of the table cell;

[0070] Fig.14 The final editable Excel file diagram is a frameless table;

[0071] Fig.15 This is the editable Excel file chart obtained by the framed table. DETAILED DESCRIPTION

[0072] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.

[0073] Example:

[0074] According to a method for reconstructing a table structure and extracting text in a PDF based on computer vision provided by the present invention, Figure 1 As shown, the following steps are included:

[0075] Step 1: Identify and locate the table in PDF through neural network to obtain the outer frame area where the table is located; Step 2: Determine whether the PDF contains a text layer. If not, embed the PDF into a text layer through OCR technology. After parsing the PDF, automatically obtain the text spacing in the PDF; Step 3: According to the table area framed in the previous step and the text spacing, based on the computer vision method, draw the table inner frame line in the table area and reconstruct the table structure; Step 4: According to the table inner frame line structure reconstructed in step M3, extract the text information from the same position in the PDF to obtain the content of the table; Step 5: Automatically generate an editable table file based on the table inner frame line structure and the corresponding text information.

[0076] Step 1 includes: training and configuring a table detection neural network, preprocessing the PDF containing the table target, converting each page into an image, and inputting each image into the neural network to detect whether there is a table. If so, the PDF page where the table is located and the relative coordinates of the PDF page where the table is located are returned. Specifically:

[0077] Step 1.1: We created our own table dataset and trained a neural network for table object detection. Input image data to the neural network and it will return the recognition result.

[0078] Step 1.2: Convert the PDF page containing the table target into an image and input it into the neural network. The neural network will detect and return the location of the PDF page where the table frame is located. A page may contain multiple tables, such as Figure 2 .

[0079] The result returned is in the form of a list. The format is as follows: [

[0081] [Page where the table is located, [upper left corner horizontal coordinate, upper left corner vertical coordinate, lower right corner horizontal coordinate, lower right corner vertical coordinate]

[0082] [Page where the table is located, [upper left corner horizontal coordinate, upper left corner vertical coordinate, lower right corner horizontal coordinate, lower right corner vertical coordinate] ...... ]

[0085] The four coordinates describing the table frame are relative to the length and width of the page. It is a floating point number between 0 and 1.

[0086] Step 2 includes: determining whether the PDF contains a text layer, and if not, embedding a text layer into the PDF using OCR technology. And automatically obtaining the text spacing in the PDF. Specifically:

[0087] Step 2.1: First, use the PDF2txt module in PDFMiner to convert the PDF page corresponding to the target table into XML format. This file contains all the text information in the PDF. Use regular expressions to extract all text characters;

[0088] Step 2.2: If the result returned in the previous step is empty, it means that the PDF is in the form of an image. At this time, add a text layer to the PDF. All subsequent operations on the PDF are based on the PDF with the text layer;

[0089] Step 2.3: Extract the position information of all characters through regular expressions. Subtract the differences of the horizontal coordinates of all characters, take the average value, and multiply it by an empirical value as the estimated value of the character spacing.

[0090] Word Gap = 1.3*Mean (the horizontal coordinate of the lower right vertex of the character - the horizontal coordinate of the lower left vertex of the character)

[0091] Step 3 includes redrawing the inner frame of the table in the table area based on the table area framed in the previous step and rebuilding the table structure based on the computer vision method. Specifically:

[0092] Step 3.1: According to the table outer frame area and the PDF page where the table is located, the specified table area is cut out from the PDF. In order to balance the speed and accuracy of the subsequent recognition of the inner frame line of the table, the present invention empirically sets the cut-out image to meet the requirement that the smallest of the length and width is greater than or equal to 1700 pixels. Since most PDFs are recorded in vectors, scaling the content in the PDF will not lose accuracy, such as Figure 3 ;

[0093] Step 3.2: Preprocess the captured image. Because table reconstruction does not require color images. Convert the image from RBG three channels to a single channel grayscale mode to speed up processing. The pixel value range is 0-255. Perform threshold and inversion processing, set the pixel value of pixels greater than 200 to 0, otherwise set it to 255;

[0094] Step 3.3: Detect vertical lines on the image to determine whether the table has a frame. First, take a rectangle with a height of 40 pixels and a width of 1 pixel as the convolution kernel. Perform morphological processing on the image obtained in the previous step by first corroding and then dilating the kernel. Obtain all vertical lines with a length greater than 40 pixels. Count the lengths of these vertical lines. If the total length exceeds 1000 pixels set by experience, it is determined that the table has a frame. Go to step 3.4.1; otherwise, it is considered that the table has no frame, and go to step 3.5.1.

[0095] Step 3.4.1: Reconstruct the inner frame structure of the table with frame lines. By setting the convolution kernel with a height of 40 pixels, a width of 1 pixel, and a height of 40, the result obtained in step 3.2 is first eroded and then expanded to obtain an image with all vertical frame line pixels of 255 and other pixels of 0, such as Figure 5 Similarly, we set the convolution kernel width to 40 and height to 1 pixel, and get an image with all horizontal frame lines of 255 pixels, such as Figure 4 Get a set of all points whose pixel value in both images is 255. Group all points whose distance is less than 5 (pixels) into one point until the distance between all coordinates in the set is greater than 5.

[0096] In the present invention, the distance between points is calculated by the following formula: D(X,Y)=sqrt((X1-Y1)^2+(X2-Y2)^2), wherein X1 and X2 are the horizontal and vertical coordinates of point X, and Y1 and Y2 are the horizontal and vertical coordinates of point Y, respectively.

[0097] Remove all isolated points. In the present invention, if there are no other points within the 2-pixel range of the horizontal or vertical coordinate of a point, that is, if the point does not share the horizontal and vertical coordinates with other points, the point is considered to be an isolated point and deleted. Set the horizontal coordinate value of all points sharing the horizontal axis to the average value of the horizontal coordinates of all these points, and similarly set the vertical coordinate value of all points sharing the vertical axis to the average value of the vertical coordinates of all these points.

[0098] After removing these redundant coordinates, the remaining coordinates can be considered as the intersection of the horizontal and vertical inner frame lines of the table, such as Figure 6 .

[0099] Step 3.4.2: Reconstruct the inner frame of the table through the intersection points, sort the horizontal and vertical axes of the intersection points obtained in the previous step, and obtain all horizontal and vertical coordinate sets.

[0100] Traverse all intersections and determine whether they are connected to adjacent intersections to form a frame line. The method is to sum the pixel values ​​of the corresponding straight lines between adjacent intersections in the graph. If the average value of the pixel values ​​is greater than 200, it is determined that a frame line exists. And record all the frame lines.

[0101] Step 3.4.3: By recording the obtained frame lines, we can finally get all the table cells. Sort and integrate all the cells in the table to get the inner frame line structure of the table, such as Figure 7 .

[0102] The data structure of the frame line in the table is defined as follows:

[0103] TableStructure (table inner frame):

[0104] Outline: [float x1, float y1, float x2, float y2] represents the position ratio information of the table outline in the entire PDF page. Each coordinate is a floating point number between 0 and 1.

[0105] Rows: List[float]: represents the position and proportion information of all horizontal lines in the table area;

[0106] Columns: List[float]: represents the position ratio information of all vertical borders in the table area;

[0107] Units: List[Unit] is a list of table cells;

[0108] The definition of Unit is as follows:

[0109] Unit:

[0110] Float X1: the horizontal coordinate of the upper left corner of the cell;

[0111] FloatY1: the vertical coordinate of the upper left corner of the cell;

[0112] Float X2: the horizontal coordinate of the lower right corner of the cell;

[0113] FloatY2: the vertical coordinate of the lower right corner of the cell;

[0114] Int RowId: The subscript number of the horizontal coordinate of the upper left corner of the cell area in Rows;

[0115] Int ColId: The subscript number of the ordinate of the upper left corner of the cell area in Columns;

[0116] Int MergeRow: The merged height of the cell;

[0117] Int MergeCol: the merged width of the cell;

[0118] Boolean Type: Whether the cell contains text.

[0119] Step 3.5.1: Process the image obtained in step 3.2 again. By setting the convolution kernel with a height of 40 pixels, a width of 1 pixel, and a height of 40, the result obtained in step 3.2 is first corroded and then expanded to obtain an image in which all vertical frame line pixels are 255 and other pixels are 0. Subtract the pixels at the same position from the image obtained in step 3.2 to obtain an image with the vertical line pixels removed. Similarly, remove the horizontal line pixels. The final image has the frame lines removed. The image is then corroded and then expanded with a convolution kernel of 3*3 to make the pixels in this article closely related and connected, and finally a median filter is performed, such as Figure 8 .

[0120] Step 3.5.2: Scan the image from the previous step horizontally from top to bottom, and sum the pixel values ​​of each row. If the sum of the pixel values ​​is 0, it is considered that a horizontal inner frame line of the table is found, and the position of the horizontal inner frame line is set to the middle position of all consecutive rows whose pixel values ​​sum to 0. Finally, all horizontal inner frame lines are obtained. The inside of two adjacent horizontal inner frame lines is a row in the table.

[0121] Step 3.5.3: Scan each line of the image vertically and sum all pixel values ​​from this line to the next line. If there are continuous vertical lines with a pixel sum of 0 exceeding the number of text spacing, mark the area as a blank area. Otherwise, mark it as a text area. Thus, we can obtain a set of areas with or without text in each line, such as Fig. 9 .

[0122] Step 3.5.4: Merge the interconnected blank areas between rows to form blank blocks, count the heights of the blank blocks, and remove all blank blocks with a height of 1 row.

[0123] Step 3.5.5: Scan vertically and find the vertical line that passes through the blank block with the largest total height. Such a vertical line is a vertical inner frame line. Make sure that the vertical lines found later do not pass through the blank block area that has been scanned, and search for vertical lines again until there are no blank blocks to scan. All the vertical lines finally obtained are all the vertical inner frame lines of the table, such as Fig.10 .

[0124] Step 3.5.6: Create the minimum cell of the table based on the horizontal and vertical inner borders. Scan each cell's vertical line to see if it passes through the text area. If so, delete the vertical line, which means the table has been merged. Delete the redundant table inner borders with empty first row and first column, and finally form the table inner border structure after merging cells. The cell structure definition and table inner border structure definition are the same as those in step 3.4.3, such as Fig.11 .

[0125] Step 4 includes extracting the text information of each cell from the same position of the PDF according to the frame structure of the table reconstructed in the previous step, and finally obtaining the content corresponding to each cell. Specifically:

[0126] The information of the specified page of PDF is extracted through PDFMiner and output in XML format, which includes the position information of each character. The position information of each character is obtained through regular expressions, and the text and position information of each character are obtained in the form of [upper left corner horizontal coordinate, upper left corner vertical coordinate, lower right corner horizontal coordinate, lower right corner vertical coordinate, character], for example [100.1, 100.2, 120.3, 130.4, "table"]. The information of these characters is formed into a list, and the boxes described by these character positions are reduced by 50% in place to avoid missing operations due to errors when extracting text later.

[0127] Traverse each cell of the inner frame structure to obtain the location information of each cell in the PDF. All characters in the cell area are classified as the content of the cell. Thus, the text information corresponding to all table cells is obtained, such as Fig.12 and Fig.13 .

[0128] Step 5 includes creating an Excel table based on the frame structure and the corresponding text content in the table, and saving the merged cell information. This step uses the xlwt module to create an Excel file by giving the cell location, merge, and text information, such as Fig.14 and Fig.15 .

[0129] First, a method for reconstructing the table structure and extracting text from PDF based on computer vision provides a method for automatically detecting tables in PDF, reconstructing the table structure, and extracting the table content to generate an Excel file. The present invention can extract tables from a large amount of PDF data without supervision. Secondly, the present invention distinguishes and differentiates between frames and frames without frames, reconstructs the inner frame structure in two different ways, and makes full use of statistical data such as text spacing to make the table reconstruction more accurate. Finally, the present invention adopts the technology of extracting text in PDF, and uses OCR technology to enable non-text PDFs to also support this method, so that the text extraction process is unified and more efficient.

[0130] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.

[0131] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.

Claims

1. A table structure reconstruction and text extraction method based on computer vision, characterized in that: include: Step 1: Use a neural network to identify and locate the table in the PDF document and obtain the outer frame area where the table is located; Step 2: Parse the text layer in the PDF document to obtain the text spacing in the PDF document; Step 3: Reconstruct the frame structure of the table in the table area through computer vision according to the framed table area and the text spacing; Step 4: Extract text information from the same position in the PDF document based on the frame structure in the table; Step 5: Generate an editable table file based on the frame structure and corresponding text information in the table; The step 2 comprises: Step 2.1: Determine whether the PDF page where the table is located contains a text layer; Step 2.2: If there is no text layer, embed the page into a text layer using optical character recognition technology, and the position of the embedded text is at the corresponding position of the text in the image; Step 2.3: Count the sizes of all the characters in the PDF document and use the average width of the characters as the estimated value of the text spacing; The step 3 comprises: Step 3.1: Extract the table as an image based on the table frame area and the PDF page where the table is located; Step 3.2: Preprocessing the captured image, including threshold processing and morphological processing, to remove noise points other than text and frame lines in the table; Step 3.3: Detect vertical lines on the table. If the vertical line pixels exceed the preset value, it means that the table contains frame lines and execute step 3.

4. Otherwise, execute step 3.

5. Step 3.4: Reconstruct the table structure of the table with frame lines, extract all vertical and horizontal lines of the table, obtain the intersection point set of the vertical and horizontal lines, and after removing redundant points, form the inner frame intersection point set of the table; based on the obtained intersection point set, determine whether the adjacent points form the inner frame line of the table, and if so, connect the two points to form an edge; form the table structure of the frame line table based on the points and edges; Step 3.5: Preprocess the image, remove the horizontal and vertical lines in the image whose length exceeds the preset threshold, and perform threshold processing on the image so that the pixel value of the blank position is 0 and the pixel value of the pixel containing text is 255; perform row scanning on the image, if the sum of the pixel values ​​of a row scanned is 0, then the row is the horizontal inner frame line of the table, and the position of the inner frame line of the table is the middle position of all rows with the sum of continuous pixel values ​​of 0, and the inside of two adjacent horizontal inner frame lines is a row in the table; perform vertical scanning between two adjacent horizontal inner frame lines, and sum the pixel values ​​of each column. If the sum of the vertical pixels that exceeds the number of text spacing is 0, then mark the area scanned by the vertical line as a blank area, otherwise mark it as a text area, so as to obtain the coordinate set of each row with or without text area; merge the rows that are interconnected and can be completely separated by a vertical line from top to bottom and from left to right The blank areas that are not penetrated are formed into blank blocks, and the height of each blank block is recorded, and all blank blocks with a height of 1 line are removed; all vertical lines that penetrate the blank blocks are traversed, and the total height of all blank blocks passed by this vertical line is recorded; finally, the vertical line with the largest total height of the blank blocks passed through is used as a vertical inner frame line of the table, and the blank blocks passed through by this line are set as passed through; the vertical line with the largest total height that can pass through other blank blocks that have not been passed through is selected as another vertical frame line of the table, and the blank blocks passed through by it are recorded as passed through, and finally vertical lines are obtained continuously until all blank blocks are passed through; the minimum cell of the table is established according to the obtained horizontal inner frame lines and vertical inner frame lines, and the vertical line of each cell is scanned to see if it passes through the area with text. If so, the vertical line is deleted, and the left and right cells of the table are merged to finally form the inner frame line structure of the table after the cells are merged.

2. The method for table structure reconstruction and text extraction based on computer vision according to claim 1, characterized in that: The step 1 comprises: Step 1.1: Train and configure the table detection neural network; Step 1.2: Convert each page of the PDF document containing a table target into an image, input each image into the table detection neural network, and if a table target exists, return the page number of the PDF document where the table target is located and the relative position of the table frame on the page of the PDF document.

3. The method for table structure reconstruction and text extraction based on computer vision according to claim 1, characterized in that: The step 4 comprises: obtaining the rectangular frame coordinates of each cell in the PDF document according to the reconstructed frame line structure of the table, extracting the text information in the same position area from the PDF document containing the text layer, and obtaining the content of the table cell by removing spaces and adjusting; The step 5 includes: creating an Excel table according to all the table inner borders and the contents of the corresponding table cells, and saving the merged cell information.

4. A table structure reconstruction and text extraction system based on computer vision, characterized in that: include: Module M1: Identify and locate the table in the PDF document through a neural network to obtain the outer frame area where the table is located; Module M2: parse the text layer in the PDF document to obtain the text spacing in the PDF document; Module M3: Reconstruct the frame structure of the table in the table area through computer vision according to the framed table area and the text spacing; Module M4: Extract text information from the same position in the PDF document based on the frame structure in the table; Module M5: Generate an editable table file based on the frame structure and corresponding text information in the table; The module M2 comprises: Module M2.1: Determine whether the PDF page where the table is located contains a text layer; Module M2.2: If there is no text layer, embed the page into the text layer using optical character recognition technology, with the position of the embedded text being the corresponding position of the text in the image; Module M2.3: Count the sizes of all the text in the PDF document and use the average text width as the estimated value of the text spacing; The module M3 comprises: Module M3.1: Extract the table as an image based on the table frame area and the PDF page where the table is located; Module M3.2: Preprocessing the captured image, including threshold processing and morphological processing, to remove noise points other than text and frame lines in the table; Module M3.3: Detect vertical lines in the table. If the vertical line pixels exceed the preset value, it indicates that the table contains frame lines and calls module M3.

4. Otherwise, calls module M3.

5. Module M3.4: Reconstruct the table structure of the table with frame lines, extract all the vertical and horizontal lines of the table, obtain the intersection point set of the vertical and horizontal lines, and after removing redundant points, form the inner frame intersection point set of the table; based on the obtained intersection point set, determine whether the adjacent points form the inner frame line of the table, and if so, connect the two points to form an edge; form the table structure of the frame line table based on the points and edges; Module M3.5: Preprocess the image, remove the horizontal and vertical lines in the image whose length exceeds the preset threshold, and perform threshold processing on the image so that the pixel value of the blank position is 0 and the pixel value of the pixel containing text is 255; perform row scanning on the image, if the sum of the pixel values ​​of a row scanned is 0, then the row is the horizontal inner frame line of the table, and the position of the inner frame line of the table is the middle position of all rows with the sum of continuous pixel values ​​of 0, and the inside of two adjacent horizontal inner frame lines is a row in the table; perform vertical scanning between two adjacent horizontal inner frame lines, sum the pixel values ​​of each column, if there is a continuous vertical pixel sum of 0 exceeding the number of text spacing, then mark the area scanned by the vertical line as a blank area, otherwise mark it as a text area, so as to obtain the coordinate set of each row with or without text area; merge the rows that are interconnected and can be separated by a vertical line from top to bottom and from left to right All blank areas that are penetrated are formed into blank blocks, the height of each blank block is recorded, and all blank blocks with a height of 1 line are removed; all vertical lines that penetrate the blank blocks are traversed, and the total height of all blank blocks passed by this vertical line is recorded; finally, the vertical line with the largest total height passing through the blank blocks is used as a vertical inner frame line of the table, and the blank blocks passed by this line are set as passed through; select the vertical line with the largest total height that can pass through other blank blocks that have not been passed through as another vertical frame line of the table, and record the blank blocks it passes through as passed through, and finally continue to obtain vertical lines until all blank blocks are passed through; establish the minimum cell of the table based on the obtained horizontal inner frame lines and vertical inner frame lines, scan whether the vertical line of each cell passes through the area with text, if so, delete the vertical line, and merge the left and right cells of the table, and finally form the table inner frame line structure after the merged cells.

5. The table structure reconstruction and text extraction system based on computer vision according to claim 4, characterized in that: The module M1 comprises: Module M1.1: Train and configure the table detection neural network; Module M1.2: Convert each page of a PDF document containing a table target into an image, input each image into a table detection neural network, and if a table target exists, return the page number of the PDF document where the table target is located and the relative position of the table frame on the page of the PDF document.

6. The table structure reconstruction and text extraction system based on computer vision according to claim 4, characterized in that: The module M4 includes: obtaining the rectangular frame coordinates of each cell in the PDF document according to the reconstructed frame line structure of the table, extracting the text information in the same position area from the PDF document containing the text layer, and obtaining the content of the table cell after removing spaces and adjusting; The module M5 includes: creating an Excel table according to all the table inner borders and the contents of the corresponding table cells, and saving the merged cell information.