InputProvider.cs 4.2 KB

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