A scene text recognition method and system
By constructing training samples and training Yolov5 and Textformer models, the problem of complex text recognition in natural scene images is solved, end-to-end scene text recognition is achieved, and recognition efficiency and accuracy are improved.
Patent Information
- Application Number
- CN202210164606.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-23
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2042-02-23
AI Technical Summary
Existing scene text recognition technology is difficult to effectively identify complex text information in natural scene images. It is limited by the interference of multiple languages, fonts, sizes, colors, brightness, contrast, shape transformation, background complexity and non-text textures, and is large-scale. Most methods are not end-to-end and cannot integrate text detection and recognition.
By building training samples, including public and non-public data sets, the Yolov5 model is trained to obtain a character detection model, and the Textformer model is used for text recognition. The Textformer model uses the SwinTransformer encoder and Transformer decoder to achieve end-to-end text positioning and recognition.
It achieves efficient end-to-end scene text recognition, improves the recognition capabilities of single characters and curved text, simplifies the model framework, and improves recognition efficiency and accuracy.
Smart Images

Figure CN114550158B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of text recognition, and in particular to a scene text recognition method and system. Background Art
[0002] OCR (Optical Character Recognition) traditionally refers to the analysis and processing of input scanned document images to identify the text information in the image. Scene Text Recognition (STR) refers to the recognition of text information in natural scene images. Text recognition in natural scene images is much more difficult than text recognition in scanned document images. The reasons are as follows: (1) The text in natural scene images allows for a mixture of texts in multiple languages, and characters can have different fonts, sizes, colors, brightness, contrast, etc.; (2) The text in natural scene images may have horizontal, vertical, curved, rotated, distorted and other problems; (3) The text area in natural scene images may be deformed (perspective, affine transformation), incomplete, blurred, etc.; (4) The background of natural scene images is extremely complex and diverse, and there may be non-text textures similar to text in the background (such as sand, grass, fences, brick walls, etc.).
[0003] Scene text recognition technology has a wide range of application scenarios. The related applications that have been implemented by Internet companies involve business card recognition, menu recognition, express delivery order recognition, ID card recognition, business license recognition, bank card recognition, license plate recognition, road sign recognition, commodity packaging bag recognition, conference whiteboard recognition, advertising main words recognition, test paper recognition, receipt recognition, etc. There are also many service providers that provide scene text recognition services, including some service companies active in vertical sub-industries such as logistics, education, security, video live broadcast, e-government, e-commerce, and tourism navigation. These companies can use pre-trained models to directly provide cloud services such as scene image and text recognition, card recognition, and scanned document recognition, or use customer-provided data sets to train customized models (such as bill recognition models) and provide customized AI service system integration. It is generally believed that scene text recognition is divided into two stages, also known as two major tasks, namely text detection and text recognition. The purpose of the text detection task is to identify areas that may contain text of any shape in natural images, and the purpose of text recognition is to identify text content in the detected text area. Today, most STR methods are not end-to-end (text detection and text recognition cannot be integrated). Summary of the invention
[0004] In view of the above problems, the present invention aims to provide a scene text recognition method and system
[0005] To achieve the above object, the present invention provides the following solutions:
[0006] A scene text recognition method, comprising:
[0007] Constructing training samples; the training samples include a public data set and a non-public data set; the public data set and the non-public data set both include scene images and label files;
[0008] The Yolov5 model is trained by the training sample to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the single characters contained in the scene image;
[0009] Training the Textformer model using training samples and all individual characters recognized by the character detection model;
[0010] The trained Textformer model is used to identify text areas and single characters in the scene images to be identified.
[0011] Optionally, before training the Yolov5 model through the training samples to obtain the character detection model, the method further includes:
[0012] The label file of the public data set is processed; the processing includes modifying the content format and file type of the label file.
[0013] Optionally, the construction of the non-public data set specifically includes:
[0014] Extracting a preset number of scene images from the public data set and online images;
[0015] Crop and splice the extracted scene images;
[0016] Insert English alphabet and Arabic numerals into the stitched image.
[0017] Optionally, it also includes performing data enhancement processing on scene images in the training samples.
[0018] Optionally, the Textformer model includes an encoder and a decoder; the encoder is a SwinTransformer model, and the decoder is a Transformer model.
[0019] The present invention also provides a scene text recognition system, comprising:
[0020] A training sample construction module, used to construct training samples; the training samples include a public data set and a non-public data set; the public data set and the non-public data set both include scene images and label files;
[0021] A first training module is used to train the Yolov5 model through the training samples to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the individual characters contained in the scene image;
[0022] A second training module is used to train the Textformer model through training samples and all single characters recognized by the character detection model;
[0023] The recognition module is used to recognize text areas and individual characters in the scene image to be recognized through the trained Textformer model.
[0024] Optionally, it also includes:
[0025] The label processing module is used to process the label file of the public data set; the processing includes modifying the content format and file type of the label file.
[0026] Optionally, the training sample construction module specifically includes:
[0027] An extraction unit, used to extract a preset number of scene images from the public data set and network images;
[0028] A cropping and splicing unit, used for cropping and splicing the extracted scene images;
[0029] The inserting unit is used to insert English alphabet and Arabic numeral table pictures into the spliced pictures.
[0030] Optionally, it also includes: a data enhancement processing module, which is used to perform data enhancement processing on scene images in training samples.
[0031] Optionally, the Textformer model includes an encoder and a decoder; the encoder is a SwinTransformer model, and the decoder is a Transformer model.
[0032] According to the specific embodiments provided by the present invention, the present invention discloses the following technical effects:
[0033] The present invention provides a scene text recognition method, comprising: constructing a training sample; the training sample includes a public data set and a non-public data set; the public data set and the non-public data set both include a scene picture and a label file; training a Yolov5 model through the training sample to obtain a character detection model; the input of the character detection model is a scene picture, and the output is all the single characters contained in the scene picture; training a Textformer model through the training sample and all the single characters recognized by the character detection model; and recognizing the text area and single characters in the scene picture to be recognized through the trained Textformer model. The present invention realizes an end-to-end text locator using only "Transoformer", which has higher efficiency than the traditional method of using a convolutional neural network to extract picture features and then inputting them into "Transformer". In addition, since only "Transformer" is used, the model framework of the present invention is simpler. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0035] Figure 1 A flow chart of a scene text recognition method according to an embodiment of the present invention;
[0036] Figure 2 The process of training Textformer;
[0037] Figure 3 This is a splicing effect diagram;
[0038] Figure 4 This is the structure diagram of Transformer;
[0039] Figure 5 This is the structural diagram of Multi-HeadAttention;
[0040] Figure 6 Schematic diagram of residual connection;
[0041] Figure 7 It is a schematic diagram of layer processing;
[0042] Figure 8 This is the framework diagram of Swin Transformer;
[0043] Fig. 9Comparison diagram of Swin Transformer (a) and ViT (b);
[0044] Fig.10 This is the Block structure diagram of Swin Transformer;
[0045] Fig.11 This is the W-MSA rendering;
[0046] Fig.12 This is the SW-MASA rendering;
[0047] Fig.13 This is the structure diagram of Textformer;
[0048] Fig.14 A flowchart for using Textformer. DETAILED DESCRIPTION
[0049] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0051] like Figure 1 As shown, the scene text recognition method provided by the present invention comprises the following steps:
[0052] Step 101: construct training samples; the training samples include a public data set and a non-public data set; the public data set and the non-public data set both include scene images and label files.
[0053] Step 102: Train the Yolov5 model using the training samples to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the individual characters contained in the scene image.
[0054] Step 103: Train the Textformer model using training samples and all single characters recognized by the character detection model.
[0055] Step 104: Use the trained Textformer model to identify the text area and single characters in the scene image to be identified. The flowchart using the Textformer model is as follows: Fig.14 shown.
[0056] Among them, Figure 2 As shown, steps 101-102 specifically include:
[0057] In the process of training the character detection model, the present invention uses public data sets and non-public data sets (self-made data sets) to form training samples. First, select existing relevant detection models (such as Yolov5 and FasterRCN) and build the experimental environment and framework required by the model.
[0058] Then prepare the training set. The present invention uses public data sets such as ICDAR2013, scut-eng-char, and ICDAR2019 (these are all public data sets commonly used by STR) as the main training samples. These data sets all contain two types of files: pictures (all in jpg format) and label files (txt format or json format). Among them, ICDAR2013 contains 229 training pictures and 229 corresponding txt-type training labels and 233 unlabeled test pictures, and the content is English; the scut-eng-char data set contains 1162 training pictures and 1162 corresponding txt-type training labels, and the content contains English and Arabic numerals; the ICDAR2019 data set contains 20,000 training pictures and 20,000 corresponding json-type training labels and thousands of unlabeled test pictures, and the content contains English, Arabic numerals and Chinese characters. The common point of these three data sets is that their label files contain not only annotations of text areas, but also annotations of individual characters. Therefore, they are used to train the character detection model to achieve the purpose of detecting individual characters.
[0059] Since different target detection models have different requirements for the content format and file type of label files, before putting the dataset into training, the present invention needs to perform necessary processing on their labels - writing specific scripts to perform batch operations on label files (modifying the content format, file type, etc. of the labels).
[0060] In addition, the present invention extracted 1088 pictures with relatively correct shooting angles, clear targets, large text areas, and text content containing English and Arabic numerals from the ICDAR2019 dataset, the TotalText dataset, the SVT dataset, and the CUTE80 dataset. These 1088 pictures were manually cropped. The shape of the cropped picture is a rectangle, and the text area occupies approximately 90% of the entire picture area, which makes the detection target clearer. Then write a script to splice them vertically in groups of four. The principle of vertical splicing is to write a python code. The code steps are to batch read all the pictures in the folder, read four pictures at a time, and use the longest picture of the four pictures as the length of the new picture, and the sum of the widths of the four pictures as the width of the new picture. Then paste the four pictures into the new picture from top to bottom. The effect of splicing is as follows Figure 3 shown.
[0061] Finally, 272 new images were synthesized, and 8 images of the English alphabet and Arabic numerals were inserted into these images at equal intervals to form a new data set containing 280 images, which was named "artificial1".
[0062] In addition, 412 images were collected from the Internet and annotated (including objects, street photos, trademarks, text advertisements, etc.). The images previously extracted from the public dataset for splicing (originally without labels) were also annotated, and the annotated English alphabet and Arabic numeral table were also inserted into them. 1,600 samples were formed, each containing a picture and a label file. It was named "artificial2".
[0063] The present invention uses LabelImg software to label it. LabelImg software can generate label files in Yolo format or VOC format. Yolo format is used for Yolov5 training, and VOC format is used for FasterRCNN training. The present invention names the new data set "artificial". During the experiment, the present invention also expands the training set by data enhancement. The data enhancement includes: adding salt and pepper noise, adding Gaussian noise, converting to grayscale, adjusting the brightness of the picture, and the above mixed operations. The focus ratio of salt and pepper noise and the variance value of Gaussian noise can be adjusted manually. After data enhancement of the training samples, the number of samples can be expanded to more than 8 times the original. These are all achieved by writing specific python codes.
[0064] ICDAR2013, scut and self-made training sets are used to train yolov5 to detect English letters and Arabic numerals. After training, the trained weights are used on TotalText for training, so that the model can detect characters in curved text. The trained weights are then used on ICDAR2019 (numbers, English letters, Chinese characters) for training. The final weights can detect characters in horizontal text as well as characters in curved text (numbers, English letters, Chinese characters). Among them, when processing ICDAR2019, the data enhancement code is still used to expand the original 20,000 pictures to 600,000.
[0065] Wherein, step 103 specifically includes:
[0066] Training Textformer: The text positioning method of the present invention is called "Textformer", which uses the "Transformer" structure. The goal of Textformer is not only to detect and identify the content of the entire text, but also to identify individual characters in the text. In addition, multi-language recognition can be performed (the function of multi-language can only be carried out after the character detection model can detect Chinese characters and other characters. The method to achieve multi-language is training. For example, if you want to detect Chinese, you need to use a large number of pictures with Chinese text for training). In the past, the methods of using Transformer for STR basically used convolutional neural networks (CNN) to extract features of pictures and generate feature sequences, and then input the feature sequences into Transformer for text recognition. The model of the present invention only uses the Transformer structure. The scene text recognition methods that used "Transformer" before all put the labels containing the entire text area into the training. After the data set is processed by the present invention, the training labels of Textformer contain both the entire text area and the individual characters in the text area. Such training will improve the sensitivity of the model to individual characters, which is equivalent to data enhancement of the training samples, and theoretically will make the training performance and prediction performance of the model better. In addition, because the training data we gave Textformer included annotations for individual characters, it can perform as well as regular text even when detecting curved text. Transformer was originally used in the field of natural language processing (NLP). It was proposed by Google in 2017. It is mainly composed of an encoder and a decoder, and uses a self-attention mechanism.
[0067] Textformer: Its structure consists of two parts: Encoder and Decoder. Encoder is SwinTransformer, and Decoder is a standard Transformer. The structure diagram is as follows Fig.13 shown.
[0068] Inputting a text image into SwinTransformer will produce an embedding vector containing the features of the entire image. This vector is then input into a standard serial Transformer to output each individual character in the image.
[0069] The basic principle of Transformer: Transformer completely replaces the previous seq2seq (sequence to sequence) model. It uses the self-attention mechanism so that the output vector can obtain global (whole sentence) context information. Its structure is an encoder and a decoder. Figure 4 Shown is an encoder-decoder module (the short one on the left is the encoder, and the long one on the right is the decoder).
[0070] Input Embedding is the input vector, which can be the original input of the entire model or the output of the previous encoder-decoder module. Positional Encoding is position encoding, that is, the position of each character in the vector is encoded with a serial number, so that the Transformer can recognize the order relationship in the language. The structure of Multi-Head Attention is as follows Figure 5 As shown. In short, self-attention has only one set of WQ, WK, WV matrices, while Multi-HeadAttention can have multiple sets of WQ, WK, WV matrices, so that a set of input vectors can generate multiple sets of different Q, K, V vectors, and each set uses self-attention. The result of using Multi-HeadAttention is better than that of using self-attention. In this way, contextual information of different distances can be extracted. The extracted information is concat (concatenated) on each feature dimension, and then FC (fully connected layer) is used to make a classification.
[0071] In Add&Norm, Add represents residual connection, which can make the model very deep, such as Figure 6 The “+” sign in the
[0072] Norm represents the processing of the layer. The principle of layer processing is: calculate the average value m and deviation of each vector in a sequence to be processed. Then perform operations on each vector. The vector obtained after the operation is the output of the layer processing. Figure 7 As shown in the figure, the left side represents traditional batch processing, and the right side represents layer processing. d represents dimension, len represents the length of a vector, and b represents batch (a batch contains several vectors). One dimension represents one feature. Traditional batch processing is to split and process each feature, while layer processing processes each vector with its complete dimension.
[0073] FeedForward is a FC (fully connected layer) that classifies a single vector.
[0074] like Figure 8 As shown in the figure, the vector processed by the encoder will be input into the Multi-HeadAttention of the decoder along the arrow. The components of the decoder are basically the same as those of the encoder. The encoder-decoder modules in the Transformer can be used in a stacked manner. It can be seen that the encoder has two inputs, one is the output from the previous module, and the other is the input from the encoder in this module. There will be a Linear layer (expanding the vector into a linear one-dimensional vector) and a Softmax layer (predicting the probability of the character at each position) on the output of the last encoder of the entire model.
[0075] SwinTransformer: The framework of SwinTransformer is as follows Fig. 9 Compared with ViT, SwinTransfomer uses merged image blocks to construct a hierarchical Transformer, as shown in Fig. 9 As shown in the figure, it divides the input image into multiple small blocks (patches), and merges adjacent patches into a window as the depth increases. The advantage of this is that it does not need to perform self-attention on the entire image every time, but only needs to perform self-attention within each window, which greatly reduces the amount of calculation. And as the patches are merged, the amount of calculation increases linearly.
[0076] The entire SwinTransformer architecture builds 4 stages, each of which contains similar repetitive units. First, the input image of size H*W*3 (length*width*number of channels) is divided into several patches through patchpartition, where each patch size is 4x4 (unit: pixel), then the feature dimension of each patch (length*width*number of channels) is 4*4*3=48, the number of patches is (H / 4)*(W / 4), so the total feature dimension is (H / 4)*(W / 4)*48.
[0077] Stage 1: A linear embedding is used to convert the feature dimension of each patch from 48 to C (C is specified by the user), and then sent to two consecutive blocks. The total feature dimension of the stage 1 output is (H / 4)*(W / 4)*C;
[0078] Stage 2: Through a patch merging, adjacent patches are merged into 2*2 (four adjacent patches are merged into one), so that the number of sub-patch blocks becomes (H / 8)*(W / 8), and the feature dimension of a single patch after the synthesis becomes 4C. Then, linear embedding is used to compress 4C into 2C (not shown in the figure) and then sent to two consecutive blocks. The total feature dimension of the stage 2 output is (H / 8)*(W / 8)*2C;
[0079] The process of stage3 and stage4 is the same as stage2. The total feature dimension of the final output is (H / 32)*(W / 32)*8C.
[0080] Block: SwinTransformer's Block is as follows Fig.10 As shown in the figure, two consecutive blocks are shown. In SwinTransformer, two consecutive blocks are a pair. The similarities between the two blocks are that they both use LN layers (to ensure the stability of data features and accelerate model convergence), MLP (a fully connected layer with at least one hidden layer to solve nonlinear problems) and residual connections (circled plus signs in the figure). The difference is that one uses W-MSA (multi-head attention mechanism within the window) and the other uses SW-MSA (multi-head attention mechanism of shifted window).
[0081] W-MSA performs multi-attention in a window composed of adjacent patches, and the size of the window is fixed. Fig.11As shown, the small block is the patch and the large box is the window.
[0082] The problem brought by MSA is the lack of communication information between non-overlapping windows, so SW-MSA is introduced, such as Fig.12 As shown in the figure, SW-MSA changes the way the window is divided (patch fusion rules) so that the size of the window is changing.
[0083] Textformer: Its structure consists of two parts: Encoder and Decoder. Encoder is SwinTransformer, and Decoder is a standard Transformer. The structure diagram is as follows Fig.13 shown.
[0084] Inputting a text image into SwinTransformer will produce an embedding vector containing the features of the entire image. This vector is then input into a standard serial Transformer to output each individual character in the image.
[0085] The method (Textformer) provided by the present invention can not only detect and recognize the content of the entire text, but also recognize individual characters in the text. In addition, multi-language recognition can be performed. And the performance in the recognition of curved text is not inferior to that of standard shape text. Compared with the traditional method of extracting features through neural networks and then using CTC algorithms or attention mechanisms for text recognition, the present invention has higher efficiency due to the use of Transformer.
[0086] The present invention also provides a scene text recognition system, comprising:
[0087] A training sample construction module, used to construct training samples; the training samples include a public data set and a non-public data set; the public data set and the non-public data set both include scene images and label files;
[0088] A first training module is used to train the Yolov5 model through the training samples to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the individual characters contained in the scene image;
[0089] A second training module is used to train the Textformer model through training samples and all single characters recognized by the character detection model;
[0090] The recognition module is used to recognize text areas and individual characters in the scene image to be recognized through the trained Textformer model.
[0091] It also includes: a label processing module, which is used to process the label file of the public data set; the processing includes modifying the content format and file type of the label file.
[0092] The training sample construction module specifically includes:
[0093] An extraction unit, used to extract a preset number of scene images from the public data set and network images;
[0094] A cropping and splicing unit, used for cropping and splicing the extracted scene images;
[0095] The inserting unit is used to insert English alphabet and Arabic numeral table pictures into the spliced pictures.
[0096] The module also includes: a data enhancement processing module for performing data enhancement processing on scene images in training samples.
[0097] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the system disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0098] The principles and implementation methods of the present invention are described in this article using specific examples. The description of the above embodiments is only used to help understand the method and core idea of the present invention. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A scene text recognition method, It is characterized in that include: Construct training samples; The training samples include a public data set and a non-public data set; both the public data set and the non-public data set include scene images and label files; the construction of the non-public data set specifically includes: extracting a preset number of scene images from the public data set and network images; cropping and splicing the extracted scene images; inserting English alphabet and Arabic numeral images into the spliced images; The Yolov5 model is trained by the training sample to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the single characters contained in the scene image; The Textformer model is trained by training samples and all single characters recognized by the character detection model; the Textformer model includes an encoder and a decoder; the encoder is a Swin Transformer model, and the decoder is a Transformer model; a text image is input into the Swin Transformer to obtain an embedding vector containing the features of the entire image, the feature embedding vector is input into a standard serial Transformer, and finally each single character in the image is output; The trained Textformer model is used to identify text areas and single characters in the scene images to be identified.
2. The scene text recognition method according to claim 1, It is characterized in that Before training the Yolov5 model through the training samples to obtain the character detection model, the method further includes: The label file of the public data set is processed; the processing includes modifying the content format and file type of the label file.
3. The scene text recognition method according to claim 1, It is characterized in that It also includes data enhancement processing of scene images in training samples.
4. A scene text recognition system, It is characterized in that include: A training sample construction module, used to construct training samples; the training samples include a public data set and a non-public data set; the public data set and the non-public data set both include scene images and label files; The training sample construction module specifically includes: an extraction unit, used to extract a preset number of scene pictures from the public data set and network pictures; a cropping and splicing unit, used to crop and splice the extracted scene pictures; an insertion unit, used to insert English alphabet and Arabic numeral table pictures into the spliced pictures; A first training module is used to train the Yolov5 model through the training samples to obtain a character detection model; the input of the character detection model is a scene image, and the output is all the individual characters contained in the scene image; The second training module is used to train the Textformer model through training samples and all single characters recognized by the character detection model; the Textformer model includes an encoder and a decoder; the encoder is a SwinTransformer model, and the decoder is a Transformer model; the text image is input into the Swin Transformer to obtain an embedding vector containing the features of the entire image, the feature embedding vector is input into a standard serial Transformer, and each single character in the image is finally output; The recognition module is used to recognize text areas and individual characters in the scene image to be recognized through the trained Textformer model.
5. The scene text recognition system according to claim 4, It is characterized in that Also includes: The label processing module is used to process the label file of the public data set; the processing includes modifying the content format and file type of the label file.
6. The scene text recognition system according to claim 4, It is characterized in that Also includes: The data enhancement processing module is used to perform data enhancement processing on the scene images in the training samples.
