Unity3d+百度AI 实现摄像头画面实时手势识别

本文将讲到,如何利用百度ai的人体分析sdk在unity中实现摄像头实时画面的手势识别

工程链接在文末

 

(本人使用的是unity2018,unity2017以上都可以,不然.NET版本不支持)

1.准备工作:首先在百度AI的官网,下载人体分析C# SDK

 

 

 

然后登陆控制台,新建一个人体分析的应用

然后你会获取到该应用的API_KEY和SECRET_KEY,后续开发需要使用

 

 

2.准备工作做完后,新建一个unity工程,在Asset下新建一个文件夹取名Plugins,将第一步下载的SDK中两个DLL文件复制进来

然后就可以编程了

 

 

 

代码很简单,注释也写的很清楚,我只讲下思路:

其实百度AI的SDK是只单纯的识别图片,而我们要做的识别摄像头实时画面中的手势,其实方法很简单

此时我们只需在固定时间截图摄像头画面就可以了,本文设定每两秒截一次图,然后调用SDK识别此图中的手势,就能实现我们所需要的实时画面中的手势

 

代码如下:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Baidu.Aip.BodyAnalysis;
using System.IO;
using UnityEngine.UI;

public class FaceDetect : MonoBehaviour
{
    public string app_id;
    public string api_key;
    public string secret_key;

    Body client;

    private string deviceName;
    private WebCamTexture webTex;

    //百度AI返回的结果数据
    public Text resultMsg;
    //提取其中的手势名称
    public Text detectedGestureMsg;



    void Awake()
    {
        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
               delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                           System.Security.Cryptography.X509Certificates.X509Chain chain,
                           System.Net.Security.SslPolicyErrors sslPolicyErrors)
               {
                   return true; // **** Always accept
               };
    }




    // Use this for initialization
    void Start()
    {
        api_key = "你自己的API_KEY";
        secret_key = "你自己的SECRET_KEY";
        StartCoroutine(CallCamera());
        client = new Body(api_key, secret_key);
        client.Timeout = 60000;  // 修改超时时间
    }

    // Update is called once per frame
    void Update()
    {
        CaptureScreen();
    }

    IEnumerator CallCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            deviceName = devices[0].name;
            //设置摄像机摄像的区域    
            webTex = new WebCamTexture(deviceName, 1024, 768, 20);
            webTex.Play();//开始摄像    
            transform.GetComponent<RawImage>().texture = webTex;
        }
    }


    public float timer = 0;
    //截屏
    void CaptureScreen()
    {
        timer += Time.deltaTime;
        //每隔两秒检测一次
        if (timer > 2)
        {
            //删除上一次检测的图片
            File.Delete(Application.streamingAssetsPath + "/capture.jpg");
            CapturePhoto();
            timer = 0;
        }
    }
    public int width;
    public int height;
    //截图摄像头
    public Camera cameras;
    public string fileName;

    public void CapturePhoto()
    {
        Texture2D screenShot;
        RenderTexture rt = new RenderTexture(width, height, 1);
        cameras.targetTexture = rt;
        cameras.Render();
        RenderTexture.active = rt;
        screenShot = new Texture2D(width, height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        screenShot.Apply();

        //运行此行代码前,先手动在Asset路径下新建一个StreamingAsset文件夹
        fileName = Application.streamingAssetsPath + "/capture.jpg";
        // byte[] bytes = screenShot.EncodeToJPG();
      
        ScaleTextureCutOut(screenShot, 0, 0, 1024, 768);
        Debug.Log(string.Format("截屏了一张照片: {0}", fileName));
   
    }


    //切图

    byte[] ScaleTextureCutOut(Texture2D originalTexture, int pos_x, int pos_y, float originalWidth, float originalHeight)
    {
        Color[] pixels = new Color[(int)(originalWidth * originalHeight)];
        //要返回的新图
        Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
        //批量获取点像素
        pixels = originalTexture.GetPixels(pos_x, pos_y, (int)originalWidth, (int)originalHeight);
        newTexture.SetPixels(pixels);
        newTexture.anisoLevel = 2;
        newTexture.Apply();
        //这一步把裁剪的新图片存下来
        byte[] jpgData = newTexture.EncodeToJPG();
        System.IO.File.WriteAllBytes(fileName, jpgData);
        GestureDemo(fileName);
        return jpgData;
    }



    public void GestureDemo(string filesPath)
    {
        var image = File.ReadAllBytes(filesPath);
        try
        {
            var result = client.Gesture(image);
            resultMsg.text = result.ToString();
            string[] msgArr = resultMsg.text.Split(',');
            //单独提取classname
            for (int i = 0; i < msgArr.Length; i++)
            {
                if (msgArr[i].Contains("classname"))
                {
                    string[] strArr = msgArr[i].Split(':');
                    detectedGestureMsg.text = strArr[1];
                    break;
                }
            }
        }
        catch (System.Exception)
        {
            throw;
        }
        

    }
}

 

工程结构如下:

 

 

运行前记得填写自己的API_KEY和SECRET_KEY

 

 

工程链接:

链接:https://pan.baidu.com/s/1CbGjzRHHVnt5UZ2rrGdCdQ 
提取码:nu3y

  • 14
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
要在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接口的基本步骤。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值