123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using System;
- using System.Runtime.InteropServices;
- using Iisu;
- using MathNet.Numerics.LinearAlgebra.Single;
- namespace bbiwarg.DataSource
- {
- class IIsuDataSource: IVideoDataSource
- {
- private IHandle handle;
- private IDevice device;
- private string moviePath;
- bool active;
- // parameters
- private IParameterHandle<float> frameRate;
- // data
- private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
- private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
- private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
- private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
- private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
- private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
- private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
- private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
- private IDataHandle<Iisu.Data.IImageData> depthImage;
- private IDataHandle<Iisu.Data.IImageData> colorImage;
- /*
- * Creates an Iisu data source.
- * params:
- * moviePath: path to movie to be used as source
- * if empty the camera is used
- */
- public IIsuDataSource(string moviePath = "")
- {
- this.moviePath = moviePath;
- active = false;
- }
- public void init()
- {
- handle = Iisu.Iisu.Context.CreateHandle();
-
- IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
- if (moviePath.Length != 0)
- conf.MoviePath = moviePath;
-
- device = handle.InitializeDevice(conf);
-
- // parameters
- device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
- frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
- // events
- device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
-
- // data
- depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
- colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
-
- handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
- handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
- palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
- palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
- tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
- tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
- forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
- forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
- palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
- palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
- fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
- fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
- fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
- fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
- handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
- handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
- }
- private void onDeviceStatusChanged(string eventName, DeviceStatus status)
- {
- active = status.HasFlag(Iisu.DeviceStatus.Playing);
- }
- public void start()
- {
- device.Start();
- }
- public void stop()
- {
- device.Stop(true);
- }
- public void updateFrame()
- {
- device.UpdateFrame(true);
- }
- public void releaseFrame()
- {
- device.ReleaseFrame();
- }
- public bool isActive()
- {
- return active;
- }
- public int getFrameRate()
- {
- return (int) frameRate.Value;
- }
- public DepthImage getDepthImage()
- {
- Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
- int width = (int) imageInfos.Width;
- int height = (int) imageInfos.Height;
- int numBytes = (int) imageInfos.BytesRaw;
- IntPtr imageData = depthImage.Value.Raw;
-
- short[] depthData = new short[width * height];
- Marshal.Copy(imageData, depthData, 0, width * height);
- return new DepthImage(width, height, depthData);
- }
- public ColorImage getColorImage()
- {
- Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
- int width = (int)imageInfos.Width;
- int height = (int)imageInfos.Height;
- int numBytes = (int)imageInfos.BytesRaw;
- Console.WriteLine("width: " + width + " height: " + height + " numBytes: " + numBytes);
- return new ColorImage();
- }
- private void checkHandIndex(uint handIndex)
- {
- if (handIndex < 1 || handIndex > 2)
- throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
- }
- public bool isHandOpen(uint handIndex)
- {
- checkHandIndex(handIndex);
- return handOpen[handIndex - 1].Value;
- }
- public Vector getPalmPosition3D(uint handIndex)
- {
- checkHandIndex(handIndex);
- return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
- }
- public Vector getTipPosition3D(uint handIndex)
- {
- checkHandIndex(handIndex);
- return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
- }
- public Vector getForearmPosition3D(uint handIndex)
- {
- checkHandIndex(handIndex);
- return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
- }
- public Vector getPalmNormal3D(uint handIndex)
- {
- checkHandIndex(handIndex);
- return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
- }
- public FingerStatus[] getFingerStatus(uint handIndex)
- {
- checkHandIndex(handIndex);
- int[] status = fingerStatus[handIndex - 1].Value;
- FingerStatus[] result = new FingerStatus[status.Length];
- for (int i = 0; i < status.Length; ++i)
- {
- switch (status[i])
- {
- case 0:
- result[i] = FingerStatus.Inactive;
- break;
- case 1:
- result[i] = FingerStatus.Detected;
- break;
- case 2:
- result[i] = FingerStatus.Tracked;
- break;
- }
- }
- return result;
- }
- public Vector[] getFingerTipPositions3D(uint handIndex)
- {
- checkHandIndex(handIndex);
- Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
- Vector[] results = new DenseVector[positions.Length];
- for (int i = 0; i < positions.Length; ++i)
- results[i] = new DenseVector(positions[i].ToArray());
- return results;
- }
- public HandSide getHandSide(uint handIndex)
- {
- checkHandIndex(handIndex);
- int side = handSides[handIndex - 1].Value;
- switch (side)
- {
- case 1:
- return HandSide.Left;
- case 2:
- return HandSide.Right;
- default:
- return HandSide.Unknown;
- }
- }
- }
- }
|