Unity 百度AI實現(xiàn)人像動漫化效果
接口介紹:
運用對抗生成網(wǎng)絡(luò)技術(shù),結(jié)合人臉檢測、頭發(fā)分割、人像分割等技術(shù),為用戶量身定制千人千面的二次元動漫形象,并支持通過參數(shù)設(shè)置,生成二次元動漫人像。
創(chuàng)建應(yīng)用:
在產(chǎn)品服務(wù)中搜索圖像增強與特效,創(chuàng)建應(yīng)用,獲取AppID、APIKey、SecretKey信息:


查閱官方文檔,以下是人像動漫畫接口返回數(shù)據(jù)參數(shù)詳情:

定義數(shù)據(jù)結(jié)構(gòu):
using System;
/// <summary>
/// 人像動漫化接口響應(yīng)數(shù)據(jù)結(jié)構(gòu)
/// </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)庫導(dǎo)入到Unity中:

以下是調(diào)用接口時傳入的參數(shù)詳情:

封裝調(diào)用函數(shù):
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 人像動漫化
/// </summary>
public class Anime
{
//以下信息于百度開發(fā)者中心控制臺創(chuàng)建應(yī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);
}
}
下面是生成的圖片:

到此這篇關(guān)于Unity 百度AI實現(xiàn)人像動漫化效果的文章就介紹到這了,更多相關(guān)Unity 人像動漫化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#客戶端程序Visual Studio遠程調(diào)試的方法詳解
這篇文章主要給大家介紹了關(guān)于C#客戶端程序Visual Studio遠程調(diào)試的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
C# MeasureString測量字符串函數(shù)的使用方法
這篇文章主要介紹了C# MeasureString測量字符串函數(shù)的使用方法,需要的朋友可以參考下2014-10-10
在C#中根據(jù)HardwareID獲取驅(qū)動程序信息的實現(xiàn)代碼
這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅(qū)動程序信息的實現(xiàn)代碼,需要的朋友可以參考下2016-12-12
C#中字符串優(yōu)化String.Intern、IsInterned詳解
這篇文章主要給大家介紹了關(guān)于C#中字符串優(yōu)化String.Intern、IsInterned的相關(guān)資料,文中通過示例代碼介紹的,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-12-12

