A document field extraction method based on double anchor point affine mapping and relative position constraint
Patent Information
- Application Number
- CN202611160757.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-03
- Publication Date
- 2026-10-27
AI Technical Summary
[0009]本发明的目的在于提供一种基于双锚点仿射映射与相对位置约束的单据字段提取方案,以解决版式相近单据在缩放、位移、旋转和局部布局变化下字段提取准确率下降的问题
[0028]第一,双锚点独立轴缩放仿射映射,对非等比缩放高度鲁棒。利用主锚点和辅锚点在模板与实际图像中的跨度比值,分别独立计算 X 方向和 Y 方向的缩放比,而非假设等比例缩放或仅依赖单锚点位移补偿。当单据因打印缩放、扫描比例调整导致水平与垂直缩放不一致时,各字段映射区域在两个方向上均能独立校正,大幅提升字段区域定位精度。
Smart Images

Figure CN122990704A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of document image processing, field location extraction, OCR post-processing, and template adaptation, specifically to a method, system, device, and storage medium for extracting document fields based on dual-anchor affine mapping and relative position constraints. Background Technology
[0002] In business documents such as purchase contracts, order confirmations, delivery notices, and project production schedules, which have relatively fixed formats but are subject to scaling, rotation, displacement, or local deformation, relying solely on fixed absolute coordinates for cropping and extracting fields often fails to adapt to different resolutions, scanning angles, and page offsets. Relying solely on full-text semantic retrieval is also easily affected by fields with the same name, multi-line layouts, and neighboring interfering text.
[0003] In existing technologies, template matching and OCR result post-processing are usually performed separately, lacking a unified solution for constructing a layout map using anchor regions and combining it with relative semantic tags to perform local relocation of target fields. Specifically, existing solutions have the following shortcomings:
[0004] (1) Single anchor point positioning cannot handle scaling deformation. Traditional solutions usually rely on a fixed title (such as "Purchase Contract") to determine the offset of the entire page, but ignore the non-uniform scaling in the X / Y direction caused by different scanning resolutions and different printing scales. When the scaling ratio of the reference template and the actual document is inconsistent in the horizontal and vertical directions, the displacement compensation of a single anchor point cannot restore all field areas, resulting in deformation or offset of the target field mapping area.
[0005] (2) Fixed coordinate cropping lacks layout adaptability. The field cropping method based on absolute pixel coordinates assumes that all input documents are completely consistent with the template. When the page is scaled, rotated or partially adjusted, the fixed coordinate cropping box is no longer aligned with the target content.
[0006] (3) Lack of field-level relative positioning constraints. Semantic retrieval is prone to mistaking fields with the same name, repeated words in the table header, or adjacent text of the same type as target content; while pure coordinate methods are sensitive to page deformation. Existing solutions lack a mechanism that can further utilize semantic tags (such as left-side labels and right-side separators) of the field context to perform local secondary positioning based on known anchor point mapping.
[0007] (4) Lack of fallback repair for OCR recognition defects. For serial number columns with extremely small font size (such as “1, 2, 3…”), high-density table areas, or parts with stains / creases, there are no targeted local re-recognition and business logic repair methods for misrecognition of a single OCR (such as misrecognizing “5” as “S”, or misrecognizing “1” as “7”).
[0008] (5) Lack of automatic segmentation and serial number verification for multiple record detail rows. Purchase contracts, production schedules and other documents often contain multiple detail rows with repetitive structures (such as multiple material rows), each containing a complete set of fields. Existing solutions either treat the entire table as a text block and lose the row-level structure, or require manual annotation of each row boundary, lacking an automated process based on dual anchor points for automatic pairing and segmentation, and then extracting and verifying the serial number row by row. Summary of the Invention
[0009] The purpose of this invention is to provide a document field extraction scheme based on dual-anchor point affine mapping and relative position constraints, so as to solve the problem of decreased field extraction accuracy of documents with similar layouts under scaling, displacement, rotation and local layout changes.
[0010] To achieve the above objectives, the present invention provides the following technical solution: a method for extracting document fields based on dual-anchor affine mapping and relative position constraints, comprising the following steps.
[0011] Step S1: Obtain the image of the document to be recognized and the corresponding OCR recognition result. The OCR recognition result includes at least: (a) the text content string of each detected text box; (b) the coordinates of the four corners of the rectangular text box, represented as [x1, y1, x2, y2]; (c) character-level coordinate information, i.e., the independent bounding box of each character within the text box, used for fine-grained character-level boundary positioning. For multi-page documents or multi-region spliced images, the preprocessing stage uniformly renders them as a single complete image and performs a global OCR once to avoid text breakage caused by region segmentation.
[0012] Step S2: Read the template configuration corresponding to the document type. The template configuration includes at least the main anchor point identifier (titlename, a unique or stable feature text in the document, such as "material number:", serving as the main positioning reference); the auxiliary anchor point identifier (aux_field_name, auxiliary positioning text that appears in pairs with the main anchor point, such as "product name:", forming a double anchor point pair with the main anchor point); the template reference area (recording the rectangular area positions startx / starty / endx / endy in the reference template image for each field, and recording its relative label configuration and extraction type for each field); the relative label configuration (each field can be configured with one or more of the left label left_text, right label right_text, above label above_text, and below label below_text, serving as the basis for candidate row positioning and candidate area shrinking); and the field post-processing strategy (including the numeric field label isdig, the denoising word list excepttext, the regular expression extraction mode value_extract_regex, the post-processing strategy enumeration, and whether to enable the local backup OCR small model as a fallback).
[0013] Step S3: Retrieve the main anchor text and auxiliary anchor text in the OCR recognition results and establish a dual-anchor affine mapping relationship. S3-1 Main and Auxiliary Anchor Text Retrieval: Traverse all OCR-recognized text boxes, matching each text content with the preset main and auxiliary anchor identifiers in the template configuration (supporting unified normalization of full-width / half-width colons). Successfully matched text boxes are respectively assigned to the main anchor set M and the auxiliary anchor set A. Each element contains the text box text, the four corner coordinates, and the center coordinate. S3-2 Intelligent Main and Auxiliary Anchor Pairing: Sort the main and auxiliary anchor sets in ascending order of vertical coordinate and horizontal coordinate, respectively. For each main anchor, find the auxiliary anchor with the closest spatial position among the unpaired auxiliary anchors. The pairing condition is that the auxiliary anchor's vertical coordinate is not lower than the main anchor's vertical coordinate minus the tolerance. Select the auxiliary anchor with the smallest vertical coordinate difference to establish a pair. After pairing, remove the auxiliary anchor from the candidate pool. Each pair corresponds to one record in the document. S3-3 Affine Mapping Parameter Calculation: Read the rectangular areas of the primary and secondary anchor points in the reference template from the template configuration and calculate the center coordinates to obtain reference anchor point pairs. For the first pair of actual anchor point pairs, calculate the horizontal scaling ratio Sx = (secondary anchor point x - primary anchor point x) / (reference secondary anchor point x - reference primary anchor point x) and the vertical scaling ratio Sy = (secondary anchor point y - primary anchor point y) / (reference secondary anchor point y - reference primary anchor point y). When the span of the reference anchor point pair is less than the threshold, the scaling ratio in the corresponding direction degenerates to 1.0. At the same time, the scaling ratio is clamped to a safe range to prevent extreme scaling. S3-4 Record-by-Record Vertical Range Entries Collection: For each successfully paired anchor point pair, starting from the upper edge of the primary anchor point text box and ending at the lower edge of the secondary anchor point text box, and combining the height of the anchor point text box to expand outward by a preset ratio to construct a vertical range, all OCR text boxes whose vertical coordinate center is within this range are included in the entry set of this record. Each entry is attached with character-level coordinate information.
[0014] Step S4: For each target field, generate candidate regions for the target field. S4-1 Row Grouping (Anchoring the first row y, no scrolling updates to avoid chain drift): Sort all entries in the entry set in ascending order of their y-coordinates, calculate the average height of the entries, and take the row height threshold as the average height multiplied by 0.65. When executing the row grouping algorithm, the y-coordinate baseline of each row is fixed as the center Y-coordinate of the first entry in that row. Subsequent entries only need to be determined whether their difference from the first entry's Y is within the row height threshold to be included in the current row. The anchor Y of the current row is not updated with the addition of new entries, thus avoiding the incorrect merging of adjacent rows due to small chain drift of the y-coordinate in high-density text areas. S4-2 Candidate Row Weighted Scoring and Positioning: When the field extraction type is relative, traverse all row grouping results and score each row for relative label hit. The left and right labels are each assigned 3 points for a hit, and the top and bottom labels are each assigned 1 point for a hit. The left and right labels have higher weights than the top and bottom labels. Select the candidate row with the highest score as the target row. S4-3 Precise Candidate Region Boundary Analysis (Character-Level Positioning): In the target line, character-level boundary positioning is performed on the left and right markers respectively. For the left marker, normalized character stream matching is performed using character-level coordinate information, and the left boundary is set to the right edge x-coordinate of the last character box of the left marker; for the right marker, the right boundary is set to the left edge x-coordinate of the first character box of the right marker; if the left marker matches the prefix in the same text box and is followed by the target field content, the suffix part is extracted directly using the marker as the separator; if the right marker is not configured, the right boundary is taken as the minimum left edge coordinate of all entry text boxes located to the right of the left boundary in the target line, thus obtaining a precise candidate rectangular region sandwiched between the left and right markers and constrained by the upper and lower marker rows.
[0015] Step S5: Extract OCR text from the candidate area of the target field and perform local fallback OCR. S5-1 Text extraction within character-level boundaries: For each OCR entry in the candidate area, first use character-level coordinate information to filter characters whose x-coordinate of the character box center is within the range of [X_min, X_max] and whose y-range intersects with the candidate area. Then, concatenate the characters in ascending order of x-coordinate to obtain the target text. If character-level coordinates are missing, use text box-level coordinates to determine and truncate the text. If the marker and the target text share the same text box, cut off the marker part according to the marker position and retain the remaining content. S5-2 Backup OCR Triggering and Execution: When the regular extraction result within the candidate region is empty or the result is obviously abnormal (e.g., non-numeric fields are extracted, or the sequence number field is extracted as 0), a local backup OCR is triggered. The candidate region is expanded and cropped, and image enhancement preprocessing is performed (including dynamically selecting the magnification ratio based on the original size, grayscale conversion, and binarization noise reduction). Then, a lightweight backup OCR mini-model is called to perform re-recognition on the local region. The backup strategy is automatically determined according to the field type. The backup OCR mini-model is enabled by default for sequence number fields and numeric fields, while the backup strategy is not used by default for encoded fields and fields without special strategies. S5-3 Deduplication and In-line Sorting: Multiple entries within the candidate region are grouped into rows by the y-coordinate. Entries within each row are arranged in ascending order by the x-coordinate and concatenated line by line. Duplicate text at the same coordinate position is filtered for deduplication based on a combination of content and position.
[0016] Step S6: Field post-processing, outputting structured field results. S6-1 Noise word loop cleaning: Using a preset noise word library, the extracted text is looped through, removing only one match at a time before rescanning until the text no longer changes. S6-2 Field type adaptive post-processing: For numeric fields, non-numeric characters are removed, while numbers and plus signs are retained; for encoded fields, alphanumeric strings containing both letters and numbers are extracted and noise words are filtered out; for quantity fields, tag matching is prioritized, followed by matching numerical values followed by unit words, and finally, the shortest independent number is taken; for unit price fields, tag matching is prioritized, with the last number in the text taken as a fallback; for name fields, prefix tags are cleaned and truncated according to subsequent field markings; for serial number fields, non-numeric characters are removed, and serial numbers of no more than 3 digits are extracted; for fields extracted using regular expressions, precise matching is performed using configured regular expressions. S6-3 Serial Number Continuity Validation and Automatic Increment Repair: For fields with item serial number attributes, an incremental sequence validation is performed on multiple record result sets. If the current serial number is null, zero, or not greater than the previous valid record's serial number, it is considered an anomaly. A backup OCR mini-model is first called to force local re-recognition of the original serial number area of that record. If the anomaly persists, it is automatically repaired by incrementing the previous valid serial number by one, ensuring that the entire table's serial numbers are monotonically increasing and without gaps. S6-4 Partial Marker Residual Cleanup: Independent residual character trimming is performed on any isolated colon prefixes and marker text boundary fragments that may remain after character box boundary positioning. Finally, all field extraction results are assembled into a structured result in key-value pair format. Each record outputs an independent object, returning a complete result array for downstream systems to consume.
[0017] Preferred solution
[0018] Preferably, in the anchor point pairing in step S3, the main anchor point set and the auxiliary anchor point set are sorted in ascending order of their ordinates. The auxiliary anchor points with the closest ordinates and not less than the ordinates of the main anchor points minus the tolerance are selected from the remaining auxiliary anchor points one by one according to the natural order of the main anchor points. After pairing, the paired auxiliary anchor points are removed from the candidate pool to ensure one-to-one pairing and no cross-regional crosstalk.
[0019] Preferably, in the affine mapping parameter calculation in step S3, the horizontal scaling ratio Sx and the vertical scaling ratio Sy are calculated independently using the ratio of the span of the main and auxiliary anchor points in the reference template to the actual matching span; when the reference span is less than 5 pixels, the scaling ratio in the corresponding direction degenerates to 1.0, and the scaling ratio is clamped to the range of [0.3, 3.0] to prevent extreme scaling of abnormal pairing.
[0020] Preferably, in the row grouping in step S4, the ordinate of each row is fixed as the center y-coordinate of the first entry in that row. Subsequent matching is determined solely by this ordinate and is not updated as new entries are added. This avoids chain drift caused by slight deviations in the ordinate of the OCR detection box, which could lead to the incorrect merging of adjacent rows.
[0021] Preferably, in the candidate row scoring in step S4, each hit of the left and right markers is assigned 3 points, and each hit of the top and bottom markers is assigned 1 point. The row with the highest score is selected as the target row. This differentiated weight design is based on the empirical observation that the left and right markers have the highest positioning accuracy in tabular documents.
[0022] Preferably, in the candidate region boundary parsing in step S4, the character-level coordinate information of the OCR result is used to perform normalized character stream matching on the left-side marked text, and the left boundary is precisely set to the right edge x-coordinate of the last character of the left-side mark; similarly, the right boundary of the right-side marked text is set to the left edge x-coordinate of the first character of the right-side mark; if no right-side mark is configured, the minimum left edge coordinate of all entry text boxes located to the right of the left boundary in the target row is taken as the right boundary to prevent cross-column diffusion.
[0023] Preferably, in the fallback OCR trigger in step S5, the system automatically determines whether to enable the backup OCR small model for local re-recognition based on the post-processing strategy of the field: the serial number field and the numeric field are enabled by default, the former focuses on high-precision character-level recognition capabilities to cope with small font size and character sticking scenarios, and the latter performs additional image binarization noise reduction enhancement preprocessing before calling; the encoding field and other fields without special strategies are not enabled by default, but can also be explicitly configured to enable it.
[0024] Preferably, in the text deduplication step S5, a unique identifier key is generated based on the text content and the approximate position after removing the decimal point (rounded to the nearest 10-pixel granularity). Text with the same key is only retained as a copy of the first occurrence, thereby avoiding the accumulation of duplicate text due to overlapping OCR areas at the same coordinate position.
[0025] Preferably, in the sequence number continuity repair in step S6, when the fallback OCR still has not produced a valid sequence number, the current sequence number is set to the sequence number of the previous valid record plus 1 to ensure that the sequence number of the entire table is monotonically increasing without gaps, thus ensuring the integrity of the data structure of the downstream system.
[0026] Beneficial effects
[0027] Compared with the prior art, the present invention has at least the following beneficial effects:
[0028] First, the dual-anchor-point independent axis scaling affine mapping is robust to non-uniform scaling. It calculates the scaling ratio in the X and Y directions independently using the span ratio of the primary and secondary anchor points in the template and the actual image, rather than assuming uniform scaling or relying solely on single anchor point displacement compensation. When inconsistent horizontal and vertical scaling occurs due to printing scaling or scanning scale adjustments, the mapped areas of each field can be independently corrected in both directions, significantly improving the field area positioning accuracy.
[0029] Second, intelligent anchor point pairing enables automatic segmentation of multiple record detail rows. By successively selecting the closest auxiliary anchor point from the candidates and pairing it with the main anchor point, multiple duplicate detail rows in a document are automatically segmented into independent records. Each record has its own set of entries within a vertical range. Compared with the traditional method of manually marking the boundaries of each row, this achieves automatic segmentation of detail rows with zero manual configuration.
[0030] Third, character-level boundary positioning significantly shrinks the candidate region. By utilizing the character-level coordinate information provided by OCR, the right edge of the last character / left edge of the first character of the left and right tags are accurately located, shrinking the field extraction area to a precise window between tags (or between tags and their neighbors), significantly reducing the inclusion of interfering text such as fields with the same name and content from adjacent columns.
[0031] Fourth, the row grouping anti-chain drift design. By anchoring the y-coordinate of the first row instead of dynamically updating the average y-coordinate of the current row, the merging of adjacent rows caused by the accumulation of small deviations in the vertical direction of the OCR detection box is avoided, ensuring the correct decomposition of the table row structure.
[0032] Fifth, a differentiated fallback OCR strategy based on field type. Addressing the recognition challenges of different field types (P / D confusion in small-sized serial numbers, blurring of small-sized numeric characters), the optimal fallback OCR engine and image preprocessing strategy are automatically selected to ensure accuracy while avoiding unnecessary full-image re-recognition overhead for all fields.
[0033] Sixth, intelligent serial number continuity repair ensures data integrity. Through a dual strategy of first performing a fallback re-identification and then incremental repair, it ensures that documents with identification flaws can still output complete incremental serial numbers that conform to business logic, avoiding data association errors caused by missing or duplicate serial numbers in downstream systems. Attached Figure Description
[0034] Figure 1 This is a schematic diagram of the overall process of Embodiment 1 of the present invention, showing the processing steps of main and auxiliary anchor point retrieval and pairing, independent axis scaling, vertical range entry collection, relative marker candidate region parsing, local fallback OCR, field post-processing, and sequence number continuity repair. Detailed Implementation
[0035] To enhance understanding of the present invention, the following description is provided. Figure 1 This embodiment will be described in detail.
[0036] Example 1: See Figure 1 As shown, a method for extracting document fields based on double-anchor affine mapping and relative position constraints includes the following steps:
[0037] Step 1, Obtain the image of the document to be recognized and the OCR recognition result: Perform a global OCR on the actual purchase contract image to obtain the text content, four-corner coordinates and character-level coordinate information of each text box, which serves as the data basis for subsequent anchor point positioning, relative marker matching and field truncation; for example, in the purchase contract image of this embodiment, the OCR recognition result includes text boxes such as "Material Number:", "Product Name:", "Quantity:", "P141206A000G0", "Light Curtain Safety Touch Panel Device", "30" and their coordinates.
[0038] Step 2, Read Template Configuration: First, take a picture or scan a standard blank purchase contract template as a reference image. In the configuration interface, mark the main anchor point `titlename` as "Material Number:" and the secondary anchor point `aux_field_name` as "Product Name:". Then, select a rectangular area on the reference image and record the startx / starty / endx / endy coordinates as the template reference base. Simultaneously, configure relative marking rules for each target field. For example, the left side of the customer material code is marked as "Material Number:", the right side as "Product Name:", and the post-processing strategy is code cleaning. The left side of the sales quantity is marked as "Quantity:", it is a numeric field, and a backup OCR small model is enabled. The post-processing strategy for the project number is serial number, and a backup OCR small model is enabled.
[0039] Step 3: Main and auxiliary anchor point retrieval and pairing, independent axis scaling, and vertical range item collection: The system retrieves all main anchor point text boxes containing "part number:" and auxiliary anchor point text boxes containing "product name:" in the OCR results. In this embodiment, the contract contains 3 lines of materials, so 3 main anchor points and 3 auxiliary anchor points are retrieved. Each main anchor point is paired with the remaining auxiliary anchor points whose ordinate is closest to and not lower than the ordinate of the main anchor point minus the tolerance, in ascending order of the ordinate. After pairing, the anchor point pairs (m1,a1), (m2,a2), and (m3,a3) correspond to the details of the 1st, 2nd, and 3rd lines, respectively. Calculate the scaling ratio using the first pair of anchor points: In the reference template, the center of the main anchor point is (55, 574) and the center of the auxiliary anchor point is (56, 670). In the actual template, the center of the main anchor point m1 is (60, 580) and the center of the auxiliary anchor point a1 is (62, 700). Since the reference horizontal span is less than 5 pixels, the horizontal scaling ratio Sx degenerates to 1.0, and the vertical scaling ratio Sy = (700 − 580) / (670 − 574) = 1.25. Then, starting from the top edge of the main anchor point text box of each pair of anchor points and ending at the bottom edge of the auxiliary anchor point text box, and extending outward by 0.5 times the line height to construct a vertical range, all text boxes whose vertical coordinate center is within this range are included in the entry set of this record.
[0040] Step 4, row grouping, relative marker scoring, and character-level candidate region generation: For each item within a vertical range, the row height threshold is set by multiplying the average item height by 0.65. Rows are grouped using the anchoring method of the first row y. For example, “Material Number:” (y=563) and “P141206A000G0” (y=562) are grouped into the first row, “Product Name:” (y=682) and “Light Curtain Safety Touch Panel Device” (y=680) are grouped into the second row, and “Quantity:” (y=740) and “30” (y=739) are grouped into the third row. Each row is traversed, and the relative marker hits are weighted and scored. The left and right marker hits are each assigned 3 points, and the top and bottom marker hits are each assigned 1 point. The row with the highest score is selected as the target row. For fields with clearly defined left and right markers, the markers are matched using character-level coordinates in the target row using normalized character stream matching. The left boundary is the right edge of the last character of the left marker "material number:" and the right boundary is the left edge of the first character of the right marker "product name:". This generates a candidate region that only covers the target field.
[0041] Step 5: Text extraction within the candidate area, local fallback OCR, and deduplication: Within the candidate area generated in Step 4, filter the OCR text boxes or character boxes, and extract the target text whose center point falls within the area. For example, the customer material code "P141206A000G0" is restricted to the narrow area between the right side of the "Material Number:" label and the left side of the "Product Name:" label, without mixing in the content of the right label. For cases where the marker is broken across the text box or the regular extraction within the candidate area is empty or abnormal, the area is expanded and cropped, and then grayscale, binarized for noise reduction, and magnified for enhancement is performed. A lightweight backup OCR small model is then called to perform local re-recognition, and the regular extraction results and fallback recognition results are then merged to remove duplicates.
[0042] Step 6: Field post-processing, sequence number continuity repair, and structured output: The extracted text is post-processed differently according to field type. For example, numeric fields are cleaned using `digit_only`, encoded fields using `alnum_clean`, and item number fields are parsed using `sequence_index` or `regex_extract`. For incremental sequence number fields such as item numbers, continuity checks are performed on multiple record result sets. If the current sequence number is null, zero, or not greater than the previous valid record number, it is considered abnormal. A backup OCR mini-model is first called to force local re-identification of the narrow column area of the sequence number in that row. If it is still abnormal, it is automatically repaired to the previous valid sequence number plus one. Taking three rows of materials in this embodiment as an example, the first record has a sequence number of "1", the second is misidentified as "1" but is corrected to "2" after fallback re-identification, and the third is missing but still fails to be corrected after re-identification and is automatically repaired to "3". The final output is 1, 2, and 3, which are continuous and complete. Each row of material fields is assembled into a key-value pair record, and a complete result array is returned.
[0043] It should be noted that the above embodiments are not intended to limit the scope of protection of the present invention. Equivalent transformations or substitutions made based on the above technical solutions all fall within the scope of protection of the claims of the present invention.
Claims
1. A method for extracting document fields based on double-anchor affine mapping and relative position constraints, characterized in that, The method includes the following steps: Step S1: Obtain the image of the document to be recognized and the corresponding OCR recognition result. Step S2: Read the template configuration corresponding to the document type. Step S3: Retrieve the main anchor text and secondary anchor text from the OCR recognition results, establish a dual-anchor affine mapping relationship, and create a set of entries, with each entry appended with character-level coordinate information. Step S4: For each target field, generate a candidate region for that target field; Step S5: Extract the OCR text from the candidate region of the target field and perform local fallback OCR. Step S6: Post-processing of fields, outputting structured field results.
2. The method according to claim 1, characterized in that, Step S1: Obtain the image of the document to be recognized and the corresponding OCR recognition result. The OCR recognition result includes at least: (a) the text content string of each detection text box; (b) the coordinates of the four corners of the rectangular text box, represented as [x1,y1,x2,y2]; (c) character-level coordinate information, that is, the independent bounding box of each character in the text box, which is used for fine-grained character-level boundary positioning. For multi-page documents or multi-region spliced images, the preprocessing stage is uniformly rendered as a single complete image and a global OCR is performed once to avoid text breakage caused by region cutting. Step S2: Read the template configuration corresponding to the document type. The template configuration includes at least the main anchor point identifier (titlename, a feature text that appears uniquely or stably in the document; auxiliary anchor point identifier (aux_field_name, auxiliary positioning text that appears in pairs with the main anchor point, forming a double anchor point pair); template reference area, and records the relative label configuration and extraction type for each field); relative label configuration (each field can be configured with one or more of the following: left_text, right_text, above_text, and below_text, as the basis for candidate row positioning and candidate area shrinking); and field post-processing strategies (including numeric field label isdig, denoised word list excepttext, regular expression extraction mode value_extract_regex, post-processing strategy enumeration, and whether to enable a local backup OCR small model as a fallback).
3. The method according to claim 1, characterized in that, Step S3 is as follows: S3-1 Primary and Secondary Anchor Point Text Retrieval: Traverse all OCR-recognized text boxes, matching each text content with the preset primary and secondary anchor point identifiers in the template configuration (supporting unified normalization of full-width / half-width colons). Successfully matched text boxes are added to the primary anchor point set M and the secondary anchor point set A, respectively. Each element contains the text box text, the coordinates of its four corners, and the center coordinate. S3-2 Intelligent Primary and Secondary Anchor Point Pairing: Sort the primary and secondary anchor point sets in ascending order of ordinate and abscissa, respectively. For each primary anchor point, find the secondary anchor point whose spatial position is closest to it among the unpaired secondary anchor points. The pairing condition is that the secondary anchor point's ordinate is not lower than the primary anchor point's ordinate minus the tolerance. Select the secondary anchor point with the smallest ordinate difference to establish a pair. After pairing, remove the secondary anchor point from the candidate pool. Each pair corresponds to one record in the document. S3-3 Affine Mapping Parameter Calculation: The rectangular regions of the primary and secondary anchor points in the reference template are read from the template configuration, and their center coordinates are calculated to obtain reference anchor point pairs. For the first pair of actual anchor points, the horizontal scaling ratio Sx = (secondary anchor point x - primary anchor point x) / (reference secondary anchor point x - reference primary anchor point x) and the vertical scaling ratio Sy = (secondary anchor point y - primary anchor point y) / (reference secondary anchor point y - reference primary anchor point y) are calculated. When the span of the reference anchor point pair is less than a threshold, the scaling ratio in the corresponding direction degenerates to 1.
0. Simultaneously, the scaling ratio is clamped to a safe range to prevent extreme scaling. S3-4 Vertical Range Entry Collection for Each Record: For each successfully paired anchor point, starting from the top edge of the main anchor point text box and ending at the bottom edge of the auxiliary anchor point text box, and combining the height of the anchor point text box to expand outward by a preset ratio to construct a vertical range, all OCR text boxes whose vertical coordinate center is located within this range are included in the entry set of this record, and each entry is attached with character-level coordinate information.
4. The method according to claim 1, characterized in that, Step S4 is as follows: S4-1 Row Grouping (Anchoring the first row y, no scrolling updates to avoid chain drift): All entries in the entry set are sorted in ascending order by y-coordinate, the average height of the entries is calculated, and the row height threshold is the average height multiplied by 0.
65. When executing the row grouping algorithm, the y-coordinate reference of each row is fixed as the center Y-coordinate of the first entry in that row. Subsequent entries only need to be determined whether the difference between their Y-coordinate and that of the first entry is within the row height threshold to be included in the current row. The anchor Y of the current row is not updated with the addition of new entries, thereby avoiding the incorrect merging of adjacent rows due to small chain drift of y-coordinates in high-density text areas. S4-2 Candidate Row Weighted Scoring Positioning: When the field extraction type is relative, iterate through all row grouping results and score each row by relative label hit. The left and right labels are each assigned 3 points for a hit, and the top and bottom labels are each assigned 1 point. The left and right labels have higher weight than the top and bottom labels. Select the candidate row with the highest score as the target row. S4-3 Precise Candidate Region Boundary Analysis (Character-Level Positioning): In the target line, character-level boundary positioning is performed on the left and right markers respectively. For the left marker, normalized character stream matching is performed using character-level coordinate information, and the left boundary is set to the right edge x-coordinate of the last character box of the left marker; for the right marker, the right boundary is set to the left edge x-coordinate of the first character box of the right marker; if the left marker matches the prefix in the same text box and is followed by the target field content, the suffix part is extracted directly using the marker as the separator; if the right marker is not configured, the right boundary is taken as the minimum left edge coordinate of all entry text boxes located to the right of the left boundary in the target line, thus obtaining a precise candidate rectangular region sandwiched between the left and right markers and constrained by the upper and lower marker rows.
5. The method according to claim 1, characterized in that, Step S5 is as follows: S5-1 Character-level text extraction within boundaries: For each OCR entry within the candidate region, first use character-level coordinate information to filter characters whose x-coordinates of the character box center are within the range of [X_min, X_max] and whose y-range intersects with the candidate region. Then, concatenate the characters in ascending order of x-coordinates to obtain the target text. If character-level coordinates are missing, use text box-level coordinates to determine and truncate the text. If the marker and the target text share the same text box, cut off the marker portion according to the marker position and retain the remaining content. S5-2 Backup OCR Triggering and Execution: When the regular extraction result within the candidate region is empty or the result is obviously abnormal, a local backup OCR is triggered. The candidate region is expanded and cropped, and image enhancement preprocessing is performed. Then, a lightweight backup OCR mini-model is called to perform re-recognition on the local region. The backup strategy is automatically determined according to the field type. The backup OCR mini-model is enabled by default for serial number fields and numeric fields. The backup strategy is not used by default for encoded fields and fields without special strategies. S5-3 Deduplication and In-line Sorting: Multiple entries within the candidate region are grouped into rows by the y-coordinate. Entries within each row are arranged in ascending order by the x-coordinate and concatenated line by line. Duplicate text at the same coordinate position is filtered for deduplication based on the combination key of content and position.
6. The method according to claim 1, characterized in that, Step S6: Post-processing of fields, outputting structured field results, as follows: S6-1 Noise Word Cyclic Cleaning: Utilizing a pre-defined noise word library, the extracted text is cyclically stripped of its beginning and end matches. Only one match is stripped at a time, and the text is rescanned until no further changes occur. S6-2 Field Type Adaptive Post-Processing: For numeric fields, remove non-numeric characters and retain numbers and plus signs; for encoded fields, extract alphanumeric strings containing both letters and numbers and filter out noisy words. The quantity field prioritizes tag matching, followed by matching numerical values followed by unit words in a numeric string, and finally selecting the shortest independent number. The unit price field prioritizes tag matching, with the last number in the text as a fallback. The name field removes prefix tags and truncates according to subsequent field markings. The serial number field removes non-numeric characters and extracts serial numbers no more than 3 digits. Regular expression extraction uses configured regular expressions for precise matching. S6-3 Serial Number Continuity Validation and Automatic Increment Repair: For fields with item serial number attributes, an incremental sequence validation is performed on multiple record result sets. If the current serial number is null, zero, or not greater than the serial number of the previous valid record, it is considered an anomaly. First, a backup OCR mini-model is called to force local re-identification of the original serial number range of that record. If the anomaly persists, it is automatically repaired by incrementing the previous valid serial number by one, ensuring that the serial numbers in the entire table are monotonically increasing and without gaps. S6-4 Partial Marker Residual Cleanup: Performs independent residual character trimming on isolated colon prefixes and marker text boundary fragments that may remain after character box boundary positioning. Finally, it assembles all field extraction results into a structured result in key-value pair form. Each record outputs an independent object and returns a complete result array for downstream systems to consume.
7. The method according to any one of claims 1 to 6, characterized in that, Further includes: For fields with project serial number attributes, serial number continuity checks are performed across record result sets. When the current serial number is detected to be null, zero, or not greater than the previous record serial number, local fallback OCR forced re-recognition and automatic increment repair are triggered in sequence to ensure that the serial numbers in the entire table are monotonically increasing.
8. A document field extraction system based on dual-anchor affine mapping and relative position constraints, characterized in that, include: The OCR result access module, template configuration module, anchor point matching and pairing module, affine mapping module, row grouping and candidate row scoring module, character-level candidate region parsing module, fallback OCR engine scheduling module, field adaptive post-processing module, and sequence number continuity verification and repair module are all used to execute the method described in any one of claims 1 to 8.
9. An electronic device, characterized in that, It includes a processor and a memory, wherein the memory stores a computer program, which, when executed by the processor, implements the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method of any one of claims 1 to 7.
