Text area detection method, device, computer equipment and storage medium
By using matching reference images and template images for text area detection, the problem of inaccurate detection caused by image deformation, blur, and misalignment is solved, and accurate positioning and extraction of text areas are achieved.
Patent Information
- Application Number
- CN202011533689.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-22
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2040-12-22
AI Technical Summary
Existing text area detection methods are inaccurate when the image is deformed, blurred, misaligned, etc., and it is easy to mistake adjacent text for the target field.
By obtaining the reference image and template image that match the image to be detected, the area where the text in the template image is located is marked, the image to be detected, the reference image and the template image are input into the text area detection model for processing, and technologies such as convolutional neural networks are used to perform feature analysis. Extraction and spatial transformation to locate the target text area.
It effectively avoids false detection of adjacent characters, improves the accuracy of text area detection, and can accurately extract the target text area under problems such as deformation, blur, and misalignment.
Smart Images

Figure CN112613402B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a text region detection method, apparatus, computer equipment and storage medium. Background Art
[0002] In company operations, various documents or certificates are usually entered into the system. When the number is large, manual entry will consume a lot of human resources and reduce the company's efficiency. Image recognition can automatically enter this information into the system to improve the efficiency of the company's financial reimbursement. To identify a type of data, we first need to extract the target text area in the image and then perform text recognition. Traditional methods, such as CTPN (Connectionist Text Proposal Network) and pixel-link, can detect text areas in images. However, the current text area detection method has the problem of inaccurate text area detection when the image is severely deformed. For example, in reality, there will be textures in the image, image distortion, text misalignment, etc. Summary of the invention
[0003] Based on this, it is necessary to provide a text area detection method, device, computer equipment and storage medium to address the above technical problems.
[0004] A text area detection method, the method comprising:
[0005] Acquire an image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; annotate the template image with an area where the text corresponding to the reference image is located;
[0006] The image to be detected, the reference image and the template image are input into a text area detection model for detection processing to obtain a target text area corresponding to the image to be detected; the text area detection model is trained based on sample reference images, sample template images, sample images to be detected and corresponding sample labels.
[0007] A training method for a text region detection model, the method comprising:
[0008] Acquire a sample image to be detected, a sample reference image matching the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image; annotate the sample template image with an area where the text corresponding to the sample reference image is located;
[0009] The sample image to be detected, the sample reference image and the sample template image are input into the text area detection model to be trained for training, and the model parameters in the text area detection model to be trained are adjusted according to the text positioning result corresponding to the sample image to be detected and the corresponding label value until a trained text area detection model is obtained.
[0010] A text area detection device, the device comprising:
[0011] An image acquisition module, used to acquire an image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; annotating the area where the text corresponding to the reference image is located in the template image;
[0012] The image detection module is used to input the image to be detected, the reference image and the template image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; the text area detection model is trained based on the sample reference image, the sample template image and the sample image to be detected and the corresponding sample labels.
[0013] A training device for a text region detection model, the device comprising:
[0014] A sample image acquisition module is used to acquire a sample image to be detected, a sample reference image matching the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image; an area in the sample template image where the text corresponding to the sample reference image is marked;
[0015] The sample image detection module is used to input the sample image to be detected, the sample reference image and the sample template image into the text area detection model to be trained, and adjust the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the image to be detected and the corresponding label value until a trained text area detection model is obtained.
[0016] A computer device includes a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps in each method embodiment when executing the computer program.
[0017] A computer-readable storage medium stores a computer program, which implements the steps in various method embodiments when executed by a processor.
[0018] The above-mentioned text area detection method, device, computer equipment and storage medium, since the image to be detected is generally an image taken by a user, and the image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a reference image that matches the content type of the image to be detected and a template image corresponding to the reference image, and the template image marks the area where the text corresponding to the reference image is located; the image to be detected, the reference image and the template image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; that is, based on a reference image that matches a more standardized content type and the corresponding template image and the image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for the image to be detected that has problems such as deformation, blur, and misalignment. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 A diagram showing an application environment of a text region detection method in an embodiment;
[0020] Figure 2 is a flowchart of a text area detection method in one embodiment;
[0021] Figure 3 is a schematic diagram of an image to be detected in one embodiment;
[0022] Figure 4 is a schematic diagram of a reference image in one embodiment;
[0023] Figure 5 is a schematic diagram of a template image in one embodiment;
[0024] Figure 6 is a schematic diagram of an image to be detected containing a target text area in one embodiment;
[0025] Figure 7 A schematic diagram of a process of obtaining a target text area in one embodiment;
[0026] Figure 8 is a schematic structural diagram of a first feature extraction unit in an embodiment;
[0027] Fig. 9 is a schematic structural diagram of a second feature extraction unit in one embodiment;
[0028] Fig.10 is an architecture diagram of a text region detection model in one embodiment;
[0029] Fig.11 is a flowchart of a text area detection method in another embodiment;
[0030] Fig.12 A schematic diagram of a flow chart of a method for training a text region detection model in one embodiment;
[0031] Fig.13 is a structural block diagram of a text area detection device in one embodiment;
[0032] Fig.14 is a structural block diagram of a training device for a text region detection model in one embodiment;
[0033] Fig.15 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0034] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0035] The text area detection method provided in this application can be applied to Figure 1 In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The terminal 102 is used to obtain the image to be detected. The reference image matching the content type of the image to be detected and the template image corresponding to the reference image can be obtained by the terminal 102, and can also be directly obtained by the server 104. The server 104 obtains the image to be detected from the terminal 102, obtains the reference image matching the content type of the image to be detected and the template image corresponding to the reference image, inputs the image to be detected, the reference image and the template image into the text area detection model for detection processing, and obtains the target text area corresponding to the image to be detected. Among them, the terminal 102 can be but not limited to various personal computers, laptops, smart phones, tablet computers and portable wearable devices, and the server 104 can be implemented with an independent server or a server cluster composed of multiple servers.
[0036] In one embodiment, Figure 2 As shown, a text area detection method is provided. This embodiment takes the method applied to a server as an example for illustration. It can be understood that the method can also be applied to a server, including the following steps:
[0037] Step 202: obtain an image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; and mark the area where the text corresponding to the reference image is located in the template image.
[0038] The image to be detected may be an image with a specific structure, such as a bill image, a certificate image, a card image, etc. A bill image may be, for example, an invoice image, a ticket image, etc. A certificate image may be, for example, an ID card image, a Hong Kong and Macau Pass image, etc. A card image may specifically be a bank card image, etc. Figure 3 FIG. 1 is a schematic diagram of an image to be detected in an embodiment. Figure 3 The image to be detected in the figure is a high-speed rail ticket image. It includes the logo "J054196", ticket gate "2", departure station "Xinxiangdong", train number "G1234", destination station "Wuhan Station", date "2019-05-30 14:09 departure", train number "10 car 06D", price "¥272.0 yuan", online signature, ID number "5432112345****1234", name "Li Si", QR code, and slogan "Please go to 12345 to buy tickets and 5432 to ship. The railway wishes you a pleasant journey."
[0039] Content type matching means that if the image to be detected is an invoice image, then the reference image is also an invoice image; if the image to be detected is a ticket image, then the reference image is also a ticket image. The reference image is an image that meets the processing size requirements and is clearer and more standardized than the image to be detected. Figure 4 As shown, it is a schematic diagram of a reference image in an embodiment. The reference image is also a ticket image. It includes the logo "J054196", ticket gate "2", departure station "Dezhoudong", train number "G1267", destination station "Jinanxi", date "May 23, 2019, 17:09", train number "02 car 17C", price "¥39.5 yuan", online signature, ID number "12345612345****1234", name "Zhang San", QR code, and slogan "Please go to 12345 to buy tickets and 5432 to ship. The railway wishes you a pleasant journey."
[0040] The template image is used to mark the area where the text is located corresponding to the reference image. The specific marking method can be to set the pixel value of the area corresponding to the text area corresponding to the reference image to 1, and set the other pixel values to 0. The template image is used to represent the field to be extracted from the image to be detected. Figure 5 FIG. 1 is a schematic diagram of a template image in an embodiment. Figure 5 The template image in the text area can be adjusted as needed. Figure 5 Marked in Figure 4 The destination station is "Jinan West Station", the date is "2019-05-23 17:09", the seat number is "02 car 17C", and the seat number is "second class seat". It is understandable that Figure 5You can also mark information such as ID number, name, high-speed train number, and departure station.
[0041] Specifically, the server may obtain the image to be detected from the terminal, and obtain a reference image matching the content type of the image to be detected and a template image corresponding to the reference image. The template image marks the area where the text corresponding to the reference image is located.
[0042] Step 204, input the image to be detected, the reference image and the template image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; the text area detection model is trained based on the sample reference image, the sample template image and the sample image to be detected and the corresponding sample labels.
[0043] The text area detection model may be trained based on a convolutional neural network model. Specifically, it may be trained with a sample reference image, a sample template image, a sample image to be detected, and corresponding sample labels. The target text area detection may be to extract the minimum circumscribed rectangular box of the text in the image to be detected. The target text area may be displayed in the form of a text box, or may be presented in the form of the coordinates of the target text area in the image to be detected.
[0044] Specifically, the server inputs the image to be detected, the reference image and the template image into the text region detection model for detection processing to obtain the target text region corresponding to the image to be detected. The target text region matches the text region corresponding to the reference image. Figure 6 FIG. 1 is a schematic diagram of an image to be detected containing a target text area in one embodiment. Figure 6 The area corresponding to the text box is the target text area. And, Figure 5 The terminal station is "Jinan West Station", the date is "May 23, 2019", the seat number is "Car 32, No. 17C", and the seat number is "Second Class Seat". Figure 6 The resulting text boxes are also the text boxes corresponding to the terminal station "Wuhan Station", the date "May 30, 2018, 14:09 departure", the seat number "Car 10, No. 06D" and the seat number "Second Class Seat".
[0045] In this embodiment, the server inputs the image to be detected, the reference image and the template image into the text area detection model for detection processing, obtains the pixel value of each pixel in the image to be detected, and obtains the target text area corresponding to the image to be detected based on the pixel value of each pixel.
[0046] The above-mentioned text area detection method, since the image to be detected is generally an image taken by a user, and the image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a reference image that matches the content type of the image to be detected and a template image corresponding to the reference image, and the template image marks the area where the text corresponding to the reference image is located; the image to be detected, the reference image and the template image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; that is, based on a reference image that matches a more standardized content type and the corresponding template image and the image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for images to be detected that have problems such as deformation, blur, and misalignment.
[0047] In one embodiment, the image to be detected, the reference image and the template image are input into a text area detection model for detection processing to obtain a target text area corresponding to the image to be detected, including: graying the reference image, merging the grayed reference image and the template image into a two-channel image, inputting the image to be detected and the two-channel image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected.
[0048] Among them, a two-channel image refers to an image with two channels for one pixel. For example, at the position of pixel (0, 0), the pixel value of (0, 0) in the image to be detected is 0.2, and the pixel value of (0, 0) in the template image is 0, then the value of (0, 0) in the two-channel image is [0.2, 0].
[0049] Specifically, the server grayscales the reference image and merges the grayscale reference image and the template image into a two-channel image. The server inputs the image to be detected and the two-channel image into the text region detection model for detection processing to obtain the target text region corresponding to the image to be detected.
[0050] In this embodiment, the server inputs the two-channel image and the image to be detected after scale adjustment into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; scale adjustment is used to adjust the image to be detected and the two-channel image to matching sizes.
[0051] In this embodiment, when it is detected that the direction of the image to be detected is inconsistent with the reference image, the direction of the image to be detected is corrected, and the image to be detected and the two-channel image after the direction correction are input into the text area detection model for detection processing.
[0052] The text area detection method in this embodiment grayscales the reference image, and merges the grayscaled reference image and the template image into a two-channel image, which can fully integrate the pixel information of the reference image and the template image; the image to be detected and the two-channel image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected. Compared with the method of directly inputting the annotation and the image to be detected into the model for detection, the accuracy of text area detection of the image to be detected can be improved.
[0053] In one embodiment, Figure 7 The figure is a schematic diagram of a process of obtaining a target text area in one embodiment. The image to be detected and the two-channel image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected, including:
[0054] Step 702: Perform feature extraction processing on the two-channel image to obtain a first feature map.
[0055] Specifically, the server uses a convolution layer and a pooling layer to perform feature extraction processing on the two-channel image to obtain a first feature map. A feature map refers to a multi-dimensional matrix, and multi-dimensional means more than one dimension.
[0056] Step 704: perform feature extraction processing on the image to be detected to obtain a second feature map.
[0057] Specifically, the server performs the same feature extraction processing on the image to be detected as on the two-channel image to obtain a second feature map.
[0058] Step 706: Based on the first feature map and the second feature map, a matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix.
[0059] Specifically, the server obtains matrix calculation parameters; performs a merge process based on the first feature map and the second feature map, performs matrix calculation on the merged feature map according to the matrix calculation parameters, and obtains a spatial transformation matrix. For example, the first feature map and the second feature map are merged to obtain a feature map F, and F*M+B is calculated on it to obtain a spatial transformation matrix. Among them, M and B are trainable matrix parameters, M is a two-dimensional matrix whose initial parameters are Gaussian distribution, and the initial parameters of B are [1,0,0; 0,1,0; 0,0,1].
[0060] Step 708: perform spatial transformation on the first feature map according to the spatial transformation matrix, merge the second feature map and the first feature map after the spatial transformation, and obtain a third feature map.
[0061] Specifically, the server performs spatial transformation on the first feature map according to the spatial transformation matrix, and merges the second feature map with the first feature map after the spatial transformation to obtain a third feature map.
[0062] Step 710: perform feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected.
[0063] Specifically, the server performs a series of feature extraction and merging processes on the third feature map to obtain a target text area corresponding to the image to be detected.
[0064] The text area detection method in this embodiment performs feature extraction processing on the two-channel image and the image to be detected respectively to obtain a first feature map and a second feature map, and merges the first feature map and the second feature map, performs matrix calculation on the merged feature map to obtain a spatial transformation matrix, performs spatial transformation on the first feature map according to the spatial transformation matrix, merges the second feature map and the first feature map after spatial transformation to obtain a third feature map, and then performs a series of feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected, that is, the two-channel image and the image to be detected are first subjected to feature extraction processing, and then subjected to spatial transformation processing, that is, spatial mapping, etc., and the obtained results are subjected to feature extraction processing, and the structure of the text area detection model is fully utilized to obtain the target text area corresponding to the image to be detected.
[0065] In one embodiment, feature extraction processing is performed on a two-channel image to obtain a first feature map, including: inputting the two-channel image into a first feature extraction unit to obtain the first feature map; the first feature extraction unit includes a convolution layer and a pooling layer connected in sequence.
[0066] Performing feature extraction processing on the image to be detected to obtain a second feature map includes: inputting the image to be detected into a first feature extraction unit to obtain the second feature map.
[0067] Based on merging the first feature map and the second feature map, matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix, including: merging the output of the first feature map after being input into the second feature extraction unit with the output of the second feature map after being input into the second feature extraction unit, performing matrix calculation on the merged feature map to obtain a spatial transformation matrix; the second feature extraction unit includes a feature map size adjustment layer, a convolution layer, a pooling layer, a vector flattening layer and a fully connected layer connected in sequence.
[0068] Among them, Figure 8 FIG. 1 is a schematic diagram of the structure of a first feature extraction unit in an embodiment. The number of convolutional layers and the number of pooling layers are not limited. Figure 8 Taking two convolutional layers and one pooling layer as an example, the first feature extraction unit includes a convolutional layer and a maximum pooling layer connected in sequence. The convolution kernel size of the convolutional layer is 3*3, and the convolution kernel size of the maximum pooling layer is 2*2 with a step size of 2.
[0069] like Fig. 9 , which is a schematic diagram of the structure of the second feature extraction unit in one embodiment. The second feature extraction unit includes a feature map resizing layer (Resize), a convolution layer (conv), a pooling layer (Maxpool, the convolution layer size is 2*2, and the step size is 2), a vector flattening layer (flatten) and a fully connected layer (Fc) connected in sequence. The feature map resizing layer is used to change the size of the feature map. The convolution layer and the pooling layer are used for feature extraction. The vector flattening layer is used to dimensionalize multidimensional features. The function of the fully connected layer is to map the learned features to the sample labeling space.
[0070] Specifically, the server inputs the two-channel image into the sequentially connected convolutional layer and pooling layer, and outputs the first feature map, which is the feature map of thinking (b, h, w, c). The server inputs the image to be detected into the sequentially connected feature map resizing layer, convolutional layer, pooling layer, vector flattening layer and fully connected layer and then outputs it. Similarly, the server inputs the second feature map into the sequentially connected feature map resizing layer, convolutional layer, pooling layer, vector flattening layer and fully connected layer and then outputs it. The two outputs are merged, and the matrix calculation is performed on the merged feature map to obtain the spatial transformation matrix; the merged feature map is a two-dimensional feature map.
[0071] The text area detection method in this embodiment inputs a two-channel image into a first feature extraction unit to obtain a four-dimensional first feature map, inputs the image to be detected into the first feature extraction unit to obtain a second feature map of the same dimension, merges the output of the first feature map after being input into the second feature extraction unit with the output of the second feature map after being input into the second feature extraction unit, performs matrix calculation on the merged feature map, and obtains a spatial transformation matrix, so that the two-channel image and the image to be detected can be mapped, thereby accurately extracting the target text area.
[0072] In one embodiment, feature extraction processing is performed on the third feature map to obtain a target text area corresponding to the image to be detected, including: inputting the third feature map into a first feature extraction unit to obtain a fourth feature map; inputting the fourth feature map into a convolutional layer, using the output of the convolutional layer as an input to an upsampling layer, and obtaining a fifth feature map output by the upsampling layer; and merging the fourth feature map and the fifth feature map to obtain the target text area corresponding to the image to be detected.
[0073] Among them, the upsampling layer is used to sample the image to a higher pixel size.
[0074] Specifically, the server inputs the third feature map into the first feature extraction unit to obtain a fourth feature map. The server inputs the fourth feature map into the convolution layer, uses the output of the convolution layer as the input of the upsampling layer, and obtains a fifth feature map output by the upsampling layer. The server merges the fourth feature map and the fifth feature map to obtain the target text area corresponding to the image to be detected.
[0075] The text area detection method in this embodiment inputs the third feature map into the first feature extraction unit to obtain a fourth feature map, inputs the fourth feature map into the convolution layer, uses the output of the convolution layer as the input of the upsampling layer, obtains the fifth feature map output by the upsampling layer, merges the fourth feature map and the fifth feature map, and obtains the target text area corresponding to the image to be detected, that is, the size of the image is compressed during the convolution pooling process, and then passes through the convolution layer and the upsampling layer to expand the receptive field.
[0076] In one embodiment, the text region detection method further includes: performing text recognition on the text images in the target text regions to obtain the characters in each target text region.
[0077] In one embodiment, the target text region matches the region where the text is marked in the template image. The text region detection method further includes: performing image segmentation on the image to be detected based on each target text region to obtain a text image set; performing text recognition on each text image in the text image set to obtain the text in each target text region.
[0078] The text image refers to an image containing only part of the text. The text image set contains at least one text image. The target text area matches the area where the text is marked in the template image, which means that when the area where the text is marked in the template image is the "area where the date is located", the output target text area is also the "area where the date is located".
[0079] Specifically, the server performs image segmentation on the image to be detected based on each target text area to obtain a text image set. The server performs text recognition on each text image in the text image set to obtain the text in each target text area. Figure 6 For example, Figure 6 The area corresponding to the text box is the target text area. Figure 6 The obtained text boxes are also the text boxes corresponding to the terminal station, date, seat number and seat number respectively. Then the text image set includes the text image of "Wuhan Station", the text image of "Departure at 14:09 on May 30, 2019", the text image of "Car No. 06D 10" and the text image of "Second Class Seat". Then, based on the text images, text recognition is performed, and the texts "Wuhan Station", "Departure at 14:09 on May 30, 2019", "Car No. 06D 10" and "Second Class Seat" are obtained.
[0080] The text area detection method in this embodiment, since the target text area matches the area where the text is marked in the template image, can detect different target text areas by setting the area where the text is marked in the template image, and perform image segmentation on the image to be detected based on each target text area to obtain a text image set; perform text recognition on each text image in the text image set to obtain the text in each target text area, and can extract the text in different target text areas, thereby improving the usability of the model.
[0081] In one embodiment, Fig.10 , which is an architecture diagram of a text region detection model in an embodiment, wherein conv is a convolution layer, 7*7 represents the convolution kernel size, S:2 represents a step size of 2, and d represents a dilation coefficient of the spatial convolution.
[0082] The input image to be detected is convolved once (convolution kernel size (7*7), step size is 2) and then processed by the first feature extraction unit three times to reduce the size of the feature map to 1 / 16 of the original size. The two-channel image undergoes the same operation.
[0083] Spatial transformation module: After a series of convolution sampling operations, the size of the feature map is changed to a fixed size, and then convolution and downsampling operations are performed. The obtained feature map is stretched into a one-dimensional vector and input into the fully connected layer. The template branch is also operated in this way, and the two feature maps are merged (concat) to obtain the feature map F. F*M+B is calculated on it, where M and B are trainable matrix parameters, M is a two-dimensional matrix whose initial parameters are Gaussian distribution, and the initial parameters of B are [1,0,0; 0,1,0; 0,0,1] to obtain the spatial transformation matrix. The output after the first feature map is input to the second feature extraction unit is merged with the output after the second feature map is input to the second feature extraction unit, and the matrix calculation is performed on the merged feature map to obtain the spatial transformation matrix. Feature_1 (i.e., the first feature map) is spatially transformed using the spatial transformation matrix and merged with the feature map Feature_0 (i.e., the second feature map) to obtain the third feature map.
[0084] Text detection module: After spatial transformation and concatenation, the feature maps are subjected to a series of convolution pooling to obtain feature maps feature1, feature2, and feature3. In order to expand the receptive field of the feature map, two layers of dilated convolution layers are introduced after feature3, and the number of convolution kernel intervals (dilation rate) is 2 and 3. The output feature map is recorded as feature4. The feature map of feature4 is subjected to convolution operation (convolution kernel size is 1*1), and then it is subjected to upsampling layer (step length is 2) and convolution layer (convolution kernel size is 3*3) to obtain the feature map as feature, and feature is concat operation is performed with feature3, and the feature map of this layer is recorded as de_feature. After the convolution operation on de_feature, upsampling (UpSampling step length is 2) and convolution operation (convolution kernel size is 3*3) are performed, and the obtained feature map is concat operation with feature2. Repeat this operation and concat with feature1 feature map. Considering the amount of computation, we did not upsample to the same size as the original image. The output feature map size is 1 / 4 of the width and height of the original image. The output feature map is convolved, with 3 convolution kernels for the pixel classification branch and 2 convolution kernels for the text box boundary branch. The activation function uses softmax. The output result is the image pixel classification and text box boundary output result predicted by the image to be detected. Image pixel classification means whether each pixel belongs to the pixel in the text area or the pixel in the background area.
[0085] In one embodiment, Fig.11 FIG. 1 is a flow chart of a text area detection method in another embodiment, comprising:
[0086] Step 1102: input an image to be detected.
[0087] Step 1104, preprocessing the detection image to obtain a preprocessed image to be detected.
[0088] Step 1106, determining the direction of the preprocessed image to be detected.
[0089] Step 1108: When the direction of the preprocessed image to be detected is incorrect, the direction of the preprocessed image to be detected is corrected.
[0090] Step 1110, input a reference image and a template image.
[0091] Step 1112, input the reference image, the template image and the image to be detected with the correct orientation into the text region detection model.
[0092] The present invention is a directional template positioning method, which can be used for positioning various types of images according to the defined target text. First, for each type of image, a target text template is created. Taking a value-added invoice as an example, if we do not need all the information of the image, we only need to extract the amount, invoice number, invoice code, and date information. We select a standard image and create a blank image of the same size as the standard image, i.e., a template. The pixel values of the corresponding positions of the template and the target text of the standard image are set to 1, and the values of other pixels are set to 0. Since the direction of the scanned or photographed image is uncertain, the invoice may be sideways or upside down, so it needs to be rotated and corrected.
[0093] Step 1114, obtaining the target text box in the image to be detected.
[0094] Step 1116, output the text in the text box.
[0095] The text area detection method in this embodiment,
[0096] In one embodiment, traditional methods such as CTPN (Connectionist Text Proposal Network) and pixel-link can detect text areas in images. However, the above methods have the following problems:
[0097] 1) Each type of image data is developed separately, and the development cycle is long.
[0098] 2) When a field is missing, other texts may be easily mistaken for the field.
[0099] 3) When image text is printed offset, adjacent text is easily used as the field.
[0100] 4) When the image is severely deformed, the position of the target text box is not accurately positioned.
[0101] Therefore, a text area detection method in an embodiment of the present application is proposed, comprising:
[0102] Step (a1), obtaining an image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; marking the area where the text corresponding to the reference image is located in the template image.
[0103] Step (a2), graying the reference image, and merging the grayed reference image and the template image into a two-channel image.
[0104] Step (a3), inputting the two-channel image into a first feature extraction unit to obtain a first feature map, the first feature extraction unit comprising a convolution layer and a pooling layer connected in sequence,
[0105] Step (a4), input the image to be detected into the first feature extraction unit to obtain the second feature map,
[0106] Step (a5), merging the output after the first feature map is input to the second feature extraction unit with the output after the second feature map is input to the second feature extraction unit, performing matrix calculation on the merged feature map to obtain a spatial transformation matrix, and the second feature extraction unit includes a feature map resizing layer, a convolution layer, a pooling layer, a vector flattening layer and a fully connected layer connected in sequence.
[0107] Step (a6), spatially transforming the first feature map according to the spatial transformation matrix, merging the second feature map with the first feature map after spatial transformation to obtain a third feature map,
[0108] Step (a7), inputting the third feature map into the first feature extraction unit to obtain a fourth feature map, the first feature extraction unit includes a convolution layer and a pooling layer connected in sequence,
[0109] Step (a8), input the fourth feature map into the convolution layer, use the output of the convolution layer as the input of the upsampling layer, and obtain the fifth feature map output by the upsampling layer.
[0110] Step (a9), merging the fourth feature map and the fifth feature map to obtain the target text area corresponding to the image to be detected.
[0111] Step (a10), performing image segmentation on the image to be detected based on each target text area to obtain a text image set.
[0112] Step (a11), performing text recognition on each text image in the text image set to obtain the text in each target text area.
[0113] The above-mentioned text area detection method, since the image to be detected is generally an image taken by a user, and the image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a reference image that matches the content type of the image to be detected and a template image corresponding to the reference image, and the template image marks the area where the text corresponding to the reference image is located; the image to be detected, the reference image and the template image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; that is, based on a reference image that matches a more standardized content type and the corresponding template image and the image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for images to be detected that have problems such as deformation, blur, and misalignment.
[0114] In one embodiment, Fig.12 FIG. 1 is a flow chart of a method for training a text region detection model in an embodiment. The method for training a text region detection model includes:
[0115] Step 1202, obtaining a sample image to be detected, a sample reference image matching the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image; marking the area where the text corresponding to the sample reference image is located in the sample template image.
[0116] The sample to-be-detected image may be a sample image with a specific structure, such as at least one of a sample bill image, a sample certificate image, and a sample card image, but not limited thereto. The sample bill image may be, for example, a sample invoice image, a sample ticket image, etc. The sample certificate image may be, for example, a sample ID card image, a sample Hong Kong and Macau Pass image, etc. The sample card image may specifically be a sample bank card image, etc. Using multiple types of sample to-be-detected images for training can enable the trained text region detection model to have the text region detection capability of multiple types of to-be-detected images.
[0117] Content type matching means that if the sample image to be detected is a sample invoice image, then the sample reference image is also an invoice image; when the sample image to be detected is a sample ticket image, then the sample reference image is also a ticket image. The sample reference image is an image that meets the processing size requirements and is clearer and more standardized than the sample image to be detected.
[0118] The sample template image is used to mark the area where the text is located corresponding to the sample reference image. The specific marking method can be to set the pixel value of the area where the text is located corresponding to the sample reference image to 1, and set other pixel values to 0. The sample template image is used to represent the field to be extracted from the sample to be detected image.
[0119] Specifically, the server can obtain a sample image to be detected from the terminal, and obtain a sample reference image that matches the content type of the sample image to be detected and a sample template image corresponding to the sample reference image. The sample template image marks the area where the text corresponding to the sample reference image is located.
[0120] Step 1204, input the sample image to be detected, the sample reference image and the sample template image into the text area detection model to be trained, and adjust the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the sample image to be detected and the corresponding label value until a trained text area detection model is obtained.
[0121] The text positioning result can be displayed in the form of a text box, or in the form of coordinates in the sample image to be detected, or in the form of pixel type. Then the label value can be a text box label, a coordinate label, or a pixel type label value.
[0122] Specifically, the server inputs the sample image to be detected, the sample reference image and the sample template image into the text area detection model to be trained, and adjusts the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the sample image to be detected and the corresponding label value, until the model convergence condition is reached, and the trained text area detection model is obtained. The model convergence condition can be that the loss value is less than the preset loss value, the model parameter change between two iterations of the model is less than the preset change value, or the number of iterations reaches the preset number.
[0123] The training method of the above-mentioned text area detection model, since the sample image to be detected is generally an image taken by a user, and the sample image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a sample reference image that matches the content type of the sample image to be detected and a sample template image corresponding to the sample reference image, and the sample template image marks the area where the text corresponding to the sample reference image is located; the sample image to be detected, the sample reference image and the sample template image are input into the text area detection model to be trained for detection and processing, and the target text area corresponding to the sample image to be detected is obtained; that is, based on the sample reference image that matches the more standardized content type and the corresponding sample template image and the sample image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for sample images to be detected that have problems such as deformation, blur, and misalignment.
[0124] In one embodiment, the label value is a type label value of a pixel in the sample image to be detected; and the text positioning result includes a type prediction value of a pixel in the sample image to be detected.
[0125] The method adjusts the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the image to be detected and the corresponding label value, including: obtaining the type prediction value of the pixel point output by the text area detection model to be trained; determining the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value; and performing back propagation in the text area detection model to be trained based on the loss value of the pixel point to adjust the model parameters in the text area detection model to be trained.
[0126] The type prediction value includes a positive sample type prediction value and a negative sample type prediction value. The positive sample type prediction value is used to characterize that the pixel point is a pixel point in the target text area. The negative sample type prediction value is used to characterize that the pixel point is a pixel point in the background area.
[0127] Specifically, the server obtains the type prediction value of the pixel point output by the text area detection model to be trained. The server determines the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value. The server performs back propagation in the text area detection model to be trained based on the loss value of the pixel point to adjust the model parameters in the text area detection model to be trained. The loss value can be calculated using a loss function, specifically sparse_categorical_crossentropy (cross entropy loss function) can be used for calculation.
[0128] In this embodiment, the training method of the text area detection model is a supervised learning method. The training set of the supervised learning method consists of an input vector and a target vector associated with the input vector. The target vector guides the adjustment of the weight to reduce the overall error.
[0129] The training method of the text area detection model in this embodiment takes the type prediction value of the pixel point as the output result, and determines the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value. Backpropagation is performed in the text area detection model to be trained based on the loss value of the pixel point to adjust the model parameters in the trained text area detection model. A more accurate text area detection model can be trained based on the pixel point method.
[0130] In one embodiment, the training method of the text region detection model further includes the following steps:
[0131] Step (b1), obtaining a first number of first pixel points marked as positive sample label values; the positive sample label values are used to mark the area where the text of the sample image to be detected is located.
[0132] The positive sample label value is used to mark the area where the text of the sample image to be detected is located. For example, the positive sample label value may be 1.
[0133] Specifically, the server obtains the first number of first pixel points marked as positive sample label values; the positive sample label value is used to mark the area where the text of the sample image to be detected is located. For example, there are a total of 1000 pixel points in the sample image to be detected, of which the first number of first pixel points marked as positive sample label values is 100. That is, the number of pixel points occupied by the area where the text is located in the sample image to be detected is 100.
[0134] The loss value of the pixel is determined based on the type prediction value of the pixel and the corresponding type label value, including:
[0135] Step (b2), based on the type prediction value of each pixel in the sample image to be detected and the corresponding type label value, determine the loss value of each pixel in the sample image to be detected.
[0136] Specifically, the server determines the loss value of each pixel of the sample to be detected based on the type prediction value of each pixel in the sample to be detected image and the corresponding type label value. Taking the number of pixels in the sample to be detected image as 1000 as an example, the server needs to determine the loss value of each pixel among the 1000 pixels to obtain 1000 loss values.
[0137] Step (b3), determining a first loss value of a first pixel from the loss values of each pixel of the sample image to be detected.
[0138] Specifically, the server determines the first loss value of the first pixel point marked as the positive sample label value from the loss values of each pixel point of the sample to be detected image. For example, if there are 100 first pixel points, then the loss values of these 100 first pixel points are determined from the 1000 loss values.
[0139] Step (b4), based on the loss value of each pixel point of the sample image to be detected, select a second number of second loss values from the loss values of the second pixel points marked as negative sample label values in descending order; the second number is determined based on the first number; the negative sample label is used to mark the background area of the sample image to be detected.
[0140] Among them, the second pixel point refers to the pixel point marked as a negative sample label value. In addition, a certain pixel point A is marked as a negative sample label value, but the type prediction value of pixel point A is a positive sample type prediction value, and the pixel point is still the second pixel point. The second number can be a preset multiple of the first number, etc. For example, the ratio of the first number to the second number is 1:3, then when the first number is 100, the second number is 300.
[0141] Specifically, the server determines the loss value of the second pixel point marked as the negative sample label value based on the loss value of each sample pixel point to be detected; and then selects the second number of second loss values from the loss values of the second pixel points in descending order. For example, taking the number of pixels in the sample image to be detected as 1000, the number of the first pixel points as 100, and the second number as 300 as an example, the second number is 900, and 300 loss values are selected from the 900 loss values in descending order.
[0142] The pixel-based loss value is back-propagated in the text region detection model to be trained to adjust the model parameters in the text region detection model to be trained, including:
[0143] Step (b5), summing the first loss values of the first quantity and the second loss values of the second quantity to obtain a target loss value.
[0144] Among them, the target loss value refers to the loss value finally used for back propagation.
[0145] Specifically, the server sums the first loss values of the first quantity and the second loss values of the second quantity to obtain a target loss value. For example, the first quantity is 100 and the second quantity is 300. The 100 first loss values and 300 second loss values are added to obtain the target loss value.
[0146] Step (b6), performing back propagation in the text region detection model to be trained based on the target loss value to adjust the model parameters in the text region detection model to be trained.
[0147] Specifically, the server performs back propagation in the text region detection model to be trained based on the target loss value to adjust model parameters in the text region detection model to be trained.
[0148] The training method of the text area detection model in this embodiment is based on the target loss value obtained by the first loss value of the first pixel point marked as the positive sample label value and the second loss value of the second number marked as the negative sample label value, that is, a part of the loss values is selected for back propagation according to a certain quantitative relationship between positive and negative samples, which can improve the accuracy of the trained text area detection model.
[0149] In one embodiment, the type prediction value includes a negative sample type prediction value and a positive sample type prediction value. Determining the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value includes: when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is less than a preset ratio, determining the loss value of the target pixel point based on the type prediction value of the target pixel point and the corresponding type label value; the target pixel point refers to the pixel point corresponding to the negative sample prediction value;
[0150] The pixel-based loss value is back-propagated in the text region detection model to be trained to adjust the model parameters in the text region detection model to be trained, including:
[0151] The loss values corresponding to the target pixel points are summed, and back-propagation is performed in the text region detection model to be trained based on the summed loss values to adjust the model parameters in the text region detection model to be trained.
[0152] Among them, the negative sample type prediction value indicates that the prediction is a background area type. The positive sample type prediction value is used to indicate that the prediction is a text area. The preset ratio can be set according to requirements. The preset ratio refers to the ratio between the second number and the first number, for example, 3:1.
[0153] Specifically, when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel is less than the preset ratio, it means that the number of pixels of the negative sample type is small, and the loss value of the target pixel is determined based on the type prediction value of the target pixel and the corresponding type label value. The server sums the loss values corresponding to the target pixel, and performs back propagation in the text area detection model to be trained based on the summed loss value to adjust the model parameters in the text area detection model to be trained. Take the preset ratio of 3:1, 1000 pixels in the sample to be detected image, 700 target pixels, and 300 pixels corresponding to the positive sample prediction value as an example, then 700:300 is less than 3:1, and the loss value of each target pixel in the 700 target pixels is determined based on the type prediction value and the corresponding type label value of the 700 target pixels. The 700 loss values are summed to obtain the summed loss value, and back propagation is performed in the text area detection model to be trained based on the summed loss value.
[0154] In the training method of the text area detection model in this embodiment, when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is less than the preset ratio, it means that the number of target pixels is small, and the result may be wrong, and the error may be serious; therefore, the loss value of the target pixel point is summed and back-propagated to adjust the model parameters, which can improve the accuracy of the trained text area detection model.
[0155] In one embodiment, the loss value of a pixel is determined based on the type prediction value of the pixel and the corresponding type label value, including: determining the loss value of each pixel based on the type prediction value of each pixel in the sample image to be detected and the corresponding type label value.
[0156] The pixel-based loss value is back-propagated in the text region detection model to be trained to adjust the model parameters in the text region detection model to be trained, including:
[0157] From the loss values of the pixels marked as positive sample label values, select the loss values of the third number of pixels with the highest loss values in descending order;
[0158] From the loss values of the pixels marked as negative sample label values, select the loss values of the fourth number of pixels with the highest loss values in descending order;
[0159] Based on the loss value obtained by summing the loss value of the third number and the loss value of the fourth number, back propagation is performed in the text region detection model to be trained to adjust the model parameters in the text region detection model to be trained.
[0160] The third number may be selected from the pixels marked as positive sample label values according to a certain ratio. Similarly, the fourth number may be selected from the pixels marked as negative sample label values according to a certain ratio. And the selection ratio of the third number and the fourth number may be the same.
[0161] Specifically, the server determines the loss value of each pixel based on the type prediction value and the corresponding type label value of each pixel in the sample image to be detected. The server determines the loss value of the pixel marked with the positive sample label value from the loss value of each pixel; and selects the loss values of the third number of pixels with the highest loss values from the loss values of the pixels marked with the positive sample label value in order from high to low. The server determines the loss value of the pixel marked with the negative sample label value from the loss value of each pixel; and selects the loss values of the fourth number of pixels with the highest loss values from the loss values of the pixels marked with the negative sample label value in order from high to low. The server performs back propagation in the text area detection model to be trained based on the loss value obtained by summing the loss values of the third number and the loss values of the fourth number to adjust the model parameters in the text area detection model to be trained.
[0162] For example, the sample image to be detected contains 1000 pixels, of which 100 pixels are marked as positive sample label values and 900 pixels are marked as negative sample label values. Then 100*0.7 (the third number) loss values are selected from the 100 pixels, and 300*0.7 (the fourth number) loss values are selected from the 900 pixels, and the sum is calculated. After the sum is calculated, back propagation is performed in the text area detection model to be trained to adjust the model parameters in the text area detection model to be trained.
[0163] The training method of the text area detection model in this embodiment selects the loss values of a third number of pixel points from the loss values of the pixel points marked as positive sample label values in descending order; selects the loss values of a fourth number of pixel points with the highest loss values from the loss values of the pixel points marked as negative sample label values in descending order, and performs back propagation after summing the loss values of the third number and the fourth number to adjust the model parameters. This can strengthen the training of the model, train some difficult samples that are prone to classification errors, and improve the accuracy of the trained model.
[0164] In one embodiment, after obtaining the trained text area detection model, the method also includes: transforming the sample image to be detected to obtain the transformed sample image to be detected; inputting the transformed sample image to be detected, the sample reference image and the sample template image into the trained text area detection model for reinforcement training, and adjusting the model parameters in the trained text area detection model according to the text positioning result corresponding to the transformed sample image to be detected and the corresponding label value, until the enhanced text area detection model is obtained.
[0165] The transformation process refers to the process that does not change the substantial content of the image to be detected. For example, if the image to be detected is a bill image, the transformation process does not replace the bill image with a card image. The transformation process may be filtering, image brightness transformation, grayscale transformation, corrosion and expansion, noise addition, shadow addition, etc., but is not limited thereto.
[0166] Specifically, the sample image to be detected is transformed to obtain the transformed sample image to be detected; the transformed sample image to be detected, the sample reference image and the sample template image are input into the trained text area detection model for reinforcement training, and the model parameters of the trained text area detection model are adjusted according to the text positioning result corresponding to the transformed sample image to be detected and the corresponding label value, until the enhanced text area detection model is obtained.
[0167] The training method of the text area detection model in this embodiment performs enhanced training on the trained text area detection model based on the transformed sample images to be detected, which can increase samples and expand the data set to improve the accuracy of the model without increasing the workload of sample collection.
[0168] In one embodiment, a method for training a text region detection model includes:
[0169] Step (c1) is to obtain a sample image to be detected, a sample reference image that matches the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image. The sample template image is marked with the area where the text corresponding to the sample reference image is located.
[0170] Step (c2), inputting the sample image to be detected, the sample reference image and the sample template image into the text region detection model to be trained.
[0171] Step (c3), obtaining a first number of first pixel points marked as positive sample label values. The positive sample label values are used to mark the area where the text of the sample image to be detected is located.
[0172] Step (c4), based on the type prediction value of each pixel in the sample image to be detected and the corresponding type label value, determine the loss value of each pixel in the sample image to be detected.
[0173] Step (c5), when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is greater than or equal to the preset ratio, the first loss value of the first pixel point is determined from the loss values of each pixel point of the sample image to be detected.
[0174] Step (c6), based on the loss value of each pixel of the sample to be detected image, select a second number of second loss values from the loss values of the second pixel points marked as negative sample label values in descending order. The second number is determined based on the first number. The negative sample label is used to mark the background area of the sample to be detected image.
[0175] Step (c7), summing the first loss values of the first quantity and the second loss values of the second quantity to obtain a target loss value.
[0176] Step (c8), performing back propagation in the text region detection model to be trained based on the target loss value to adjust the model parameters in the text region detection model to be trained.
[0177] Step (c9), when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is less than a preset ratio, the loss value of the target pixel point is determined based on the type prediction value of the target pixel point and the corresponding type label value. The target pixel point refers to the pixel point corresponding to the negative sample prediction value.
[0178] Step (c10), summing the loss values corresponding to each target pixel point, and performing back propagation in the text area detection model to be trained based on the summed loss values to adjust the model parameters in the text area detection model to be trained.
[0179] Step (c11), adjusting the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the sample image to be detected and the corresponding label value, until a trained text area detection model is obtained.
[0180] Step (c12), determining the loss value of each pixel based on the type prediction value of each pixel in the sample image to be detected and the corresponding type label value.
[0181] Step (c13), selecting loss values of a third number of pixel points from the loss values of the pixel points marked as positive sample label values in descending order.
[0182] Step (c14), selecting the loss values of a fourth number of pixel points from the loss values of the pixel points marked as negative sample label values in descending order.
[0183] Step (c15), based on the loss value obtained by summing the loss value of the third number and the loss value of the fourth number, back-propagation is performed in the trained text area detection model to adjust the model parameters in the trained text area detection model.
[0184] Step (c16), transforming the sample image to be detected to obtain the transformed sample image to be detected.
[0185] Step (c17), input the transformed sample image to be detected, the sample reference image and the sample template image into the trained text area detection model for reinforcement training, and adjust the model parameters in the trained text area detection model according to the text positioning result corresponding to the transformed sample image to be detected and the corresponding label value until the enhanced text area detection model is obtained.
[0186] The training method of the above-mentioned text area detection model, since the sample image to be detected is generally an image taken by the user, and the sample image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a sample reference image that matches the content type of the sample image to be detected and a sample template image corresponding to the sample reference image, and the sample template image marks the area where the text corresponding to the sample reference image is located; the sample image to be detected, the sample reference image and the sample template image are input into the text area detection model to be trained for detection and processing, and the target text area corresponding to the sample image to be detected is obtained; that is, based on the sample reference image that matches the more standardized content type and the corresponding sample template image and the sample image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for images to be detected that have problems such as deformation, blur, and misalignment.
[0187] It should be understood that although Figure 2 , 7 The steps in the flowcharts of FIG. 1 to FIG. 12 are shown in sequence as indicated by arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 2 , 7At least part of the steps in 1 to 12 may include multiple steps or multiple stages. These steps or stages do not necessarily have to be performed at the same time, but can be performed at different times. The execution order of these steps or stages does not necessarily have to be sequential, but can be performed in rotation or alternation with other steps or at least part of the steps or stages in other steps.
[0188] In one embodiment, Fig.13 , which is a structural block diagram of a text region detection device in an embodiment, provides a text region detection device, including: an image acquisition module 1302 and an image detection module 1304, wherein:
[0189] The image acquisition module 1302 is used to acquire the image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; and annotate the area in the template image where the text corresponding to the reference image is located;
[0190] The image detection module 1304 is used to input the image to be detected, the reference image and the template image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; the text area detection model is trained based on the sample reference image, the sample template image and the sample image to be detected and the corresponding sample labels.
[0191] The above-mentioned text area detection device, since the image to be detected is generally an image taken by a user, and the image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a reference image that matches the content type of the image to be detected and a template image corresponding to the reference image, and the template image marks the area where the text corresponding to the reference image is located; the image to be detected, the reference image and the template image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected; that is, based on a reference image that matches a more standardized content type and the corresponding template image and the image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for images to be detected that have problems such as deformation, blur, and misalignment.
[0192] In one embodiment, the image detection module 1304 is used to input the image to be detected, the reference image and the template image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected, including: graying the reference image, merging the grayed reference image and the template image into a two-channel image, inputting the image to be detected and the two-channel image into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected.
[0193] The text area detection device in this embodiment grayscales the reference image, and merges the grayscaled reference image and the template image into a two-channel image, which can fully integrate the pixel information of the reference image and the template image; the image to be detected and the two-channel image are input into the text area detection model for detection processing to obtain the target text area corresponding to the image to be detected. Compared with the method of directly inputting the annotation and the image to be detected into the model for detection, the accuracy of text area detection of the image to be detected can be improved.
[0194] In one embodiment, the image detection module 1304 is used to perform feature extraction processing on a two-channel image to obtain a first feature map; perform feature extraction processing on the image to be detected to obtain a second feature map; merge the first feature map and the second feature map based on the merged feature map, perform matrix calculation on the merged feature map to obtain a spatial transformation matrix; perform spatial transformation on the first feature map according to the spatial transformation matrix, merge the second feature map and the first feature map after spatial transformation to obtain a third feature map; perform feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected.
[0195] The text area detection device in this embodiment performs feature extraction processing on the two-channel image and the image to be detected respectively to obtain a first feature map and a second feature map, and merges the first feature map and the second feature map, performs matrix calculation on the merged feature map to obtain a spatial transformation matrix, performs spatial transformation on the first feature map according to the spatial transformation matrix, merges the second feature map and the first feature map after spatial transformation to obtain a third feature map, and then performs a series of feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected, that is, the two-channel image and the image to be detected are first subjected to feature extraction processing, and then subjected to spatial transformation processing, that is, spatial mapping, etc., and the obtained results are subjected to feature extraction processing, and the structure of the text area detection model is fully utilized to obtain the target text area corresponding to the image to be detected.
[0196] In one embodiment, the image detection module 1304 is used to input a two-channel image into a first feature extraction unit to obtain a first feature map; the first feature extraction unit includes a convolution layer and a pooling layer connected in sequence; the image to be detected is input into the first feature extraction unit to obtain a second feature map; the output after the first feature map is input into the second feature extraction unit is merged with the output after the second feature map is input into the second feature extraction unit, and a matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix; the second feature extraction unit includes a feature map size adjustment layer, a convolution layer, a pooling layer, a vector flattening layer and a fully connected layer connected in sequence.
[0197] The text area detection device in this embodiment inputs the two-channel image into the first feature extraction unit to obtain a four-dimensional first feature map, inputs the image to be detected into the first feature extraction unit to obtain a second feature map of the same dimension, merges the output of the first feature map after being input into the second feature extraction unit with the output of the second feature map after being input into the second feature extraction unit, performs matrix calculation on the merged feature map, and obtains a spatial transformation matrix, so that the two-channel image and the image to be detected can be mapped, thereby accurately extracting the target text area.
[0198] In one embodiment, the image detection module 1304 is used to input the third feature map into the first feature extraction unit to obtain a fourth feature map; input the fourth feature map into the convolution layer, use the output of the convolution layer as the input of the upsampling layer, and obtain the fifth feature map output by the upsampling layer; merge the fourth feature map and the fifth feature map to obtain the target text area corresponding to the image to be detected.
[0199] The text area detection device in this embodiment inputs the third feature map into the first feature extraction unit to obtain a fourth feature map, inputs the fourth feature map into the convolution layer, uses the output of the convolution layer as the input of the upsampling layer, obtains the fifth feature map output by the upsampling layer, merges the fourth feature map and the fifth feature map, and obtains the target text area corresponding to the image to be detected, that is, the size of the image is compressed during the convolution pooling process, and then passes through the convolution layer and the upsampling layer to expand the receptive field.
[0200] In one embodiment, the target text region matches the region where the text is marked in the template image. The text region detection device also includes a text recognition module, which is used to perform image segmentation on the image to be detected based on each target text region to obtain a text image set; perform text recognition on each text image in the text image set to obtain the text in each target text region.
[0201] The text area detection device in this embodiment, since the target text area matches the area where the text is marked in the template image, can detect different target text areas by setting the area where the text is marked in the template image, and perform image segmentation on the image to be detected based on each target text area to obtain a text image set; perform text recognition on each text image in the text image set to obtain the text in each target text area, and can extract the text in different target text areas, thereby improving the usability of the model.
[0202] In one embodiment, Fig.14 , which is a structural block diagram of a training device for a text region detection model in an embodiment, provides a training device for a text region detection model, and the device includes a sample image acquisition module 1402 and a sample image detection module 1404, wherein:
[0203] The sample image acquisition module 1402 is used to acquire a sample image to be detected, a sample reference image matching the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image; and annotate the area of the sample template image where the text corresponding to the sample reference image is located;
[0204] The sample image detection module 1404 is used to input the sample image to be detected, the sample reference image and the sample template image into the text area detection model to be trained, and adjust the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the image to be detected and the corresponding label value until a trained text area detection model is obtained.
[0205] The training module of the above-mentioned text area detection model, since the sample image to be detected is generally an image taken by the user, and the sample image to be detected may have problems such as deformation, blur, and misalignment, it is necessary to obtain a sample reference image that matches the content type of the sample image to be detected and a sample template image corresponding to the sample reference image, and the sample template image marks the area where the text corresponding to the sample reference image is located; the sample image to be detected, the sample reference image and the sample template image are input into the text area detection model to be trained for detection and processing, and the target text area corresponding to the sample image to be detected is obtained; that is, based on the sample reference image that matches the more standardized content type and the corresponding sample template image and the sample image to be detected, the text area is located, the target text area can be located, and irrelevant text areas do not need to be detected, which can effectively avoid mistaking adjacent text for the fields to be extracted, and the target text area can also be accurately extracted for sample images to be detected that have problems such as deformation, blur, and misalignment.
[0206] In one embodiment, the label value is the type label value of the pixel in the sample image to be detected; the text positioning result includes the type prediction value of the pixel in the sample image to be detected. The sample image detection module 1404 is used to obtain the type prediction value of the pixel output by the text area detection model to be trained; determine the loss value of the pixel based on the type prediction value of the pixel and the corresponding type label value; perform back propagation in the text area detection model to be trained based on the loss value of the pixel to adjust the model parameters in the text area detection model to be trained.
[0207] The training device of the text area detection model in this embodiment takes the type prediction value of the pixel point as the output result, and determines the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value, and performs backpropagation in the text area detection model to be trained based on the loss value of the pixel point to adjust the model parameters in the trained text area detection model. A more accurate text area detection model can be trained based on the pixel point method.
[0208] In one embodiment, the sample image detection module 1404 is used to obtain a first number of first pixels marked as positive sample label values; the positive sample label values are used to mark the area where the text of the sample image to be detected is located; based on the type prediction value of each pixel in the sample image to be detected and the corresponding type label value, the loss value of each pixel of the sample image to be detected is determined; from the loss values of each pixel of the sample image to be detected, a first loss value of the first pixel is determined; based on the loss values of each pixel of the sample image to be detected, a second number of second loss values are selected from the loss values of the second pixel marked as the negative sample label value in order from high to low; the second number is determined based on the first number; the negative sample label is used to mark the area where the background of the sample image to be detected is located; the first loss values of the first number and the second loss values of the second number are summed to obtain a target loss value; based on the target loss value, backpropagation is performed in the text area detection model to be trained to adjust the model parameters in the text area detection model to be trained.
[0209] The training device of the text area detection model in this embodiment obtains a target loss value based on a first loss value of a first pixel point marked as a positive sample label value and a second loss value of a second number marked as a negative sample label value, that is, a part of the loss values is selected for back propagation according to a certain quantitative relationship between positive and negative samples, which can improve the accuracy of the trained text area detection model.
[0210] In one embodiment, the type prediction value includes a negative sample type prediction value and a positive sample type prediction value. The sample image detection module 1404 is used to determine the loss value of the target pixel based on the type prediction value of the target pixel and the corresponding type label value when the ratio of the negative sample type prediction value to the positive sample type prediction value is less than a preset ratio based on the type prediction value of the pixel; the target pixel refers to the pixel corresponding to the negative sample prediction value; sum the loss values corresponding to the target pixel, and perform back propagation in the text area detection model to be trained based on the summed loss values to adjust the model parameters in the text area detection model to be trained.
[0211] In the training device of the text area detection model in this embodiment, when the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is less than the preset ratio, it means that the number of target pixels is small, and the result may be wrong, and the error may be serious; therefore, the loss value of the target pixel point is summed and back-propagated to adjust the model parameters, so as to improve the accuracy of the trained text area detection model.
[0212] In one embodiment, the sample image detection module 1404 is used to determine the loss value of each pixel based on the type prediction value and the corresponding type label value of each pixel in the sample image to be detected; from the loss values of the pixels marked with positive sample label values, the loss values of the third number of pixels with the highest loss values are selected in descending order; from the loss values of the pixels marked with negative sample label values, the loss values of the fourth number of pixels with the highest loss values are selected in descending order; based on the loss value obtained by summing the third number of loss values and the fourth number of loss values, back propagation is performed in the text area detection model to be trained to adjust the model parameters in the text area detection model to be trained.
[0213] The training device of the text area detection model in this embodiment selects the loss values of a third number of pixel points from the loss values of the pixel points marked as positive sample label values in descending order; selects the loss values of a fourth number of pixel points with the highest loss values from the loss values of the pixel points marked as negative sample label values in descending order, and performs back propagation after summing the loss values of the third number and the fourth number to adjust the model parameters, which can strengthen the training of the model, train some difficult samples that are prone to classification errors, and improve the accuracy of the trained model.
[0214] In one embodiment, the sample image detection module 1404 is also used to transform the sample image to be detected to obtain the transformed sample image to be detected; the transformed sample image to be detected, the sample reference image and the sample template image are input into the trained text area detection model for enhanced training, and the model parameters in the trained text area detection model are adjusted according to the text positioning result corresponding to the transformed sample image to be detected and the corresponding label value, until the enhanced text area detection model is obtained.
[0215] The training device of the text area detection model in this embodiment performs intensive training on the trained text area detection model based on the transformed sample images to be detected, which can increase samples and expand the data set to improve the accuracy of the model without increasing the workload of sample collection.
[0216] The specific definition of the text area detection device can be found in the definition of the text area detection method above, which will not be repeated here. Each module in the above-mentioned text area detection device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0217] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Fig.15 As shown. The computer device includes a processor, a memory and a network interface connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store image data. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a text area detection method is implemented.
[0218] Those skilled in the art will understand that Fig.15 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0219] In one embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiments when executing the computer program.
[0220] In one embodiment, a computer-readable storage medium is provided, storing a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0221] In one embodiment, a computer program product or computer program is provided, the computer program product or computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs the steps in the above-mentioned method embodiments.
[0222] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory or optical memory, etc. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM).
[0223] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0224] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A text region detection method, characterized in that: The method comprises: Acquire an image to be detected, a reference image matching the content type of the image to be detected, and a template image corresponding to the reference image; annotate the template image with an area where the text corresponding to the reference image is located; Inputting the image to be detected, the reference image and the template image into a text region detection model for detection processing to obtain a target text region corresponding to the image to be detected; the text region detection model is trained based on sample reference images, sample template images, sample images to be detected and corresponding sample labels; The step of inputting the image to be detected, the reference image and the template image into a text region detection model for detection processing to obtain a target text region corresponding to the image to be detected includes: Gray-scale the reference image, merge the gray-scaled reference image and the template image into a two-channel image, and perform feature extraction processing on the two-channel image to obtain a first feature map; Performing feature extraction processing on the image to be detected to obtain a second feature map; Based on the first feature map and the second feature map, a matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix; the first feature map is spatially transformed according to the spatial transformation matrix, and the second feature map and the first feature map after the spatial transformation are merged to obtain a third feature map; Perform feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected.
2. The method according to claim 1, characterized in that: The merging process based on the first feature map and the second feature map, performing matrix calculation on the merged feature map, and obtaining a spatial transformation matrix includes: Get matrix calculation parameters; Based on the first feature map and the second feature map, a merging process is performed, and matrix calculation is performed on the merged feature map according to the matrix calculation parameters to obtain a spatial transformation matrix.
3. The method according to claim 1, characterized in that The step of performing feature extraction processing on the two-channel image to obtain a first feature map includes: Inputting the two-channel image into a first feature extraction unit to obtain a first feature map; the first feature extraction unit includes a convolution layer and a pooling layer connected in sequence; The step of performing feature extraction processing on the image to be detected to obtain a second feature map includes: Inputting the image to be detected into the first feature extraction unit to obtain a second feature map; The merging the first feature map and the second feature map, performing matrix calculation on the merged feature map, and obtaining a spatial transformation matrix includes: The output after the first feature map is input into the second feature extraction unit is merged with the output after the second feature map is input into the second feature extraction unit, and matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix; the second feature extraction unit includes a feature map size adjustment layer, a convolution layer, a pooling layer, a vector flattening layer and a fully connected layer connected in sequence.
4. According to the method of claim 3, the first feature extraction unit includes two convolutional layers and one maximum pooling layer connected in sequence.
5. The method according to claim 1, characterized in that The step of performing feature extraction processing on the third feature map to obtain a target text area corresponding to the image to be detected includes: Inputting the third feature map into a first feature extraction unit to obtain a fourth feature map; the first feature extraction unit includes a convolution layer and a pooling layer connected in sequence; Inputting the fourth feature map into a convolution layer, using the output of the convolution layer as the input of an upsampling layer, and obtaining a fifth feature map output by the upsampling layer; The fourth feature map and the fifth feature map are combined to obtain a target text area corresponding to the image to be detected.
6. The method according to any one of claims 1 to 5, characterized in that: The target text area matches the area where the text is marked in the template image; The method further comprises: Performing image segmentation on the image to be detected based on each of the target text regions to obtain a text image set; Text recognition is performed on each text image in the text image set to obtain the text in each target text area.
7. A training method for a text region detection model, characterized in that: The method comprises: Acquire a sample image to be detected, a sample reference image matching the content type of the sample image to be detected, and a sample template image corresponding to the sample reference image; annotate the sample template image with an area where the text corresponding to the sample reference image is located; The sample image to be detected, the sample reference image and the sample template image are input into the text area detection model to be trained, and during the training process, the sample reference image is grayed, the grayed sample reference image and the sample template image are merged into a two-channel image, feature extraction processing is performed on the two-channel image to obtain a first feature map, feature extraction processing is performed on the sample image to be detected to obtain a second feature map, merging processing is performed based on the first feature map and the second feature map, matrix calculation is performed on the merged feature map to obtain a spatial transformation matrix, spatial transformation is performed on the first feature map according to the spatial transformation matrix, the second feature map and the first feature map after spatial transformation are merged to obtain a third feature map, feature extraction processing is performed on the third feature map to obtain a text positioning result corresponding to the sample image to be detected, and model parameters in the text area detection model to be trained are adjusted according to the text positioning result corresponding to the sample image to be detected and the corresponding label value, until a trained text area detection model is obtained.
8. The method according to claim 7, characterized in that The label value includes the type label value of the pixel in the sample image to be detected; the text positioning result includes the type prediction value of the pixel in the sample image to be detected; The adjusting the model parameters in the text area detection model to be trained according to the text positioning result corresponding to the image to be detected and the corresponding label value includes: Obtaining a type prediction value of a pixel point output by the to-be-trained text region detection model; Determine the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value; Back-propagation is performed in the to-be-trained text region detection model based on the loss value of the pixel point to adjust the model parameters in the to-be-trained text region detection model.
9. The method according to claim 8, characterized in that The method further comprises: Acquire a first number of first pixel points marked as positive sample label values; the positive sample label values are used to mark the area where the text of the sample image to be detected is located; The determining the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value includes: Determining a loss value of each pixel point in the sample image to be detected based on a type prediction value and a corresponding type label value of each pixel point in the sample image to be detected; Determining a first loss value of the first pixel from the loss values of each pixel of the sample image to be detected; Based on the loss value of each pixel of the sample image to be detected, a second number of second loss values are selected from the loss values of the second pixel points marked as negative sample label values in descending order; the second number is determined based on the first number; the negative sample label is used to mark the background area of the sample image to be detected; The loss value based on the pixel point is back-propagated in the to-be-trained text region detection model to adjust the model parameters in the to-be-trained text region detection model, including: The first loss value of the first quantity and the second loss value of the second quantity are summed to obtain a target loss value; Back propagation is performed in the text region detection model to be trained based on the target loss value to adjust model parameters in the text region detection model to be trained.
10. The method according to claim 8, characterized in that The type prediction value includes a negative sample type prediction value and a positive sample type prediction value; The determining the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value includes: When the ratio of the negative sample type prediction value to the positive sample type prediction value determined based on the type prediction value of the pixel point is less than a preset ratio, the loss value of the target pixel point is determined based on the type prediction value of the target pixel point and the corresponding type label value; the target pixel point refers to the pixel point corresponding to the negative sample prediction value; The loss value based on the pixel point is back-propagated in the to-be-trained text region detection model to adjust the model parameters in the to-be-trained text region detection model, including: The loss values corresponding to the target pixels are summed, and back-propagation is performed in the to-be-trained text region detection model based on the summed loss values to adjust model parameters in the to-be-trained text region detection model.
11. The method according to claim 8, characterized in that The determining the loss value of the pixel point based on the type prediction value of the pixel point and the corresponding type label value includes: Determine the loss value of each pixel point based on the type prediction value of each pixel point in the sample to be detected image and the corresponding type label value; The loss value based on the pixel point is back-propagated in the to-be-trained text region detection model to adjust the model parameters in the to-be-trained text region detection model, including: From the loss values of the pixels marked as positive sample label values, select the loss values of a third number of pixels in descending order; Selecting loss values of a fourth number of pixels from the loss values of the pixels marked as negative sample label values in descending order; Based on the loss value obtained by summing the loss values of the third number and the loss values of the fourth number, back propagation is performed in the text region detection model to be trained to adjust model parameters in the text region detection model to be trained.
12. The method according to any one of claims 7 to 11, characterized in that After obtaining the trained text region detection model, the method further includes: Performing transformation processing on the sample image to be detected to obtain a transformed sample image to be detected; The transformed sample image to be detected, the sample reference image and the sample template image are input into the trained text area detection model for reinforcement training, and the model parameters in the trained text area detection model are adjusted according to the text positioning result corresponding to the transformed sample image to be detected and the corresponding label value until an enhanced text area detection model is obtained.
13. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 12 are implemented.
14. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 12 are implemented.
