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