InputProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using Iisu;
  8. namespace bbiwarg.Input.InputProviding
  9. {
  10. public delegate void DeviceStartedEventHandler(object sender, EventArgs e);
  11. public delegate void NewFrameEventHandler(object sender, NewFrameEventArgs e);
  12. public class NewFrameEventArgs : EventArgs
  13. {
  14. public int FrameID { get; private set; }
  15. public int Width { get; private set; }
  16. public int Height { get; private set; }
  17. public IntPtr RawDepthData { get; private set; }
  18. public IntPtr RawConfidenceData { get; private set; }
  19. public NewFrameEventArgs(int frameID, int width, int height, IntPtr rawDepthData, IntPtr rawConfidenceData)
  20. {
  21. FrameID = frameID;
  22. Width = width;
  23. Height = height;
  24. RawDepthData = rawDepthData;
  25. RawConfidenceData = rawConfidenceData;
  26. }
  27. }
  28. public class InputProvider
  29. {
  30. protected IHandle handle;
  31. protected IDevice device;
  32. protected IParameterHandle<float> frameRate;
  33. protected IParameterHandle<int> imageWidth;
  34. protected IParameterHandle<int> imageHeight;
  35. protected IDataHandle<Iisu.Data.IImageData> depthImage;
  36. protected IDataHandle<Iisu.Data.IImageData> confidenceImage;
  37. protected int lastFrameID;
  38. public int ImageWidth { get { return imageWidth.Value; } }
  39. public int ImageHeight { get { return imageHeight.Value; } }
  40. public bool IsActive { get; private set; }
  41. public virtual int CurrentFrameID { get { return device.FrameId; } }
  42. public event DeviceStartedEventHandler DeviceStartedEvent;
  43. public event NewFrameEventHandler NewFrameEvent;
  44. public InputProvider()
  45. {
  46. IsActive = false;
  47. lastFrameID = -1;
  48. }
  49. public void initialize()
  50. {
  51. createDevice();
  52. registerHandles();
  53. }
  54. public void start()
  55. {
  56. device.Start();
  57. IsActive = true;
  58. if (DeviceStartedEvent != null)
  59. DeviceStartedEvent(this, new EventArgs());
  60. run();
  61. }
  62. public void stop()
  63. {
  64. IsActive = false;
  65. device.Stop(true);
  66. }
  67. protected void createDevice()
  68. {
  69. handle = Iisu.Iisu.Context.CreateHandle();
  70. IDeviceConfiguration conf = createDeviceConfiguration();
  71. device = handle.InitializeDevice(conf);
  72. }
  73. protected virtual IDeviceConfiguration createDeviceConfiguration()
  74. {
  75. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  76. conf.IsAsynchronous = false;
  77. return conf;
  78. }
  79. protected virtual void registerHandles()
  80. {
  81. imageWidth = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
  82. imageHeight = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
  83. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  84. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  85. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  86. }
  87. protected virtual void run()
  88. {
  89. while (IsActive)
  90. nextFrame();
  91. }
  92. protected virtual void nextFrame()
  93. {
  94. device.UpdateFrame(true);
  95. provideNewFrame();
  96. device.ReleaseFrame();
  97. }
  98. protected void provideNewFrame()
  99. {
  100. if (NewFrameEvent != null)
  101. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, ImageWidth, ImageHeight, depthImage.Value.Raw, confidenceImage.Value.Raw));
  102. }
  103. }
  104. }