Unity结合Kinect快速入坑指南(手势识别,姿势识别,人物抠图,拍照上传)

版权声明:目前在职,联系16675190421 https://blog.csdn.net/qq_29019031/article/details/81698962

官方脚本Kinect Manager 为核心脚本

设置问题 :最好把User Multi Source Reader勾上

Compute User Map : 默认为 Raw User Depth ,但是抠图选择为 BodyTesture 

Compute Color Map :计算彩色数据

Compute Infrared Map :计算红外数据

Use Multi Source Reader :计算所有数据

因为我要抠图,所以设置如下 :

如果需要姿势识别:1.新建一个物体,然后添加官方脚本Kinect Gestures ,然后新建一个脚本User Kinect Gestures继承接口

KinecyGestures.GestureListenerInterface实现接口

主要实现姿势完成时方法,这里有个坑就是,T型姿势只要完成时,就启动方法,并且保持姿势,1.8秒内可以重新执行

而推的姿势就只是启动就执行一次,保持也不会持续执行,其他的姿势要测试才知。所以T型要加个时间协程,要在2秒后取消状态

完整脚本时要拖拽到kinect下设置好

人物抠图

原理就是从一个底层的Canvas 收集到人物的数据,然后赋值到新的canvas上,新的canvas距离相机位置更近些,让扣出来的人物贴在原来的图像上,然后可以达到抠图的现象,后面可以放东西在背后出来,或者切换的背景

脚本不发上来了,有需要的伙伴可以联系我的QQ175483505

抠图脚本设置

背景Canvas :

人物抠图后 :

人物抠图管理器设置 :

关于切换场景

最好不要切换场景,刚开始切换了场景,屏蔽了DontDestroyOnLoad方法,但是发现跳转场景中会自动关闭和自动打开设备,

kinect案例中有解决办法, 但是没时间一一测试了

拍照上传 

截图方法 :

注释的不用启用

 Texture2D CaptureCamera(Camera camera, Rect rect)
    {
        // 创建一个RenderTexture对象  
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
        camera.targetTexture = rt;
        camera.Render();
        //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。  
        //ps: camera2.targetTexture = rt;  
        //ps: camera2.Render();  
        //ps: -------------------------------------------------------------------  

        // 激活这个rt, 并从中中读取像素。  
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素  
        screenShot.Apply();

        // 重置相关参数,以使用camera继续在屏幕上显示  
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;  
        RenderTexture.active = null; // JC: added to avoid errors  
        GameObject.Destroy(rt);
        //// 最后将这些纹理数据,成一个png图片文件  
        //byte[] bytes = screenShot.EncodeToPNG();
        //string filename = path + "/Screenshot.png";
        //System.IO.File.WriteAllBytes(filename, bytes);
        //Debug.Log(string.Format("截屏了一张照片: {0}", filename));

        return screenShot;
    }

IEnumerator CaptureScreenshot(Camera camera,string path, string photoname, Image Up, Image Down)
    {
        //拍照时间调整
        yield return new WaitForSeconds(0.2f);

        Texture2D screenShot = CaptureCamera(camera, new Rect(0, 0, 1920, 1080));
        Debug.LogError(Time.time);
        yield return new WaitForSeconds(0.1f);
        Sprite temp = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0, 0));
        Up.sprite = temp;
        Up.gameObject.SetActive(true);
        FrameUp.SetActive(true);

        #region 保存
        byte[] bytes = screenShot.EncodeToJPG();
        string pathFile = path + photoname + ".jpg";
        File.WriteAllBytes(pathFile, bytes);
        Debug.Log(string.Format("截屏了一张图片: {0}", pathFile));
        #endregion


        #region 上传
        string url = "XXXXX";//图片要上传到的地址wm
        WWWForm form = new WWWForm();
        form.AddBinaryData("imagefile", bytes, photoname + ".jpg", "image/jpg");
        WWW www = new WWW(url, form);
        #endregion

        #region 下载


        while (!www.isDone)
        {
            //下载
            Debug.Log("aa" + www.progress * 100);
            yield return null;
        }
        yield return www;
        if (www.isDone)
        {
            Debug.Log(www.text);
            Model model = JsonUtility.FromJson<Model>(www.text) as Model;
            Debug.LogError(model);
            string QR_url = model.data;
            WWW w = new WWW(QR_url);
            while (!w.isDone)
            {
                Debug.Log("bb" + www.progress * 100);
                yield return null;
            }
            yield return w;
            if (w.isDone)
            {
                Texture2D pic = w.texture;
                Sprite spr = Sprite.Create(pic, new Rect(0, 0, pic.width, pic.height), new Vector2(0, 0));
                Down.sprite = spr;
                Down.transform.parent.gameObject.SetActive(true);
                FrameDown.SetActive(true);
                ////RenderTexture.active = null;
                Resources.UnloadUnusedAssets();

                // 延迟10s销毁图片
                StartCoroutine(WaitForCapturePlayOver());
            }

        }

        #endregion

    }

Json格式 要前后端协商好

猜你喜欢

转载自blog.csdn.net/qq_29019031/article/details/81698962