Optimized ocr recognition method and system for pdf format contract
Patent Information
- Application Number
- CN202610391918.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-03-27
- Publication Date
- 2026-08-07
AI Technical Summary
[0007]为了如何提高对PDF格式合同文档中复杂表格结构的OCR识别与转换精度,克服现有单一OCR引擎在表格检测、文字-表格匹配及复杂表格结构还原方面的缺陷,从而实现合同文档特定场景下从PDF到Word的高保真、结构化转换的问题,本发明提供一种优化PDF格式合同的OCR识别办法,包括以下步骤:
本发明转换后的Word文档能够高度还原原始PDF中的复杂表格结构,包括合并单元格等,有效避免了现有技术中常见的表格错乱、内容错位和结构丢失等问题,显著地提高了合同文档中表格内容的识别与转换精度。
Smart Images

Figure CN122531044A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of optical character recognition technology, specifically to an optimized OCR recognition method and system for PDF format contracts. Background Technology
[0002] Currently, in the field of contract digitization, Optical Character Recognition (OCR) technology is commonly used to convert scanned PDF contracts into editable Word formats. Existing technical solutions mainly rely on a single end-to-end OCR engine (such as PaddleOCR) or commercial PDF conversion tools (such as Adobe Acrobat) for processing. These general solutions are relatively mature when processing plain text content, but they have significant limitations when dealing with the complex table structures commonly found in contracts.
[0003] Specifically, the existing technology mainly has the following problems: 1. Insufficient accuracy in table structure detection: When faced with tables of diverse formats and complex layouts in contract documents, the table detection module built into general OCR engines often suffers from low positioning accuracy of its detection boxes. Detection boxes are prone to offset, breakage, or false detections, making it impossible to accurately define the actual boundaries of the table.
[0004] 2. Difficulty in matching table content with text recognition results: Due to inaccurate table detection boxes, it is difficult to accurately associate and match the text cells recognized by OCR with the corresponding table structure. This often leads to disordered table content, misaligned text, or even the loss or fragmentation of the entire table structure in the converted Word document, severely damaging the original layout and semantic integrity of the contract.
[0005] 3. Weak ability to handle complex structures such as merged cells: The merged cell structure frequently used in contract tables poses a significant challenge to the logical structure analysis capabilities of existing solutions. Mainstream solutions struggle to accurately reproduce the row and column relationships of merged cells, causing the converted table to lose its original form and greatly reducing the readability and usability of the conversion results.
[0006] The root of these problems lies in the fact that a single OCR model designed for general scenarios struggles to simultaneously achieve high-precision text recognition and robust table analysis. Therefore, a new technical solution is urgently needed that can accurately separate and collaboratively process text and table elements in contract documents to ensure the fidelity of the converted contract document's structure and content. Summary of the Invention
[0007] To improve the accuracy of OCR recognition and conversion of complex table structures in PDF contract documents, and to overcome the shortcomings of existing single OCR engines in table detection, text-table matching, and complex table structure restoration, thereby achieving high-fidelity, structured conversion from PDF to Word in specific contract document scenarios, this invention provides an optimized OCR recognition method for PDF contracts, comprising the following steps: S1. The user uploads a PDF contract file and retrieves the total number of pages in the file; S2. Recognize each page of the file to generate an RGB image pixel map, and create a corresponding copy image; S3. Input the RGB image pixel map and its corresponding copy image into two independent process pools for parallel processing to obtain table structure information and text recognition information; S4. Perform data matching on the data obtained in step S3, and calculate the overlap using the improved IoU; S5. Sort the matched data from step S4 to restore the document flow order; S6. Generate and output the corresponding Word document based on the sorted content sequence.
[0008] Furthermore, step S2 specifically includes the following sub-steps: S21. Generate a corresponding high-resolution RGB image pixel map for each page of the file; S22. Convert the generated RGB image pixel map into a NumPy array to form an image matrix; wherein the format of the image matrix is: height × width × RGB channels; S23. Create a corresponding copy image for the RGB image pixel map, and preprocess the created copy image.
[0009] Furthermore, step S23 specifically includes: converting the created copy image into a grayscale image, then performing binarization processing based on a set threshold, and performing morphological closing operation on the binarized image.
[0010] Furthermore, step S3 specifically includes the following sub-steps: Text recognition: Perform OCR recognition on the copy image, and output the polygon coordinates of each detected text block and its corresponding recognized text; Table processing: Perform table detection on the pixel map of RGB image, perform structural analysis on the image of each detected table area, and output the absolute coordinates of all cells in the table and the logical point array describing the cell merging relationship; Each logical point represents the start row, end row, start column, and end column of a logical cell.
[0011] Further, step S4 specifically includes: S41. Traversing the table structure information and its text recognition information according to their corresponding coordinates; S42. Calculating the overlap of the traversed table structure information and its text recognition information using an improved IoU calculation.
[0012] Furthermore, the improved IoU calculation specifically includes the following sub-steps: S421. Let the input parameter box_a=[x1_a, y1_a, x2_a, y2_a] be the horizontal rectangle of the table cell, and box_b be the list of vertex coordinates of the text block polygon; where x1_a: the x-coordinate of the top left corner of the cell rectangle; y1_a: the y-coordinate of the top left corner of the cell rectangle; x2_a: the x-coordinate of the bottom right corner of the cell rectangle; y2_a: the y-coordinate of the bottom right corner of the cell rectangle; S422. Calculate the geometric properties of the horizontal rectangle of the table cell and construct the horizontal rectangle object: Center point coordinates: center_x = (x1_a + x2_a) / 2, center_y = (y1_a + y2_a) / 2; Width: width = x2_a - x1_a; Height: height = y2_a - y1_a; Angle: angle = 0.0; S423. Convert the text block polygon to a minimum bounding rotated rectangle; S424. Calculate the intersection of the smallest bounding rotated rectangle and the horizontal rectangle of the table cell: r = cv2.rotatedRectangleIntersection(rect_a, rect_b); when r[0] == 0, return IoU=0.0; otherwise, calculate the intersection area: inter_area = cv2.contourArea(r[1]); S425. Calculate the area of the smallest circumscribed rotated rectangle and the area of the horizontal rectangle of the table cell, respectively, and the area of their union: The area of the horizontal rectangle in the table cell is: area_a = (x2_a - x1_a) * (y2_a - y1_a); Minimum area of the rotated rectangle: area_b = int(rect_b[1][0]) * int(rect_b[1][1]); Union area: outer_area = area_a + area_b - inter_area; S426. Calculate the traditional intersection-union ratio, the ratio of intersection to cell area, and the ratio of intersection to text block area, respectively: Traditional intersection-union ratio: iou_1 = inter_area / outer_area; The ratio of the intersection to the area of a cell: iou_2 = inter_area / area_a; The ratio of the intersection to the area of the text block: iou_3 = inter_area / area_b; S427. Calculate the overlap IoU and determine whether the text block belongs to the cell based on the set threshold and overlap.
[0013] Further, step S427 specifically includes: IoU overlap calculation: If max(iou_1, iou_2, iou_3) > 0.5, return max(iou_1, iou_2, iou_3); otherwise, return iou_1. When the overlap IoU is greater than or equal to the set threshold, the text block is determined to belong to the cell, and the table structure is restored based on the logical point array.
[0014] An optimized OCR recognition system for PDF format contracts includes a document receiving and preprocessing module, a text recognition module, a table processing module, a data fusion and reconstruction module, and a document generation module. The document receiving and preprocessing module is used to receive PDF files uploaded by users, and to preprocess the received PDF files to generate corresponding RGB image pixel maps and copy images. The text recognition module is used to perform OCR recognition on the copy image and output the polygon coordinates of each detected text block and its corresponding recognized text. The table processing module is used to perform table detection on the RGB image pixel map and perform structural analysis on each detected table area of the image; The data fusion and reconstruction module is used to calculate the overlap between the data generated by the text recognition module and the table processing module, and to perform logical sorting on the data; The document generation module is used to generate corresponding Word files from the sorted data content and output them.
[0015] This invention provides an optimized OCR recognition method and system for PDF format contracts, which has the following beneficial effects: The Word document converted by this invention can highly restore the complex table structure in the original PDF, including merged cells, effectively avoiding common problems in existing technologies such as table disorder, content misalignment, and structural loss, and significantly improving the recognition and conversion accuracy of table content in contract documents.
[0016] This invention can effectively process complex contract documents containing slanted text, dense tables, and mixed layouts of multiple types. It overcomes the performance degradation of a single model when faced with unconventional layouts and enhances the adaptability and robustness of processing complex contract documents.
[0017] This invention enables end-to-end automatic conversion from PDF scans to structured Word documents, reducing reliance on manual proofreading and post-editing, significantly shortening processing time for business processes such as contract review and archiving, and improving the automation and efficiency of contract digitization. Attached Figure Description
[0018] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the structures shown in these drawings without creative effort.
[0019] Figure 1 The method flowchart provided by the present invention. Detailed Implementation
[0020] It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of the invention.
[0021] The following detailed description of the implementation method of the present invention is in conjunction with the accompanying drawings. The description is only a partial embodiment and not all embodiments. For clarity, representations and descriptions unrelated to the present invention are omitted in the drawings and description.
[0022] To provide a clearer understanding of the technical features, objectives, and beneficial effects of this invention, the following detailed description of the technical solution is provided. Obviously, the described embodiments are only a portion of the embodiments of this invention, not all of them, and should not be construed as limiting the scope of implementation of this invention. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without inventive effort are within the protection scope of this invention.
[0023] To improve the accuracy of OCR recognition and conversion of complex table structures in PDF contract documents, and to overcome the shortcomings of existing single OCR engines in table detection, text-table matching, and complex table structure restoration, thereby achieving high-fidelity, structured conversion from PDF to Word in specific contract document scenarios, this invention provides an optimized OCR recognition method for PDF contracts, comprising the following steps: S1. The user uploads a PDF contract file, and the total number of pages in the file is obtained. A third-party library is used to convert each page of the input PDF contract document into a high-resolution RGB image array. A multi-threaded parallel approach is employed to accelerate the conversion process, and adaptive binarization and morphological closing operations are performed on the images for preprocessing to enhance the contrast of text areas and repair broken strokes, providing high-quality input for subsequent OCR recognition.
[0024] S2. Recognize each page of the file to generate an RGB image pixel map, and create a corresponding copy image.
[0025] S21. Generate a corresponding high-resolution RGB image pixel map for each page of the file; S22. Convert the generated RGB image pixel map into a NumPy array to form an image matrix; wherein the format of the image matrix is: height × width × RGB channels; S23. Create a corresponding copy image for the RGB image pixel map, and preprocess the created copy image.
[0026] Furthermore, step S23 specifically includes: converting the created copy image into a grayscale image, then performing binarization processing based on a set threshold, and performing morphological closing operation on the binarized image.
[0027] S3. Input the RGB image pixel map and its corresponding copy image into two independent process pools for parallel processing to obtain table structure information and text recognition information.
[0028] Text Recognition: Performs OCR recognition on the copied image, outputting the polygon coordinates of each detected text block and its corresponding recognized text. The preprocessed image is then fed into a separate text recognition processing process. This process initializes a PaddlePaddle-based OCR pipeline, performs specialized text detection and recognition, and outputs a list of all detected text lines in the image, their corresponding polygon coordinate vertices, and the recognized text content. This branch focuses on achieving the highest text recognition accuracy.
[0029] Table Processing: Table detection is performed on the RGB image pixel map. For each detected table region, structural analysis is conducted, outputting the absolute coordinates of all cells within the table and an array of logical points describing the cell merging relationships. Each logical point represents the start row, end row, start column, and end column of a logical cell. The original image is then fed into a parallel table processing process. This process employs a specialized table recognition model, whose advantage lies in accurately locating the bounding rectangles of all table regions in the image and parsing the internal logical structure of the table, outputting the bounding box coordinates of each cell and an array of logical points describing the cell merging relationships. Its detection accuracy surpasses the built-in functions of general-purpose OCR engines.
[0030] S4. Perform data matching on the data obtained in step S3 and calculate the overlap using an improved IoU: S41. Traverse the table structure information and its text recognition information according to their corresponding coordinates. Establish an independent matching process that receives two heterogeneous data sources: a text list from the text recognition module and table structure information from the table processing module.
[0031] S42. The overlap of the traversed table structure information and its text recognition information is calculated using an improved IoU calculation. The coordinates of each cell in the table structure are traversed and matched with the polygon coordinates of all OCR text blocks. Using an improved Intersection over Union (IoU) ratio, the spatial overlap between the text region and the cell is calculated to accurately determine the cell to which the text block belongs. The improved IoU calculation specifically includes the following sub-steps: S421. Let the input parameter box_a=[x1_a, y1_a, x2_a, y2_a] be the horizontal rectangle of the table cell, and box_b be the list of vertex coordinates of the text block polygon; where x1_a: the x-coordinate of the top left corner of the cell rectangle; y1_a: the y-coordinate of the top left corner of the cell rectangle; x2_a: the x-coordinate of the bottom right corner of the cell rectangle; y2_a: the y-coordinate of the bottom right corner of the cell rectangle.
[0032] S422. Calculate the geometric properties of the horizontal rectangle of the table cell and construct the horizontal rectangle object: Center point coordinates: center_x = (x1_a + x2_a) / 2, center_y = (y1_a + y2_a) / 2; Width: width = x2_a - x1_a; Height: height = y2_a - y1_a; Angle: angle = 0.0.
[0033] S423. Convert the text block polygon to a minimum bounding revolved rectangle.
[0034] S424. Calculate the intersection of the smallest bounding rotated rectangle and the horizontal rectangle of the table cell: r = cv2.rotatedRectangleIntersection(rect_a, rect_b); when r[0] == 0, return IoU=0.0; otherwise, calculate the intersection area: inter_area = cv2.contourArea(r[1]).
[0035] S425. Calculate the area of the smallest circumscribed rotated rectangle and the area of the horizontal rectangle of the table cell, respectively, and the area of their union: The area of the horizontal rectangle in the table cell is: area_a = (x2_a - x1_a) * (y2_a - y1_a); Minimum area of the rotated rectangle: area_b = int(rect_b[1][0]) * int(rect_b[1][1]); Union area: outer_area = area_a + area_b - inter_area.
[0036] S426. Calculate the traditional intersection-union ratio, the ratio of intersection to cell area, and the ratio of intersection to text block area, respectively: Traditional intersection-union ratio: iou_1 = inter_area / outer_area; The ratio of the intersection to the area of a cell: iou_2 = inter_area / area_a; The ratio of the intersection to the area of the text block: iou_3 = inter_area / area_b.
[0037] S427. Calculate the overlap (IoU) and determine whether the text block belongs to the cell based on the set threshold and overlap: IoU overlap calculation: If max(iou_1, iou_2, iou_3) > 0.5, return max(iou_1, iou_2, iou_3); otherwise, return iou_1. When the overlap IoU is greater than or equal to the set threshold, the text block is determined to belong to the cell, and the table structure is restored based on the logical point array.
[0038] S5. Sort the matched data from step S4 to restore the document flow order. Based on the matching results, fill the text content into the corresponding logical cell positions, and use logical point arrays to restore complex structures such as merged cells to form a complete table data object. Mark all text blocks that do not match the table as ordinary paragraphs, and sort them together with the reconstructed table object according to their spatial coordinates in the entire page to restore the correct document flow order.
[0039] S6. Generate and output the corresponding Word document based on the sorted content sequence. The sorted content sequence is output to the Word document in sequence. Plain text is written into paragraphs, and tables are drawn based on the reconstructed data objects, ultimately generating an editable Word document that retains the original PDF layout and semantic structure.
[0040] An optimized OCR recognition system for PDF format contracts includes a document receiving and preprocessing module, a text recognition module, a table processing module, a data fusion and reconstruction module, and a document generation module. The document receiving and preprocessing module is used to receive PDF files uploaded by users, and to preprocess the received PDF files to generate corresponding RGB image pixel maps and copy images. The text recognition module is used to perform OCR recognition on the copy image and output the polygon coordinates of each detected text block and its corresponding recognized text. The table processing module is used to perform table detection on the RGB image pixel map and perform structural analysis on each detected table area of the image; The data fusion and reconstruction module is used to calculate the overlap between the data generated by the text recognition module and the table processing module, and to perform logical sorting on the data; The document generation module is used to generate corresponding Word files from the sorted data content and output them.
[0041] Example: An optimized OCR recognition method for PDF format contracts according to the present invention mainly includes the following steps: System Initialization and Environment Configuration: Deploy the software environment required for this invention, including a Python integrated environment and Python third-party libraries. Configure computing equipment; a server with GPU acceleration is recommended to improve the inference speed of deep learning models. Initialize two independent process pools, one for text recognition and the other for table processing; the number of processes can be set according to the number of CPU cores.
[0042] Document preprocessing and parallel image conversion: Receives user-uploaded PDF contract files. Opens the PDF file using a third-party library and obtains the total number of pages.
[0043] For each page, perform the following operations: Use third-party libraries to generate high-resolution (e.g., scaling the DPI to 3x) RGB image pixel maps.
[0044] The pixel image is directly converted into a NumPy array to form an image matrix in the format H (height) x W (width) x 3 (RGB channels).
[0045] Create a copy of the image for subsequent table processing.
[0046] Preprocess the copy image: first convert it to grayscale, then binarize it using a threshold (e.g., 200), and finally perform a morphological closing operation (e.g., twice) using a suitable kernel (e.g., 3x3 pixels) to enhance the text and repair breaks.
[0047] This step uses multi-threaded parallel processing (e.g., setting the maximum number of threads to 5) to process multiple pages simultaneously in order to improve efficiency.
[0048] Heterogeneous model parallel processing: The preprocessed image (for text recognition) and the original image (for table processing) obtained in the above steps are sent to two independent process pools for parallel processing.
[0049] Text recognition branch: In the text processing process, the PaddlePaddle OCR pipeline is initialized, and key parameters are configured. The interface is called to perform OCR recognition on the input image. The model outputs a list of results, which includes the polygon coordinates (rec_polys, in the format [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]) and the corresponding recognized text (rec_texts) for each detected text block.
[0050] Table processing branch: In the table processing, the TableStructureRec table recognition model is first initialized. The model's inference interface is then called to detect the presence of tables in the original image. For each image where a table region is detected, the recognition engine performs structural analysis and obtains the output, which includes the absolute coordinates of all cells in the table (cell_bboxes, in the format [x1,y1,x2,y2]) and an array of logical points describing the cell merging relationships (logic_points, in the format [row_begin, row_end, col_begin, col_end]). Each logical point represents the start row, end row, start column, and end column of a logical cell.
[0051] Data matching and reconstruction: This step is completed by executing a custom get_table function. Input: A list of table structure information (table_structure, containing polygons and logic_points) and a list of text recognition results (ocr_text, containing text coordinates and content).
[0052] Cell and text matching: Iterate through each table in `table_structure` and the coordinates of each cell (`polygons[i]`, which is a horizontal rectangle [x1, y1, x2, y2]). For each cell, iterate through the coordinates of each text block in `ocr_text` (`ocr_text[j][0]`, which is a list of polygon vertices). Calculate the overlap between the two by calling the improved IoU calculation function.
[0053] Detailed explanation of the improved IoU calculation: Let the input parameter box_a=[x1_a, y1_a, x2_a, y2_a] be the horizontal rectangle of the table cell, and box_b be the list of vertex coordinates of the text block polygon. Where x1_a: x-coordinate of the top-left corner of the cell rectangle; y1_a: y-coordinate of the top-left corner of the cell rectangle; x2_a: x-coordinate of the bottom-right corner of the cell rectangle; y2_a: y-coordinate of the bottom-right corner of the cell rectangle.
[0054] Calculate the geometric properties of the horizontal rectangle in the cell: Center point coordinates: center_x = (x1_a + x2_a) / 2, center_y = (y1_a + y2_a) / 2; Width: width = x2_a - x1_a; Height: height = y2_a - y1_a; Angle: angle = 0.0 (horizontal rectangle); To construct a horizontal rectangle object: rect_a = ((center_x, center_y), (width, height), angle).
[0055] Convert the text block polygon to the minimum bounding rectangle: rect_b = cv2.minAreaRect(np.array(box_b, dtype=np.float32)), which can obtain the center point, width (rect_b[1][0]), height (rect_b[1][1]), and rotation angle of the minimum bounding rectangle.
[0056] Calculate the intersection of two rectangles: r = cv2.rotatedRectangleIntersection(rect_a, rect_b); if there is no intersection (r[0] == 0), return IoU=0.0; otherwise calculate the intersection area: inter_area = cv2.contourArea(r[1]).
[0057] Calculate the area measurement: Area of a horizontal rectangle in a cell: area_a = (x2_a - x1_a) * (y2_a - y1_a); Minimum area of the rotating rectangle of the text block: area_b = int(rect_b[1][0]) * int(rect_b[1][1]); Union area: outer_area = area_a + area_b - inter_area.
[0058] Calculate the three crossover / union ratios: iou_1 = inter_area / outer_area (traditional intersection-union ratio); iou_2 = inter_area / area_a (the ratio of the intersection to the area of the cell); iou_3 = inter_area / area_b (the ratio of the intersection to the area of the text block).
[0059] Calculate the intersection-union ratio (IoU): If max(iou_1, iou_2, iou_3) > 0.5, return max(iou_1, iou_2, iou_3); otherwise, return iou_1.
[0060] Set a matching threshold (e.g., 0.5). When the IoU value is greater than or equal to the threshold, the text block is considered to belong to this cell.
[0061] Table content filling: Reconstruct the table structure based on the logic point array (logic_points): Calculate the maximum number of rows in the table: max_row = np.max(logic_points[:,1]) + 1; Calculate the maximum number of columns in the table: max_col = np.max(logic_points[:,3]) + 1; Initialize the table matrix: table = np.full((max_row, max_col), "", dtype=object).
[0062] Iterate through each logical point in the logical point array [row_begin, row_end, col_begin, col_end]: The range of rows from `row_begin` to `row_end` and columns from `col_begin` to `col_end` in the table matrix is labeled with the same "logical cell identifier" (the same logical cell identifier represents an actual physical (merged) cell). Using the aforementioned IoU calculation, the text content recognized by the OCR is associated with the logical cells. Based on all physical locations matched by the "logical cell identifier," the text content is then completely filled in at once.
[0063] By restoring merged cells using logic_points, the text content is correctly merged when multiple physical cells point to the same logical cell. The system simply "projects" the text onto all physical locations covered by that logical cell, thus perfectly and efficiently restoring various complex table structures.
[0064] Non-table text separation: Mark all text blocks that do not match any table cell as ordinary paragraph text.
[0065] Global content sorting: The filled table object (converted to a DataFrame) and all plain text blocks are placed into the same list. The entire list is then sorted according to the top-left corner position of each element to restore the original reading order of the document.
[0066] Word document generation and output: Create a new Word document object (docx.Document()). Iterate through the sorted list of content obtained in step S4. For each element in the list: If it is a string, add it to the document as paragraph text. If it is a DataFrame table object, create a Word table based on the dimensions of the DataFrame, and fill the table cells row by row and column by column with the contents of the DataFrame.
[0067] The final generated Word document is saved to a memory byte stream and then returned to the user for download.
[0068] This invention effectively solves the limitations of a single model in processing complex tables through the innovative architecture of heterogeneous model collaboration and data fusion, and significantly improves the recognition accuracy and conversion fidelity of table content in PDF contracts.
[0069] The Word document converted by this invention can highly restore the complex table structure in the original PDF, including merged cells, effectively avoiding common problems in existing technologies such as table disorder, content misalignment, and structural loss, and significantly improving the recognition and conversion accuracy of table content in contract documents.
[0070] This invention can effectively process complex contract documents containing slanted text, dense tables, and mixed layouts of multiple types. It overcomes the performance degradation of a single model when faced with unconventional layouts and enhances the adaptability and robustness of processing complex contract documents.
[0071] This invention enables end-to-end automatic conversion from PDF scans to structured Word documents, reducing reliance on manual proofreading and post-editing, significantly shortening processing time for business processes such as contract review and archiving, and improving the automation and efficiency of contract digitization.
[0072] The above description is merely a preferred embodiment of the present invention. It should be understood that the present invention is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. It can be used in various other combinations, modifications, and environments, and can be altered within the scope of the concept described herein through the above teachings or related technologies or knowledge. Modifications and variations made by those skilled in the art that do not depart from the spirit and scope of the present invention should be within the protection scope of the appended claims.
Claims
1. A method for optimizing OCR recognition of PDF format contracts, characterized in that, Includes the following steps: S1. The user uploads a PDF contract file and retrieves the total number of pages in the file; S2. Recognize each page of the file to generate an RGB image pixel map, and create a corresponding copy image; S3. Input the RGB image pixel map and its corresponding copy image into two independent process pools for parallel processing to obtain table structure information and text recognition information; S4. Perform data matching on the data obtained in step S3, and calculate the overlap using the improved IoU; S5. Sort the matched data from step S4 to restore the document flow order; S6. Generate and output the corresponding Word document based on the sorted content sequence.
2. The OCR recognition method for optimized PDF format contracts according to claim 1, characterized in that, Step S2 specifically includes the following sub-steps: S21. Generate a corresponding high-resolution RGB image pixel map for each page of the file; S22. Convert the generated RGB image pixel map into a NumPy array to form an image matrix; wherein the format of the image matrix is: height × width × RGB channels; S23. Create a corresponding copy image for the RGB image pixel map, and preprocess the created copy image.
3. The method for optimizing OCR recognition of PDF format contracts according to claim 2, characterized in that, The S23 step specifically includes: converting the created copy image into a grayscale image, then performing binarization processing based on a set threshold, and performing morphological closing operation on the binarized image.
4. The method for optimizing OCR recognition of PDF format contracts according to claim 1, characterized in that, Step S3 specifically includes the following sub-steps: Text recognition: Perform OCR recognition on the copy image, and output the polygon coordinates of each detected text block and its corresponding recognized text; Table processing: Perform table detection on the pixel map of RGB image, perform structural analysis on the image of each detected table area, and output the absolute coordinates of all cells in the table and the logical point array describing the cell merging relationship; Each logical point represents the start row, end row, start column, and end column of a logical cell.
5. The method for optimizing OCR recognition of PDF format contracts according to claim 4, characterized in that, The S4 step specifically includes: S41. Traversing the table structure information and its text recognition information according to their corresponding coordinates; S42. Calculating the overlap of the traversed table structure information and its text recognition information using an improved IoU calculation.
6. The method for optimizing OCR recognition of PDF format contracts according to claim 5, characterized in that, The improved IoU calculation specifically includes the following sub-steps: S421. Let the input parameter box_a=[x1_a, y1_a, x2_a, y2_a] be the horizontal rectangle of the table cell, and box_b be the list of vertex coordinates of the text block polygon; where x1_a: the x-coordinate of the top left corner of the cell rectangle; y1_a: the y-coordinate of the top left corner of the cell rectangle; x2_a: the x-coordinate of the bottom right corner of the cell rectangle; y2_a: the y-coordinate of the bottom right corner of the cell rectangle; S422. Calculate the geometric properties of the horizontal rectangle of the table cell and construct the horizontal rectangle object: Center point coordinates: center_x = (x1_a + x2_a) / 2, center_y = (y1_a + y2_a) / 2; Width: width = x2_a - x1_a; Height: height = y2_a - y1_a; Angle: angle = 0.0; S423. Convert the text block polygon to a minimum bounding rotated rectangle; S424. Calculate the intersection of the smallest bounding rotated rectangle and the horizontal rectangle of the table cell: r = cv2.rotatedRectangleIntersection(rect_a, rect_b); when r[0] == 0, return IoU=0.0; otherwise, calculate the intersection area: inter_area = cv2.contourArea(r[1]); S425. Calculate the area of the smallest circumscribed rotated rectangle and the area of the horizontal rectangle of the table cell, respectively, and the area of their union: The area of the horizontal rectangle in the table cell is: area_a = (x2_a - x1_a) * (y2_a - y1_a); Minimum area of the rotated rectangle: area_b = int(rect_b[1][0]) * int(rect_b[1][1]); Union area: outer_area = area_a + area_b - inter_area; S426. Calculate the traditional intersection-union ratio, the ratio of intersection to cell area, and the ratio of intersection to text block area, respectively: Traditional intersection-union ratio: iou_1 = inter_area / outer_area; The ratio of the intersection to the area of a cell: iou_2 = inter_area / area_a; The ratio of the intersection to the area of the text block: iou_3 = inter_area / area_b; S427. Calculate the overlap IoU and determine whether the text block belongs to the cell based on the set threshold and overlap.
7. The optimized OCR recognition method for PDF format contracts according to claim 6, characterized in that, The specific steps of S427 are as follows: IoU overlap calculation: If max(iou_1, iou_2, iou_3) > 0.5, return max(iou_1, iou_2, iou_3); otherwise, return iou_1. When the overlap IoU is greater than or equal to the set threshold, the text block is determined to belong to the cell, and the table structure is restored based on the logical point array.
8. An optimized OCR recognition system for PDF format contracts, used to implement the optimized OCR recognition method for PDF format contracts as described in any one of claims 1 to 7, characterized in that, It includes a document receiving and preprocessing module, a text recognition module, a table processing module, a data fusion and reconstruction module, and a document generation module; The document receiving and preprocessing module is used to receive PDF files uploaded by users, and to preprocess the received PDF files to generate corresponding RGB image pixel maps and copy images. The text recognition module is used to perform OCR recognition on the copy image and output the polygon coordinates of each detected text block and its corresponding recognized text. The table processing module is used to perform table detection on the RGB image pixel map and perform structural analysis on each detected table area of the image; The data fusion and reconstruction module is used to calculate the overlap between the data generated by the text recognition module and the table processing module, and to perform logical sorting on the data; The document generation module is used to generate corresponding Word files from the sorted data content and output them.