VideoHandle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 bbiwarg.Detectors.Fingers;
  8. using bbiwarg.Detectors.Palm;
  9. using bbiwarg.Detectors.Touch;
  10. using bbiwarg.Images;
  11. using bbiwarg.InputProviders;
  12. using Emgu.CV;
  13. using Emgu.CV.Structure;
  14. namespace bbiwarg
  15. {
  16. class VideoHandle
  17. {
  18. private IInputProvider inputProvider;
  19. private InputFrame inputFrame;
  20. private int width;
  21. private int height;
  22. private DepthImage depthImage;
  23. private EdgeImage edgeImage;
  24. private PalmImage palmImage;
  25. private TouchImage touchImage;
  26. private FingerImage fingerImage;
  27. private FingerDetector fingerDetector;
  28. private PalmDetector palmDetector;
  29. private TouchDetector touchDetector;
  30. private PalmTouchDetector palmTouchDetector;
  31. private FingerTracker fingerTracker;
  32. private TouchTracker touchTracker;
  33. public VideoHandle(IInputProvider inputProvider) {
  34. this.inputProvider = inputProvider;
  35. //initialize trackers
  36. touchTracker = new TouchTracker();
  37. fingerTracker = new FingerTracker();
  38. }
  39. public void start() {
  40. inputProvider.init();
  41. inputProvider.start();
  42. inputProvider.updateFrame();
  43. processFrameUpdate();
  44. }
  45. public void stop() {
  46. inputProvider.stop();
  47. }
  48. public bool sourceIsMovie()
  49. {
  50. return inputProvider.sourceIsMovie();
  51. }
  52. public void reversePlay()
  53. {
  54. inputProvider.reversePlay();
  55. }
  56. public void pauseMovie()
  57. {
  58. inputProvider.pauseMovie();
  59. }
  60. public void unpauseMovie()
  61. {
  62. inputProvider.unpauseMovie();
  63. }
  64. public void nextFrame()
  65. {
  66. if (inputProvider.isActive())
  67. {
  68. inputProvider.releaseFrame();
  69. inputProvider.updateFrame();
  70. processFrameUpdate();
  71. }
  72. else
  73. {
  74. inputProvider.stop();
  75. }
  76. }
  77. public int getWidth()
  78. {
  79. return width;
  80. }
  81. public int getHeight()
  82. {
  83. return height;
  84. }
  85. public Int16 getDepthAt(int x, int y) {
  86. return depthImage.getDepthAt(x, y);
  87. }
  88. public float getRelativeDepth(int x, int y) {
  89. return depthImage.getRelativeDepth(x, y);
  90. }
  91. public bool isEdgeAt(int x, int y) {
  92. return edgeImage.isEdgeAt(x, y);
  93. }
  94. public FingerImageState getFingerImageStateAt(int x, int y) {
  95. return fingerImage.getStateAt(x, y);
  96. }
  97. public PalmImageState getPalmImageStateAt(int x, int y)
  98. {
  99. return palmImage.getStateAt(x, y);
  100. }
  101. public TouchImageState getTouchImageStateAt(int x, int y) {
  102. return touchImage.getStateAt(x, y);
  103. }
  104. public List<PalmTouchEvent> getTouchEvents()
  105. {
  106. return palmTouchDetector.getPalmTouchEvents();
  107. }
  108. private void processFrameUpdate()
  109. {
  110. //read data from inputProvider
  111. inputFrame = inputProvider.getInputFrame();
  112. width = inputFrame.getWidth();
  113. height = inputFrame.getHeight();
  114. //create depthImage
  115. Image<Gray, Int16> image = new Image<Gray, Int16>(width, height);
  116. for (int x = 0; x < width; x++) {
  117. for (int y = 0; y < height; y++) {
  118. image.Data[y, x, 0] = inputFrame.getDepthAt(x, y);
  119. }
  120. }
  121. depthImage = new DepthImage(width, height, image);
  122. //create images
  123. edgeImage = new EdgeImage(depthImage);
  124. touchImage = new TouchImage(width, height);
  125. fingerImage = new FingerImage(width, height);
  126. palmImage = new PalmImage(width, height);
  127. //detect+track fingers
  128. fingerDetector = new FingerDetector(depthImage, edgeImage, fingerImage);
  129. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, fingerImage);
  130. //detect palm
  131. palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, palmImage);
  132. //detect+track touchEvents
  133. touchDetector = new TouchDetector(fingerTracker.getFingers(), depthImage, touchImage);
  134. if(palmDetector.getPalmQuad() != null)
  135. palmTouchDetector = new PalmTouchDetector(touchDetector.getTouchEvents(), palmDetector.getPalmQuad());
  136. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents(), touchImage);
  137. }
  138. }
  139. }