1. 程式人生 > >Unity結合Kinect快速入坑指南(手勢識別,姿勢識別,人物摳圖,拍照上傳)

Unity結合Kinect快速入坑指南(手勢識別,姿勢識別,人物摳圖,拍照上傳)

官方指令碼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格式 要前後端協商好