InputProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 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 bool IsActive { get; private set; }
  39. public virtual int CurrentFrameID { get { return device.FrameId; } }
  40. public event DeviceStartedEventHandler DeviceStartedEvent;
  41. public event NewFrameEventHandler NewFrameEvent;
  42. public InputProvider()
  43. {
  44. IsActive = false;
  45. lastFrameID = -1;
  46. }
  47. public void start()
  48. {
  49. createDevice();
  50. registerHandles();
  51. device.Start();
  52. IsActive = true;
  53. if (DeviceStartedEvent != null)
  54. DeviceStartedEvent(this, new EventArgs());
  55. run();
  56. }
  57. public void stop()
  58. {
  59. IsActive = false;
  60. device.Stop(true);
  61. }
  62. protected void createDevice()
  63. {
  64. handle = Iisu.Iisu.Context.CreateHandle();
  65. IDeviceConfiguration conf = createDeviceConfiguration();
  66. device = handle.InitializeDevice(conf);
  67. }
  68. protected virtual IDeviceConfiguration createDeviceConfiguration()
  69. {
  70. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  71. conf.IsAsynchronous = false;
  72. return conf;
  73. }
  74. protected virtual void registerHandles()
  75. {
  76. imageWidth = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
  77. imageHeight = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
  78. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  79. Parameters.setImageParameters(imageWidth.Value, imageHeight.Value);
  80. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  81. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  82. }
  83. protected virtual void run()
  84. {
  85. while (IsActive)
  86. nextFrame();
  87. }
  88. protected void nextFrame()
  89. {
  90. device.UpdateFrame(true);
  91. provideNewFrame();
  92. device.ReleaseFrame();
  93. }
  94. protected void provideNewFrame()
  95. {
  96. if (CurrentFrameID != lastFrameID)
  97. {
  98. if (NewFrameEvent != null)
  99. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, imageWidth.Value, imageHeight.Value, depthImage.Value.Raw, confidenceImage.Value.Raw));
  100. lastFrameID = CurrentFrameID;
  101. }
  102. }
  103. }
  104. }