Method for detecting characters in image, electronic equipment and medium
By using the preset text detection model, high-order semantic estimation module, and detail prediction optimization module in the image, the problem of being unable to obtain refined masks for text in the image in the existing technology is solved, and accurate text and its background are achieved. detection and output.
Patent Information
- Application Number
- CN202410613697.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-17
- Publication Date
- 2024-08-23
AI Technical Summary
The existing technology cannot obtain the refined mask of the text in the image, and can only output the position of the rectangular area where the text is located.
The preset text detection model is used to detect the text image to be detected. Through the high-order semantic estimation module and the detail prediction optimization module, multiple scales of detail features are extracted to generate a refined text mask image.
It achieves accurate detection of text and the solid color background of the area where it is located. The output is no longer a simple rectangular area, but a refined text mask, which is suitable for text outline extraction, vectorized image text, and subtitle animation special effects. .
Smart Images

Figure CN118537866A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, electronic device and medium for detecting text in an image. Background Art
[0002] The task of text detection is to find the location of text in an image or video, which is the basis of text recognition. In recent years, with the advancement of deep learning algorithms, text detection and positioning based on deep learning algorithms have also made significant progress.
[0003] In the related art, the text detection and positioning in the text image based on the deep learning algorithm can only output the rectangular area where the text is located. However, in the related art, the text detection algorithm cannot obtain the refined text mask in the text image. Summary of the invention
[0004] The purpose of this application is to provide a method, electronic device and medium for detecting text in an image in order to solve the above-mentioned technical problems existing in the related art in view of the above-mentioned deficiencies in the prior art.
[0005] To achieve the above purpose, the technical solution adopted in the embodiment of the present application is as follows:
[0006] In a first aspect, an embodiment of the present application provides a method for detecting text in an image, comprising:
[0007] Acquire a text image to be detected, wherein the text image to be detected includes text and a scene background of an area where the text is located;
[0008] Using a preset text detection model, the text image to be detected is detected to obtain a text mask image for the text image to be detected, wherein the text mask image includes the text and a solid color background of the area where the text is located;
[0009] The text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: a plurality of sample text images, and a sample text mask image corresponding to each of the sample text images.
[0010] Optionally, the text detection model includes a connected high-order semantic estimation module and a detail prediction optimization module; the use of a preset text detection model to detect the text image to be detected to obtain a text mask image for the text image to be detected includes:
[0011] Using the high-order semantic estimation module, the text image to be detected is processed to obtain a text mask outline image;
[0012] The detail prediction optimization module is used to process the text mask outline image and the to-be-detected text image to obtain the text mask image.
[0013] Optionally, the high-order semantic estimation module includes a backbone network, a convolutional pooling pyramid network, and a convolutional activation network connected in sequence; the high-order semantic estimation module is used to process the text image to be detected to obtain a text mask outline map, including:
[0014] The backbone network is used to extract feature information from the text image to be detected; the convolutional pooling pyramid network is used to extract detail features of multiple scales from the feature information;
[0015] The convolutional activation network is used to determine the text mask outline according to the detail features of the multiple scales.
[0016] Optionally, the adopting the detail prediction optimization module to process the text mask outline image and the to-be-detected text image to obtain the text mask image includes:
[0017] The detail prediction optimization module is used to process the text mask outline image, the feature information and the text image to be detected to obtain the text mask image.
[0018] Optionally, the initial text detection model includes an initial high-order semantic estimation module and an initial detail prediction optimization module connected to each other; the text detection model is trained in the following manner:
[0019] Acquire the preset data set;
[0020] Using the initial high-order semantic estimation module, the sample text image is processed to obtain a sample text mask outline image;
[0021] Using the initial detail prediction optimization module, the sample text mask outline image and the sample text image are processed to obtain an intermediate text mask image;
[0022] According to the intermediate text mask image and the sample text mask image corresponding to the sample text image, the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module are updated until the preset training termination conditions are met to obtain the text detection model.
[0023] Optionally, updating the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image includes:
[0024] Using the first loss function calculation formula corresponding to the initial high-order semantic estimation module, calculate the first loss function value according to the sample text mask outline diagram and the sample text mask image;
[0025] Using the second loss function calculation formula corresponding to the initial detail prediction optimization module, calculate the second loss function value according to the intermediate text mask image and the sample text mask image;
[0026] Using a third loss function calculation formula corresponding to the initial text detection model, and calculating a third loss function value according to the first loss function value and the second loss function value;
[0027] Updating the model parameters of the initial high-order semantic estimation module according to the first loss function value and the third loss function value;
[0028] The model parameters of the initial detail prediction optimization are updated according to the second loss function value and the third loss function value.
[0029] Optionally, the preset data set includes: a first type of data set, the first type of data set includes a plurality of first sample text images and a first sample text mask image corresponding to each of the first sample text images, and acquiring the preset data set includes:
[0030] Acquire a plurality of first sample text images, wherein information of regions where sample text annotated in the first sample text images are located;
[0031] According to the information of the area where the sample text is located, the first sample text image is processed to obtain a sample text sub-image;
[0032] Processing the sample text sub-image to obtain a binary sample text initial mask image;
[0033] The sample text initial mask image is processed according to the normal direction of the edge contour to determine the first sample text mask image.
[0034] Optionally, the preset data set includes: a second type of data set, the second type of data set includes a plurality of second sample text images and a second sample text mask image corresponding to each of the second sample text images, and acquiring the preset data set includes:
[0035] Get multiple sample background images;
[0036] Draw and generate multiple sample text images according to multiple text parameters; wherein the multiple text parameters include a combination of at least two of the following parameters: font parameter, size parameter, color parameter, shadow parameter, stroke parameter, and expression parameter;
[0037] synthesizing a plurality of the second sample text images according to the plurality of the sample background images and the plurality of the sample text images;
[0038] The second sample text mask image is determined according to the multiple sample text images.
[0039] In a second aspect, an embodiment of the present application further provides a device for detecting text in an image, comprising:
[0040] An acquisition module is used to acquire a text image to be detected, wherein the text image to be detected includes text and a scene background of an area where the text is located;
[0041] A detection module is used to use a preset text detection model to detect the text image to be detected, and obtain a text mask image for the text image to be detected, wherein the text mask image includes the text and the solid color background of the area where the text is located; wherein the text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: multiple sample text images, and sample text mask images corresponding to each of the sample text images.
[0042] Optionally, the text detection model includes a connected high-order semantic estimation module and a detail prediction and optimization module; the detection module is specifically used to use the high-order semantic estimation module to process the text image to be detected to obtain a text mask outline image; and use the detail prediction and optimization module to process the text mask outline image and the text image to be detected to obtain the text mask image.
[0043] Optionally, the high-order semantic estimation module includes a backbone network, a convolutional pooling pyramid network, and a convolutional activation network connected in sequence; the detection module is specifically used to use the backbone network to extract feature information of the text image to be detected; use the convolutional pooling pyramid network to extract detailed features of multiple scales from the feature information; use the convolutional activation network to determine the text mask outline according to the detailed features of the multiple scales.
[0044] Optionally, the detection module is specifically configured to use the detail prediction optimization module to process the text mask outline image, the feature information and the text image to be detected to obtain the text mask image.
[0045] Optionally, the initial text detection model includes an initial high-order semantic estimation module and an initial detail prediction optimization module connected to each other; the text detection model is trained in the following manner:
[0046] A first acquisition module, used to acquire the preset data set;
[0047] A processing module, used to process the sample text image using the initial high-order semantic estimation module to obtain a sample text mask outline image;
[0048] Using the initial detail prediction optimization module, the sample text mask outline image and the sample text image are processed to obtain an intermediate text mask image;
[0049] An updating module is used to update the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image, until the preset training termination conditions are met to obtain the text detection model.
[0050] Optionally, the update module is specifically used to adopt the first loss function calculation formula corresponding to the initial high-order semantic estimation module to calculate the first loss function value according to the sample text mask outline image and the sample text mask image; adopt the second loss function calculation formula corresponding to the initial detail prediction optimization module to calculate the second loss function value according to the intermediate text mask image and the sample text mask image; adopt the third loss function calculation formula corresponding to the initial text detection model to calculate the third loss function value according to the first loss function value and the second loss function value; update the model parameters of the initial high-order semantic estimation module according to the first loss function value and the third loss function value; update the model parameters of the initial detail prediction optimization according to the second loss function value and the third loss function value.
[0051] Optionally, the preset data set includes: a first type of data set, the first type of data set includes multiple first sample text images and first sample text mask images corresponding to each of the first sample text images, the first acquisition module is specifically used to acquire the multiple first sample text images, wherein the first sample text images include information about the area where the sample text is marked; based on the information about the area where the sample text is located, the first sample text images are processed to obtain sample text sub-images; the sample text sub-images are processed to obtain a binary sample text initial mask image; based on the normal direction of the edge contour, the sample text initial mask image is processed to determine the first sample text mask image.
[0052] Optionally, the preset data set includes: a second type of data set, the second type of data set includes multiple second sample text images and second sample text mask images corresponding to each second sample text image, and the first acquisition module is specifically used to obtain multiple sample background images; based on multiple text parameters, draw and generate multiple sample text images; wherein the multiple text parameters include a combination of at least two of the following parameters: font parameters, size parameters, color parameters, shadow parameters, stroke parameters, expression parameters; based on multiple sample background images and multiple sample text images, synthesize multiple second sample text images; based on the multiple sample text images, determine the second sample text mask image.
[0053] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a memory and a processor, wherein the memory stores a computer program executable by the processor, and when the processor executes the computer program, the method for detecting text in an image as described in any one of the first aspects above is implemented.
[0054] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is read and executed, the method for detecting text in an image as described in any one of the first aspects above is implemented.
[0055] The beneficial effects of the present application are as follows: the embodiment of the present application provides a method for detecting text in an image, including: obtaining a text image to be detected, the text image to be detected includes text, and the scene background of the area where the text is located; using a preset text detection model to detect the text image to be detected, and obtaining a text mask image for the text image to be detected, the text mask image includes text and the solid color background of the area where the text is located; wherein the text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: multiple sample text images, and sample text mask images corresponding to each sample text image. Using a pre-trained text detection model, a text mask image including text and the solid color background of the area where the text is located can be accurately output, that is, the output is no longer a simple area where the text is located, but a refined text mask. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0057] Figure 1A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 1 ;
[0058] Figure 2 A structural diagram of a text detection model provided in an embodiment of the present application;
[0059] Figure 3 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 2 ;
[0060] Figure 4 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 3 ;
[0061] Figure 5 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 4 ;
[0062] Figure 6 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 5 ;
[0063] Figure 7 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 6 ;
[0064] Figure 8 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 7 ;
[0065] Fig. 9 A schematic diagram of the structure of a text detection device in an image provided by an embodiment of the present application;
[0066] Fig.10 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0067] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments.
[0068] Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the present application for which protection is sought, but merely represents selected embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without creative work are within the scope of protection of the present application.
[0069] In the description of the present application, it should be noted that if the terms "upper", "lower", etc. appear, the orientation or position relationship indicated is based on the orientation or position relationship shown in the drawings, or is the orientation or position relationship in which the product of the application is usually placed when used. It is only for the convenience of describing the present application and simplifying the description, and does not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation on the present application.
[0070] In addition, the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0071] It should be noted that, in the absence of conflict, the features in the embodiments of the present application may be combined with each other.
[0072] An embodiment of the present application provides a method for detecting text in an image, which is applied to an electronic device, which may be a terminal device or a server. The terminal device may be any of the following: a computer, a laptop computer, a tablet computer, a smart phone, etc.
[0073] The following is an explanation of a method for detecting text in an image provided in an embodiment of the present application.
[0074] Figure 1 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 1 ,like Figure 1 As shown, the method may include:
[0075] S101, obtaining a text image to be detected.
[0076] The text image to be detected includes text and the scene background of the area where the text is located.
[0077] In an embodiment of the present application, a text image to be detected can be obtained in response to a user's input operation; the text image to be detected can also be obtained randomly or according to a preset order from a set of images to be processed; the text image to be detected can also be obtained in other ways, and the embodiment of the present application does not impose specific restrictions on this.
[0078] It should be noted that the text can be in a variety of styles, such as stickers, light shadows, animated subtitles, etc. The scene background of the area where the text is located can be rich text and patterns, etc.
[0079] S102 : using a preset text detection model to detect the text image to be detected, and obtaining a text mask image for the text image to be detected.
[0080] The text mask image includes text and a solid color background of the area where the text is located. For example, the solid color background of the area where the text is located can be a white background or a black background.
[0081] It is worth noting that compared with the scene background in the text image to be detected, the solid color background of the text area is simpler and can better highlight the text. In practical applications, the text mask image can be well used in scenes such as text contour extraction, vectorized image text, subtitle animation effects, and automatic erasing of video subtitles.
[0082] In addition, the text detection model is a model obtained by training the initial text detection model using a preset data set, and the preset data set includes: multiple sample text images, and sample text mask images corresponding to each sample text image. Before the above-mentioned process of S101 or S102, the text detection model can be trained first. Among them, the multiple sample text images may include: sample text, and a sample scene background of the area where the sample text is located, and the sample text mask image may include: sample text, and a sample solid color background.
[0083] In some implementations, the text image to be detected is input into a preset text detection model; the text detection model is used to detect the text image to be detected, and the text detection model can output a text mask image for the text image to be detected.
[0084] In summary, the embodiment of the present application provides a method for detecting text in an image, including: obtaining a text image to be detected, the text image to be detected includes text and the scene background of the area where the text is located; using a preset text detection model to detect the text image to be detected, and obtaining a text mask image for the text image to be detected, the text mask image includes text and the solid color background of the area where the text is located; wherein the text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: multiple sample text images, and sample text mask images corresponding to each sample text image. Using a pre-trained text detection model, a text mask image including text and the solid color background of the area where the text is located can be accurately output, that is, the output is no longer a simple area where the text is located, but a refined text mask.
[0085] Optionally, Figure 2 A structural diagram of a text detection model provided in an embodiment of the present application is shown in FIG. Figure 2 As shown, the text detection model includes a connected high-order semantic estimation module and a detail prediction optimization module.
[0086] Figure 3 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 2 ,like Figure 3 As shown, a preset text detection model is used to detect the text image to be detected, and a text mask image for the text image to be detected is obtained, including:
[0087] S201, using a high-order semantic estimation module to process the text image to be detected to obtain a text mask outline.
[0088] S202, using a detail prediction optimization module to process the text mask outline image and the text image to be detected to obtain a text mask image.
[0089] In some embodiments, the text image to be detected is input into the high-order semantic estimation module, and the network structure in the high-order semantic estimation module is used to process the text image to be detected to obtain a text mask outline. The high-order semantic estimation module is used to output the text mask outline to the detail prediction optimization module, and the detail prediction optimization module is used to receive the text mask outline, and then the text mask outline and the text image to be detected are processed to obtain a text mask image.
[0090] Alternatively, if Figure 2 As shown in Figure 1, the high-order semantic estimation module includes a backbone network, a convolutional pooling pyramid network, and a convolutional activation network, which are connected in sequence.
[0091] Figure 4 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 3 ,like Figure 4 As shown, the process of using the high-order semantic estimation module in the above S201 to process the text image to be detected to obtain the text mask outline image may include:
[0092] S301, using a backbone network to extract feature information from the text image to be detected.
[0093] In order to optimize the performance, ResNet (Residual Network, a deep residual network structure) or MobileNet is used as the backbone network on different target platforms to extract feature information from the text image to be detected.
[0094] It should be noted that the network structure of ResNet can be divided into multiple stages, each of which contains multiple residual modules. In each stage, the size of the feature map gradually decreases, but the number of channels gradually increases. The MobileNet network focuses on lightweight CNN (Convolutional Neural Networks) in mobile or embedded devices. Compared with traditional convolutional neural networks, it greatly reduces model parameters and computational complexity while slightly reducing accuracy.
[0095] S302: Use a convolutional pooling pyramid network to extract detailed features of multiple scales from feature information.
[0096] Among them, the convolutional pooling pyramid network can be ASPP (Atrous Spatial Pyramid Pooling, efficient atrous spatial convolutional pooling pyramid).
[0097] In some implementations, a backbone network is used to output feature information to a convolutional pooling pyramid network, a convolutional pooling pyramid network is used to obtain feature information, and detailed features of multiple scales are extracted from the feature information.
[0098] It is worth noting that ASPP can process detail features of different scales and improve the segmentation accuracy of the model by combining feature information extracted from receptive fields of different sizes. Feature information is extracted to obtain finer features, namely, detail features of multiple scales. Detail features of multiple scales can be considered as feature information of multiple levels.
[0099] S303, using a convolutional activation network to determine a text mask outline according to detail features at multiple scales.
[0100] Among them, a convolutional pooling pyramid network is used to output details of multiple scales to a convolutional activation network; a convolutional activation network is used to obtain details of multiple scales, and a text mask outline is determined based on the detail features of multiple scales.
[0101] In an embodiment of the present application, a convolutional activation network is used to perform convolution and Sigmoid activation (an activation function of a neural network that maps variables between 0 and 1) on detail features of multiple scales, extract high-order semantic segmentation results, and obtain a text mask outline.
[0102] Alternatively, if Figure 2 As shown, the output of the backbone network and the output of the convolutional activation network are both connected to the detail prediction optimization module. The text image to be detected is input into the detail prediction optimization module.
[0103] The process of using the detail prediction optimization module in the above S202 to process the text mask outline image and the text image to be detected to obtain the text mask image may include:
[0104] The detail prediction optimization module is used to process the text mask outline, feature information and the text image to be detected to obtain the text mask image.
[0105] In some embodiments, the text image to be detected is input into the detail prediction and optimization module, the backbone network is used to output feature information to the detail prediction and optimization module, and the convolutional activation network is used to output the text mask outline to the detail prediction and optimization module. The detail prediction and optimization module can obtain the text mask outline, feature information and the text image to be detected, and then process the text mask outline, feature information and the text image to be detected, and jointly optimize the text mask outline by fusing the original text image to be detected, high-level semantics and feature information to obtain the text mask image.
[0106] It is worth noting that, in response to the problem that refined text masks cannot be directly obtained in related technologies, in the embodiments of the present application, a convolutional pooling pyramid network is set up to introduce the idea of image cutout into the text detection processing process, and an end-to-end algorithm model, namely a text detection model, is designed. The atrous space convolutional pooling pyramid is used to capture and fuse feature information of multi-scale and different fields of view. At the same time, in order to minimize the amount of calculation, the model is split into two stages, namely a high-order semantic estimation module and a detail prediction optimization module. The text detection process is realized by coarse-to-fine and cascade fusion optimization. The text detection model can obtain pixel-level text masks.
[0107] Among them, image matting is one of the basic tasks in the field of computer vision, which aims to separate the foreground object of interest from a given image and filter the background part. Its mathematical definition is:
[0108] I i =α i F i +(1-α i )B i α i ∈[0,1]
[0109] Among them, I i 、F i , B i , α i Represent the given image, foreground object, background part and transparency respectively.
[0110] Optionally, the initial text detection model includes an initial high-order semantic estimation module and an initial detail prediction optimization module that are connected, wherein the text detection model is trained using an end-to-end approach. Figure 5 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 4 ,like Figure 5 As shown, the above text detection model is trained in the following way:
[0111] S401: Obtain a preset data set.
[0112] The preset data set includes: a plurality of sample text images, and a sample text mask image corresponding to each of the sample text images.
[0113] S402: using an initial high-order semantic estimation module to process the sample text image to obtain a sample text mask outline image.
[0114] Among them, the initial high-order semantic estimation module includes: an initial backbone network, an initial convolutional pooling pyramid network, and an initial convolutional activation network connected in sequence.
[0115] In some embodiments, an initial backbone network is used to extract sample feature information from a sample text image; an initial convolutional pooling pyramid network is used to extract sample detail features of multiple scales from the sample feature information; and an initial convolutional activation network is used to determine a sample text mask outline based on sample detail features of multiple scales.
[0116] S403, using the initial detail prediction optimization module to process the sample text mask outline image and the sample text image to obtain an intermediate text mask image.
[0117] Among them, an initial detail prediction optimization module is used to process the sample text mask outline, the sample text image and the sample text mask outline to obtain an intermediate text mask image.
[0118] S404. Update the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image, until the preset training termination condition is met to obtain a text detection model.
[0119] In an embodiment of the present application, the loss function value is calculated based on the intermediate text mask image and the sample text mask image corresponding to the sample text image, and the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module are updated according to the loss function value until the preset training termination conditions are met to obtain a text detection model.
[0120] In practical applications, in order to achieve the optimal balance between performance and effect, this article uses different backbone networks for different platforms. For platforms with three operating systems such as Linux / Windows / macOS, ResNet is used as the backbone network, while for mobile and embedded platforms such as iOS / Android, MobileNetV2 is used as the backbone network.
[0121] In some embodiments, an end-to-end model training is performed on an Nvidia V100 32G GPU (graphics processing unit) using a preset data set, and parameter optimization is performed using an Adam (an adaptive learning rate optimizer based on a gradient descent algorithm) optimizer, with a batch size set to 16. During the training process, the parameter learning rate is saved as 0.01 for the first 20 epochs (the number of times the entire training data set is completely traversed by the neural network), and then the learning rate is reduced by 10 times every 10 epochs.
[0122] Figure 6 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 5 ,like Figure 6 As shown, the process of updating the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image in S404 may include:
[0123] S501, using a first loss function calculation formula corresponding to the initial high-order semantic estimation module, and calculating a first loss function value according to the sample text mask outline diagram and the sample text mask image;
[0124] It should be noted that the calculation formula of the first loss function corresponding to the initial high-order semantic estimation module can be expressed as:
[0125]
[0126] Among them, L s Represents the first loss function value, S p Represents the sample text mask outline, G(α g ) represents the sample text mask image after 16x16 downsampling.
[0127] S502, using a second loss function calculation formula corresponding to the initial detail prediction optimization module, and calculating a second loss function value according to the intermediate text mask image and the sample text mask image;
[0128] It should be noted that the calculation formula of the second loss function corresponding to the initial detail prediction optimization module can be expressed as:
[0129] L d =||D p -α g || 1
[0130] Among them, L d Represents the second loss function value, D p Represents the intermediate text mask image, α g Represents a sample text mask image.
[0131] S503: Use the third loss function calculation formula corresponding to the initial text detection model to calculate the third loss function value according to the first loss function value and the second loss function value.
[0132] In the embodiment of the present application, the third loss function calculation formula corresponding to the initial text detection model can be expressed as:
[0133] L=λ s L s +λ d L d
[0134] Among them, L s Represents the first loss function value, L d represents the second loss function value, L represents the third loss function value, λ s , d Represents the model hyperparameters of the initial text detection model. For example, they can be set to 1.0 and 5.0 respectively.
[0135] S504: Update the model parameters of the initial high-order semantic estimation module according to the first loss function value and the third loss function value.
[0136] S505. Update the model parameters of the initial detail prediction optimization according to the second loss function value and the third loss function value.
[0137] It should be noted that updating the model parameters of the initial high-order semantic estimation module and updating the model parameters of the initial detail prediction optimization can also be considered as updating the model parameters of the initial text detection model, until the first loss function value, the second loss function value, and the third loss function value converge, or the number of iterations is greater than or equal to the preset number of iterations, it is determined that the preset training termination conditions are met, and the text detection model is obtained.
[0138] Optionally, the preset data set includes: a first type of data set, the first type of data set includes a plurality of first sample text images and a first sample text mask image corresponding to each first sample text image. Figure 7 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 6,like Figure 7 As shown, the process of obtaining the preset data set in the above S401 may include:
[0139] S601: Acquire a plurality of first sample text images.
[0140] The first sample text image is annotated with information about the area where the sample text is located.
[0141] In some embodiments, the plurality of first sample text images refer to sample text images in datasets such as HierText, TotalText, and SynthText. Among them, the HierText dataset refers to a dataset with hierarchical annotations, the first dataset characterized by hierarchical annotations of text in natural scenes and documents. TotalText is an English curve text dataset based on word level, a text detection dataset consisting of 1555 images, covering more than three different text directions (horizontal, multi-directional and curved). SynthText is a synthetic text image dataset, originally used for scene text detection. This dataset provides not only text-level labels, but also character-level annotations.
[0142] S602: Process the first sample text image according to the information of the area where the sample text is located to obtain a sample text sub-image.
[0143] In an embodiment of the present application, the first sample text image is cropped according to information about a region where the marked sample text is located in the first sample text image to obtain at least one sample text sub-image.
[0144] S603: Process the sample text sub-image to obtain a binary sample text initial mask image.
[0145] In some implementations, color filtering, adaptive threshold selection, and edge contour extraction methods are used to sequentially process text sub-images to obtain a binary sample text initial mask image.
[0146] S604: Process the sample text initial mask image according to the normal direction of the edge contour to determine a first sample text mask image.
[0147] It should be noted that, according to the normal direction of the edge contour, the sample text initial mask image is eroded and expanded to obtain a ternary image; the sample text sub-image and the ternary image are input into the deep image matting algorithm (a cutout algorithm) to obtain the initial text mask image. The initial text mask image is manually checked and modified to obtain a pixel-level text mask, i.e., the first sample text mask image.
[0148] It is worth noting that, in view of the lack of refined text masks in existing data sets, the above-mentioned process from S501 to S504 is adopted in the embodiment of the present application to supplement the first sample text mask images for the multiple first sample text images, which provides a basis for the training of the model. The text detection model trained based on the multiple first sample text images and the first sample text mask images can output the text mask image according to the text image to be detected.
[0149] Optionally, the preset data set includes: a second type of data set, the second type of data set includes a plurality of second sample text images and a second sample text mask image corresponding to each second sample text image.
[0150] Figure 8 A flowchart of a method for detecting text in an image provided in an embodiment of the present application Figure 7 ,like Figure 8 As shown, the process of obtaining the preset data set in the above S401 may include:
[0151] S701. Obtain multiple sample background images.
[0152] In some implementations, a preset number of images from the Internet are obtained using mainstream AIGC (Artificial Intelligence Graphics Computing) tools and script crawling methods as text background images. For example, the preset number may be 20,000.
[0153] S702: Draw and generate multiple sample text images according to multiple text parameters.
[0154] The plurality of text parameters include a combination of at least two of the following parameters: font parameter, size parameter, color parameter, shadow parameter, stroke parameter, and expression parameter.
[0155] In an embodiment of the present application, a drawing engine is used to generate multiple sample text images with transparency based on at least two of the font parameters, size parameters, color parameters, shadow parameters, stroke parameters, and expression parameters. For example, 20,000 sample text images of various styles can be obtained.
[0156] S703: synthesize a plurality of second sample text images according to a plurality of sample background images and a plurality of sample text images.
[0157] It should be noted that, multiple sample background images and multiple sample text images are selected using random seeds for transparency mixing, and multiple second sample text images are synthesized. For example, 20,000 second sample text images can be obtained, of which 18,000 are randomly selected to add to the training data set, and 2,000 are used as the test data set.
[0158] S704: Determine a second sample text mask image according to a plurality of sample text images.
[0159] The preset data set may include: a plurality of second sample text images and a second sample text mask image.
[0160] In the embodiments of the present application, for various subtitles that appear in scenes such as short video social networking and live streaming, various styles of text such as flower characters, expressions, shadows, and strokes are supplemented, and a proprietary text training and test data set suitable for the text detection model is constructed, and its data expansion. The text detection model trained in this way detects text mask images for styles with richer text forms, such as text in the form of sticker flower characters, light shadows, animated subtitles, etc., and the scene background of the area where the text is located can be a rich text and pattern of the text image to be detected.
[0161] In the text detection model deployment stage: In order to maximize processing efficiency and maximize the versatility of the algorithm, the embodiment of the present application will select the optimal model deployment inference framework and model pre- and post-processing based on parallel computing according to the GPU hardware capabilities and operating system of the specific target platform, and use hardware acceleration and heterogeneous systems to complete the entire process.
[0162] Specifically, for Windows and Linux PC systems (personal computer systems), for Nvidia GPU (a type of GPU), choose to use TensorRT (a high-performance deep learning inference engine) + CUDA (a general parallel computing platform and programming model), for AMD GPU (a type of GPU), choose ROCm (an open source stack for GPU computing) + OpenCL (an open standard parallel computing framework), and for others, choose OpenVINO (a cross-platform deep learning toolkit) + TBB (Thread Building Blocks). In Android mobile systems, for Qualcomm SoC (a type of processing system, SoC is a complete processing system contained in a single package), choose to use QNN (quantum neural network) + OpenCL, for MediaTek SoC (a type of processing system), choose NeuroPilot (an artificial intelligence platform based on heterogeneous computing) + OpenCL, and for others, choose ArmNN (an inference engine) + OpenCL. In Apple iOS, iPadOS and macOS systems, choose to use CoreML (a framework for machine learning that does not require an Internet connection) + Metal (a rendering / heterogeneous computing framework for iOS systems).
[0163] Taking the application of the text detection method in the image in the video editing scene of the Nvidia GPU hardware system as an example, a specific implementation example of the method in this paper is given as follows:
[0164] Using TensorRT SDK (Software Development Kit, software development kit) tool, converts the trained network model weights into TensorRT special format, performs automatic operator merging and other performance optimization processing on the target hardware device platform to obtain high-precision model weights; for high-performance and high-throughput application scenarios, TensorRT quantitative perception training is performed on the target platform and a 20% text detection cutout data set is used to further improve the algorithm's running speed and obtain low-precision model weights; a hardware-accelerated video decoder is used to decode various formats of video streams such as H264 / H265 / AV1 (a format) to obtain video image data in the video memory; Cuda is used to write custom kernel functions to implement the pre- and post-processing process of the model input, and the video image content is directly scaled and channel converted in the video memory to obtain the model input data; TensorRT is used to perform model forward reasoning, and after CUDA model post-processing, a pixel-level text target mask located in the video memory is obtained; the text target mask image in the video memory is processed or saved according to the specific scene requirements, such as applying text masks to realize animated subtitles, and the video memory image content can be directly shared with the rendering engine, and the animation engine and custom shaders are used to achieve specific effects.
[0165] In summary, the embodiment of the present application provides a method for detecting text in an image, which uses a pre-trained text detection model to accurately output a text mask image including text and a solid color background of the area where the text is located, that is, the output is no longer a simple area where the text is located, but a refined text mask. Among them, the segmentation-based text detection algorithm is expanded and optimized, and the text detection model directly predicts and outputs a sub-pixel text mask, which can not only greatly reduce the difficulty of subsequent text recognition, but also can be reused in other scenarios, including text contour extraction, vectorized image text, and the realization of subtitle animation effects based on text mask.
[0166] Among the segmentation-based text detection algorithms, DBNet (segmentation-based text detection network) is widely used due to its good detection accuracy and computing performance. DBNet designs an end-to-end differentiable binarization model. By allowing the network to autonomously learn and optimize the threshold used for binarization during the training process, it not only improves the detection accuracy, but also simplifies the post-processing and improves the detection performance. DBNet++ (the latest text detection algorithm, a better model obtained by making some optimizations on the basis of DBnet) adds the Adaptive Scale Fusion (ASF) process on the basis of DBNet, and uses the spatial attention mechanism to fuse features at different scales, achieving higher detection accuracy. However, ASF requires features of different scales to be upsampled to the same scale space, which requires a certain amount of calculation.
[0167] In addition, we use our own synthesis engine to expand the data set, supplement the common styles of text in various videos, such as flower characters, expressions, shadows, strokes, etc. Based on HierText, TotalText and SynthText, we construct a cutout text data set through synthesis and manual methods, that is, a preset data set, and train an efficient text detection model, which can well detect and segment text in various video scenes today;
[0168] Moreover, it makes full use of the efficient and powerful parallel pipelines of modern processors and the multi-threaded processing capabilities of heterogeneous systems, and uses hardware acceleration to complete the entire process of text detection and cutout calculations in an end-to-end manner, greatly improving the processing efficiency of the algorithm, effectively reducing the CPU (Central Processing Unit) load, and meeting real-time processing requirements.
[0169] The following describes the text detection device, electronic device, storage medium, etc. used to execute the text detection method in the image provided by the present application. Its specific implementation process and technical effects can be found in the relevant content of the text detection method in the above-mentioned image, which will not be repeated below.
[0170] Fig. 9 A schematic diagram of the structure of a text detection device in an image provided by an embodiment of the present application is shown in FIG. Fig. 9 As shown, the device may include:
[0171] The acquisition module 101 is used to acquire a text image to be detected, wherein the text image to be detected includes text and a scene background of an area where the text is located;
[0172] The detection module 102 is used to use a preset text detection model to detect the text image to be detected, and obtain a text mask image for the text image to be detected, wherein the text mask image includes the text and the solid color background of the area where the text is located; wherein the text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: multiple sample text images, and sample text mask images corresponding to each of the sample text images.
[0173] Optionally, the text detection model includes a connected high-order semantic estimation module and a detail prediction optimization module; the detection module 102 is specifically used to use the high-order semantic estimation module to process the text image to be detected to obtain a text mask outline image; and use the detail prediction optimization module to process the text mask outline image and the text image to be detected to obtain the text mask image.
[0174] Optionally, the high-order semantic estimation module includes a backbone network, a convolutional pooling pyramid network, and a convolutional activation network connected in sequence; the detection module 102 is specifically used to use the backbone network to extract feature information of the text image to be detected; use the convolutional pooling pyramid network to extract detailed features of multiple scales from the feature information; use the convolutional activation network to determine the text mask outline according to the detailed features of the multiple scales.
[0175] Optionally, the detection module 102 is specifically configured to use the detail prediction optimization module to process the text mask outline image, the feature information and the text image to be detected to obtain the text mask image.
[0176] Optionally, the initial text detection model includes an initial high-order semantic estimation module and an initial detail prediction optimization module connected to each other; the text detection model is trained in the following manner:
[0177] A first acquisition module, used to acquire the preset data set;
[0178] A processing module, used to process the sample text image using the initial high-order semantic estimation module to obtain a sample text mask outline image;
[0179] Using the initial detail prediction optimization module, the sample text mask outline image and the sample text image are processed to obtain an intermediate text mask image;
[0180] An updating module is used to update the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image, until the preset training termination conditions are met to obtain the text detection model.
[0181] Optionally, the update module is specifically used to adopt the first loss function calculation formula corresponding to the initial high-order semantic estimation module to calculate the first loss function value according to the sample text mask outline image and the sample text mask image; adopt the second loss function calculation formula corresponding to the initial detail prediction optimization module to calculate the second loss function value according to the intermediate text mask image and the sample text mask image; adopt the third loss function calculation formula corresponding to the initial text detection model to calculate the third loss function value according to the first loss function value and the second loss function value; update the model parameters of the initial high-order semantic estimation module according to the first loss function value and the third loss function value; update the model parameters of the initial detail prediction optimization according to the second loss function value and the third loss function value.
[0182] Optionally, the preset data set includes: a first type of data set, the first type of data set includes multiple first sample text images and first sample text mask images corresponding to each of the first sample text images, the first acquisition module is specifically used to acquire the multiple first sample text images, wherein the first sample text images include information about the area where the sample text is marked; based on the information about the area where the sample text is located, the first sample text images are processed to obtain sample text sub-images; the sample text sub-images are processed to obtain a binary sample text initial mask image; based on the normal direction of the edge contour, the sample text initial mask image is processed to determine the first sample text mask image.
[0183] Optionally, the preset data set includes: a second type of data set, the second type of data set includes multiple second sample text images and second sample text mask images corresponding to each second sample text image, and the first acquisition module is specifically used to obtain multiple sample background images; based on multiple text parameters, draw and generate multiple sample text images; wherein the multiple text parameters include a combination of at least two of the following parameters: font parameters, size parameters, color parameters, shadow parameters, stroke parameters, expression parameters; based on multiple sample background images and multiple sample text images, synthesize multiple second sample text images; based on the multiple sample text images, determine the second sample text mask image.
[0184] The above-mentioned device is used to execute the method provided by the aforementioned embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0185] The above modules may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASIC), or one or more digital singnal processors (DSP), or one or more field programmable gate arrays (FPGA). For another example, when a module is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0186] Fig.10 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Fig.10 As shown, the electronic device includes: a processor 201 and a memory 202.
[0187] The memory 202 is used to store programs, and the processor 201 calls the programs stored in the memory 202 to execute the above method embodiment. The specific implementation method and technical effect are similar and will not be repeated here.
[0188] Optionally, the present application also provides a program product, such as a computer-readable storage medium, including a program, which is used to execute the above method embodiment when executed by a processor.
[0189] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0190] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0191] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0192] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (English: processor) to perform some steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (English: Read-Only Memory, abbreviated: ROM), random access memory (English: Random Access Memory, abbreviated: RAM), disk or optical disk and other media that can store program codes.
[0193] The above are only preferred embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method for detecting text in an image, characterized in that: include: Acquire a text image to be detected, wherein the text image to be detected includes text and a scene background of an area where the text is located; Using a preset text detection model, the text image to be detected is detected to obtain a text mask image for the text image to be detected, wherein the text mask image includes the text and a solid color background of the area where the text is located; The text detection model is a model obtained by training an initial text detection model using a preset data set, and the preset data set includes: a plurality of sample text images, and a sample text mask image corresponding to each of the sample text images.
2. The method according to claim 1, characterized in that The text detection model includes a connected high-order semantic estimation module and a detail prediction optimization module; The method of using a preset text detection model to detect the text image to be detected and obtaining a text mask image for the text image to be detected includes: Using the high-order semantic estimation module, the text image to be detected is processed to obtain a text mask outline image; The detail prediction optimization module is used to process the text mask outline image and the to-be-detected text image to obtain the text mask image.
3. The method according to claim 2, characterized in that The high-order semantic estimation module includes a backbone network, a convolutional pooling pyramid network, and a convolutional activation network connected in sequence; the high-order semantic estimation module is used to process the text image to be detected to obtain a text mask outline, including: Using the backbone network to extract feature information from the text image to be detected; Using the convolutional pooling pyramid network, extracting detailed features of multiple scales from the feature information; The convolutional activation network is used to determine the text mask outline according to the detail features of the multiple scales.
4. The method according to claim 3, characterized in that: The step of using the detail prediction optimization module to process the text mask outline image and the text image to be detected to obtain the text mask image includes: The detail prediction optimization module is used to process the text mask outline image, the feature information and the text image to be detected to obtain the text mask image.
5. The method according to claim 2, characterized in that: The initial text detection model includes an initial high-order semantic estimation module and an initial detail prediction optimization module connected to each other; the text detection model is trained in the following manner: Acquire the preset data set; Using the initial high-order semantic estimation module, the sample text image is processed to obtain a sample text mask outline image; Using the initial detail prediction optimization module, the sample text mask outline image and the sample text image are processed to obtain an intermediate text mask image; According to the intermediate text mask image and the sample text mask image corresponding to the sample text image, the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module are updated until the preset training termination conditions are met to obtain the text detection model.
6. The method according to claim 5, characterized in that The updating of the model parameters of the initial high-order semantic estimation module and the model parameters of the initial detail prediction optimization module according to the intermediate text mask image and the sample text mask image corresponding to the sample text image includes: Using the first loss function calculation formula corresponding to the initial high-order semantic estimation module, calculate the first loss function value according to the sample text mask outline diagram and the sample text mask image; Using the second loss function calculation formula corresponding to the initial detail prediction optimization module, calculate the second loss function value according to the intermediate text mask image and the sample text mask image; Using a third loss function calculation formula corresponding to the initial text detection model, and calculating a third loss function value according to the first loss function value and the second loss function value; Updating the model parameters of the initial high-order semantic estimation module according to the first loss function value and the third loss function value; The model parameters of the initial detail prediction optimization are updated according to the second loss function value and the third loss function value.
7. The method according to claim 1 or 5, characterized in that: The preset data set includes: a first type of data set, wherein the first type of data set includes a plurality of first sample text images and a first sample text mask image corresponding to each of the first sample text images, and obtaining the preset data set includes: Acquire a plurality of first sample text images, wherein information of regions where sample text annotated in the first sample text images are located; According to the information of the area where the sample text is located, the first sample text image is processed to obtain a sample text sub-image; Processing the sample text sub-image to obtain a binary sample text initial mask image; The sample text initial mask image is processed according to the normal direction of the edge contour to determine the first sample text mask image.
8. The method according to claim 7, characterized in that The preset data set includes: a second type of data set, wherein the second type of data set includes a plurality of second sample text images and second sample text mask images corresponding to each of the second sample text images, and obtaining the preset data set includes: Get multiple sample background images; Draw and generate multiple sample text images according to multiple text parameters; wherein the multiple text parameters include a combination of at least two of the following parameters: font parameter, size parameter, color parameter, shadow parameter, stroke parameter, and expression parameter; synthesizing a plurality of the second sample text images according to the plurality of the sample background images and the plurality of the sample text images; The second sample text mask image is determined according to the multiple sample text images.
9. An electronic device, characterized in that: include: A memory and a processor, wherein the memory stores a computer program executable by the processor, and when the processor executes the computer program, the method for detecting text in an image as described in any one of claims 1 to 8 is implemented.
10. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is read and executed, the method for detecting text in an image as described in any one of claims 1 to 8 is implemented.
