Unity 百度AI實(shí)現(xiàn)人像動(dòng)漫化效果
接口介紹:
運(yùn)用對(duì)抗生成網(wǎng)絡(luò)技術(shù),結(jié)合人臉檢測(cè)、頭發(fā)分割、人像分割等技術(shù),為用戶量身定制千人千面的二次元?jiǎng)勇蜗?,并支持通過(guò)參數(shù)設(shè)置,生成二次元?jiǎng)勇讼瘛?/p>
創(chuàng)建應(yīng)用:
在產(chǎn)品服務(wù)中搜索圖像增強(qiáng)與特效,創(chuàng)建應(yīng)用,獲取AppID、APIKey、SecretKey信息:
查閱官方文檔,以下是人像動(dòng)漫畫接口返回?cái)?shù)據(jù)參數(shù)詳情:
定義數(shù)據(jù)結(jié)構(gòu):
using System; /// <summary> /// 人像動(dòng)漫化接口響應(yīng)數(shù)據(jù)結(jié)構(gòu) /// </summary> [Serializable] public class AnimeResponse { /// <summary> /// 唯一的log id,用于問(wèn)題定位 /// </summary> public int log_id; /// <summary> /// 處理后圖片的Base64編碼 /// </summary> public string image; }
下載C# SDK:
下載完成后將AipSdk.dll動(dòng)態(tài)庫(kù)導(dǎo)入到Unity中:
以下是調(diào)用接口時(shí)傳入的參數(shù)詳情:
封裝調(diào)用函數(shù):
using System; using System.Collections.Generic; using UnityEngine; /// <summary> /// 人像動(dòng)漫化 /// </summary> public class Anime { //以下信息于百度開發(fā)者中心控制臺(tái)創(chuàng)建應(yīng)用獲取 private const string appID = ""; private const string apiKey = ""; private const string secretKey = ""; /// <summary> /// 發(fā)起人像動(dòng)漫畫請(qǐng)求 /// </summary> /// <param name="bytes">圖片字節(jié)數(shù)據(jù)</param> /// <param name="withMask">是否帶口罩</param> /// <param name="maskID">口罩ID 取值范圍1-8</param> /// <returns>返回的動(dòng)漫畫圖片字節(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ā)起人像動(dòng)漫畫請(qǐng)求 /// </summary> /// <param name="url">圖片url地址</param> /// <param name="withMask">是否帶口罩</param> /// <param name="maskID">口罩ID 取值范圍1-8</param> /// <returns>返回的動(dòng)漫畫圖片字節(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; } }
測(cè)試圖片:
using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { //讀取圖片字節(jié)數(shù)據(jù) 發(fā)起請(qǐng)求 var bytes = Anime.SendRequest(File.ReadAllBytes(Application.dataPath + "/Picture.jpg")); //根據(jù)返回的字節(jié)數(shù)據(jù)生成圖片 File.WriteAllBytes(Application.dataPath + "/Test.png", bytes); } }
下面是生成的圖片:
到此這篇關(guān)于Unity 百度AI實(shí)現(xiàn)人像動(dòng)漫化效果的文章就介紹到這了,更多相關(guān)Unity 人像動(dòng)漫化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#客戶端程序Visual Studio遠(yuǎn)程調(diào)試的方法詳解
這篇文章主要給大家介紹了關(guān)于C#客戶端程序Visual Studio遠(yuǎn)程調(diào)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能(附源碼)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07C# MeasureString測(cè)量字符串函數(shù)的使用方法
這篇文章主要介紹了C# MeasureString測(cè)量字符串函數(shù)的使用方法,需要的朋友可以參考下2014-10-10在C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-12-12WPF彈出右鍵菜單時(shí)判斷鼠標(biāo)是否選中該項(xiàng)
這篇文章介紹了WPF彈出右鍵菜單時(shí)判斷鼠標(biāo)是否選中該項(xiàng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#中字符串優(yōu)化String.Intern、IsInterned詳解
這篇文章主要給大家介紹了關(guān)于C#中字符串優(yōu)化String.Intern、IsInterned的相關(guān)資料,文中通過(guò)示例代碼介紹的,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-12-12