Unity 百度AI实现人像动漫化效果
接口介绍:
运用对抗生成网络技术,结合人脸检测、头发分割、人像分割等技术,为用户量身定制千人千面的二次元动漫形象,并支持通过参数设置,生成二次元动漫人像。
创建应用:
在产品服务中搜索图像增强与特效,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是人像动漫画接口返回数据参数详情:
定义数据结构:
using System; /// <summary> /// 人像动漫化接口响应数据结构 /// </summary> [Serializable] public class AnimeResponse { /// <summary> /// 唯一的log id,用于问题定位 /// </summary> public int log_id; /// <summary> /// 处理后图片的Base64编码 /// </summary> public string image; }
下载C# SDK:
下载完成后将AipSdk.dll动态库导入到Unity中:
以下是调用接口时传入的参数详情:
封装调用函数:
using System; using System.Collections.Generic; using UnityEngine; /// <summary> /// 人像动漫化 /// </summary> public class Anime { //以下信息于百度开发者中心控制台创建应用获取 private const string appID = ""; private const string apiKey = ""; private const string secretKey = ""; /// <summary> /// 发起人像动漫画请求 /// </summary> /// <param name="bytes">图片字节数据</param> /// <param name="withMask">是否带口罩</param> /// <param name="maskID">口罩ID 取值范围1-8</param> /// <returns>返回的动漫画图片字节数据</returns> 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<string, object> { { "type", withMask ? "anime_mask" : "anime" }, { "mask_id", Mathf.Clamp(maskID, 1, 8) } }; var response = client.SelfieAnime(bytes, options); AnimeResponse animeResponse = JsonUtility.FromJson<AnimeResponse>(response.ToString()); byte[] buffer = Convert.FromBase64String(animeResponse.image); return buffer; } catch(Exception error) { Debug.LogError(error); } return null; } /// <summary> /// 发起人像动漫画请求 /// </summary> /// <param name="url">图片url地址</param> /// <param name="withMask">是否带口罩</param> /// <param name="maskID">口罩ID 取值范围1-8</param> /// <returns>返回的动漫画图片字节数据</returns> 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<string, object> { { "type", withMask ? "anime_mask" : "anime" }, { "mask_id", Mathf.Clamp(maskID, 1, 8) } }; var response = client.SelfieAnimeUrl(url, options); AnimeResponse animeResponse = JsonUtility.FromJson<AnimeResponse>(response.ToString()); byte[] buffer = Convert.FromBase64String(animeResponse.image); return buffer; } catch (Exception error) { Debug.LogError(error); } return null; } }
测试图片:
using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { //读取图片字节数据 发起请求 var bytes = Anime.SendRequest(File.ReadAllBytes(Application.dataPath + "/Picture.jpg")); //根据返回的字节数据生成图片 File.WriteAllBytes(Application.dataPath + "/Test.png", bytes); } }
下面是生成的图片:
到此这篇关于Unity 百度AI实现人像动漫化效果的文章就介绍到这了,更多相关Unity 人像动漫化内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)