Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Animation effect of Portrait by Unity

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Unity how to achieve the animation effect of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Unity how to achieve the animation effect of the article will have a harvest, let's take a look at it.

API description:

The use of confrontation generation network technology, combined with face detection, hair segmentation, portrait segmentation and other technologies, tailor-made two-dimensional animation images for users, and support through parameter settings to generate two-dimensional animation portraits.

Create an application:

Search for image enhancements and special effects in product services, create applications, and obtain AppID, APIKey, SecretKey information:

Check the official documents. The following is the details of the data parameters returned by the API for portraits and cartoons:

Define the data structure:

Using System; / Animation interface response data structure / / [Serializable] public class AnimeResponse {/ unique log id for problem location / / public int log_id; / Base64 coding of processed images / public string image;}

Download C# SDK:

Import the AipSdk.dll dynamic library into Unity when the download is complete:

The following are the details of the parameters passed when the API is called:

Encapsulate the calling function:

Using System;using System.Collections.Generic;using UnityEngine; / Animation / public class Anime {/ / the following information can be found in the Baidu developer Center console to create an application and get private const string appID = ""; private const string apiKey = ""; private const string secretKey = "" / / initiate a request for portrait cartoon / picture byte data / whether to wear a mask / mask ID value range 1-8 / / returned animation and cartoon picture byte data public static byte [] SendRequest (byte [] bytes, bool withMask = false, int maskID = 1) {var client = new Baidu.Aip.ImageProcess.ImageProcess (apiKey) SecretKey) Try {var options = new Dictionary {{"type", withMask? "anime_mask": "anime"}, {"mask_id", Mathf.Clamp (maskID, 1,8)}}; var response = client.SelfieAnime (bytes, options); AnimeResponse animeResponse = JsonUtility.FromJson (response.ToString ()); byte [] buffer = Convert.FromBase64String (animeResponse.image); return buffer } catch (Exception error) {Debug.LogError (error);} return null } / initiate a request for portrait cartoon / picture url address / whether to wear a mask / mask ID value range 1-8 / / returned animation and cartoon picture byte data public static byte [] SendRequest (string url, bool withMask = false, int maskID = 1) {var client = new Baidu.Aip.ImageProcess.ImageProcess (apiKey, secretKey) Try {var options = new Dictionary {{"type", withMask? "anime_mask": "anime"}, {"mask_id", Mathf.Clamp (maskID, 1,8)}}; var response = client.SelfieAnimeUrl (url, options); AnimeResponse animeResponse = JsonUtility.FromJson (response.ToString ()); byte [] buffer = Convert.FromBase64String (animeResponse.image); return buffer } catch (Exception error) {Debug.LogError (error);} return null;}}

Test picture:

Using System.IO;using UnityEngine; public class Example: MonoBehaviour {private void Start () {/ / request for reading picture byte data var bytes = Anime.SendRequest (File.ReadAllBytes (Application.dataPath + "/ Picture.jpg")); / / generate picture File.WriteAllBytes (Application.dataPath + "/ Test.png", bytes) based on the returned byte data;}}

Here is the generated picture:

This is the end of the article on "how to animate portraits in Unity". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to realize the animation effect of Unity". If you still want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report