Unity 百度AI實現(xiàn)人像動漫化效果
接口介紹:
運用對抗生成網(wǎng)絡技術,結合人臉檢測、頭發(fā)分割、人像分割等技術,為用戶量身定制千人千面的二次元動漫形象,并支持通過參數(shù)設置,生成二次元動漫人像。
創(chuàng)建應用:
在產(chǎn)品服務中搜索圖像增強與特效,創(chuàng)建應用,獲取AppID、APIKey、SecretKey信息:
查閱官方文檔,以下是人像動漫畫接口返回數(shù)據(jù)參數(shù)詳情:
定義數(shù)據(jù)結構:
using System; /// <summary> /// 人像動漫化接口響應數(shù)據(jù)結構 /// </summary> [Serializable] public class AnimeResponse { /// <summary> /// 唯一的log id,用于問題定位 /// </summary> public int log_id; /// <summary> /// 處理后圖片的Base64編碼 /// </summary> public string image; }
下載C# SDK:
下載完成后將AipSdk.dll動態(tài)庫導入到Unity中:
以下是調用接口時傳入的參數(shù)詳情:
封裝調用函數(shù):
using System; using System.Collections.Generic; using UnityEngine; /// <summary> /// 人像動漫化 /// </summary> public class Anime { //以下信息于百度開發(fā)者中心控制臺創(chuàng)建應用獲取 private const string appID = ""; private const string apiKey = ""; private const string secretKey = ""; /// <summary> /// 發(fā)起人像動漫畫請求 /// </summary> /// <param name="bytes">圖片字節(jié)數(shù)據(jù)</param> /// <param name="withMask">是否帶口罩</param> /// <param name="maskID">口罩ID 取值范圍1-8</param> /// <returns>返回的動漫畫圖片字節(jié)數(shù)據(jù)</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> /// 發(fā)起人像動漫畫請求 /// </summary> /// <param name="url">圖片url地址</param> /// <param name="withMask">是否帶口罩</param> /// <param name="maskID">口罩ID 取值范圍1-8</param> /// <returns>返回的動漫畫圖片字節(jié)數(shù)據(jù)</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() { //讀取圖片字節(jié)數(shù)據(jù) 發(fā)起請求 var bytes = Anime.SendRequest(File.ReadAllBytes(Application.dataPath + "/Picture.jpg")); //根據(jù)返回的字節(jié)數(shù)據(jù)生成圖片 File.WriteAllBytes(Application.dataPath + "/Test.png", bytes); } }
下面是生成的圖片:
到此這篇關于Unity 百度AI實現(xiàn)人像動漫化效果的文章就介紹到這了,更多相關Unity 人像動漫化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# MeasureString測量字符串函數(shù)的使用方法
這篇文章主要介紹了C# MeasureString測量字符串函數(shù)的使用方法,需要的朋友可以參考下2014-10-10在C#中根據(jù)HardwareID獲取驅動程序信息的實現(xiàn)代碼
這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅動程序信息的實現(xiàn)代碼,需要的朋友可以參考下2016-12-12C#中字符串優(yōu)化String.Intern、IsInterned詳解
這篇文章主要給大家介紹了關于C#中字符串優(yōu)化String.Intern、IsInterned的相關資料,文中通過示例代碼介紹的,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-12-12