In the article " NtKinect: How to make Kinect V2 program as DLL and use it from Unity ", it is explained how to create a DLL file that uses the basic functions of OpenCV and Kinect V2 via NtKinect, and use it from other programs and Unity.
In this article, we will explain how to create a DLL file for skeleton recognition.
Let's craete a DLL library that perform face recognition as an example of Kinect V2 functions that actually requieres other DLLs and additional files.
The magenta color part is the prototype declaration of the function added this time.
NtKinectDll.h |
|
Add the function definition of int setSkeleton(void *, float *, int *, int *). In this function, the image acquired by the RGB camera is reduced to 1/16, and is displayed with cv::imshow(). On the image, the joints are drawn in red. Notice that calling cv::waitKey(1) is needed to display the OpenCV window properly. Since 25 joints are recognized per person, and 6 people may be recognized, so the data area should be allocated more than "sizeof(float) * 3 * 25 * 6" bytes on the caller side. Return value is the number of recognized skeletons.
NtKinectDll.cpp |
|
[Caution](Oct/07/2017 added) If you encounter "dllimport ..." error when building with Visual Studio 2017 Update 2, please refer to here and deal with it to define NTKINECTDLL_EXPORTS in NtKinectDll.cpp.
Since the above zip file may not include the latest "NtKinect.h", Download the latest version from here and replace old one with it.
Let's use NtKinectDll.dll in Unity.
From the top menu, "Assets"-> "Create" -> "C# Script" -> Filename is "NtKinectBehaviour"
The pointers in C++ are treated as System.IntPtr in C#.
NtKinectBehaviour.cs |
|
The default position of Main Camera is (x,y,z)=(0,1,-10). The z coordinates of the recognized joints are positive , so move the Main Camera to (x,y,z)=(0,0,-1).
[Notice] We generates an OpenCV window in DLL to display the skeleton recognition state. Note that when the OpenCV window is focused, that is, when the Unity game window is not focused, the screen of Unity will not change. Click on the top of Unity's window and make it focused, then try the program's behaviour.