Unity利用百度AI实现人体抠像

首先进入百度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();
        }
    }

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Unity中接入百度AI接口,你需要进行以下步骤: 1. 注册百度AI开放平台账号并创建应用,获取AppID、API Key和Secret Key。 2. 下载百度AI SDK for Unity,并将其导入到Unity项目中。 3. 在Unity项目中创建一个脚本,并在其中编写调用百度AI接口的代码。例如,你可以使用语音识别接口,将用户的语音转换成文本。代码示例如下: ``` using Baidu.Aip.Speech; using UnityEngine; public class SpeechRecognition : MonoBehaviour { private const string APP_ID = "你的AppID"; private const string API_KEY = "你的API Key"; private const string SECRET_KEY = "你的Secret Key"; private readonly AudioClip _microphoneClip = Microphone.Start(null, true, 10, 16000); private SpeechRecognizer _speechRecognizer; private void Start() { _speechRecognizer = new SpeechRecognizer(API_KEY, SECRET_KEY); _speechRecognizer.Timeout = 60000; } private void Update() { // 等待录音结束 if (Microphone.IsRecording(null) && Microphone.GetPosition(null) > 0) { return; } // 停止录音 Microphone.End(null); // 调用语音识别接口 var result = _speechRecognizer.Recognize(_microphoneClip.GetData(), "pcm", 16000); if (result != null && result.ErrorCode == 0) { Debug.Log(result.Result[0]); } } } ``` 4. 在Unity中添加麦克风权限,以允许应用访问麦克风。 5. 对于其他的百度AI接口,你可以参考百度AI SDK for Unity中的示例代码,并根据具体需求进行修改。 以上就是在Unity中接入百度AI接口的基本步骤。希望对你有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值