当前位置:   article > 正文

Unity 接入百度AI人像抠图_unity 调用百度人体扣像

unity 调用百度人体扣像
  1. using LitJson;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Web;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. public class OpenCamera : MonoBehaviour
  12. {
  13. public Image img;
  14. private void Start()
  15. {
  16. }
  17. public void OPEn()
  18. {
  19. Invoke("open", 2);
  20. }
  21. public void open()
  22. {
  23. Base64ToImg(img, body_seg());
  24. }
  25. static string APP_ID = "";
  26. static string API_KEY = "";
  27. static string SECRET_KEY = "";
  28. // 人像分割 POST
  29. public static String TOKEN = "";
  30. // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
  31. private static String clientId = "M3GAqRj1QeQGsATjFKTsVBVI";
  32. // 百度云中开通对应服务应用的 Secret Key
  33. private static String clientSecret = "hXo5EdROAzzagouzoHgC07EezrytNzQI";
  34. public String getAccessToken()
  35. {
  36. String authHost = "https://aip.baidubce.com/oauth/2.0/token";
  37. HttpClient client = new HttpClient();
  38. List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
  39. paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
  40. paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
  41. paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
  42. HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
  43. String result = response.Content.ReadAsStringAsync().Result;
  44. //Debug.Log("REsult:" + result);
  45. return result;
  46. }
  47. public string body_seg()
  48. {
  49. string token = "24.ad0cd3223538a86613ba068acb99bb73.2592000.1602990697.282335-19410153";
  50. string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + token;
  51. Encoding encoding = Encoding.Default;
  52. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
  53. request.Method = "post";
  54. request.KeepAlive = true;
  55. // 图片的base64编码
  56. string base64 = getFileBase64(Application.streamingAssetsPath + "/1.png");
  57. String str = "image=" + HttpUtility.UrlEncode(base64);
  58. byte[] buffer = encoding.GetBytes(str);
  59. request.ContentLength = buffer.Length;
  60. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  61. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  62. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
  63. string result = reader.ReadToEnd();
  64. Debug.Log(result);
  65. string newBase64= json(result);
  66. return newBase64;
  67. }
  68. public string json(string str)
  69. {
  70. JsonData jd;
  71. jd = JsonMapper.ToObject(str);
  72. return jd["foreground"].ToString();
  73. }
  74. public static String getFileBase64(String fileName)
  75. {
  76. FileStream filestream = new FileStream(fileName, FileMode.Open);
  77. byte[] arr = new byte[filestream.Length];
  78. filestream.Read(arr, 0, (int)filestream.Length);
  79. string baser64 = Convert.ToBase64String(arr);
  80. filestream.Close();
  81. return baser64;
  82. }
  83. public void Base64ToImg(Image imgComponent, string base64)
  84. {
  85. byte[] bytes = Convert.FromBase64String(base64);
  86. Texture2D tex2D = new Texture2D(100, 100);
  87. tex2D.LoadImage(bytes);
  88. Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
  89. imgComponent.sprite = s;
  90. Resources.UnloadUnusedAssets();
  91. }
  92. }

 

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

闽ICP备14008679号