UnityKinectDepthExplorer - 如何在Kinect v1中使用此示例?

时间:2017-01-18 12:42:22

标签: unity3d kinect

我有兴趣在Unity中使用Kinect for Windows显示深度数据。不幸的是,我有Kinect v1.8,我知道这很容易用Kinect V2(Kinect Fusion)完成。

我找到了这个例子:https://github.com/rickbarraza/UnityKinectDepthExplorer 我安装了所有必要的组件,现在获得了DLLNOTFOUNDEXCEPTION:

无法加载资产/插件/ x86_64 / KinectUnityAddin.dll'

关于我到目前为止所阅读的所有帖子和论坛,这是因为我使用的是SDK 1.8(而不是SDK 2.0)。

有什么办法可以在Kinect v1.8中使用这个例子吗?我试图联系作者,但他还没有回复。

非常感谢!

1 个答案:

答案 0 :(得分:0)

我在大学项目中遇到了同样的问题,这对您来说不迟到。 这是我制作的Kinect v1.8包装器,只能读取kinect深度图像。我使用了来自Unity-Assetstore的Kinect和MS-SDK作为定向,您不必下载它。您只需要此代码。 使用Init()方法启动并使用GetDepthArray()函数获取深度帧。 希望它对您有用!

您必须安装Kinect 1.8 sdk或Windows / System32文件夹中至少具有1.8 sdk中的Kinect10.dll

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Runtime.CompilerServices;

public class OwnKinectWrapper : MonoBehaviour {

DepthBuffer db;

#region Nui Variables/Structs/Intefraces
IntPtr streamReferenz;

public struct NuiImageViewArea
{
    public int eDigitalZoom;
    public int lCenterX;
    public int lCenterY;
}

public struct NuiSurfaceDesc
{
    uint width;
    uint height;
}

public struct DepthBuffer
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 640 * 480, ArraySubType = UnmanagedType.U2)]
    public ushort[] pixels;
}

public struct NuiLockedRect
{
    public int pitch;
    public int size;
    public IntPtr pBits;

}
public struct NuiImageFrame
{
    public Int64 liTimeStamp;
    public uint dwFrameNumber;
    public int eImageType;
    public int eResolution;
    public IntPtr pFrameTexture;
    public uint dwFrameFlags_NotUsed;
    public NuiImageViewArea ViewArea_NotUsed;
}



[Guid("13ea17f5-ff2e-4670-9ee5-1297a6e880d1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport()]
public interface INuiFrameTexture
{
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int BufferLen();
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int Pitch();
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int LockRect(uint Level, ref NuiLockedRect pLockedRect, IntPtr pRect, uint Flags);
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int GetLevelDesc(uint Level, ref NuiSurfaceDesc pDesc);
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [PreserveSig]
    int UnlockRect(uint Level);
}
#endregion
// Use this for initialization
public int Init() {
    int init = 0;
    try {
        init = NuiInitialize(0x00000020);
        Debug.Log("init : " + init);

        streamReferenz = IntPtr.Zero;
        NuiImageStreamOpen(4, 2, 0, 2, IntPtr.Zero, ref streamReferenz);



    }
    catch (DllNotFoundException e)
    {
        string message = "Please check the Kinect SDK installation.";
        Debug.LogError(message);
        Debug.LogError(e.ToString());

        return -1;
    }
    catch (Exception e)
    {
        Debug.LogError(e.ToString());

        return -1;
    }

    return init;
}

// Update is called once per frame
void Update()
{
    IntPtr imageStreamFrameReferenz = IntPtr.Zero;
    int test = NuiImageStreamGetNextFrame(streamReferenz, 0, ref imageStreamFrameReferenz);
    if (test == 0) {
        NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageStreamFrameReferenz, typeof(NuiImageFrame));
        INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

        NuiLockedRect lockedRectPtr = new NuiLockedRect();
        IntPtr r = IntPtr.Zero;
        frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
        db = (DepthBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(DepthBuffer));
        frameTexture.UnlockRect(0);
        NuiImageStreamReleaseFrame(streamReferenz, imageStreamFrameReferenz);
    }
}

void OnDisable() {
    NuiShutdown();
}

public ushort[] GetDepthArray(){
    return db.pixels;
}


[DllImportAttribute(@"Kinect10.dll",EntryPoint="NuiInitialize")]
public static extern int NuiInitialize (uint dwFlags);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamOpen")]
public static extern int NuiImageStreamOpen(int enumImageType,int enumImgageResolution, uint image_Flags, uint frameBufferLimit, IntPtr nextFrameEvent, ref IntPtr streamHandle );


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamGetNextFrame")]
public static extern int NuiImageStreamGetNextFrame(IntPtr streamReferenz, uint dwMillisecondsToWait, ref IntPtr ImageFrameReferenz);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiImageStreamReleaseFrame")]
public static extern int NuiImageStreamReleaseFrame(IntPtr phStreamHandle, IntPtr ppcImageFrame);


[DllImportAttribute(@"Kinect10.dll",EntryPoint="NuiDepthPixelToDepth")]
public static extern ushort NuiDepthPixelToDepth (ushort depthPixel);


[DllImportAttribute(@"Kinect10.dll", EntryPoint = "NuiShutdown")]
public static extern void NuiShutdown();


}

如果某些功能不起作用或需要帮助,请随时写一些行