A method, system, electronic device and storage medium for generating a handwritten Chinese character library
Through the content encoder and style multi-input encoder training in the generative adversarial network, combined with the consistency loss, the problem of missing glyph picture strokes and long generation cycle in the generation of handwritten Chinese character library is solved, achieving high-quality and rapid generation in compliance with GB2312 Standard font library.
Patent Information
- Application Number
- CN202210752549.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-28
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2042-06-28
AI Technical Summary
When generating a handwritten Chinese character library, the existing technology suffers from missing strokes in glyph pictures, a long generation cycle, and an inability to control strokes and font structures, resulting in poor generation quality and failure to meet the GB2312 standard.
The content encoder and style multi-input encoder in the generative adversarial network are used to train glyph images, and Chinese character images are generated through content feature vectors and style feature vectors, and consistency loss is added during the training process to shorten the generation time and improve the generation effect.
Generate high-quality handwritten Chinese fonts that comply with GB2312 standards in a short time, shorten the production cycle, reduce labor costs, solve the problem of stroke loss, and improve the generation effect.
Smart Images

Figure CN115146587B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of character library generation, and in particular to a method, system, electronic device and storage medium for generating a handwritten Chinese character library. Background Art
[0002] With the development of the Internet, the upgrade of portable smart devices and the continuous innovation of Internet companies, the services provided on the Internet can meet most of the basic life needs and entertainment needs. Therefore, the number of people using the Internet is also very large. As a medium for information transmission, the most basic element on the Internet is text. Many people read text every day, and text not only conveys information, but also conveys personality and power, culture and connotation, such as the business logos of some companies, the personalized store text on the plaques in the streets and alleys, and personalized signatures. Personalized font production is becoming more and more important.
[0003] According to the requirements of GB2312, a font library must contain at least 6763 simplified Chinese characters. The data is usually obtained by artists or font lovers who write a lot of handwritten characters while ensuring the uniformity of the style. This is a huge workload for the writer, so data acquisition is particularly time-consuming and troublesome, ranging from weeks to months. After the data is obtained, the manuscript file is obtained, and then the existing character segmentation and contour extraction are used to make a primary font library. Finally, professionals will refine each character to obtain a font file that can be sold and meets the requirements.
[0004] With the development of artificial intelligence technology, many practitioners have tried to use artificial intelligence technology to simplify the above traditional processes, but there are some defects. Zi2zi (Zi2zi. https: / / github.com / kaonashi-tyc / zi2i) and rewrite (YuchenTian. 2016. Rewrite: Neural Style Transfer For Chinese Fonts. (2016). Retrieved Nov 23, 2016 from https: / / github.com / kaonashi-tyc / Rewrite) are two iconic works. The main content of these two methods is that one style font can be transformed into another style font without the need for one-to-one character data of the two styles to train. However, the other style to be transformed can only be a fixed number and is within the training data set. There are problems such as lost strokes and unclear style for the text generated in the new style. The MXFont document (Multiple Heads are Better than One: Few-shot Font Generation with Multiple Localized Experts, 2021) uses component information to improve the local style generation capability, that is, to improve the local detail quality of the generated text. The main steps are to first pass the input image through multiple expert networks composed of convolutional neural networks, then transform the output of the expert network into style feature vectors and content feature vectors through a fully connected network, and then splice multiple style feature vectors and content feature vectors together and input them into the generator network composed of convolutional neural networks to output the final generated results. The component information of Chinese characters is used as a supervisory signal during the training process. However, considering the dimension of the components, not all of the 6,367 Chinese characters can be split, and the distribution of the split components is extremely uneven. Some components appear very many times, while most of the other components appear very few times. Therefore, only a part of the generated results meet the font production standards, and most of the other generated results do not meet the font production standards. There is also a document (Generating Handwritten Chinese Characters using CycleGAN, 2018) that uses training adversarial neural networks and dense link structure training. It also only requires unpaired data for training to achieve the transformation from the original font to the target font. The input font is first passed through the encoder neural network to obtain the feature vector of the word, and then the feature vector is converted into the feature vector of the target style font through the dense link structure. Finally, the feature vector of the target style font is passed through the generator network to generate the target style font.This method uses a one-to-one generation method. If you want to generate a new style of fonts, you have to re-train them. The training cycle is too long, often several days or dozens of days, and the generated fonts are not clear enough and do not meet the requirements of making a font library.
[0005] In summary, the shortcomings of the prior art are as follows:
[0006] 1) The generated glyph images may have missing strokes or components
[0007] 2) The generation of a new style requires dozens of days of training, and the generation cycle is long.
[0008] 3) The strokes and font structure of the generated content cannot be controlled. Summary of the invention
[0009] The present disclosure provides a method, system, electronic device and storage medium for generating a handwritten Chinese character library, which can solve the many shortcomings of the existing methods, such as poor quality, blurred images and long period of generating a set of new style fonts. The time for producing a handwritten character library is shortened from several weeks of manual collection or dozens of days of retraining using other deep neural network methods to 2-3 hours. In addition, the content style of the generated characters can be specified, and the effect also meets the standards for producing the character library. In order to solve the above technical problems, the present disclosure provides the following technical solutions:
[0010] As one aspect of an embodiment of the present disclosure, a method for generating a handwritten Chinese character library is provided, comprising the following steps:
[0011] Get the glyph image;
[0012] The glyph image is trained using a content encoder and a style multi-input encoder in a generative adversarial network, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights;
[0013] The content feature vector and the style feature vector are fed into a generator to generate a Chinese character image.
[0014] Optionally, the obtaining of the glyph image specifically comprises the steps of: selecting a plurality of font files, and rendering the plurality of font files into a plurality of glyph images with black characters on a white background.
[0015] Optionally, before obtaining the glyph image, a step of training a generative adversarial network is also included, and the loss function of training the generative adversarial network includes adversarial loss, L1 loss and content loss.
[0016] Optionally, before obtaining the glyph image, a step of fine-tuning some parameters of the generative adversarial network is also included, and the fine-tuning is achieved by adding a new consistency loss, and the consistency loss L1_loss formula is as follows:
[0017] L1_loss=||ContEnc(I_c)-ContEnc(I_f)||,
[0018] Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font.
[0019] Optionally, the content encoder consists of a plurality of convolutional layer-normalization layer-activation layer structures.
[0020] Optionally, the style multi-input encoder consists of an attention layer-residual layer, the attention layer is used to receive input of multiple style samples, and the residual layer is used to obtain and output style feature vectors of the multiple samples.
[0021] As another aspect of the present disclosure, a handwritten Chinese character library generation system is provided, comprising:
[0022] A font image acquisition module is used to acquire font images;
[0023] An encoder module, using a content encoder and a style multi-input encoder in a generative adversarial network to train the glyph image, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights;
[0024] A generator is provided to input the content feature vector and the style feature vector into the generator to generate a Chinese character image.
[0025] Optionally, the system further includes a fine-tuning module, which is used to fine-tune the loss function in the training process of the generative adversarial network, wherein the loss function adds a consistency loss, and the formula of the consistency loss is as follows:
[0026] L1_loss=||ContEnc(I_c)-ContEnc(I_f)||,
[0027] Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font.
[0028] As another aspect of an embodiment of the present disclosure, an electronic device is also provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned handwritten Chinese character library generation method when executing the computer program.
[0029] As another aspect of the embodiment of the present disclosure, there is also a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the steps of the above-mentioned handwritten Chinese character library generation method are implemented.
[0030] When the embodiment of the present disclosure is implemented, the user only needs to write a small amount of handwritten characters (more than 300 characters) and the system can generate a font image of any content in the corresponding style in a short time, so that the time for producing the handwritten Chinese character library specified in GB2312 using the existing method is shortened from dozens of days to a few hours, and compared with other methods for quickly producing handwritten Chinese character libraries, the generated effect is better. The entire method does not rely on stroke components, and does not require a large amount of manpower to fine-tune the generated results. Therefore, the cycle of character library production and the manpower cost of fine-tuning the character library are greatly shortened, making the generation of personalized character libraries simpler, more convenient, and of high quality, thereby promoting the personalized application of fonts. The present disclosure also solves the problem of missing strokes in the glyphs generated by the existing method by using consistency loss when fine-tuning the generative adversarial network. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 This is a flow chart of the method for generating a handwritten Chinese character library in Example 1;
[0032] Figure 2 A diagram of the training process for generating adversarial networks;
[0033] Figure 3 Generate effect diagram for fonts;
[0034] Figure 4 Generate a system block diagram for the handwritten Chinese character library.
[0035] FIG5(a), FIG5(b) and FIG5(c) are examples of generating Chinese character images. DETAILED DESCRIPTION
[0036] The following will be combined with the drawings in the embodiments of the present disclosure to clearly and completely describe the technical solutions in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present disclosure.
[0037] Example 1
[0038] As one aspect of the embodiments of the present disclosure, the present embodiment provides a method for generating a handwritten Chinese character library, such as Figure 1 As shown, the following steps are included:
[0039] S10, obtaining a font image;
[0040] S20, using a content encoder and a style multi-input encoder in a generative adversarial network to train the glyph image, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights;
[0041] S30, sending the content feature vector and the style feature vector to a generator to generate a Chinese character image.
[0042] Based on the above configuration, the disclosed embodiment only requires the user to write a small amount of handwritten characters to generate a font image of any content in a corresponding style in a short time, so that the time for producing the handwritten Chinese character library specified in GB2312 using the existing method is shortened from dozens of days to a few hours, and compared with other methods for quickly producing handwritten Chinese character libraries, the generated effect is better. The entire method does not rely on stroke components, and does not require a lot of manpower to refine the generated results. Therefore, the cycle of character library production and the labor cost of refining the character library are greatly shortened, making the generation of personalized character libraries simpler, more convenient, and of higher quality, thereby promoting the personalized application of fonts.
[0043] The following describes in detail each step of the embodiment of the present disclosure.
[0044] S10, obtaining a glyph image; wherein the obtaining a glyph image specifically comprises the steps of: selecting a plurality of font files, rendering the plurality of the font files into a plurality of glyph images with black characters on a white background. The plurality of font files may be font files in *.ttf or *.otf formats, and then rendering the characters specified in GB2312 from the font file format into a glyph image file, such as rendering into a glyph image with black characters on a white background.
[0045] S20, using a content encoder and a style multi-input encoder in a generative adversarial network to train the glyph image, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights;
[0046] Among them, Figure 2 As shown in the figure, the training of the generative adversarial network mainly includes four neural networks composed of convolutional layers, namely the content encoder ContEnc, the style multi-input encoder StyleEnc, the generator G and the discriminator D.
[0047] In some embodiments, the content encoder is composed of multiple convolutional layers-normalization layers-activation layer structures. For example, the content encoder ContEnc is composed of 5 convolutional layers-normalization layers-activation layer structures, and its function is to extract a 256x256 pixel glyph image into a 512x16x16 content feature vector as the content representation of the glyph image.
[0048] In some embodiments, the style multi-input encoder is composed of an attention layer-residual layer, the attention layer is used to receive the input of multiple style samples, and the residual layer is used to obtain and output the style feature vectors of the multiple samples. For example, the attention layer accepts the input of multiple style samples, such as 5 style samples, and then calculates the weight relationship between the 5 style samples and the generated target style, obtains different weights corresponding to the 5 samples, and finally outputs the obtained style weights and style samples together through the residual layer to output 1x256-dimensional style features as the style representation of the 5 samples.
[0049] In some embodiments, before obtaining the glyph image, the step of training a generative adversarial network is also included, and the loss function of training the generative adversarial network includes adversarial loss, L1 loss and content loss. Among them, adversarial loss, L1 loss and content loss can be represented by loss functions in the prior art, and the goal of training is to minimize the sum of these three losses.
[0050] In some embodiments, before obtaining the glyph image, the method further includes the step of fine-tuning some parameters of the generative adversarial network, wherein the font is first rendered into a glyph image, and then some parameters of the basic model are fine-tuned to generate a new style font generated by the fine-tuned model. The fine-tuning only takes 2-3 hours to complete. The fine-tuning is achieved by adding a new consistency loss, and the consistency loss L1_loss formula is as follows:
[0051] L1_loss=||ContEnc(I_c)-ContEnc(I_f)||,
[0052] Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font, which ensures that the generated content is consistent with the given content.
[0053] like Figure 3 As shown, this is a diagram showing the effect of generating fonts in the character library generated by the generator.
[0054] The disclosed embodiment can use the new style as input, and by modifying the given content font, it can generate 6763 characters specified in GB2312. It can also generate a character library of 20,000 character sets specified in GB18030.
[0055] Example 2
[0056] As another aspect of the embodiment of the present disclosure, the present embodiment provides a handwritten Chinese character library generation system 100. Figure 4 As shown, including:
[0057] The font image acquisition module 1 acquires the font image; it is used to select multiple font files, such as selecting multiple font files in the GB2312 library, and rendering the multiple font files into multiple font images with black characters on a white background. The multiple font files can be font files in *.ttf or *.otf formats, and then the characters specified in GB2312 are rendered from the font file format into a font image file, such as a font image with black characters on a white background.
[0058] Encoder module 2, using a content encoder and a style multi-input encoder in a generative adversarial network to train the glyph image, the content encoder is used to extract a content feature vector in the glyph image, the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights;
[0059] Generator 3 sends the content feature vector and the style feature vector to the generator to generate a Chinese character image.
[0060] For example, as shown in Figure 5, it is an example diagram of the generation of a handwritten Chinese character library. Figure 5(a) is the specified content input, that is, the content input in the glyph image; Figure 5(b) is a plurality of style samples, that is, the specified style; Figure 5(c) is the generated Chinese character image.
[0061] Among them, Figure 2 As shown in the figure, the training of the generative adversarial network mainly includes four neural networks composed of convolutional layers, namely the content encoder ContEnc, the style multi-input encoder StyleEnc, the generator G and the discriminator D.
[0062] In some embodiments, the content encoder is composed of multiple convolutional layers-normalization layers-activation layer structures. For example, the content encoder ContEnc is composed of 5 convolutional layers-normalization layers-activation layer structures, and its function is to extract a 256x256 pixel glyph image into a 512x16x16 content feature vector as the content representation of the glyph image.
[0063] In some embodiments, the style multi-input encoder is composed of an attention layer-residual layer, the attention layer is used to receive the input of multiple style samples, and the residual layer is used to obtain and output the style feature vectors of the multiple samples. For example, the attention layer accepts the input of multiple style samples, such as 5 style samples, and then calculates the weight relationship between the 5 style samples and the generated target style, obtains different weights corresponding to the 5 samples, and finally outputs the obtained style weights and style samples together through the residual layer to output 1x256-dimensional style features as the style representation of the 5 samples.
[0064] In some embodiments, before obtaining the glyph image, the step of training a generative adversarial network is also included, and the loss function of training the generative adversarial network includes adversarial loss, L1 loss and content loss. Among them, adversarial loss, L1 loss and content loss can be represented by loss functions in the prior art, and the goal of training is to minimize the sum of these three losses.
[0065] In some embodiments, the system further includes a fine-tuning module, which is used to fine-tune the loss function in the training process of the generative adversarial network, wherein the loss function adds a consistency loss, and the formula of the consistency loss is as follows:
[0066] L1_loss=||ContEnc(I_c)-ContEnc(I_f)||,
[0067] Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font.
[0068] In some embodiments, the discriminator D adopts the same structure as VGG16 (Karen Simonyan and Andrew Zisserman. 2014. Very deep convolutional networks for large-scale image recognition. arXiv preprint arXiv: 1409.1556 (2014).), inputs a generated image or a real image, and outputs the probability of being a generated image and the probability of being a real image, respectively.
[0069] The disclosed embodiment can use the new style as input, and by modifying the given content font, it can generate 6763 characters specified in GB2312. It can also generate a character library of 20,000 character sets specified in GB18030.
[0070] Implementation 3
[0071] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the handwritten Chinese character library generation method in embodiment 1 when executing the computer program.
[0072] Embodiment 3 of the present disclosure is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0073] The electronic device may be in the form of a general-purpose computing device, for example, it may be a server device. The components of the electronic device may include, but are not limited to: at least one processor, at least one memory, and a bus connecting different system components (including the memory and the processor).
[0074] The bus includes data bus, address bus and control bus.
[0075] The memory may include volatile memory, such as random access memory (RAM) and / or cache memory, and may further include read-only memory (ROM).
[0076] The memory may also include a program tool having a set (at least one) of program modules, such program modules including but not limited to: an operating system, one or more application programs, other program modules and program data, each of which or some combination may include the implementation of a network environment.
[0077] The processor executes various functional applications and data processing by running computer programs stored in the memory.
[0078] The electronic device can also communicate with one or more external devices (e.g., keyboard, pointing device, etc.). Such communication can be performed through an input / output (I / O) interface. In addition, the electronic device can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter. The network adapter communicates with other modules of the electronic device through a bus. It should be understood that, although not shown in the figure, other hardware and / or software modules can be used in conjunction with the electronic device, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID (disk array) systems, tape drives, and data backup storage systems, etc.
[0079] It should be noted that although several units / modules or sub-units / modules of the electronic device are mentioned in the above detailed description, this division is merely exemplary and not mandatory. In fact, according to the embodiments of the present application, the features and functions of two or more units / modules described above can be embodied in one unit / module. Conversely, the features and functions of one unit / module described above can be further divided into multiple units / modules to be embodied.
[0080] Implementation 4
[0081] A computer-readable storage medium stores a computer program, and when the program is executed by a processor, the steps of the handwritten Chinese character library generation method in embodiment 1 are implemented.
[0082] The readable storage medium may include but is not limited to: a portable disk, a hard disk, a random access memory, a read-only memory, an erasable programmable read-only memory, an optical storage device, a magnetic storage device or any suitable combination of the above.
[0083] In a possible implementation, the present disclosure may also be implemented in the form of a program product, which includes program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps of the handwritten Chinese character library generation method described in Example 1.
[0084] Among them, the program code for executing the present disclosure can be written in any combination of one or more programming languages, and the program code can be executed completely on the user device, partially on the user device, as an independent software package, partially on the user device and partially on a remote device, or completely on the remote device.
[0085] Although embodiments of the present disclosure have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present disclosure, and the scope of the present disclosure is defined by the appended claims and their equivalents.
Claims
1. A method for generating a handwritten Chinese character library, characterized in that: The steps include: Get the glyph image; The glyph image is trained using a content encoder and a style multi-input encoder in a generative adversarial network, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights; Sending the content feature vector and the style feature vector into a generator to generate a Chinese character image; The content encoder is composed of a plurality of convolutional layer-normalization layer-activation layer structures; Before obtaining the glyph image, the method further includes the step of training a generative adversarial network, wherein the loss function of training the generative adversarial network includes adversarial loss, L1 loss and content loss; Before obtaining the glyph image, the step of fine-tuning some parameters of the generative adversarial network is also included. The fine-tuning is achieved by adding a new consistency loss. The consistency loss L1_loss formula is as follows: L1_loss=||ContEnc(I_c)-ContEnc(I_f)||, Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font.
2. The method for generating a handwritten Chinese character library as claimed in claim 1, characterized in that: The obtaining of the glyph image specifically comprises the steps of: selecting a plurality of font files, and rendering the plurality of the font files into a plurality of glyph images with black characters on a white background.
3. The method for generating a handwritten Chinese character library as claimed in claim 1, characterized in that: The style multi-input encoder consists of an attention layer-residual layer, the attention layer is used to receive the input of multiple style samples, and the residual layer is used to obtain and output style feature vectors of the multiple samples.
4. A handwritten Chinese character library generation system, characterized in that: include: A font image acquisition module is used to acquire font images; An encoder module, using a content encoder and a style multi-input encoder in a generative adversarial network to train the glyph image, wherein the content encoder is used to extract a content feature vector in the glyph image, and the style multi-input encoder receives an input of multiple style samples of a specified style, obtains a weight relationship between the multiple style samples and a generated target style to obtain different style weights corresponding to the multiple style samples, and outputs style feature vectors of the multiple samples according to the multiple style samples and the corresponding different style weights; A generator, sending the content feature vector and the style feature vector into the generator to generate a Chinese character image; The content encoder is composed of a plurality of convolutional layer-normalization layer-activation layer structures; The fine-tuning module is used to fine-tune the loss function in the training process of the generative adversarial network. The loss function also includes consistency loss. The formula of the consistency loss is as follows: L1_loss=||ContEnc(I_c)-ContEnc(I_f)||, Among them, ContEnc(I_c) is the content feature vector of the content font, and ContEnc(I_f) is the content feature vector of the generated font.
5. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method for generating a handwritten Chinese character library according to any one of claims 1 to 3 is implemented.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method for generating a handwritten Chinese character library according to any one of claims 1 to 3 are implemented.
