Text detection and template matching-based bill identification method and device

Through methods based on text detection and template matching, the problems of missed text detection and poor robustness in bill recognition are solved, achieving efficient and accurate recognition of bills and reducing labor costs.

CN118736613APending Publication Date: 2024-10-01CLOUDCHAIN GRP CO LTD

Patent Information

Application Number
CN202411043326.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-10-01

AI Technical Summary

Technical Problem

The existing technology has problems with missed text detection and poor robustness in bill recognition. It relies heavily on text detection models, and the accuracy of the model cannot be guaranteed. It also requires a large amount of manual annotation of training data, resulting in high labor costs.

Method used

Using a method based on text detection and template matching, through image direction recognition correction, bill area detection, text area detection, perspective transformation and inverse mapping transformation, it ensures the complete detection of the text box, and uses the pre-trained text recognition model to extract the text content , perform structured output in the form of a bill template.

Benefits of technology

It achieves efficient and accurate identification of bills, avoids missed detections in text detection, improves the robustness of identification, reduces reliance on manual annotation of data, and reduces labor costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118736613A_ABST
    Figure CN118736613A_ABST
Patent Text Reader

Abstract

The invention provides a bill recognition method and device based on text detection and template matching, and the method comprises the steps: carrying out the angle recognition correction of a bill image based on an image direction recognition correction model, recognizing the position of a bill in the way through a bill region detection model, and recognizing the position of a textbox through a text region detection model; the size and the shape of the bill image are consistent with those of the bill template through perspective transformation, and the recognized textboxes in the bill image are matched with the textboxes in the standard format in the bill template by calculating the overlapping proportion of the textboxes; and according to the position deviation between the bill image and the standard format textbox in the bill template, based on inverse mapping transformation, finding out the residual textbox which cannot be detected by the text area detection model. And text content in the text box is extracted through the text recognition model, and is directly structured according to the form of the bill template, so that efficient and accurate recognition of the bill is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of image recognition, and in particular to a bill recognition method and device based on text detection and template matching. Background Art

[0002] VAT general invoices, special invoices, and electronic invoices are formal invoices issued by enterprises when selling goods or providing services. The information on VAT invoices usually includes basic invoice information, purchaser information, seller information, product information, price and tax information, etc. VAT invoice recognition is the process of automatically extracting and processing the information on VAT invoices through technical means to improve financial management efficiency, reduce error rates, and standardize invoice management processes.

[0003] In the prior art, methods of text detection, text direction recognition, text recognition and rule processing can be used, that is, firstly all text lines in the image are detected by a machine learning model, and then the detected text lines are input into a text recognition model, and the characters in the text lines are extracted respectively, so that the text content in the unstructured image obtained by the model can be obtained. Finally, a regular expression is used to structure the text content recognized by the model, so as to obtain structured data. Alternatively, the key information extraction method is used to structure the text data in text recognition, that is, by training a machine learning model, the recognized text content and the image area where the text is located are input into the key information extraction model, so as to directly obtain the key value information to which the text content belongs. However, the prior art relies heavily on the text detection model, the text direction recognition model and the text recognition model, and the accuracy of the model cannot be guaranteed to be 100%, if the initial text detection model has missed detection. Secondly, the premise of using the model is that the training data needs to be manually annotated. Regardless of whether the text detection model, the text direction is modeled, the text recognition model or the key information extraction model of method 2, a large amount of training data needs to be manually annotated, and the annotation work will also cost a lot of manpower. Furthermore, it relies heavily on regular expressions. If text detection misses a detection or text recognition makes a mistake, the regular expression will not produce the desired result and will have poor robustness.

[0004] Therefore, when faced with the problem of bill recognition, a new recognition solution is urgently needed. Summary of the invention

[0005] In view of this, an embodiment of the present invention provides a bill recognition method and device based on text detection and template matching to eliminate or improve one or more defects existing in the prior art and solve the problem of missed detection and poor robustness in the prior art when performing text recognition on bills.

[0006] One aspect of the present invention provides a bill recognition method based on text detection and template matching, the method comprising the following steps:

[0007] Obtain a bill image to be processed, input a pre-trained image direction recognition and correction model, identify the angle of the bill image and perform compensation adjustment;

[0008] Input the bill image after angle compensation adjustment into a pre-trained bill area detection model, and output the location of each bill area;

[0009] Input each bill area into the pre-trained text area detection model and output the location of the localized text box;

[0010] Obtain a preloaded bill template, the bill template is set according to a standard size, the bill template marks multiple template text boxes corresponding to key items in the bill and the values ​​of the key items, and saves it in JSON format;

[0011] Performing perspective transformation on the position of the bill area detected in the bill image so as to make it match the size and shape of the bill template and then aligning it;

[0012] Calculate the overlap ratio between each first-category candidate text box and each template text box in the bill image after perspective transformation, and associate and match each first-category candidate text box with the template text box with the highest overlap ratio; wherein, according to the offset between the text box corresponding to the key item and its value in the bill template and the first-category text box, use the first-category candidate text box that has completed the association matching to search and determine the second-category candidate text box that has not been detected by the text area detection model;

[0013] Performing an inverse mapping transformation on the first category candidate text boxes and the second category candidate text boxes according to the perspective transformation to obtain all target text boxes in the original bill image;

[0014] Input the content of each target text box into the pre-trained text recognition model to obtain the text content of each target text box;

[0015] According to the correspondence between each template text box in the bill template and the target text box in the original bill image, the text content of the target text box is directly saved in a structured manner and output in a JSON format.

[0016] In some embodiments, the pre-training step of the image direction recognition and correction model includes:

[0017] Obtain a first training sample set, wherein the first training sample set includes a plurality of samples, each sample includes a bill sample image, and the angle of the bill image is marked as a label using a three-step phase method; the three-step phase method represents an angle using three cosine values;

[0018] Obtain an initial image direction recognition model, wherein the initial image direction recognition model adopts a ResNet18 network, takes the bill sample image as input and outputs three fitting values ​​from the last hidden layer, and outputs an angle prediction value of the bill sample image through an activation function;

[0019] The initial image direction recognition model is trained using the first training sample set, an L1 loss is constructed using the angle prediction value and the label, and parameters are iteratively updated based on a stochastic gradient optimizer to obtain an image direction recognition model;

[0020] The image direction recognition model is connected to an angle adjustment module to obtain the image direction recognition correction model, and the angle adjustment module is used to perform direction correction according to the angle prediction value output by the image direction recognition model.

[0021] In some embodiments, the pre-training step of the bill area detection model includes:

[0022] Obtain a second training sample set, wherein the second training sample set includes a plurality of samples, each sample includes a picture and marks a position of a bill area in the picture as a label;

[0023] Obtain an initial bill area detection model, wherein the initial bill area detection model adopts an R-CNN or YOLO target detection model;

[0024] The initial bill area detection model is trained using the second training sample set, and a cross entropy loss function is constructed to iteratively update parameters to obtain the bill area detection model.

[0025] In some embodiments, the pre-training step of the text region detection model includes:

[0026] Obtain a third training sample set, wherein the third training sample set includes multiple samples, each sample includes a bill image and a position of a marked text as a label;

[0027] Obtaining an initial text region detection model, wherein the initial text region detection model adopts an efficient and accurate scene text detection network model or a connectionist text proposal network model;

[0028] The initial text region detection model is trained using the third training sample set, a cross entropy loss function is constructed to iteratively update parameters, and the text region detection model is obtained.

[0029] In some embodiments, the position of the bill area detected in the bill image is perspective transformed to match the size and shape of the bill template and then aligned, including:

[0030] Locating the corner points of the bill area in the bill image as source point coordinates, and locating the corner points of the bill template as target point coordinates;

[0031] Use OpenCV's cv2.getPerspectiveTransform function to calculate the perspective transformation matrix from the source point coordinates to the target point coordinates;

[0032] Based on the perspective transformation matrix, the cv2.warpPerspective function of OpenCV is used to perform perspective transformation on the bill area in the bill image.

[0033] In some embodiments, the pre-training step of the text recognition model includes:

[0034] Obtain a fourth training sample set, wherein the fourth training sample set includes a plurality of samples, each sample includes a sample image containing text and marks the text string contained therein as a label;

[0035] Performing image preprocessing on the sample images in the fourth training sample set, including adjusting image size, graying and normalizing; converting the text string into a character sequence, and creating a character-to-index mapping;

[0036] Acquire an initial text recognition model, the initial text recognition model comprising a convolution layer, a recurrent layer and a transcription layer, the convolution layer is used to extract image features of the sample image to obtain a feature map, the recurrent layer converts the feature map into sequence features, and the transcription layer converts the sequence features into a predicted character sequence;

[0037] The initial text recognition model is trained using the fourth training sample set, and a CTC loss is constructed to iteratively update parameters to obtain an intermediate text recognition model;

[0038] A post-processing module is added to the intermediate text recognition model to convert the predicted character sequence into text to obtain the text recognition model.

[0039] In some embodiments, the text recognition model is an Attention OCR text recognition model.

[0040] On the other hand, the present invention also provides a bill recognition device based on text detection and template matching, comprising a processor, a memory, and a computer program / instructions stored in the memory, wherein the processor is used to execute the computer program / instructions, and when the computer program / instructions are executed, the device implements the steps of the above method.

[0041] On the other hand, the present invention also provides a computer-readable storage medium having a computer program / instruction stored thereon, which implements the steps of the above method when the computer program / instruction is executed by a processor.

[0042] On the other hand, the present invention also provides a computer program product, comprising a computer program / instruction, characterized in that the computer program / instruction implements the steps of the above method when executed by a processor.

[0043] The beneficial effects of the present invention are at least:

[0044] The bill recognition method and device based on text detection and template matching of the present invention firstly performs angle recognition correction on the bill image based on the image direction recognition correction model to avoid inaccurate text detection caused by image direction deviation; then, the position of the bill in the middle is recognized by the bill area detection model, and the position of the text box is recognized by the text area detection model; the bill image is made consistent with the size and shape of the bill template through perspective transformation, and the text boxes recognized in the bill image are matched with the text boxes of the standard format in the bill template by calculating the overlap ratio of the text boxes; then, according to the position deviation between the bill image and the text boxes of the standard format in the bill template, based on the inverse mapping transformation, the remaining text boxes that the text area detection model fails to detect are found, ensuring that all text boxes corresponding to the text in the bill are fully detected to prevent omission. Then, the text content in the text box is extracted by the text recognition model, and directly structured in the form of the bill template, so as to realize efficient and accurate recognition of the bill.

[0045] Additional advantages, purposes, and features of the present invention will be described in part in the following description, and will become apparent to those skilled in the art after studying the following, or may be learned from the practice of the present invention. The purposes and other advantages of the present invention may be achieved and obtained by the structures specifically indicated in the specification and the accompanying drawings.

[0046] Those skilled in the art will appreciate that the objectives and advantages that can be achieved with the present invention are not limited to the above specific description, and the above and other objectives that can be achieved by the present invention will be more clearly understood from the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of the present application, and do not constitute a limitation of the present invention. In the drawings:

[0048] Figure 1 The figure is a flow chart of a method for identifying a value-added tax invoice according to an embodiment of the present invention.

[0049] Figure 2 This is a logical diagram of a method for identifying a value-added tax invoice according to another embodiment of the present invention. DETAILED DESCRIPTION

[0050] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the embodiments and the accompanying drawings. Here, the illustrative embodiments of the present invention and their descriptions are used to explain the present invention, but are not intended to limit the present invention.

[0051] It should also be noted that, in order to avoid obscuring the present invention due to unnecessary details, only structures and / or processing steps closely related to the solutions according to the present invention are shown in the accompanying drawings, while other details that are not closely related to the present invention are omitted.

[0052] It should be emphasized that the term “include / comprises” when used herein refers to the presence of features, elements, steps or components, but does not exclude the presence or addition of one or more other features, elements, steps or components.

[0053] For text recognition of bill images, it is necessary to identify the bill in the image and the location of the text in it based on image processing technology, and then extract the text information in the text box based on the text recognition model. However, in practice, due to problems such as shooting angle, clarity, and light, image processing based on computer vision may not be able to identify all text boxes containing text information in the image. Therefore, this application also needs to use the positional relationship between text boxes in the standard bill template to search for text boxes in the bill image.

[0054] Specifically, the present invention provides a bill recognition method based on text detection and template matching, the method comprising the following steps S101 to S109:

[0055] Step S101: Acquire a bill image to be processed, input a pre-trained image direction recognition and correction model, identify the angle of the bill image and perform compensation adjustment.

[0056] Step S102: input the bill image after angle compensation adjustment into a pre-trained bill area detection model, and output the location of each bill area.

[0057] Step S103: input each bill area into a pre-trained text area detection model, and output the location of the positioning text box.

[0058] Step S104: Obtain a preloaded bill template, which is set according to a standard size. The bill template marks multiple template text boxes corresponding to key items and key item values ​​in the bill, and saves them in JSON format.

[0059] Step S105: Perform perspective transformation on the position of the bill area detected in the bill image to make it match the size and shape of the bill template and then perform alignment processing.

[0060] Step S106: Calculate the overlap ratio between each first-category candidate text box and each template text box in the bill image after perspective transformation, and associate and match each first-category candidate text box with the template text box with the highest overlap ratio; wherein, according to the offset between the text box corresponding to the key items and their values ​​in the bill template and the first-category text box, use the first-category candidate text box that has completed the association match to search and determine the second-category candidate text box that has not been detected by the text area detection model.

[0061] Step S107: performing inverse mapping transformation on the first category candidate text boxes and the second category candidate text boxes according to perspective transformation to obtain all target text boxes in the original bill image.

[0062] Step S108: input the content of each target text box into a pre-trained text recognition model to obtain the text content of each target text box.

[0063] Step S109: According to the correspondence between each template text box in the bill template and the target text box in the original bill image, the text content of the target text box is directly saved in a structured manner and output in a JSON format.

[0064] In step S101, the image orientation is normalized to ensure that the subsequent image processing and text recognition steps can process consistent inputs and reduce the complexity caused by different image orientations. Images with incorrect orientations will increase noise and errors, which may lead to misrecognized characters and words. Through orientation correction, these interferences can be reduced and recognition quality can be improved.

[0065] In some embodiments, the pre-training step of the image direction recognition and correction model includes steps S1011 to S1014:

[0066] Step S1011: Obtain a first training sample set, which includes multiple samples, each sample includes a bill sample image, and uses the three-step phase method to mark the angle of the bill image as a label; the three-step phase method uses three cosine values ​​to represent an angle. Exemplarily, an angle is represented by three cosine values, and the expression is:

[0067] x 1 =cos(θ);x 2 =cos(θ+120°); x 3 =cos(θ+240°);

[0068] Step S1012: Obtain the initial image direction recognition model. The initial image direction recognition model uses the ResNet18 network, takes the bill sample image as input and outputs three fitting values ​​from the last hidden layer, and outputs the angle prediction value of the bill sample image through the activation function. Among them, the ResNet18 network is a deep convolutional neural network, which includes 17 convolutional layers and 1 fully connected layer. It uses a residual block to implement it. By introducing skip connections, the output of each residual block is the result of its input plus the processing of two convolutional layers, thereby alleviating the gradient vanishing problem in the deep neural network and accelerating the training of the network.

[0069] To fit the three cos values, the output of the last hidden layer of the model is The predicted value of the model is calculated by the following formula:

[0070]

[0071] Step S1013: Use the first training sample set to train the initial image direction recognition model, use the angle prediction value and the label to construct the L1 loss and perform parameter iterative update based on the stochastic gradient optimizer to obtain the image direction recognition model. The loss is calculated with the three angle cosine encoded values.

[0072] Step S1014: the image direction recognition model is connected to an angle adjustment module to obtain an image direction recognition correction model, and the angle adjustment module is used to perform direction correction according to the angle prediction value output by the image direction recognition model.

[0073] In step S102, based on the angle-corrected bill image, the bill area is identified by the bill area detection model. In some embodiments, the pre-training steps of the bill area detection model include S1021 to S1023:

[0074] Step S1021: Obtain a second training sample set, where the second training sample set includes multiple samples, each sample includes a picture and marks the position of the bill area in the picture as a label.

[0075] Step S1022: Obtain an initial bill area detection model, where the initial bill area detection model uses an R-CNN or YOLO target detection model.

[0076] The R-CNN object recognition model achieves high-precision object detection by using pre-trained convolutional neural networks (such as AlexNet, VGG16, etc.) for feature extraction, and achieves end-to-end object detection by combining image region generation, feature extraction, classification, and bounding box regression in a complete pipeline. Its modular design facilitates the improvement and expansion of each submodule, and the use of transfer learning greatly reduces training time and computing resources.

[0077] The YOLO (You Only Look Once) target detection model is known for its real-time detection capabilities. It can complete target detection in a single neural network forward propagation, achieving a speed of dozens of frames per second, which is suitable for real-time applications. YOLO uses an end-to-end training method, from input image to output target category and position in one neural network, which simplifies the training and reasoning process. It considers the global information of the image when predicting, performs well in processing background information and avoiding false detection. The unified neural network structure is adopted, and there is no need for a separate region extraction step, making the model more concise and efficient. Despite the pursuit of speed, YOLO still maintains high accuracy in most cases, especially in complex backgrounds and small object detection.

[0078] Step S1023: Use the second training sample set to train the initial bill area detection model, construct a cross entropy loss function to iteratively update parameters, and obtain the bill area detection model.

[0079] In step S103, the text region detection model may adopt a text detection model such as an efficient and accurate scene text detection network model (EAST), a connectionist text proposal network model (CTPN), or the like.

[0080] The EAST (Efficient and Accurate Scene Text) text detection model is a method for text detection in natural scenes. It combines FCN (Fully Convolutional Networks) and PN (Prediction Networks) to efficiently and accurately detect horizontal and multi-directional text regions. It avoids complex post-processing steps in the detection stage by directly regressing the geometric shape parameters of the text region, thereby improving detection efficiency and accuracy.

[0081] The CTPN (Connectionist Text Proposal Network) text detection model is a method based on convolutional neural networks (CNN) and recurrent neural networks (RNN), focusing on vertical text line detection. CTPN achieves accurate detection of multi-line text by dividing the image into vertical slices of fixed width and then predicting whether each slice contains text and its location, which is particularly suitable for dense and complex text scenes.

[0082] In some embodiments, the pre-training step of the text region detection model includes S1031 to S1033:

[0083] Step S1031: Obtain a third training sample set, where the third training sample set includes multiple samples, each sample includes a bill image and a marked text location as a label.

[0084] Step S1032: obtaining an initial text region detection model, where the initial text region detection model adopts an efficient and accurate scene text detection network model or a connectionist text proposal network model.

[0085] Step S1034: Use the third training sample set to train the initial text region detection model, construct a cross entropy loss function to iteratively update parameters, and obtain a text region detection model.

[0086] In step S104, a standard template is established for one or more bills to be detected. The standard template is constructed according to the bill style actually used, and records include standard size, key items and corresponding value content contained in the bill, text boxes and position coordinates of key items and values ​​in the bill, and is recorded and saved in JSON format. The purpose of establishing the bill template here is to compare with the bill image to be processed. On the one hand, it searches for undetected text boxes based on the relative positions of the key item text boxes, and on the other hand, the text content in the bill that is subsequently identified is stored in a standard format.

[0087] In step S105, in order to compare the bill image with the bill template, the size and shape need to be transformed to be in the same proportion as a prerequisite.

[0088] In some embodiments, the position of the bill area detected in the bill image is perspective transformed to match the size and shape of the bill template and then aligned, including steps S1051 to S1053:

[0089] Step S1051: locate the corner points of the bill area in the bill image as source point coordinates, and locate the corner points of the bill template as target point coordinates.

[0090] Step S1052: Use the cv2.getPerspectiveTransform function of OpenCV to calculate the perspective transformation matrix from the source point coordinates to the target point coordinates.

[0091] Step S1054: Based on the perspective transformation matrix, use the cv2.warpPerspective function of OpenCV to perform perspective transformation on the bill area in the bill image.

[0092] In step S106, after perspective transformation and alignment, the overlap ratio of the first-class text boxes in the transformed bill image and the text boxes in the bill template is calculated respectively. For each first-class text box, the template text box with the highest overlap ratio is associated and matched with it. In this process, there may be some template text boxes that do not have corresponding first-class text boxes. This is because the text area detection model in step S103 fails to fully identify all text boxes in the bill image. Since the relative position of each part of the text in the bill is fixed, based on the offset between the bill template and the first-class text box, the unrecognized and marked text box can be found in the bill image, and this text box is marked as a second-class text box.

[0093] In step S107, the first type of text boxes and the second type of text boxes can be restored to the original bill image through inverse mapping transformation to obtain all target text boxes.

[0094] In step S108, the text in the target text box is recognized, and the pre-training steps of the text recognition model include S1081 to S1085:

[0095] Step S1081: Obtain a fourth training sample set, where the fourth training sample set includes multiple samples, each sample includes a sample image containing text and marks the text string contained therein as a label.

[0096] Step S1082: performing image preprocessing on the sample images in the fourth training sample set, including adjusting the image size, graying and normalizing; converting the text string into a character sequence, and creating a character-to-index mapping.

[0097] Step S1083: Obtain an initial text recognition model. The initial text recognition model includes a convolutional layer, a recurrent layer, and a transcription layer. The convolutional layer is used to extract image features of the sample image to obtain a feature map. The recurrent layer converts the feature map into sequence features. The transcription layer converts the sequence features into a predicted character sequence.

[0098] Step S1084: Use the fourth training sample set to train the initial text recognition model, and construct a CTC loss to iteratively update the parameters to obtain an intermediate text recognition model.

[0099] Step S1085: adding a post-processing module to the intermediate text recognition model to convert the predicted character sequence into text to obtain a text recognition model.

[0100] In some embodiments, the text recognition model is an Attention OCR text recognition model.

[0101] In step S109, the recognized text is directly structured and saved according to the format of the bill template and output in JSON format.

[0102] On the other hand, the present invention also provides a bill recognition device based on text detection and template matching, comprising a processor, a memory, and a computer program / instructions stored in the memory, wherein the processor is used to execute the computer program / instructions, and when the computer program / instructions are executed, the device implements the steps of the above method.

[0103] On the other hand, the present invention also provides a computer-readable storage medium having a computer program / instruction stored thereon, which implements the steps of the above method when the computer program / instruction is executed by a processor.

[0104] On the other hand, the present invention also provides a computer program product, comprising a computer program / instruction, characterized in that the computer program / instruction implements the steps of the above method when executed by a processor.

[0105] The present invention is described below in conjunction with a specific embodiment:

[0106] This embodiment aims to solve the challenges encountered in the direction, text detection and recognition process of VAT invoice images. By using technologies such as image direction correction, region detection, text detection, template matching and perspective transformation, an efficient and accurate VAT invoice recognition method is provided, and finally the structured extraction of invoice content is achieved. Figure 1 As shown, the basic principles are as follows:

[0107] 1. The input image is first passed through the image direction recognition model to obtain a direction-corrected image, avoiding inaccurate text detection due to deviations in the image direction. If the image of the invoice is positive, then all key text lines on the invoice are positive, so the direction of the invoice as a whole is recognized, that is, there is no need to recognize the direction of individual text lines. The image direction correction model uses the ResNet18 network structure, and the loss function uses L1-loss. However, since the angle is periodic, 0 degrees and 360 degrees are the same angle. In order to avoid this problem, a three-step phase is used. The three-step phase shift formula is:

[0108] Three-step phase shift: x 1 =cos(θ);x 2=cos(θ+120°); x 3 =cos(θ+240°).

[0109] That is, an angle is represented by three cosine values. In order to fit the three cosine values, the output of the last hidden layer of the model is The predicted value of the model is calculated by the following formula:

[0110]

[0111] Final calculation The loss is calculated with the cosine encoding values ​​of the three angles. The training is performed using a stochastic gradient descent optimizer with a learning rate of 0.003, a momentum of 0.9, and a weight decay of 0.00005.

[0112] 2. Invoice area detection: In actual business scenarios, invoice images may be scanned, electronic, or photographed. Therefore, there are non-invoice areas in the image. If it is directly used for text detection, it will affect the model effect. Therefore, a deep learning model is needed to detect the invoice area of ​​the image.

[0113] 3. Text box detection: Through the deep learning model, the text box in the invoice area can be obtained.

[0114] 4. Design invoice template: The size of the designed invoice template is 2400*1400. It is known that the content of VAT general invoice, special invoice and electronic invoice consists of two parts, one is the key such as "invoice number", "name", "taxpayer identification number", and the other is the value such as "*** Co., Ltd.". The relative position of the key in the invoice relative to the invoice area is fixed, and the value needs to be printed in the invoice. Therefore, the relative position of the value relative to the invoice area is not fixed, but the basic difference is very small. And the relative position between the values ​​is also fixed. Therefore, different invoice templates are designed according to different VAT invoice types, and the coordinate points of all keys and values ​​are marked in the template.

[0115] 5. Perspective transformation of invoice area and text box: In the photo and scanning scenarios, the invoice area in the image is generally not rectangular and may have curved lines. Therefore, it is necessary to perform perspective transformation on the detected invoice area based on the invoice template to obtain a transformed image of the standard invoice template size. At the same time, the identified text box also uses the same transformation matrix as the invoice perspective transformation to obtain the transformed text box coordinates.

[0116] 6. Key point coordinates match template coordinates:

[0117] 6.1 Since both the invoice area and the text box have undergone perspective transformation, the coordinates of the transformed text box and the text content in the invoice template will overlap. Since the original invoice area is curved, the transformed text may not completely overlap with the text content in the template. Therefore, it is necessary to calculate the overlap ratio of the two areas. That is, calculate the percentage of the intersection of the two areas to the text content of the template. The text content box in the template may overlap with multiple text boxes, so all the overlapping ratios are traversed and calculated to obtain the text box with the largest overlap. It can be considered that the transformed text box and the text box in the template have the same value.

[0118] 6.2 Reference Figure 2 Since the accuracy of text detection cannot be guaranteed to be 100%, there are some text boxes in the image that cannot be detected. Therefore, the processing logic is to first traverse all the logic in 6.1, and traverse all the text boxes in the template to find the corresponding converted text boxes. If there are text boxes that are not found. Extract them separately for processing. As described in step 4, the relative positions of the text contents of all keys and values ​​in the template are fixed, and after the image is transformed through perspective, the relative positions of the text contents of keys and values ​​will basically conform to the relative position logic of the template. Therefore, for the corresponding text boxes in the template and the transformed image, the offset between the template text box and the transformed text box. According to the offset between the template text box and the text box with the unfinished corresponding value, the coordinates of the undetected text box in the perspective transformed image of the text box with the unfinished corresponding value are obtained.

[0119] 7. Coordinate inverse mapping: After the operation in step 6, the text boxes of all texts in all perspective transformations can be obtained. Through coordinate inverse mapping, the text boxes in all original images can be obtained.

[0120] 8. Input all text box contents into the text recognition model to obtain the text content of each text box.

[0121] 9. Because in step 6, we already know which text box each text box corresponds to in the template. Therefore, once we get the value of each text box, we can directly structure it and output json structured data.

[0122] Detailed description of the technical solution:

[0123] In step 1, the image is used to identify and correct the image direction using a deep learning model. In order to ensure the correct direction of the input image and avoid inaccurate text detection due to direction deviation, the ResNet18 network structure is used to identify and correct the image direction. The L1-loss is used as the loss function to train the ResNet18 network. During the training process, stochastic gradient descent (SGD) is used, with a learning rate of 0.003, a momentum of 0.9, and a weight decay of 0.00005. At the same time, in order to deal with the periodicity of the angle, the three-step phase method is used to represent the angle with three cosine values.

[0124] Specifically, the input image passes through the ResNet18 model, and the last hidden layer output of the model is The predicted value of the model is calculated by the formula: calculate The L1-loss of the cosine encoding values ​​of the three angles is used to optimize the model parameters.

[0125] In step 2, in order to prevent non-invoice areas from interfering with text detection, a deep learning model is used to detect the invoice area in the image. Object detection models such as R-CNN and YOLO are used, which have high detection accuracy and speed. Training data containing multiple invoice types is used to mark the invoice area and train the object detection model.

[0126] In step 3, after obtaining the invoice area, text box detection is performed on the invoice area, and text detection models such as EAST and CTPN are used to accurately locate the text box. During the training process, the text box position in the invoice area is marked in the training data for training the text detection model.

[0127] In step 4, design the invoice template. According to different types of VAT invoices (general invoices, special invoices, electronic invoices), design the corresponding invoice templates with a template size of 2400*1400. Mark the relative coordinate positions of the key (such as "invoice number", "name", "taxpayer identification number") and value (such as "*** Co., Ltd.") in the invoice. Save the templates of different types of invoices in JSON format for subsequent calls.

[0128] In step 5, the perspective transformation of the invoice area and the text box is performed. In order to transform the detected invoice area into an image of the standard template size, a perspective transformation is performed. Specifically, the cv2.getPerspectiveTransform and cv2.warpPerspective functions of OpenCV are used to calculate the transformation matrix and apply the transformation. The same transformation matrix is ​​applied to the identified text box to obtain the transformed text box coordinates.

[0129] In step 6, the key point coordinates of the text box in the image are matched with the template coordinates, and the text box in the template is matched through the perspective transformation of the text box in the image. The overlap ratio between the transformed text box and the corresponding text box in the template is calculated, and the text box with the largest overlap is selected as the matching result. For the text box that failed to match, its position in the original image is inferred based on the position relationship of the matched text box, and a second match is performed.

[0130] In step 7, all perspective-transformed text box coordinates are inversely mapped back to the original image coordinates, including the text boxes detected in step 4 and the text boxes determined based on the text box position relationship in step 6. Specifically, the inverse transformation matrix is ​​calculated and the inverse transformation is applied using OpenCV's cv2.getPerspectiveTransform and cv2.warpPerspective functions.

[0131] In step 8, text recognition is performed on the text boxes in the image, and the contents of all text boxes are input into the text recognition model to obtain the text content of each text box. Specifically, text recognition models such as CRNN and Attention OCR are used. These models are trained using labeled invoice text data.

[0132] In step 9, the recognized text content is converted into structured data output. According to the matching results, the key and value relationship in the template is used to structure the value of each text box and output it as JSON format data for subsequent processing and storage.

[0133] In this embodiment, the overall direction correction is first performed through the ResNet18 model when the image is input, which avoids the error accumulation of direction recognition of text lines line by line and improves the overall accuracy of image direction correction. Invoice area detection is performed before text detection to eliminate non-invoice areas, thereby ensuring the accuracy and efficiency of subsequent text detection. By designing a fixed template and using perspective transformation to standardize the invoice image, it is ensured that the key points and text boxes on all invoices are consistent with the template. In this way, even if there is an angle deviation or curvature in the invoice image, it can be corrected by perspective transformation to ensure the accurate positioning and recognition of the invoice content. This is not available in the prior art. By directly matching the key point coordinates with the template coordinates and calculating the percentage of the overlapping area, it is ensured that the key-value pairs in the text box and the template are accurately matched, reducing the probability of misidentification and missed recognition. By performing perspective transformation and inverse mapping on all text boxes, the position of the text box in the original image can be accurately located. This method ensures the accurate position of the recognized text content in the original image, which is conducive to subsequent text recognition and data structuring.

[0134] Corresponding to the above method, the present invention also provides an apparatus / system, which includes a computer device, the computer device includes a processor and a memory, the memory stores computer instructions, the processor is used to execute the computer instructions stored in the memory, and when the computer instructions are executed by the processor, the apparatus / system implements the steps of the method described above.

[0135] The embodiment of the present invention also provides a computer-readable storage medium on which a computer program is stored, and when the computer program is executed by a processor, the steps of the aforementioned edge computing server deployment method are implemented. The computer-readable storage medium can be a tangible storage medium, such as a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a floppy disk, a hard disk, a removable storage disk, a CD-ROM, or any other form of storage medium known in the technical field.

[0136] In summary, the bill recognition method and device based on text detection and template matching described in the present invention firstly performs angle recognition correction on the bill image based on the image direction recognition correction model to avoid inaccurate text detection caused by image direction deviation; then, the position of the bill in the middle is recognized by the bill area detection model, and the position of the text box is recognized by the text area detection model; the bill image is made consistent with the size and shape of the bill template through perspective transformation, and the recognized text boxes in the bill image are matched with each text box in the standard format in the bill template by calculating the overlap ratio of the text boxes; then, according to the position deviation between the bill image and the text box in the standard format in the bill template, based on the inverse mapping transformation, the remaining text boxes that the text area detection model failed to detect are found, ensuring that all text boxes corresponding to the text in the bill are fully detected to prevent omissions. Then, the text content in the text box is extracted by the text recognition model, and directly structured in the form of the bill template, so as to realize efficient and accurate recognition of the bill.

[0137] It should be understood by those skilled in the art that the exemplary components, systems and methods described in conjunction with the embodiments disclosed herein can be implemented in hardware, software or a combination of the two. Whether it is performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention. When implemented in hardware, it can be, for example, an electronic circuit, an application specific integrated circuit (ASIC), appropriate firmware, a plug-in, a function card, etc. When implemented in software, the elements of the present invention are programs or code segments used to perform the required tasks. The program or code segment can be stored in a machine-readable medium, or transmitted on a transmission medium or a communication link via a data signal carried in a carrier.

[0138] It should be clear that the present invention is not limited to the specific configuration and processing described above and shown in the figures. For the sake of simplicity, a detailed description of the known method is omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present invention is not limited to the specific steps described and shown, and those skilled in the art can make various changes, modifications and additions, or change the order between the steps after understanding the spirit of the present invention.

[0139] In the present invention, features described and / or illustrated for one embodiment may be used in the same or similar manner in one or more other embodiments, and / or combined with features of other embodiments or replace features of other embodiments.

[0140] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A bill recognition method based on text detection and template matching, characterized in that: The method comprises the following steps: Obtain a bill image to be processed, input a pre-trained image direction recognition and correction model, identify the angle of the bill image and perform compensation adjustment; Input the bill image after angle compensation adjustment into a pre-trained bill area detection model, and output the location of each bill area; Input each bill area into the pre-trained text area detection model and output the location of the localized text box; Obtain a preloaded bill template, the bill template is set according to a standard size, the bill template marks multiple template text boxes corresponding to key items in the bill and the values ​​of the key items, and saves it in JSON format; Performing perspective transformation on the position of the bill area detected in the bill image so as to match the size and shape of the bill template and then aligning it; Calculate the overlap ratio between each first-category candidate text box and each template text box in the bill image after perspective transformation, and associate and match each first-category candidate text box with the template text box with the highest overlap ratio; wherein, according to the offset between the text box corresponding to the key item and its value in the bill template and the first-category text box, use the first-category candidate text box that has completed the association matching to search and determine the second-category candidate text box that has not been detected by the text area detection model; Performing an inverse mapping transformation on the first category candidate text boxes and the second category candidate text boxes according to the perspective transformation to obtain all target text boxes in the original bill image; Input the content of each target text box into the pre-trained text recognition model to obtain the text content of each target text box; According to the correspondence between each template text box in the bill template and the target text box in the original bill image, the text content of the target text box is directly saved in a structured manner and output in a JSON format.

2. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: The pre-training step of the image direction recognition and correction model includes: Obtain a first training sample set, wherein the first training sample set includes a plurality of samples, each sample includes a bill sample image, and the angle of the bill image is marked as a label using a three-step phase method; the three-step phase method represents an angle using three cosine values; Obtain an initial image direction recognition model, wherein the initial image direction recognition model adopts a ResNet18 network, takes the bill sample image as input and outputs three fitting values ​​from the last hidden layer, and outputs an angle prediction value of the bill sample image through an activation function; The initial image direction recognition model is trained using the first training sample set, an L1 loss is constructed using the angle prediction value and the label, and parameters are iteratively updated based on a stochastic gradient optimizer to obtain an image direction recognition model; The image direction recognition model is connected to an angle adjustment module to obtain the image direction recognition correction model, and the angle adjustment module is used to perform direction correction according to the angle prediction value output by the image direction recognition model.

3. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: The pre-training steps of the bill area detection model include: Obtain a second training sample set, wherein the second training sample set includes a plurality of samples, each sample includes a picture and marks a position of a bill area in the picture as a label; Obtain an initial bill area detection model, wherein the initial bill area detection model adopts an R-CNN or YOLO target detection model; The initial bill area detection model is trained using the second training sample set, and a cross entropy loss function is constructed to iteratively update parameters to obtain the bill area detection model.

4. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: The pre-training steps of the text region detection model include: Obtain a third training sample set, wherein the third training sample set includes multiple samples, each sample includes a bill image and a position of a marked text as a label; Obtaining an initial text region detection model, wherein the initial text region detection model adopts an efficient and accurate scene text detection network model or a connectionist text proposal network model; The initial text region detection model is trained using the third training sample set, a cross entropy loss function is constructed to iteratively update parameters, and the text region detection model is obtained.

5. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: Performing perspective transformation on the position of the bill area detected in the bill image so as to match the size of the bill template includes: Locating the corner points of the bill area in the bill image as source point coordinates, and locating the corner points of the bill template as target point coordinates; Use OpenCV's cv2.getPerspectiveTransform function to calculate the perspective transformation matrix from the source point coordinates to the target point coordinates; Based on the perspective transformation matrix, the cv2.warpPerspective function of OpenCV is used to perform perspective transformation on the bill area in the bill image.

6. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: The pre-training steps of the text recognition model include: Obtain a fourth training sample set, wherein the fourth training sample set includes a plurality of samples, each sample includes a sample image containing text and marks the text string contained therein as a label; Performing image preprocessing on the sample images in the fourth training sample set, including adjusting image size, graying and normalizing; converting the text string into a character sequence, and creating a character-to-index mapping; Acquire an initial text recognition model, the initial text recognition model comprising a convolution layer, a recurrent layer and a transcription layer, the convolution layer is used to extract image features of the sample image to obtain a feature map, the recurrent layer converts the feature map into sequence features, and the transcription layer converts the sequence features into a predicted character sequence; The initial text recognition model is trained using the fourth training sample set, and a CTC loss is constructed to iteratively update parameters to obtain an intermediate text recognition model; A post-processing module is added to the intermediate text recognition model to convert the predicted character sequence into text to obtain the text recognition model.

7. The bill recognition method based on text detection and template matching according to claim 1 is characterized in that: The text recognition model is an Attention OCR text recognition model.

8. A bill recognition device based on text detection and template matching, comprising a processor, a memory and a computer program / instruction stored in the memory, characterized in that: The processor is used to execute the computer program / instructions. When the computer program / instructions are executed, the device implements the steps of the method according to any one of claims 1 to 7.

9. A computer-readable storage medium having a computer program / instruction stored thereon, characterized in that: When the computer program / instructions are executed by a processor, the steps of the method as claimed in any one of claims 1 to 7 are implemented.

10. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Taxi ticket recognition method, device and equipment and medium

    CN114511868A

  • Method for identifying information of bills with labels

    CN116052197A

  • Information extraction method and device of bill text, equipment and storage medium

    CN116110066A

  • End-to-end recognition method for scene text in any shape

    WO2019192397A1

Cited By

  • Step-by-step refinement type automatic weight correction method for oracle bone copy image

    CN119763132A

  • Receipt signature position and angle intelligent identification method and device based on deep learning

    CN120411978A

  • Method and device for intelligent recognition of receipt signature position and angle based on deep learning

    CN120411978B

  • Label text recognition and defect detection method and device based on image alignment

    CN122312638A