Hello everyone, I am new to this forums and I am currently working on building a custom library for the leap motion. It is designed to give you data based on gestures. The problem I am having is even with the device connected and streaming with good lighting and no smudges, it will not detect my hands even with it over the device. I have the code for the library here:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leap;
namespace leap_motion_v3
{
public class LeapManager
{
private byte[] imagedata = new byte[1];
private Controller controller = new Controller();
Bitmap bitmap = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
public LeapManager()
{
controller.StartConnection();
controller.FrameReady += newFrameHandler;
controller.ImageReady += onImageReady;
controller.ImageRequestFailed += onImageRequestFailed;
controller.DeviceFailure += failed;
ColorPalette grayscale = bitmap.Palette;
for (int i = 0; i < 256; i++)
{
grayscale.Entries[i] = Color.FromArgb((int)255, i, i, i);
}
bitmap.Palette = grayscale;
}
public void failed(Object sender, DeviceFailureEventArgs e)
{
Controller controller = sender as Controller;
FailedDeviceList badDevices = controller.FailedDevices();
foreach (FailedDevice badDevice in badDevices)
{
string failureReason = "";
switch (badDevice.Failure)
{
case FailedDevice.FailureType.FAIL_CALIBRATION:
failureReason = "has bad calibration record.";
break;
case FailedDevice.FailureType.FAIL_CONTROL:
failureReason = "failed to establish USB control interfaces.";
break;
case FailedDevice.FailureType.FAIL_FIRMWARE:
failureReason = "has corrupt firmware or failed to update";
break;
case FailedDevice.FailureType.FAIL_TRANSPORT:
failureReason = "not responding.";
break;
case FailedDevice.FailureType.FAIL_UNKNOWN:
failureReason = "failed for unknown reason.";
break;
}
Console.WriteLine("USB device with pnpID: " + badDevice.PnpId + " " + failureReason);
}
}
public getCurrentLeapInfo getRightHandLeapInfo()
{
return leapers_right;
}
public getCurrentLeapInfo getLeftHandLeapInfo()
{
return leapers_left;
}
public DeviceInfo getDeviceInfo()
{
return dev_info;
}
private getCurrentLeapInfo leapers_right;
private getCurrentLeapInfo leapers_left;
private DeviceInfo dev_info;
void newFrameHandler(object sender, FrameEventArgs eventArgs)
{
if (controller.IsConnected && controller.IsServiceConnected)
{
Frame frame = controller.Frame();
Device dev = controller.Devices[0];
DeviceInfo inf = new DeviceInfo();
inf.HorizontalViewAngle = dev.HorizontalViewAngle;
inf.isEmbedded = dev.IsEmbedded;
inf.isLightingBad = dev.IsLightingBad;
inf.IsSmudged = dev.IsSmudged;
inf.isStreaming = dev.IsStreaming;
inf.range = dev.Range;
dev_info = inf;
//The following are Label controls added in design view for the form
var hands = frame.Hands;
if (frame.Hands.Count > 0)
{
Console.WriteLine("");
}
getCurrentLeapInfo leaper = new getCurrentLeapInfo();
vectorConvert vv = new vectorConvert();
foreach (Hand h in hands)
{
if (h.IsRight)
{
leaper.palmNormal = vv.vecToCustVec(h.PalmNormal);
leaper.palmPosition = vv.vecToCustVec(h.PalmPosition);
leaper.palmVelocity = vv.vecToCustVec(h.PalmVelocity);
leaper.palmWidth = h.PalmWidth;
List<Finger> fingers = h.Fingers;
foreach (Finger f in fingers)
{
leapFingerData fing = new leapFingerData();
fing.Direction = vv.vecToCustVec(f.Direction);
fing.isExtended = f.IsExtended;
fing.Length = f.Length;
fing.StabilizedTipPosition = vv.vecToCustVec(f.StabilizedTipPosition);
fing.tipPosition = vv.vecToCustVec(f.TipPosition);
fing.tipVelocity = vv.vecToCustVec(f.TipVelocity);
leaper.fingers.Add(fing);
}
leapers_right = leaper;
}
else
{
leaper.palmNormal = vv.vecToCustVec(h.PalmNormal);
leaper.palmPosition = vv.vecToCustVec(h.PalmPosition);
leaper.palmVelocity = vv.vecToCustVec(h.PalmVelocity);
leaper.palmWidth = h.PalmWidth;
List<Finger> fingers = h.Fingers;
foreach (Finger f in fingers)
{
leapFingerData fing = new leapFingerData();
fing.Direction = vv.vecToCustVec(f.Direction);
fing.isExtended = f.IsExtended;
fing.Length = f.Length;
fing.StabilizedTipPosition = vv.vecToCustVec(f.StabilizedTipPosition);
fing.tipPosition = vv.vecToCustVec(f.TipPosition);
fing.tipVelocity = vv.vecToCustVec(f.TipVelocity);
leaper.fingers.Add(fing);
}
leapers_right = leaper;
}
}
}
//controller.RequestImages(frame.Id, Leap.Image.ImageType.DEFAULT, imagedata);
}
void onImageRequestFailed(object sender, ImageRequestFailedEventArgs e)
{
if (e.reason == Leap.Image.RequestFailureReason.Insufficient_Buffer)
{
imagedata = new byte[e.requiredBufferSize];
}
Console.WriteLine("Image request failed: " + e.message);
}
void onImageReady(object sender, ImageEventArgs e)
{
Rectangle lockArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(lockArea, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte[] rawImageData = imagedata;
System.Runtime.InteropServices.Marshal.Copy(rawImageData, 0, bitmapData.Scan0, e.image.Width * e.image.Height * 2 * e.image.BytesPerPixel);
bitmap.UnlockBits(bitmapData);
}
}
public class getCurrentLeapInfo
{
public customVector palmVelocity;
public customVector palmPosition;
public float palmWidth;
public customVector palmNormal;
public List<leapFingerData> fingers = new List<leapFingerData>();
}
public class leapFingerData
{
public customVector Direction;
public bool isExtended;
public float Length;
public customVector StabilizedTipPosition;
public customVector tipPosition;
public customVector tipVelocity;
}
public class customVector
{
public float x;
public float y;
public float z;
public float pitch;
public float roll;
public float magnitude;
public float normalized;
}
public class vectorConvert
{
public customVector vecToCustVec(Vector v)
{
customVector cust = new customVector();
cust.magnitude = v.Magnitude;
cust.pitch = v.Pitch;
cust.roll = v.Roll;
cust.x = v.x;
cust.y = v.y;
cust.z = v.z;
return cust;
}
}
public class DeviceInfo
{
public bool IsSmudged;
public bool isStreaming;
public bool isEmbedded;
public bool isLightingBad;
public bool setPaused;
public float range;
public float VerticalViewAngle;
public float HorizontalViewAngle;
}
}