Hi Everyone,
I am working on a project where i have to detect custom gestures from a gesture database made with the Visual Gesture Builder.The code is the following :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Windows.Kinect;
using Microsoft.Kinect.VisualGestureBuilder;
using System.Linq;
public class GestureManager : MonoBehaviour
{
Gesture walkInPlace;
public string databaseName = "OVRGestures.gbd";
public BodySourceManager bodyManager;
VisualGestureBuilderDatabase db;
VisualGestureBuilderFrameSource gestureSource;
VisualGestureBuilderFrameReader gestureReader;
KinectSensor sensor= null;
void Start()
{
sensor = KinectSensor.GetDefault();
if (sensor != null)
{
if (!sensor.IsOpen)
{
Debug.Log("is OPEN");
sensor.Open();
}
}
gestureSource = VisualGestureBuilderFrameSource.Create(sensor, 0);
gestureReader = gestureSource.OpenReader();
if (gestureReader != null)
{
gestureReader.IsPaused = true;
gestureReader.FrameArrived += GestureFrameArrived;
Debug.Log("IS PAUSED");
}
bodyManager = this.gameObject.GetComponent<BodySourceManager>();
string path = System.IO.Path.Combine(Application.streamingAssetsPath,databaseName);
db = VisualGestureBuilderDatabase.Create(path);
Debug.Log("1");
walkInPlace = db.AvailableGestures[0];
Debug.Log("2");
gestureSource.AddGesture(walkInPlace);
Debug.Log("3");
}
// Update Loop, set body if we need one
void Update()
{
if (!gestureSource.IsTrackingIdValid)
{
FindValidBody();
}
}
// Check Body Manager, grab first valid body
void FindValidBody()
{
if (bodyManager != null)
{
Body[] bodies = bodyManager.GetData();
if (bodies != null)
{
foreach (Body body in bodies)
{
if (body.IsTracked)
{
SetBody(body.TrackingId);
break;
}
}
}
}
}
// Public setter for Body ID to track
public void SetBody(ulong id)
{
if (id > 0)
{
gestureSource.TrackingId = id;
gestureReader.IsPaused = false;
}
else
{
gestureSource.TrackingId = 0;
gestureReader.IsPaused = true;
}
}
void GestureFrameArrived(object sender, VisualGestureBuilderFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
var discreteResults = frame.DiscreteGestureResults;
Debug.Log("FRAME ARRIVED");
if ((discreteResults != null) &&
(discreteResults.ContainsKey(this.walkInPlace)))
{
var result = discreteResults[this.walkInPlace];
if (result.Detected == true)
{
Debug.Log("working");
}
}
}
}
}
}
The line that creates problems is this one:
gestureSource.AddGesture(walkInPlace);
after that unity crashes miserably without even showing an error.
I tried to fix it in several kind of ways. One of those is to replace that line with:
Gesture[] gestures = db.AvailableGestures.ToArray<Gesture>();
gestureSource.AddGestures(gestures);
With the method AddGestures it doesn’t crash but give me this erro on the unity console:
ArgumentException: Value does not fall within the expected range.
Rethrow as ArgumentException: This API has returned an exception from an HRESULT: 0x80070057
Helper.ExceptionHelper.CheckLastError () (at Assets/Standard Assets/ExceptionHelper.cs:39)
Microsoft.Kinect.VisualGestureBuilder.VisualGestureBuilderFrameSource.AddGestures (Microsoft.Kinect.VisualGestureBuilder.Gesture[] gestures) (at Assets/Standard Assets/Microsoft/Kinect/VisualGestureBuilder/VisualGestureBuilderFrameSource.cs:351)
GestureManager.Start () (at Assets/KinectView/Scripts/GestureManager.cs:46)
If someone knows a way to fix this I would be very thankful.
Thank you in advance