首先进入百度AI百度AI开放平台-全球领先的人工智能服务平台
登录后创建相关应用拿到AK,SK
下面是部分功能代码
/// <summary>
/// 分割图像
/// </summary>
/// <param name="classify">前景/灰度</param>
private void FaceseGmentation(string classify)//分割
{
//WebCamera.Instance.SaveScreenShot(UtilityTool.StreamingAssetsPath);//获取截图
var curTex = WebCamera.Instance.lastShotText;
ImageInfoFG imgTarget = new ImageInfoFG();
imgTarget.image = UtilityTool.Texture2DToBase64(curTex);//转64
//分割
mattingImage.texture = FGFace(body_seg(imgTarget.image), new Texture2D(216, 384), classify);
}
/// <summary>
/// 解析返回图片结果
/// </summary>
/// <param name="info">信息</param>
/// <param name="resultTexture">结果</param>
/// <param name="classify">类型</param>
/// <returns></returns>
private Texture2D FGFace(string info, Texture2D resultTexture, string classify)//解析结果返回图片
{
Debug.Log(info);
FGResponse FGresponse = JsonMapper.ToObject<FGResponse>(info);
if (classify == "for")
{
if (FGresponse.foreground.Length > 0) //
{
string ImgBase64 = FGresponse.foreground;
resultTexture = UtilityTool.Base64ToTexture2D(resultTexture.width, resultTexture.height, ImgBase64);
return resultTexture;
}
}
if (classify == "lam")
{
if (FGresponse.labelmap.Length > 0) //
{
string ImgBase64 = FGresponse.labelmap;
resultTexture = UtilityTool.Base64ToTexture2D(resultTexture.width, resultTexture.height, ImgBase64);
return resultTexture;
}
}
if (classify == "sco")
{
if (FGresponse.scoremap.Length > 0) //
{
string ImgBase64 = FGresponse.scoremap;
resultTexture = UtilityTool.Base64ToTexture2D(resultTexture.width, resultTexture.height, ImgBase64);
return resultTexture;
}
}
return null;
}
/// <summary>
/// 上传分割图像
/// </summary>
/// <param name="json">图片信息</param>
/// <returns></returns>
public string body_seg(string json)//获取返回值
{
string token = UtilityTool.GetToken(UtilityTool.BodySegAK, UtilityTool.BodySegSK);
string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
String str = "image=" + HttpUtility.UrlEncode(json);
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.Default);
string result = reader.ReadToEnd();
Debug.Log(result);
return result;
}
/// <summary>
/// 截图
/// </summary>
/// <param name="rect">大小</param>
/// <returns></returns>
IEnumerator CaptureScreenshot2(Rect rect)
{
yield return new WaitForEndOfFrame();
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);//先创建一个的空纹理,大小可根据实现需要来设置
#pragma warning disable UNT0017 // SetPixels invocation is slow
screenShot.ReadPixels(rect, 0, 0);//读取屏幕像素信息并存储为纹理数据,
#pragma warning restore UNT0017 // SetPixels invocation is slow
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();//然后将这些纹理数据,成一个png图片文件
string filename = Application.streamingAssetsPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
yield return new WaitForEndOfFrame();
Debug.Log(string.Format("截屏了一张图片: {0}", filename));
//最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
StartCoroutine(UploadPhotoCoroutine(filename));
}
/// <summary>
/// Texture转换成Texture2D
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
/// <summary>
/// 图片信息
/// </summary>
public class ImageInfo
{
public string image; //图片信息
public string image_type; //图片类型 BASE64 URL FACE_TOKEN
public string quality_control; //质量控制 NONE LOW NORMAL HIGH HIGH
}
public class ImageInfoFG
{
public string image;
public string type;//1)可选值说明:labelmap - 二值图像,需二次处理方能查看分割效果
// scoremap - 人像前景灰度图
//foreground - 人像前景抠图,透明背景
//2)type 参数值可以是可选值的组合,用逗号分隔;如果无此参数默认输出全部3类结果图
}
private void OnDestroy()
{
if (WebCamera.Instance != null)
{
WebCamera.Instance.CloseCamera();
}
}