OCR-based table format recovery method, device and storage medium
Through the OCR-based table layout recovery method, the Y-axis projection after multiple rotations and the keyword set of a specific table type are used to accurately perform row and column cutting, which solves the accuracy problem of table content extraction without table lines and improves The effect of table layout restoration.
Patent Information
- Application Number
- CN202010076101.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-01-22
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2040-01-22
AI Technical Summary
It is difficult to accurately extract table contents without table lines with the existing technology, resulting in low accuracy in table layout restoration, and it is difficult to quickly and accurately extract table contents from tables without table lines.
An OCR-based table layout recovery method is adopted. By receiving table images and performing OCR recognition, the Y-axis projection after multiple rotations is used to determine the row cutting line, and the header information and keyword set are determined based on the specific table type, and the column is accurately processed. Cutting, adjust column cutting lines to reduce false crossings and ensure accurate cell analysis.
It improves the accuracy of table layout recovery, ensures accurate extraction of table content, and enhances the processing capabilities of tables without table lines.
Smart Images

Figure CN111310426B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image recognition, and in particular to an OCR-based table layout recovery method, an electronic device and a computer-readable storage medium. Background Art
[0002] With the increasing use of paper forms in offices, a large amount of table data needs to be processed and counted, which requires a lot of manpower. If the scanned form image is extracted and restored into an electronic form by computer, the data of the form can be batch processed and analyzed through the program to improve office efficiency.
[0003] With high detection and recognition accuracy rates for general OCR recognition, there are already mature methods for extracting structured data from standard paragraphs and tables with table lines. For example, deep learning is used to directly extract table features from images, but this method has a low recognition rate. Morphology can also be used to detect some features of tables, such as table borders, but this method relies on the characteristics of table borders. If a table without borders is encountered, this method will fail. Therefore, there are still great challenges in structuring table images without table lines, and the difficulty mainly lies in how to divide the cells. How to quickly and accurately extract table content from tables without table lines has become an urgent problem to be solved. Summary of the invention
[0004] In view of the above, the present invention provides an OCR-based table layout recovery method, an electronic device and a computer-readable storage medium, the main purpose of which is to improve the accuracy of table layout recovery.
[0005] To achieve the above object, the present invention provides a table layout recovery method based on OCR, the method comprising:
[0006] Receiving step: receiving a table layout recovery instruction issued by a user, wherein the table layout recovery instruction includes a table image to be recovered and a picture type of the table image to be recovered;
[0007] Recognition step: performing OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes a plurality of text boxes and text information corresponding to each of the plurality of text boxes;
[0008] Row cutting step: performing row cutting on the table image to be restored based on the recognition result and a preset row cutting rule to obtain a first cutting result;
[0009] Column cutting step: determining the header information corresponding to the first cutting result based on the image type, and performing column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result; and
[0010] Restoration step: generate a target table structure based on the second cutting result, write the text information corresponding to the multiple text boxes into the cells of the target table structure, generate a target table corresponding to the table image to be restored, and send the target table to the user.
[0011] In addition, to achieve the above-mentioned purpose, the present invention also provides an electronic device, which includes: a memory and a processor, wherein the memory stores an OCR-based table layout recovery program that can be run on the processor, and when the OCR-based table layout recovery program is executed by the processor, any step in the OCR-based table layout recovery method as described above can be implemented.
[0012] In addition, to achieve the above-mentioned purpose, the present invention also provides a computer-readable storage medium, which includes an OCR-based table layout recovery program. When the OCR-based table layout recovery program is executed by a processor, any step in the OCR-based table layout recovery method as described above can be implemented.
[0013] The table layout recovery method, electronic device and computer-readable storage medium based on OCR proposed in the present invention use the text box identified by OCR to rotate multiple times and then project on the Y axis, select the best angle from the multiple projection results, and determine the row cutting line of the table according to the projection result of the best angle, and determine the column cutting line of the table by identifying the header information of the table, which can effectively improve the accuracy of the row cutting line and the column cutting line, and lay a foundation for accurately extracting the table content; in the process of identifying the header information of the table, the corresponding keyword set is determined according to the specific header information corresponding to the specific table type, and the text information corresponding to the text box identified by the general OCR is matched with the keyword set to determine the header information, thereby improving the accuracy of header information extraction and further improving the accuracy of column cutting; after the column cutting line is preliminarily determined according to the header information, the column cutting line is adjusted to reduce the situation where the column cutting line passes through the text box, thereby further improving the accuracy of column cutting; in the process of extracting the table details, each cell and text box is analyzed to determine the text box corresponding to each cell, thereby improving the accuracy of table content extraction. In summary, the accuracy of table layout recovery is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 It is a flow chart of a preferred embodiment of the table format recovery method based on OCR of the present invention;
[0015] Figure 2 A schematic diagram of a preferred embodiment of the electronic device of the present invention;
[0016] Figure 3 for Figure 2 A schematic diagram of program modules of a preferred embodiment of an OCR-based table layout recovery program.
[0017] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0018] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.
[0019] The present invention provides a table format recovery method based on OCR. The method can be executed by a device, and the device can be implemented by software and / or hardware.
[0020] Reference Figure 1 As shown, it is a flow chart of a preferred embodiment of the table layout recovery method based on OCR of the present invention.
[0021] In a preferred embodiment of the OCR-based table layout recovery method of the present invention, the OCR-based table layout recovery method includes: steps S1 to S5.
[0022] Step S1, receiving a table layout recovery instruction issued by a user, wherein the table layout recovery instruction includes a table image to be recovered and the image type of the table image to be recovered.
[0023] The following describes various embodiments of the present invention using an electronic device as an execution subject.
[0024] In this embodiment, the table image to be restored is a table without table lines. It should be noted that in order to ensure the accuracy of table layout restoration, the table image to be restored must meet several characteristics: rows and columns are arranged neatly; there is a certain spacing between rows and columns (the larger the spacing, the higher the accuracy); the table has a header and each column information corresponds to the header; the header information of tables of the same type is fixed.
[0025] The image type of the table image to be restored is used to distinguish the table images corresponding to different application scenarios. In this embodiment, different tables of the same type correspond to the same header information by default. For example, the header information of table images such as expense lists includes: "unit price", "quantity", "amount", "project name", etc., and the header information of table images such as financial statements includes: "project", "current year number", "amount", "beginning balance", etc.
[0026] The user selects the table image to be restored and its corresponding image type through the APP on the client, and issues a table format restoration instruction. After receiving the instruction issued by the client, the electronic device performs a table detail structure operation on the table image to be restored carried in the instruction.
[0027] Step S2, performing OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes multiple text boxes and text information corresponding to each of the multiple text boxes.
[0028] The recognition result obtained by performing OCR recognition on the table image to be restored includes: multiple text boxes, coordinate information of the multiple text boxes and text information corresponding to the text boxes. In view of the fact that there are mature OCR recognition technologies in the prior art, they will not be described in detail here.
[0029] Step S3, performing row cutting on the table image to be restored based on the recognition result and a preset row cutting rule to obtain a first cutting result.
[0030] In this embodiment, the performing row cutting on the table image to be restored based on the recognition result and the preset row cutting rule includes:
[0031] a1. Rotate the multiple text boxes in the recognition result according to multiple preset angles, project the rotated multiple text boxes on the y-axis, and determine multiple projection intervals and multiple overlapping projection segments corresponding to the multiple preset angles on the y-axis;
[0032] a2. Selecting the smallest projection interval among the multiple projection intervals, or selecting the preset angle with the most overlapping projection segments among the multiple overlapping projection segments as the target angle, and determining the overlapping projection segments corresponding to the target angle;
[0033] a3, determining the row cutting lines of the multiple text boxes at the target angle according to the overlapping projection segments corresponding to the target angle; and
[0034] a4. Determine the text box corresponding to each row according to the row cutting lines of the multiple text boxes at the target angle.
[0035] Generally speaking, in a neat table image, there is a certain distance between rows. In terms of algorithm performance, the projection of the text box recognized by general OCR on the y-axis also has a relatively neat pattern, and the projections of the text boxes in the same row on the y-axis are relatively close.
[0036] The range of the above preset angle can be set to -1° to 1°, with a step size of 0.1°.
[0037] By rotating the image with the center of the table image to be restored, and projecting all the text boxes of the rotated image on the y-axis, the projection coordinates of each text box on the y-axis are obtained, and the projection interval corresponding to the current rotation angle is determined based on the projection coordinates of each text box, and the number and coordinates of overlapping projection fragments are recorded. The angle with the most overlapping projection fragments is selected from these angles, and the line cutting line is drawn according to the overlapping projection fragments.
[0038] Specifically, determining the row cutting lines of the plurality of text boxes at the target angle according to the overlapping projection segments corresponding to the target angle includes:
[0039] The coordinates of each overlapping projection segment in the overlapping projection segments corresponding to the target angle are obtained, and the minimum coordinate of each overlapping projection segment is used as a lower cutting line of a row, and the maximum coordinate of each overlapping projection segment is used as an upper cutting line of a row.
[0040] The finally determined rotated table image to be restored and the row cutting line generate a first cutting result, the first cutting result including: row information of the rotated table image to be restored and a text box corresponding to each row.
[0041] In other embodiments, if the rows are too close to each other or the image is 3D twisted, it may be impossible to determine a valid dividing line even if the image is rotated along the center. In this case, the part of the text box that cannot be divided is taken as the center of the circle, and steps a1-a3 are iteratively performed.
[0042] Step S4: determining the header information corresponding to the first cutting result based on the image type, and performing column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result.
[0043] After determining the row information of the table image to be restored, it is necessary to further determine the column information of the table image to be restored. It should be noted that, in general, the column information of the table is closely related to the header information of the table. Therefore, it is necessary to first determine the header information in the table image to be restored. In this embodiment, the determining the header information corresponding to the first cutting result based on the image type includes:
[0044] b1. Acquire mapping data of a predetermined image type and a header keyword set from a preset storage path, and determine a header keyword set corresponding to the first segmentation result according to the image type and the mapping data;
[0045] b2. Obtaining text information of a text box in each row of the first segmentation result, and matching the text information of the text box in each row with the keyword set; and
[0046] b3. When there is text information matching the keyword set, a row of text boxes corresponding to the text information matching the keyword set is used as header information of the first cutting result.
[0047] Each image type in the above mapping data corresponds to a keyword set. It should be noted that it is necessary to predetermine the header information corresponding to the various image types, determine the keyword sets corresponding to the various image types, generate mapping data of the image type and the keyword set, and save the mapping data to a preset storage path.
[0048] Since there is no table line to assist, the column segmentation cannot be performed accurately. In this embodiment, the column segmentation line is calculated using the position information of the table header. In actual business scenarios, tables of the same type have similar header content, and the header content generally does not appear in the redundant information at the top of the table, nor does it appear in the detailed items. Therefore, locating the header by searching for keywords is an effective method. Taking the expense list as an example, after the row cutting of the table image to be restored is performed to generate the first cutting result, the text boxes in the first cutting result are arranged from top to bottom, and the text information of the text boxes is compared with the determined keyword set in turn. Whenever the keywords "unit price", "quantity", and "amount" appear, the search stops and the row where the element is located is found. This row is the header of the first cutting result.
[0049] After determining the header information in the first cutting result, further determining the column cutting line according to the header information. In this embodiment, performing column cutting on the first cutting result based on the header information and the preset column cutting rule to obtain the second cutting result includes:
[0050] c1. Obtain each header element in the header information of the first cutting result, and make a vertical cutting line at the rightmost of the text box corresponding to each header element; and
[0051] c2. Perform column cutting on the first cutting result according to the vertical cutting line to obtain a second cutting result.
[0052] For example, when the header information contains 6 header elements, determine the text boxes corresponding to the 6 header elements respectively, read the position information of each text box, that is, the coordinates, determine the rightmost point of the text box, use the 6 rightmost points corresponding to the 6 header elements as column cutting points, and make vertical cutting lines for column cutting.
[0053] In order to ensure the accuracy of column segmentation, in other embodiments, the performing column segmentation on the first cutting result according to the vertical cutting line to obtain the second cutting result further includes:
[0054] d1, taking the vertical cutting line as the first column of cutting lines;
[0055] d2. Calculate the center point of each text box in the first cutting result, record the text boxes whose center points are within the first column of cutting lines, and calculate the average and standard deviation of the boundary points of the outer frame of each column of text boxes in the X-axis direction;
[0056] d3. When the standard deviation is less than or equal to a preset standard deviation threshold, adjusting the first column cutting line of a corresponding column of text boxes according to the average to generate a second column cutting line; and
[0057] d4. Perform column cutting on the first cutting result according to the second column cutting line to obtain the second cutting result.
[0058] It is understandable that the column cutting point is initially located by making a vertical dividing line at the rightmost of the outer border of each header element. Such a column cutting point may not be accurate. For example, the column cutting line of a header element may pass through its corresponding detailed content. Although there are deviations, the horizontal arrangement of the table details is relatively neat, with certain boundaries, and is usually aligned to the left or right in the table. This feature can be used in the correction of column cutting. First, record the text boxes whose center points are within the two adjacent column dividing lines, and calculate the average and standard deviation of the outer border boundary points of the text box of each column (for example, the x-axis coordinate of the rightmost point). The standard deviation represents whether this column has the characteristics of left or right alignment. If the standard deviation is lower than the preset standard deviation threshold (the average width of the characters in the picture), it can be considered that the table has an alignment feature, and the corresponding column cutting point can be moved to the average of the outer border boundary points of the text box of the column (for example, the x-axis coordinate of the rightmost point), otherwise the initially located column cutting point remains unchanged. Compared with directly drawing the outer boundary line, the accuracy of the column cutting line adjusted by the above steps is improved.
[0059] Step S5, generating a target table structure based on the second cutting result, writing the text information corresponding to the multiple text boxes into the cells of the target table structure, generating a target table corresponding to the table image to be restored, and sending the target table to the user.
[0060] The second cutting result includes both row cutting lines and column cutting lines, that is, a target table structure can be determined. A new empty table with the same target table structure as that determined by the second cutting result is created, and the text information of the text boxes within the target table structure is filled into the table cells in sequence to extract the table details in the table image to be restored.
[0061] In order to avoid missing or repeated filling of text information, the text information corresponding to the text box is filled into the cell according to a preset order. In this embodiment, the preset order is: according to the order of rows / columns from top to bottom and from left to right.
[0062] In this embodiment, writing the text information corresponding to the multiple text boxes into the cells of the target table structure includes:
[0063] e1. Determine a plurality of intersection text boxes corresponding to each cell in the target table structure, and respectively calculate the areas of the plurality of intersection text boxes and the overlapping areas of the plurality of intersection text boxes and the cells; and
[0064] e2. When the ratio of the overlapping area to the intersection text box area exceeds a preset ratio threshold, the intersection text box is determined to be a target text box corresponding to the cell, and the text information of the target text box is written into the cell.
[0065] It should be noted that the same text box may be between two adjacent cells in the target table structure. In order to further ensure the accuracy of table detail extraction, the area of the quadrilateral formed by the column cutting line and the row cutting line and the overlapping area of the text box that intersects with it are calculated in each cell. If the overlapping area accounts for more than 0.5 of the area of the text box, the text box is considered to belong to the corresponding cell. In other embodiments, if multiple text boxes meet the above conditions, they are placed according to the priority of the top and left positions.
[0066] The OCR-based table layout recovery method proposed in the above embodiment uses the text box recognized by OCR to rotate multiple times and then project it on the Y axis, selects the best angle from the multiple projection results, and determines the row cutting line of the table according to the projection result of the best angle. The column cutting line of the table is determined by identifying the header information of the table, which can effectively improve the accuracy of the row cutting line and the column cutting line, and lay the foundation for accurately extracting the table content; in the process of identifying the header information of the table, the corresponding keyword set is determined according to the specific header information corresponding to the specific table type, and the text information corresponding to the text box recognized by the general OCR is matched with the keyword set to determine the header information, thereby improving the accuracy of header information extraction and further improving the accuracy of column cutting; after the column cutting line is preliminarily determined according to the header information, the column cutting line is adjusted to reduce the situation where the column cutting line passes through the text box, thereby further improving the accuracy of column cutting; in the process of extracting the table details, each cell and text box is analyzed to determine the text box corresponding to each cell, thereby improving the accuracy of table content extraction. In summary, the accuracy of table layout recovery is improved.
[0067] The present invention also provides an electronic device. Figure 2 FIG. 1 is a schematic diagram of a preferred embodiment of the electronic device of the present invention.
[0068] In this embodiment, the electronic device 1 can be a terminal device with data processing function such as a server, a smart phone, a tablet computer, a portable computer, a desktop computer, etc. The server can be a rack server, a blade server, a tower server or a cabinet server.
[0069] The electronic device 1 includes a memory 11 , a processor 12 , and a network interface 13 .
[0070] The memory 11 includes at least one type of readable storage medium, and the readable storage medium includes a flash memory, a hard disk, a multimedia card, a card-type memory (e.g., an SD or DX memory, etc.), a magnetic memory, a magnetic disk, an optical disk, etc. In some embodiments, the memory 11 may be an internal storage unit of the electronic device 1, such as a hard disk of the electronic device 1. In other embodiments, the memory 11 may also be an external storage device of the electronic device 1, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card, etc. equipped on the electronic device 1. Furthermore, the memory 11 may also include both an internal storage unit of the electronic device 1 and an external storage device.
[0071] The memory 11 can be used not only to store application software and various data installed in the electronic device 1, such as the OCR-based table layout recovery program 10, but also to temporarily store data that has been output or is to be output.
[0072] In some embodiments, the processor 12 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor or other data processing chip, used to run program codes or process data stored in the memory 11, such as an OCR-based table layout recovery program 10.
[0073] The network interface 13 may optionally include a standard wired interface, a wireless interface (such as a WI-FI interface), and is generally used to establish a communication connection between the electronic device 1 and other electronic devices, such as a client (not shown in the figure). The components 11-13 of the electronic device 1 communicate with each other via a communication bus.
[0074] Figure 2 Only the electronic device 1 having components 11-13 is shown, and those skilled in the art will understand that Figure 2 The structure shown does not constitute a limitation on the electronic device 1 , and the electronic device 1 may include fewer or more components than shown in the figure, or combine some components, or arrange the components differently.
[0075] Optionally, the electronic device 1 may further include a user interface, which may include a display (Display) and an input unit such as a keyboard (Keyboard). The optional user interface may also include a standard wired interface and a wireless interface.
[0076] Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an organic light-emitting diode (OLED) touch device, etc. The display may also be referred to as a display screen or a display unit, which is used to display information processed in the electronic device 1 and to display a visual user interface.
[0077] exist Figure 2 In the embodiment of the electronic device 1 shown, the program code of the OCR-based table format recovery program 10 is stored in the memory 11 as a computer storage medium. When the processor 12 executes the program code of the OCR-based table format recovery program 10, the following steps are implemented:
[0078] Receiving step: receiving a table layout recovery instruction issued by a user, wherein the table layout recovery instruction includes a table image to be recovered and an image type of the table image to be recovered.
[0079] The following describes various embodiments of the present invention using an electronic device as an execution subject.
[0080] In this embodiment, the table image to be restored is a table without table lines. It should be noted that in order to ensure the accuracy of table layout restoration, the table image to be restored must meet several characteristics: rows and columns are arranged neatly; there is a certain spacing between rows and columns (the larger the spacing, the higher the accuracy); the table has a header and each column information corresponds to the header; the header information of tables of the same type is fixed.
[0081] The image type of the table image to be restored is used to distinguish the table images corresponding to different application scenarios. In this embodiment, different tables of the same type correspond to the same header information by default. For example, the header information of table images such as expense lists includes: "unit price", "quantity", "amount", "project name", etc., and the header information of table images such as financial statements includes: "project", "current year number", "amount", "beginning balance", etc.
[0082] The user selects the table image to be restored and its corresponding image type through the APP on the client, and issues a table format restoration instruction. After receiving the instruction issued by the client, the electronic device 1 performs a table detail structuring operation on the table image to be restored carried in the instruction.
[0083] Recognition step: performing OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes multiple text boxes and text information corresponding to each of the multiple text boxes.
[0084] The recognition result obtained by performing OCR recognition on the table image to be restored includes: multiple text boxes, coordinate information of the multiple text boxes and text information corresponding to the text boxes. In view of the fact that there are mature OCR recognition technologies in the prior art, they will not be described in detail here.
[0085] Row cutting step: based on the recognition result and the preset row cutting rule, the table image to be restored is subjected to row cutting to obtain a first cutting result.
[0086] In this embodiment, the performing row cutting on the table image to be restored based on the recognition result and the preset row cutting rule includes:
[0087] a1. Rotate the multiple text boxes in the recognition result according to multiple preset angles, project the rotated multiple text boxes on the y-axis, and determine multiple projection intervals and multiple overlapping projection segments corresponding to the multiple preset angles on the y-axis;
[0088] a2. Selecting the smallest projection interval among the multiple projection intervals, or selecting the preset angle with the most overlapping projection segments among the multiple overlapping projection segments as the target angle, and determining the overlapping projection segments corresponding to the target angle;
[0089] a3, determining the row cutting lines of the multiple text boxes at the target angle according to the overlapping projection segments corresponding to the target angle; and
[0090] a4. Determine the text box corresponding to each row according to the row cutting lines of the multiple text boxes at the target angle.
[0091] Generally speaking, in a neat table image, there is a certain distance between rows. In terms of algorithm performance, the projection of the text box recognized by general OCR on the y-axis also has a relatively neat pattern, and the projections of the text boxes in the same row on the y-axis are relatively close.
[0092] The range of the above preset angle can be set to -1° to 1°, with a step size of 0.1°.
[0093] By rotating the image with the center of the table image to be restored, and projecting all the text boxes of the rotated image on the y-axis, the projection coordinates of each text box on the y-axis are obtained, and the projection interval corresponding to the current rotation angle is determined based on the projection coordinates of each text box, and the number and coordinates of overlapping projection fragments are recorded. The angle with the most overlapping projection fragments is selected from these angles, and the line cutting line is drawn according to the overlapping projection fragments.
[0094] Specifically, determining the row cutting lines of the plurality of text boxes at the target angle according to the overlapping projection segments corresponding to the target angle includes:
[0095] The coordinates of each overlapping projection segment in the overlapping projection segments corresponding to the target angle are obtained, and the minimum coordinate of each overlapping projection segment is used as a lower cutting line of a row, and the maximum coordinate of each overlapping projection segment is used as an upper cutting line of a row.
[0096] The finally determined rotated table image to be restored and the row cutting line generate a first cutting result, the first cutting result including: row information of the rotated table image to be restored and a text box corresponding to each row.
[0097] In other embodiments, if the rows are too close to each other or the image is 3D twisted, it may be impossible to determine a valid dividing line even if the image is rotated along the center. In this case, the part of the text box that cannot be divided is taken as the center of the circle, and steps a1-a3 are iteratively performed.
[0098] Column cutting step: determining the header information corresponding to the first cutting result based on the image type, and performing column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result.
[0099] After determining the row information of the table image to be restored, it is necessary to further determine the column information of the table image to be restored. It should be noted that, in general, the column information of the table is closely related to the header information of the table. Therefore, it is necessary to first determine the header information in the table image to be restored. In this embodiment, the determining the header information corresponding to the first cutting result based on the image type includes:
[0100] b1. Acquire mapping data of a predetermined image type and a header keyword set from a preset storage path, and determine a header keyword set corresponding to the first segmentation result according to the image type and the mapping data;
[0101] b2. Obtaining text information of a text box in each row of the first segmentation result, and matching the text information of the text box in each row with the keyword set; and
[0102] b3. When there is text information matching the keyword set, a row of text boxes corresponding to the text information matching the keyword set is used as header information of the first cutting result.
[0103] Each image type in the above mapping data corresponds to a keyword set. It should be noted that it is necessary to predetermine the header information corresponding to the various image types, determine the keyword sets corresponding to the various image types, generate mapping data of the image type and the keyword set, and save the mapping data to a preset storage path.
[0104] Since there is no table line to assist, the column segmentation cannot be performed accurately. In this embodiment, the column segmentation line is calculated using the position information of the table header. In actual business scenarios, tables of the same type have similar header content, and the header content generally does not appear in the redundant information at the top of the table, nor does it appear in the detailed items. Therefore, locating the header by searching for keywords is an effective method. Taking the expense list as an example, after the row cutting of the table image to be restored is performed to generate the first cutting result, the text boxes in the first cutting result are arranged from top to bottom, and the text information of the text boxes is compared with the determined keyword set in turn. Whenever the keywords "unit price", "quantity", and "amount" appear, the search stops and the row where the element is located is found. This row is the header of the first cutting result.
[0105] After determining the header information in the first cutting result, further determining the column cutting line according to the header information. In this embodiment, performing column cutting on the first cutting result based on the header information and the preset column cutting rule to obtain the second cutting result includes:
[0106] c1. Obtain each header element in the header information of the first cutting result, and make a vertical cutting line at the rightmost of the text box corresponding to each header element; and
[0107] c2. Perform column cutting on the first cutting result according to the vertical cutting line to obtain a second cutting result.
[0108] For example, when the header information contains 6 header elements, determine the text boxes corresponding to the 6 header elements respectively, read the position information of each text box, that is, the coordinates, determine the rightmost point of the text box, use the 6 rightmost points corresponding to the 6 header elements as column cutting points, and make vertical cutting lines for column cutting.
[0109] In order to ensure the accuracy of column segmentation, in other embodiments, the performing column segmentation on the first cutting result according to the vertical cutting line to obtain the second cutting result further includes:
[0110] d1, taking the vertical cutting line as the first column of cutting lines;
[0111] d2. Calculate the center point of each text box in the first cutting result, record the text boxes whose center points are within the first column of cutting lines, and calculate the average and standard deviation of the boundary points of the outer frame of each column of text boxes in the X-axis direction;
[0112] d3. When the standard deviation is less than or equal to a preset standard deviation threshold, adjusting the first column cutting line of a corresponding column of text boxes according to the average to generate a second column cutting line; and
[0113] d4. Perform column cutting on the first cutting result according to the second column cutting line to obtain the second cutting result.
[0114] It is understandable that the column cutting point is initially located by making a vertical dividing line at the rightmost of the outer border of each header element. Such a column cutting point may not be accurate. For example, the column cutting line of a header element may pass through its corresponding detailed content. Although there are deviations, the horizontal arrangement of the table details is relatively neat, with certain boundaries, and is usually aligned to the left or right in the table. This feature can be used in the correction of column cutting. First, record the text boxes whose center points are within the two adjacent column dividing lines, and calculate the average and standard deviation of the outer border boundary points of the text box of each column (for example, the x-axis coordinate of the rightmost point). The standard deviation represents whether this column has the characteristics of left or right alignment. If the standard deviation is lower than the preset standard deviation threshold (the average width of the characters in the picture), it can be considered that the table has an alignment feature, and the corresponding column cutting point can be moved to the average of the outer border boundary points of the text box of the column (for example, the x-axis coordinate of the rightmost point), otherwise the initially located column cutting point remains unchanged. Compared with directly drawing the outer boundary line, the accuracy of the column cutting line adjusted by the above steps is improved.
[0115] Restoration step: generate a target table structure based on the second cutting result, write the text information corresponding to the multiple text boxes into the cells of the target table structure, generate a target table corresponding to the table image to be restored, and send the target table to the user.
[0116] The second cutting result includes both row cutting lines and column cutting lines, that is, a target table structure can be determined. A new empty table with the same target table structure as that determined by the second cutting result is created, and the text information of the text boxes within the target table structure is filled into the table cells in sequence to extract the table details in the table image to be restored.
[0117] In order to avoid missing or repeated filling of text information, the text information corresponding to the text box is filled into the cell according to a preset order. In this embodiment, the preset order is: according to the order of rows / columns from top to bottom and from left to right.
[0118] In this embodiment, writing the text information corresponding to the multiple text boxes into the cells of the target table structure includes:
[0119] e1. Determine a plurality of intersection text boxes corresponding to each cell in the target table structure, and respectively calculate the areas of the plurality of intersection text boxes and the overlapping areas of the plurality of intersection text boxes and the cells; and
[0120] e2. When the ratio of the overlapping area to the intersection text box area exceeds a preset ratio threshold, the intersection text box is determined to be a target text box corresponding to the cell, and the text information of the target text box is written into the cell.
[0121] It should be noted that the same text box may be between two adjacent cells in the target table structure. In order to further ensure the accuracy of table detail extraction, the area of the quadrilateral formed by the column cutting line and the row cutting line and the overlapping area of the text box that intersects with it are calculated in each cell. If the overlapping area accounts for more than 0.5 of the area of the text box, the text box is considered to belong to the corresponding cell. In other embodiments, if multiple text boxes meet the above conditions, they are placed according to the priority of the top and left positions.
[0122] It should be noted that the same table file has a similar row and column structure. However, due to printer limitations, the overlong part will be paged, and the paged part may not have header information. Due to the angle and three-dimensional rotation errors between each image during the photo shooting and scanning process, the cutting line may not be directly determined. In order to further improve the accuracy of the column cutting line, in other embodiments, when the table image to be restored includes a header page and a non-header page with the same row and column structure, the column cutting step includes:
[0123] f1, identifying the column cutting line of the page with a header and calculating the column cutting ratio of the page with a header;
[0124] f2. Performing row cutting on the page without a header based on the preset row cutting rule to obtain a first cutting result of the page without a header, and determining the row with the most text boxes in the page without a header according to the first cutting result; and
[0125] f3. Map the column cutting lines of the row with the most text boxes in the page without a header according to the column cutting ratio of the page with a header, and perform column cutting on the first cutting result of the page without a header according to the mapped column cutting lines of the page without a header to obtain the second cutting result of the page without a header.
[0126] It should be noted that the specific implementation of the column cutting rule and the preset row cutting rule of the header page is the same as that in the above embodiment, and will not be described in detail here.
[0127] When calculating the column cutting ratio of the header page, the outer boundary cutting line of the first column with content in the header page is used as the starting point, and the outer boundary line of the last column with content is used as the end point to calculate the ratio of each column, that is, to determine the relative position of each column cutting line.
[0128] In the process of determining the row with the most text boxes in the page without a header, the closer the number of text boxes is to the number of columns in the header of the page with a header, the more accurate the determined column cutting line is.
[0129] After determining the row and column cutting ratio, a range is determined with the minimum horizontal boundary point of the first text box in the row with the most text boxes as the starting point and the maximum horizontal boundary point of the last text box as the end point, and column cutting mapping is performed within the range according to the column cutting ratio. The minimum boundary point can be understood as the point corresponding to the minimum x value in the coordinate value of the first text box, and the maximum boundary point can be understood as the point corresponding to the maximum x value in the coordinate value of the last text box.
[0130] Optionally, in other embodiments, the OCR-based table layout recovery program 10 can also be divided into one or more modules, one or more modules are stored in the memory 11 and executed by one or more processors 12 to complete the present invention. The module referred to in the present invention refers to a series of computer program instruction segments that can perform specific functions.
[0131] For example, refer to Figure 3 As shown, Figure 2 A schematic diagram of program modules of an OCR-based table layout recovery program 10.
[0132] In an embodiment of the OCR-based table layout recovery program 10, the OCR-based table layout recovery program 10 includes: modules 110-150, wherein:
[0133] The receiving module 110 is used to receive a table layout restoration instruction issued by a user, wherein the table layout restoration instruction includes a table image to be restored and an image type of the table image to be restored;
[0134] A recognition module 120 is used to perform OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes a plurality of text boxes and text information corresponding to each of the plurality of text boxes;
[0135] A row cutting module 130, configured to perform row cutting on the table image to be restored based on the recognition result and a preset row cutting rule to obtain a first cutting result;
[0136] A column cutting module 140 is used to determine the header information corresponding to the first cutting result based on the image type, and perform column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result; and
[0137] The recovery module 150 is used to generate a target table structure based on the second cutting result, write the text information corresponding to the multiple text boxes into the cells of the target table structure, generate a target table corresponding to the table image to be restored, and send the target table to the user.
[0138] The functions or operation steps implemented by the modules 110-150 are similar to those described above and will not be described in detail here.
[0139] In addition, an embodiment of the present invention further proposes a computer-readable storage medium, wherein the computer-readable storage medium includes an OCR-based table layout recovery program 10, and when the OCR-based table layout recovery program 10 is executed by a processor, any step of the OCR-based table layout recovery method is implemented. The specific implementation of the computer-readable storage medium of the present invention is substantially the same as the above-mentioned method embodiment, and will not be repeated here.
[0140] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0141] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, device, article or method. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the existence of other identical elements in the process, device, article or method including the element.
[0142] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes a number of instructions for a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0143] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A table format recovery method based on OCR, applicable to electronic devices, characterized in that: The method includes: Receiving step: receiving a table layout recovery instruction issued by a user, wherein the table layout recovery instruction includes a table image to be recovered and a picture type of the table image to be recovered; Recognition step: performing OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes a plurality of text boxes and text information corresponding to each of the plurality of text boxes; Row cutting step: performing row cutting on the table image to be restored based on the recognition result and a preset row cutting rule to obtain a first cutting result; Column cutting step: determining the header information corresponding to the first cutting result based on the image type, and performing column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result; and Restoration step: generating a target table structure based on the second cutting result, writing text information corresponding to the multiple text boxes into cells of the target table structure, generating a target table corresponding to the table image to be restored, and sending the target table to the user; Wherein, the row cutting of the table image to be restored based on the recognition result and the preset row cutting rule includes: rotating the multiple text boxes in the recognition result according to multiple preset angles, projecting the rotated multiple text boxes on the y-axis, and determining multiple projection intervals and multiple overlapping projection segments corresponding to the multiple preset angles on the y-axis; selecting the preset angle with the smallest projection interval among the multiple projection intervals, or selecting the preset angle with the most overlapping projection segments among the multiple overlapping projection segments as the target angle, and determining the overlapping projection segments corresponding to the target angle; determining the row cutting lines of the multiple text boxes at the target angle according to the overlapping projection segments corresponding to the target angle; and determining the text box corresponding to each row according to the row cutting lines of the multiple text boxes at the target angle; The performing column cutting on the first cutting result based on the header information and the preset column cutting rule to obtain the second cutting result includes: obtaining each header element in the header information of the first cutting result, and making a vertical cutting line at the rightmost of the text box corresponding to each header element; and performing column cutting on the first cutting result according to the vertical cutting line to obtain the second cutting result; The method of performing column cutting on the first cutting result according to the vertical cutting line to obtain the second cutting result also includes: using the vertical cutting line as the first column cutting line; calculating the center point of each text box in the first cutting result, recording the text box whose center point is within the first column cutting line, and calculating the average and standard deviation of the boundary points of the outer frame of each column of text boxes in the X-axis direction; when the standard deviation is less than or equal to a preset standard deviation threshold, adjusting the first column cutting line of the corresponding column of text boxes according to the average to generate a second column cutting line; and performing column cutting on the first cutting result according to the second column cutting line to obtain the second cutting result; The method of writing the text information corresponding to the multiple text boxes into the cells of the target table structure includes: determining multiple intersection text boxes corresponding to each cell in the target table structure, and respectively calculating the areas of the multiple intersection text boxes and the overlapping areas of the multiple intersection text boxes and the cells; and when the ratio of the overlapping area to the area of the intersection text box exceeds a preset ratio threshold, judging that the intersection text box is the target text box corresponding to the cell, and writing the text information of the target text box into the cell.
2. The OCR-based table layout recovery method according to claim 1, characterized in that: The determining of the row cutting lines of the plurality of text boxes at the target angle according to the overlapping projection segments corresponding to the target angle comprises: The coordinates of each overlapping projection segment in the overlapping projection segments corresponding to the target angle are obtained, and the minimum coordinate of each overlapping projection segment is used as a lower cutting line of a row, and the maximum coordinate of each overlapping projection segment is used as an upper cutting line of a row.
3. The OCR-based table layout recovery method according to claim 1, characterized in that: The determining, based on the image type, header information corresponding to the first cutting result includes: Acquire mapping data of a predetermined image type and a header keyword set from a preset storage path, and determine a header keyword set corresponding to the first segmentation result according to the image type and the mapping data; Acquire text information of a text box in each row of the first segmentation result, and match the text information of the text box in each row with the keyword set; and When there is text information matching the keyword set, a row of text boxes corresponding to the text information matching the keyword set is used as header information of the first cutting result.
4. An electronic device for implementing the OCR-based table layout recovery method as claimed in any one of claims 1 to 3, characterized in that: The device includes a memory and a processor, wherein the memory stores an OCR-based table format recovery program that can be run on the processor, and when the OCR-based table format recovery program is executed by the processor, the following steps can be implemented: Receiving step: receiving a table layout recovery instruction issued by a user, wherein the table layout recovery instruction includes a table image to be recovered and a picture type of the table image to be recovered; Recognition step: performing OCR recognition on the table image to be restored to obtain a recognition result, wherein the recognition result includes a plurality of text boxes and text information corresponding to each of the plurality of text boxes; Row cutting step: performing row cutting on the table image to be restored based on the recognition result and a preset row cutting rule to obtain a first cutting result; Column cutting step: determining the header information corresponding to the first cutting result based on the image type, and performing column cutting on the first cutting result based on the header information and a preset column cutting rule to obtain a second cutting result; and Restoration step: generate a target table structure based on the second cutting result, write the text information corresponding to the multiple text boxes into the cells of the target table structure, generate a target table corresponding to the table image to be restored, and send the target table to the user.
5. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes an OCR-based table layout recovery program. When the OCR-based table layout recovery program is executed by a processor, the OCR-based table layout recovery method according to any one of claims 1 to 3 is implemented.
