当前位置:   article > 正文

Unity 接入百度AI - 人像动漫化_unity 接入ai绘图

unity 接入ai绘图

接口介绍:

运用对抗生成网络技术,结合人脸检测、头发分割、人像分割等技术,为用户量身定制千人千面的二次元动漫形象,并支持通过参数设置,生成二次元动漫人像。

创建应用:     

在产品服务中搜索图像增强与特效,创建应用,获取AppID、APIKey、SecretKey信息:

查阅官方文档,以下是人像动漫画接口返回数据参数详情:

定义数据结构:

  1. using System;
  2. /// <summary>
  3. /// 人像动漫化接口响应数据结构
  4. /// </summary>
  5. [Serializable]
  6. public class AnimeResponse
  7. {
  8. /// <summary>
  9. /// 唯一的log id,用于问题定位
  10. /// </summary>
  11. public int log_id;
  12. /// <summary>
  13. /// 处理后图片的Base64编码
  14. /// </summary>
  15. public string image;
  16. }

下载C# SDK:

下载完成后将AipSdk.dll动态库导入到Unity中:

以下是调用接口时传入的参数详情:

封装调用函数: 

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 人像动漫化
  6. /// </summary>
  7. public class Anime
  8. {
  9. //以下信息于百度开发者中心控制台创建应用获取
  10. private const string appID = "";
  11. private const string apiKey = "";
  12. private const string secretKey = "";
  13. /// <summary>
  14. /// 发起人像动漫画请求
  15. /// </summary>
  16. /// <param name="bytes">图片字节数据</param>
  17. /// <param name="withMask">是否带口罩</param>
  18. /// <param name="maskID">口罩ID 取值范围1-8</param>
  19. /// <returns>返回的动漫画图片字节数据</returns>
  20. public static byte[] SendRequest(byte[] bytes, bool withMask = false, int maskID = 1)
  21. {
  22. var client = new Baidu.Aip.ImageProcess.ImageProcess(apiKey, secretKey);
  23. try
  24. {
  25. var options = new Dictionary<string, object>
  26. {
  27. { "type", withMask ? "anime_mask" : "anime" },
  28. { "mask_id", Mathf.Clamp(maskID, 1, 8) }
  29. };
  30. var response = client.SelfieAnime(bytes, options);
  31. AnimeResponse animeResponse = JsonUtility.FromJson<AnimeResponse>(response.ToString());
  32. byte[] buffer = Convert.FromBase64String(animeResponse.image);
  33. return buffer;
  34. }
  35. catch(Exception error)
  36. {
  37. Debug.LogError(error);
  38. }
  39. return null;
  40. }
  41. /// <summary>
  42. /// 发起人像动漫画请求
  43. /// </summary>
  44. /// <param name="url">图片url地址</param>
  45. /// <param name="withMask">是否带口罩</param>
  46. /// <param name="maskID">口罩ID 取值范围1-8</param>
  47. /// <returns>返回的动漫画图片字节数据</returns>
  48. public static byte[] SendRequest(string url, bool withMask = false, int maskID = 1)
  49. {
  50. var client = new Baidu.Aip.ImageProcess.ImageProcess(apiKey, secretKey);
  51. try
  52. {
  53. var options = new Dictionary<string, object>
  54. {
  55. { "type", withMask ? "anime_mask" : "anime" },
  56. { "mask_id", Mathf.Clamp(maskID, 1, 8) }
  57. };
  58. var response = client.SelfieAnimeUrl(url, options);
  59. AnimeResponse animeResponse = JsonUtility.FromJson<AnimeResponse>(response.ToString());
  60. byte[] buffer = Convert.FromBase64String(animeResponse.image);
  61. return buffer;
  62. }
  63. catch (Exception error)
  64. {
  65. Debug.LogError(error);
  66. }
  67. return null;
  68. }
  69. }

 测试图片:

  1. using System.IO;
  2. using UnityEngine;
  3. public class Example : MonoBehaviour
  4. {
  5. private void Start()
  6. {
  7. //读取图片字节数据 发起请求
  8. var bytes = Anime.SendRequest(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));
  9. //根据返回的字节数据生成图片
  10. File.WriteAllBytes(Application.dataPath + "/Test.png", bytes);
  11. }
  12. }

下面是生成的图片:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/123556
推荐阅读
相关标签
  

闽ICP备14008679号