Photo image generation method and device, electronic device and storage medium
By obtaining the face feature map, highlight feature map and shadow feature map, and inputting the modified controlnet model, it solves the problem of high computing power and poor generalization ability in improving the light and shadow effects of AIGC-generated photo images, achieving low cost and high efficiency. , High-quality light and shadow effects are improved.
Patent Information
- Application Number
- CN202410741616.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-11
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-06-11
AI Technical Summary
Existing methods for improving the light and shadow effects of photo images generated by AIGC have problems such as high computing power requirements, high training difficulty, and poor generalization capabilities. It is difficult to achieve low-cost, high-efficiency, high-quality, and high-generalization light and shadow effects.
By obtaining the face feature map, highlight feature map and shadow feature map, input the modified controlnet model, and use the feature input layer, encoding layer and decoding layer to generate high-quality photo images with light and shadow effects, avoiding high computing power requirements and maintaining the model generalization ability.
It achieves low-cost, high-efficiency, high-quality and high-generalization improvement in the light and shadow effects of photo images, and is suitable for the generation of different light and shadow effects.
Smart Images

Figure CN118334161B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image processing, and in particular to a method and device for generating a portrait image, an electronic device and a storage medium. Background Art
[0002] Taking personal portraits has always been an inherent need for people to pursue beauty and self-expression. In the past, people used to go to photo studios to take portraits, but with the rapid development of AI technology, personal portraits generated by the Diffusion model of AIGC (AI Generated Content) have reached a very natural and realistic high-quality level.
[0003] Compared with traditional photo studios, photos generated by AIGC have many advantages, including low economic cost, convenient and fast production, etc. At the same time, its level of composition and setting is not much different from that of photos taken in photo studios. However, photo studios have a wealth of professional lighting facilities, such as fill lights and reflectors, so the light and shadow effects of the photos taken are often more outstanding. Therefore, how to improve the light and shadow effects of photos generated by AIGC through technical means is an important topic to enable it to be better implemented and adapt to market demand. At present, AIGC mainly uses the following means to improve the light and shadow effects: 1. Finetuning large models; 2. Training lora models for specific light and shadow effects; 3. Connecting to a separate light and shadow beautification processing algorithm module for processing. However, these methods have defects to varying degrees. Among them, method 1 requires high computing power support, is difficult to train, and the training effect is not easy to guarantee; methods 2 and 3 are often adjusted for specific features, and have the problem of poor generalization ability, that is, the light and shadow effects may be good for some pictures, but the light and shadow effects for other pictures may not perform well or even deteriorate.
[0004] Therefore, how to provide a low-cost, high-efficiency, high-quality and high-generalization solution for generating portrait images to improve the light and shadow effects of portrait images has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] In view of the technical problems existing in the prior art, the embodiments of the present application provide a method and device for generating a portrait image, an electronic device and a storage medium.
[0006] In a first aspect, an embodiment of the present application provides a method for generating a portrait image, comprising:
[0007] Obtaining a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or the first image;
[0008] The face feature map, the highlight feature map and the shadow feature map are input into the modified controlnet model to obtain the second picture, wherein the modified controlnet model comprises a feature input layer, an encoding layer, an intermediate layer and a decoding layer. The feature input layer is a newly added layer structure for inputting integrated features of face contour features, highlight features and shadow features. The feature input layer is connected to the first zero convolution layer, the intermediate layer is connected to the second zero convolution layer, and the second picture is a target photo picture.
[0009] In a second aspect, the embodiment of the present application further provides a portrait image generating device, including:
[0010] A first generating unit is used to obtain a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or a first picture;
[0011] The second generating unit is used to input the face feature map, the highlight feature map and the shadow feature map into the modified ControlNet model to obtain a second picture, wherein the modified ControlNet model comprises a feature input layer, a coding layer, an intermediate layer and a decoding layer, the feature input layer is a newly added layer structure, used to input the integrated features of face contour features, highlight features and shadow features, the feature input layer is externally connected to the first zero convolution layer, the intermediate layer is externally connected to the second zero convolution layer, and the second picture is a target photo picture.
[0012] In a third 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 executed by a processor, the steps of the photo image generation method described in the first aspect are executed.
[0013] In a fourth aspect, an embodiment of the present application further provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to perform the steps of the photo image generation method as described in the first aspect.
[0014] In the above scheme, the face feature map, highlight feature map and shadow feature map are input into the modified controlnet model, which can generate a photo with high-quality light and shadow effects. In the specific implementation, the face feature map, highlight feature map and shadow feature map automatically generated by the photo image itself can be used as the input of the modified controlnet model to improve the light and shadow effects of the generated photo image, or the face feature map, highlight feature map and shadow feature map produced by professionals can be accepted as the input of the modified controlnet model to generate a photo with special light and shadow effects, thereby realizing a low-cost, high-efficiency, high-quality and high-generalization solution for improving the light and shadow effects of photo images. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 A schematic diagram of a flow chart of an embodiment of a photo image generation method provided in an embodiment of the present application;
[0016] Figure 2 This is the architecture diagram of the modified controlnet model;
[0017] Figure 3 A structural schematic diagram of an embodiment of a portrait image generating device provided in an embodiment of the present application;
[0018] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0019] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme 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. It should be understood that the drawings in the present application only serve the purpose of explanation and description and are not used to limit the scope of protection of the present application. In addition, it should be understood that the schematic drawings are not drawn in real proportion. The flowchart used in this application shows the operations implemented according to some embodiments of the present application. It should be understood that the operations of the flowchart can be implemented out of sequence, and the steps without logical context can be reversed in order or implemented simultaneously. In addition, those skilled in the art can add one or more other operations to the flowchart under the guidance of the content of the present application, or remove one or more operations from the flowchart.
[0020] In addition, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0021] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.
[0022] Reference Figure 1 FIG. 1 is a flow chart of a method for generating a portrait image provided in an embodiment of the present application. The method for generating a portrait image includes:
[0023] S10, obtaining a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or the first image;
[0024] S11. Input the face feature map, the highlight feature map and the shadow feature map into the modified controlnet model to obtain a second image, wherein the modified controlnet model comprises a feature input layer, an encoding layer, an intermediate layer and a decoding layer. The feature input layer is a newly added layer structure for inputting integrated features of face contour features, highlight features and shadow features. The feature input layer is externally connected to a first zero convolution layer, and the intermediate layer is externally connected to a second zero convolution layer. The second image is a target photo image.
[0025] The photo image generation method provided in the embodiment of the present application inputs a modified ControlNet model based on a face feature map, a highlight feature map, and a shadow feature map, and can generate a photo image with high-quality light and shadow effects.
[0026] Figure 2 This is the architecture diagram of the modified controlnet model (the time step timestamp and prompt word are omitted in this architecture diagram), refer to Figure 2As shown, based on the foregoing method embodiment, the feature input layer may include a first convolution layer Conv (GN / SILU), a second convolution layer Conv (GN / SILU) and a concatenation layer Concat, wherein the first convolution layer Conv (GN / SILU) and the second convolution layer Conv (GN / SILU) are connected to the concatenation layer Concat, the concatenation layer Concat is connected to the first zero convolution layer, the input of the first convolution layer Conv (GN / SILU) is the shadow feature map cl, the input of the second convolution layer Conv (GN / SILU) is the highlight feature map ch, the input of the concatenation layer Concat is the output of the first convolution layer Conv (GN / SILU), the output of the second convolution layer Conv (GN / SILU) and the face feature map cf, the concatenation layer Concat is used to concatenate the output of the first convolution layer Conv (GN / SILU), the output of the second convolution layer Conv (GN / SILU) and the face feature map cf in the channel dimension, and the input of the first zero convolution layer is the output of the concatenation layer Concat:
[0027] The coding layer may include a first coding layer and a second coding layer, and the modified controlnet model may also include an input layer input and an output layer output;
[0028] The input layer input is connected to the first encoding layer, the first encoding layer is connected to the middle layer Middle Block, the middle layer Middle Block is connected to the decoding layer, the decoding layer is connected to the output layer output, the output of the input layer input is input to the first encoding layer, the output of the first encoding layer is input to the middle layer Middle Block, the output of the middle layer Middle Block is input to the decoding layer, and the output of the decoding layer is input to the output layer output;
[0029] The second encoding layer is connected to the second zero convolution layer. The output of the input layer input and the output of the first zero convolution layer are added and input into the second encoding layer. The output of the second encoding layer is input into the second zero convolution layer. The output of the second zero convolution layer is input into the middle layer Middle Block.
[0030] In this embodiment, it should be noted that the first convolutional layer Conv (GN / SILU) and the second convolutional layer Conv (GN / SILU) are connected after cl and ch respectively, so that the influence of highlights and shadows is more inclined to the region rather than the single pixel value. The three feature maps are concatenated (concat) in the channel dimension through the concatenation layer, and the generated feature map is used as input. Assuming that the sizes of the three feature maps are 1×H×W (where H represents height and W represents width), the size of the feature map generated after concatenating the three feature maps in the channel dimension is 3×H×W. Figure 2 In the architecture shown, the modification of the original controlnet model actually copies the diffusion model (including Figure 2 The first coding layer in the first coding layer, the middle layer Middle Block and the decoding layer) is obtained, and the zero convolution layer connected to the decoding layer is deleted, and the feature input layer and the first zero convolution layer are added. Different from the original controlnet model, each layer of the output part is connected to a zero convolution layer. In this embodiment, only a zero conv layer (i.e., the second zero convolution layer) is connected to the middle layer Middle Block of the output part, so that the training of the modified controlnet model will not be overfitted in a small sample space. It should be noted that Figure 2 The first encoding layer and the second encoding layer each contain N Encoder Blocks, and the decoding layer contains N DecoderBlocks. Figure 2 The Encoder Blocki (i∈(1, 2, ..., N)) in the first encoding layer and the second encoding layer, the Middle Block in the middle layer, and the Decoder Blocki (i∈(1, 2, ..., N)) in the decoding layer are existing structures in the unet network, and their specific meanings are not repeated here.
[0031] Based on the foregoing method embodiment, the first convolution layer and the second convolution layer may each include a convolution layer Conv (which may be a 2×2 convolution layer), a group normalization layer GN, and an activation function layer SILU.
[0032] Based on the foregoing method embodiment, generating a facial feature map based on the first image may include:
[0033] Perform face recognition on the first image to obtain a face area and generate a wireframe image of the face area. Convert each pixel value of the wireframe image of the face area into a value in the range of [-1,1], and use the image obtained after the pixel value conversion as a face feature map.
[0034] In this embodiment, the face area at least includes the facial area, and in addition, may also include other areas, such as part of the neck area, etc. The wireframe diagram of the face area may be generated using the canny algorithm. The conversion formula for converting a pixel value into a value in the range of [-1,1] may be pix_1=(pix / 255-0.5)×2, where pix_1 is the pixel value after conversion and pix is the pixel value before conversion.
[0035] Based on the foregoing method embodiment, generating a highlight feature map based on the first image may include:
[0036] Convert the first image into a YCbCr format image;
[0037] For each pixel value of the Y channel in the YCbCr format image, if it is determined that the pixel value is less than the highlight threshold, the pixel value is adjusted to 0, otherwise, the pixel value is maintained unchanged;
[0038] The pixel value of each Y channel in the YCbCr format image is converted into a value in the range of [-1,1], and the image obtained after the pixel value conversion is used as the highlight feature map.
[0039] On the basis of the foregoing method embodiment, generating a shadow feature map based on the first image may include:
[0040] Convert the first image into a YCbCr format image;
[0041] For each pixel value of the Y channel in the YCbCr format image, if it is determined that the pixel value is greater than the shadow threshold, the pixel value is adjusted to 0; otherwise, the pixel value is adjusted to the difference between 255 and the pixel value;
[0042] The pixel value of each Y channel in the YCbCr format image is converted into a value in the range of [-1,1], and the image obtained after the pixel value conversion is used as the shadow feature map.
[0043] Based on the foregoing method embodiment, before inputting the face feature map, the highlight feature map and the shadow feature map into the modified ControlNet model to obtain the second image, the method may further include:
[0044] The modified ControlNet model is trained using multiple photo images, wherein during the training of the ControlNet model, the parameters of the first encoding layer, the middle layer, and the decoding layer are kept unchanged.
[0045] In this embodiment, in the training stage, in order to ensure low computing power requirements, only 5k-10k size portrait pictures with high-quality light and shadow effects are needed to form a data set. At the same time, due to the small data set, the controlnet model structure needs to be adjusted (the adjusted controlnet model is the modified controlnet model) to ensure that the training will not be overfitted. During training, for each portrait picture, three corresponding conditional feature maps need to be generated: face feature map, highlight feature map and shadow feature map. After that, the parameters of the first encoding layer, the middle layer and the decoding layer are kept unchanged, and the modified controlnet model is trained using the face feature map, the highlight feature map and the shadow feature map (that is, the first convolution layer Conv (GN / SILU), the second convolution layer Conv (GN / SILU), the concatenation layer Concat, the first zero convolution layer, the second encoding layer and the second zero convolution layer The optimal parameters). After that, the trained controlnet model can be used to generate portrait pictures with obvious light and dark changes in the face area. When using the modified ControlNet model, users can generate photo images in the following two ways: 1. The user uses a light and shadow map of their choice to generate three feature maps, CL, CH and CF, using the method in the aforementioned embodiment, and inputs CL, CH and CF into the ControlNet model to generate high-quality light and shadow photo images; 2. The user uses the diffusion model to directly generate a photo image, and then uses the photo image to generate three feature maps, CL, CH and CF, and inputs the three feature maps into the ControlNet model to generate high-quality light and shadow photo images.
[0046] Reference Figure 3 FIG. 1 is a schematic diagram of a structure of a photo image generating device provided in an embodiment of the present application, the device comprising:
[0047] A first generating unit 30 is used to obtain a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or a first picture;
[0048] The second generating unit 31 is used to input the face feature map, the highlight feature map and the shadow feature map into the modified ControlNet model to obtain a second picture, wherein the modified ControlNet model includes a feature input layer, a coding layer, an intermediate layer and a decoding layer, the feature input layer is a newly added layer structure, used to input the integrated features of the face contour features, the highlight features and the shadow features, the feature input layer is externally connected to the first zero convolution layer, the intermediate layer is externally connected to the second zero convolution layer, and the second picture is a target photo picture.
[0049] The portrait image generation device provided in the embodiment of the present application inputs a modified ControlNet model based on a face feature map, a highlight feature map, and a shadow feature map, and can generate a portrait image with high-quality light and shadow effects.
[0050] The implementation process of the portrait image generation device provided in the embodiment of the present application is consistent with the portrait image generation method provided in the embodiment of the present application, and the effect that can be achieved is also the same as the portrait image generation method provided in the embodiment of the present application, which will not be repeated here.
[0051] like Figure 4 As shown, an electronic device provided in an embodiment of the present application includes: a processor 40, a memory 41 and a bus 42, wherein the memory 41 stores machine-readable instructions executable by the processor 40, and when the electronic device is running, the processor 40 communicates with the memory 41 through the bus 42, and the processor 40 executes the machine-readable instructions to perform the steps of the above-mentioned photo image generation method.
[0052] Specifically, the memory 41 and the processor 40 can be general-purpose memories and processors, which are not specifically limited here. When the processor 40 runs the computer program stored in the memory 41, the photo image generation method can be executed.
[0053] Corresponding to the above-mentioned photo image generation method, 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 executed by a processor, the steps of the above-mentioned photo image generation method are executed.
[0054] The above are only specific implementations of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for generating a portrait image, characterized in that: include: Obtaining a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or the first image; Inputting the face feature map, the highlight feature map and the shadow feature map into the modified controlnet model to obtain the second picture, wherein the modified controlnet model comprises a feature input layer, an encoding layer, an intermediate layer and a decoding layer, the feature input layer is a newly added layer structure, which is used to input the integrated features of the face contour features, the highlight features and the shadow features, the feature input layer is externally connected to the first zero convolution layer, the intermediate layer is externally connected to the second zero convolution layer, and the second picture is a target photo picture; The feature input layer includes a first convolution layer, a second convolution layer and a splicing layer, wherein the first convolution layer and the second convolution layer are connected to the splicing layer, the splicing layer is connected to the first zero convolution layer, the input of the first convolution layer is a shadow feature map, the input of the second convolution layer is a highlight feature map, the input of the splicing layer is the output of the first convolution layer, the output of the second convolution layer and the face feature map, the splicing layer is used to splice the output of the first convolution layer, the output of the second convolution layer and the face feature map in the channel dimension, and the input of the first zero convolution layer is the output of the splicing layer: The coding layer includes a first coding layer and a second coding layer, and the modified controlnet model also includes an input layer and an output layer; The input layer is connected to the first encoding layer, the first encoding layer is connected to the middle layer, the middle layer is connected to the decoding layer, the decoding layer is connected to the output layer, the output of the input layer is input to the first encoding layer, the output of the first encoding layer is input to the middle layer, the output of the middle layer is input to the decoding layer, and the output of the decoding layer is input to the output layer; The second coding layer is connected to the second zero convolution layer. The output of the input layer and the output of the first zero convolution layer are added and input into the second coding layer. The output of the second coding layer is input into the second zero convolution layer. The output of the second zero convolution layer is input into the middle layer.
2. The method according to claim 1, characterized in that The first convolutional layer and the second convolutional layer both contain a convolutional layer, a group normalization layer, and an activation function layer.
3. The method according to claim 1 or 2, characterized in that Generating a facial feature map based on the first image includes: Perform face recognition on the first image to obtain a face area and generate a wireframe image of the face area. Convert each pixel value of the wireframe image of the face area into a value in the range of [-1,1], and use the image obtained after the pixel value conversion as a face feature map.
4. The method according to claim 1 or 2, characterized in that: Generating a highlight feature map based on the first image, including: Convert the first image into a YCbCr format image; For each pixel value of the Y channel in the YCbCr format image, if it is determined that the pixel value is less than the highlight threshold, the pixel value is adjusted to 0, otherwise, the pixel value is maintained unchanged; The pixel value of each Y channel in the YCbCr format image is converted into a value in the range of [-1,1], and the image obtained after the pixel value conversion is used as the highlight feature map.
5. The method according to claim 1 or 2, characterized in that: Generating a shadow feature map based on the first image includes: Convert the first image into a YCbCr format image; For each pixel value of the Y channel in the YCbCr format image, if it is determined that the pixel value is greater than the shadow threshold, the pixel value is adjusted to 0; otherwise, the pixel value is adjusted to the difference between 255 and the pixel value; The pixel value of each Y channel in the YCbCr format image is converted into a value in the range of [-1,1], and the image obtained after the pixel value conversion is used as the shadow feature map.
6. The method according to claim 1, characterized in that Before inputting the face feature map, the highlight feature map and the shadow feature map into the modified ControlNet model to obtain the second image, the method further includes: The modified ControlNet model is trained using multiple photo images, wherein during the training of the ControlNet model, the parameters of the first encoding layer, the middle layer, and the decoding layer are kept unchanged.
7. A photo image generating device, characterized in that: include: A first generating unit is used to obtain a face feature map, a highlight feature map, and a shadow feature map, wherein the face feature map, the highlight feature map, and the shadow feature map are input by a user, or generated based on text or a first picture; The second generating unit is used to input the face feature map, the highlight feature map and the shadow feature map into the modified controlnet model to obtain a second picture, wherein the modified controlnet model includes a feature input layer, an encoding layer, an intermediate layer and a decoding layer, the feature input layer is a newly added layer structure, and is used to input the integrated features of the face contour features, the highlight features and the shadow features, the feature input layer is externally connected to the first zero convolution layer, the intermediate layer is externally connected to the second zero convolution layer, and the second picture is a target photo picture; The feature input layer includes a first convolution layer, a second convolution layer and a splicing layer, wherein the first convolution layer and the second convolution layer are connected to the splicing layer, the splicing layer is connected to the first zero convolution layer, the input of the first convolution layer is a shadow feature map, the input of the second convolution layer is a highlight feature map, the input of the splicing layer is the output of the first convolution layer, the output of the second convolution layer and the face feature map, the splicing layer is used to splice the output of the first convolution layer, the output of the second convolution layer and the face feature map in the channel dimension, and the input of the first zero convolution layer is the output of the splicing layer: The coding layer includes a first coding layer and a second coding layer, and the modified controlnet model also includes an input layer and an output layer; The input layer is connected to the first encoding layer, the first encoding layer is connected to the middle layer, the middle layer is connected to the decoding layer, the decoding layer is connected to the output layer, the output of the input layer is input to the first encoding layer, the output of the first encoding layer is input to the middle layer, the output of the middle layer is input to the decoding layer, and the output of the decoding layer is input to the output layer; The second coding layer is connected to the second zero convolution layer. The output of the input layer and the output of the first zero convolution layer are added and input into the second coding layer. The output of the second coding layer is input into the second zero convolution layer. The output of the second zero convolution layer is input into the middle layer.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the photo image generation method according to any one of claims 1 to 6 are executed.
9. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium via the bus, and the processor executes the machine-readable instructions to perform the steps of the photo image generation method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Face beautifying method based on deep learning
CN113743243A
Image processing method and device, electronic equipment and storage medium
CN117252791A
