接口介绍:
运用对抗生成网络技术,结合人脸检测、头发分割、人像分割等技术,为用户量身定制千人千面的二次元动漫形象,并支持通过参数设置,生成二次元动漫人像。
创建应用:
在产品服务中搜索图像增强与特效,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是人像动漫画接口返回数据参数详情:
定义数据结构:
usingSystem; /// <summary>/// 人像动漫化接口响应数据结构/// </summary>[Serializable] publicclassAnimeResponse{ /// <summary>/// 唯一的log id,用于问题定位/// </summary>publicintlog_id; /// <summary>/// 处理后图片的Base64编码/// </summary>publicstringimage; }
下载C# SDK:
下载完成后将AipSdk.dll动态库导入到Unity中:
以下是调用接口时传入的参数详情:
封装调用函数:
usingSystem; usingSystem.Collections.Generic; usingUnityEngine; /// <summary>/// 人像动漫化/// </summary>publicclassAnime{ //以下信息于百度开发者中心控制台创建应用获取privateconststringappID=""; privateconststringapiKey=""; privateconststringsecretKey=""; /// <summary>/// 发起人像动漫画请求/// </summary>/// <param name="bytes">图片字节数据</param>/// <param name="withMask">是否带口罩</param>/// <param name="maskID">口罩ID 取值范围1-8</param>/// <returns>返回的动漫画图片字节数据</returns>publicstaticbyte[] SendRequest(byte[] bytes, boolwithMask=false, intmaskID=1) { varclient=newBaidu.Aip.ImageProcess.ImageProcess(apiKey, secretKey); try { varoptions=newDictionary<string, object> { { "type", withMask?"anime_mask" : "anime" }, { "mask_id", Mathf.Clamp(maskID, 1, 8) } }; varresponse=client.SelfieAnime(bytes, options); AnimeResponseanimeResponse=JsonUtility.FromJson<AnimeResponse>(response.ToString()); byte[] buffer=Convert.FromBase64String(animeResponse.image); returnbuffer; } catch(Exceptionerror) { Debug.LogError(error); } returnnull; } /// <summary>/// 发起人像动漫画请求/// </summary>/// <param name="url">图片url地址</param>/// <param name="withMask">是否带口罩</param>/// <param name="maskID">口罩ID 取值范围1-8</param>/// <returns>返回的动漫画图片字节数据</returns>publicstaticbyte[] SendRequest(stringurl, boolwithMask=false, intmaskID=1) { varclient=newBaidu.Aip.ImageProcess.ImageProcess(apiKey, secretKey); try { varoptions=newDictionary<string, object> { { "type", withMask?"anime_mask" : "anime" }, { "mask_id", Mathf.Clamp(maskID, 1, 8) } }; varresponse=client.SelfieAnimeUrl(url, options); AnimeResponseanimeResponse=JsonUtility.FromJson<AnimeResponse>(response.ToString()); byte[] buffer=Convert.FromBase64String(animeResponse.image); returnbuffer; } catch (Exceptionerror) { Debug.LogError(error); } returnnull; } }
测试图片:
usingSystem.IO; usingUnityEngine; publicclassExample : MonoBehaviour{ privatevoidStart() { //读取图片字节数据 发起请求varbytes=Anime.SendRequest(File.ReadAllBytes(Application.dataPath+"/Picture.jpg")); //根据返回的字节数据生成图片File.WriteAllBytes(Application.dataPath+"/Test.png", bytes); } }
下面是生成的图片: