CSharp: Image Background Matting
作者:互联网
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Windows.Media.Imaging; //PresentationCore //WindowsBase using System.Windows.Media; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Net; using System.IO; using System.Net.Http; using System.Text; public partial class Default2 : System.Web.UI.Page { /// <summary> /// 2020-12-26 涂聚文(Geovin Du) /// 人像抠图 百度AI的API /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { JsonSerializerSettings settings = new JsonSerializerSettings(); // string sToken = AccessToken.getAccessToken(); JToken st = JToken.Parse(sToken); JObject jsonst = JObject.Parse(sToken); BaiduAITokenData we = JsonConvert.DeserializeObject<BaiduAITokenData>(sToken); //Response.Write(we.access_token+"<br/><br/>"); // string sResult = body_seg(); JToken jt = JToken.Parse(sResult); JObject jsonimg = JObject.Parse(sResult); BaiduAIBody body = JsonConvert.DeserializeObject<BaiduAIBody>(sResult); // Bitmap bmp = GetImageFromBase64(body.foreground); this.Image1.ImageUrl = "data:image/png;base64," + body.foreground; //data:image/jpeg;base64, this.Image2.ImageUrl = "data:image/png;base64," + body.scoremap; //BitmapImage bmp = parseImageFromBase64(body.foreground); } /// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public String getFileBase64(String fileName) { FileStream filestream = new FileStream(fileName, FileMode.Open); byte[] arr = new byte[filestream.Length]; filestream.Read(arr, 0, (int)filestream.Length); string baser64 = Convert.ToBase64String(arr); filestream.Close(); return baser64; } /// <summary> /// 人像图获取 /// </summary> /// <returns></returns> public string body_seg() { string token = AccessToken.TOKEN;// AccessToken.getAccessToken();// "[调用鉴权接口获取的token]"; string sToken = AccessToken.getAccessToken(); JToken st = JToken.Parse(sToken); JObject jsonst = JObject.Parse(sToken); BaiduAITokenData we = JsonConvert.DeserializeObject<BaiduAITokenData>(sToken); //Response.Write(token + "<br/>"); string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + we.access_token; Encoding encoding = Encoding.ASCII; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; string file = Server.MapPath("Images/涂聚文1.jpg"); // 图片的base64编码 string base64 = getFileBase64(file); String str = "image=" + HttpUtility.UrlEncode(base64);// byte[] buffer = encoding.GetBytes(str); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII); string result = reader.ReadToEnd(); Console.WriteLine("人像分割:"); Console.WriteLine(result); //Response.Write(result); return result; } /// <summary> /// /// </summary> /// <param name="sBase64"></param> /// <returns></returns> BitmapImage parseImageFromBase64(string sBase64) { char[] c = sBase64.ToCharArray();// Properties.Resources.testImgData .ToCharArray(); byte[] buffer = System.Convert.FromBase64CharArray(c, 0, c.Length); MemoryStream m = new MemoryStream(buffer); m.Position = 0; BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = m; bmp.EndInit(); return bmp; } }
百度AI开放平台-人像分割: http://ai.baidu.com/tech/body/seg
标签:body,string,Image,System,bmp,Background,using,sToken,Matting 来源: https://www.cnblogs.com/geovindu/p/14191320.html