InputHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.InputProviders;
  8. using bbiwarg.Images;
  9. using bbiwarg.Recognition.FingerRecognition;
  10. using bbiwarg.Recognition.HandRecognition;
  11. using bbiwarg.Recognition.PalmRecognition;
  12. using bbiwarg.Recognition.TouchRecognition;
  13. using bbiwarg.Recognition.Tracking;
  14. using bbiwarg.Server;
  15. using bbiwarg.Graphics;
  16. using bbiwarg.Utility;
  17. using Emgu.CV;
  18. using Emgu.CV.Structure;
  19. namespace bbiwarg
  20. {
  21. class InputHandler
  22. {
  23. private InputProvider inputProvider;
  24. private InputFrame currentInputFrame;
  25. private DepthImage depthImage;
  26. private EdgeImage edgeImage;
  27. private ConfidenceImage confidenceImage;
  28. private FingerDetector fingerDetector;
  29. private HandDetector handDetector;
  30. private PalmDetector palmDetector;
  31. private TouchDetector touchDetector;
  32. private FingerTracker fingerTracker;
  33. private HandTracker handTracker;
  34. private PalmTracker palmTracker;
  35. private TouchTracker touchTracker;
  36. private TouchEventVisualizer touchEventVisualizer;
  37. private TuioCommunicator tuioCommunicator;
  38. public OutputImage[] OutputImages { get; private set; }
  39. public int CurrentFrameID { get; private set; }
  40. public InputHandler(InputProvider inputProvider)
  41. {
  42. this.inputProvider = inputProvider;
  43. this.CurrentFrameID = inputProvider.CurrentFrameID;
  44. initializeConsistentObjects();
  45. }
  46. private void initializeConsistentObjects()
  47. {
  48. fingerTracker = new FingerTracker();
  49. handTracker = new HandTracker();
  50. palmTracker = new PalmTracker();
  51. touchTracker = new TouchTracker();
  52. touchEventVisualizer = new TouchEventVisualizer(Parameters.ImageWidth, Parameters.ImageHeight);
  53. touchTracker.TouchDown += touchEventVisualizer.touchDown;
  54. touchTracker.TouchMove += touchEventVisualizer.touchMove;
  55. touchTracker.TouchUp += touchEventVisualizer.touchUp;
  56. if (Parameters.TuioEnabled)
  57. {
  58. tuioCommunicator = new TuioCommunicator(Parameters.TuioIP.ToString(), Parameters.TuioPort);
  59. touchTracker.TouchDown += tuioCommunicator.touchDown;
  60. touchTracker.TouchMove += tuioCommunicator.touchMove;
  61. touchTracker.TouchUp += tuioCommunicator.touchUp;
  62. }
  63. }
  64. public void resetConsistentObjects()
  65. {
  66. touchTracker.reset();
  67. handTracker.reset();
  68. palmTracker.reset();
  69. fingerTracker.reset();
  70. touchEventVisualizer.reset();
  71. if (Parameters.TuioEnabled)
  72. tuioCommunicator.reset();
  73. }
  74. public void updateFrame()
  75. {
  76. Timer.start("updateFrame");
  77. beforeUpdateFrame();
  78. createConfidenceImage();
  79. createDepthImage();
  80. createEdgeImage();
  81. detectFingers();
  82. trackFingers();
  83. detectHands();
  84. trackHands();
  85. detectPalm();
  86. trackPalm();
  87. detectTouchEvents();
  88. trackTouchEvents();
  89. afterUpdateFrame();
  90. Timer.stop("updateFrame");
  91. }
  92. private void beforeUpdateFrame()
  93. {
  94. Timer.start("beforeUpdateFrame");
  95. currentInputFrame = inputProvider.CurrentFrame;
  96. CurrentFrameID = currentInputFrame.FrameID;
  97. Logger.CurrentFrame = CurrentFrameID;
  98. if (Parameters.TuioEnabled)
  99. tuioCommunicator.initFrame();
  100. Timer.stop("beforeUpdateFrame");
  101. }
  102. private void afterUpdateFrame()
  103. {
  104. Timer.start("afterUpdateFrame");
  105. if (Parameters.TuioEnabled)
  106. tuioCommunicator.commitFrame();
  107. createOutputImages();
  108. Timer.stop("afterUpdateFrame");
  109. }
  110. private void createConfidenceImage()
  111. {
  112. Timer.start("createCnfdncImg");
  113. Image<Gray, Int16> rawConfidenceImage = new Image<Gray, Int16>(Parameters.ImageWidth, Parameters.ImageHeight, Parameters.ImageWidth * 2, currentInputFrame.RawConfidenceData);
  114. confidenceImage = new ConfidenceImage(rawConfidenceImage);
  115. Timer.stop("createCnfdncImg");
  116. }
  117. private void createDepthImage()
  118. {
  119. Timer.start("createDepthImage");
  120. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Parameters.ImageWidth, Parameters.ImageHeight, Parameters.ImageWidth * 2, currentInputFrame.RawDepthData);
  121. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  122. depthImage = new DepthImage(rawDepthImage);
  123. Timer.stop("createDepthImage");
  124. }
  125. private void createEdgeImage()
  126. {
  127. Timer.start("createEdgeImage");
  128. edgeImage = new EdgeImage(depthImage);
  129. Timer.stop("createEdgeImage");
  130. }
  131. private void detectFingers()
  132. {
  133. Timer.start("detectFingers");
  134. fingerDetector = new FingerDetector(depthImage, edgeImage);
  135. Timer.stop("detectFingers");
  136. }
  137. private void trackFingers()
  138. {
  139. Timer.start("trackFingers");
  140. fingerTracker.updateFrame(fingerDetector.Fingers);
  141. Timer.stop("trackFingers");
  142. }
  143. private void detectHands()
  144. {
  145. Timer.start("detectHands");
  146. handDetector = new HandDetector(depthImage, fingerTracker.Fingers);
  147. Timer.stop("detectHands");
  148. }
  149. private void trackHands()
  150. {
  151. Timer.start("trackHands\t");
  152. handTracker.updateFrame(handDetector.Hands);
  153. Timer.stop("trackHands\t");
  154. }
  155. private void detectPalm()
  156. {
  157. Timer.start("detectPalm\t");
  158. palmDetector = new PalmDetector(depthImage, handTracker.Hands);
  159. Timer.stop("detectPalm\t");
  160. }
  161. private void trackPalm()
  162. {
  163. Timer.start("trackPalm\t");
  164. palmTracker.updateFrame(palmDetector.Palms);
  165. Timer.stop("trackPalm\t");
  166. }
  167. private void detectTouchEvents()
  168. {
  169. Timer.start("detectTouchEvents");
  170. touchDetector = new TouchDetector(depthImage, fingerTracker.Fingers, palmTracker.OptimizedPalms);
  171. Timer.stop("detectTouchEvents");
  172. }
  173. private void trackTouchEvents()
  174. {
  175. Timer.start("trackTouchEvents");
  176. touchTracker.updateFrame(touchDetector.TouchEvents);
  177. Timer.stop("trackTouchEvents");
  178. }
  179. private void createOutputImages()
  180. {
  181. Timer.start("createOutputImages");
  182. int numImages = Parameters.OutputNumImages;
  183. OutputImages = new OutputImage[numImages];
  184. for (int i = 0; i < numImages; i++)
  185. {
  186. OutputImages[i] = new OutputImage(Parameters.ImageWidth, Parameters.ImageHeight);
  187. }
  188. //image0
  189. OutputImages[0].drawImage((depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image, Parameters.DepthImageColor);
  190. foreach (Finger f in fingerTracker.Fingers)
  191. {
  192. Color color = (f.TouchEvent != null) ? Parameters.TouchEventDetectedColor : Parameters.FingerTipColor;
  193. OutputImages[0].fillCircle(f.TipPoint.IntX, f.TipPoint.IntY, 5, color);
  194. }
  195. foreach (TrackedFinger tf in fingerTracker.TrackedObjects)
  196. {
  197. if (tf.CurrentState == TrackingState.Tracked)
  198. {
  199. OutputImages[0].fillCircle(tf.TipPointPrediction.IntX, tf.TipPointPrediction.IntY, 3, Parameters.FingerPointsPredictionColor);
  200. OutputImages[0].fillCircle(tf.HandPointPrediction.IntX, tf.HandPointPrediction.IntY, 3, Parameters.FingerPointsPredictionColor);
  201. Vector2D mid = (tf.TipPointPrediction + tf.HandPointPrediction) / 2;
  202. OutputImages[0].drawLineSegment(new Utility.LineSegment2D(mid, mid + Parameters.FingerTrackerNumDirectionsForMeanDirection * tf.MeanDirection), Parameters.FingerMeanDirectionColor);
  203. }
  204. }
  205. //image1
  206. OutputImages[1].drawImage(edgeImage.Image.ThresholdBinary(new Gray(0), new Gray(255)), Parameters.EdgeImageColor);
  207. foreach (Finger f in fingerTracker.Fingers)
  208. {
  209. for (int i = 0; i < f.SliceTrail.NumSlices; i++)
  210. OutputImages[1].drawLineSegment(f.SliceTrail[i].LineSegment, Parameters.FingerSliceColor);
  211. OutputImages[1].drawContour(f.getContour(Parameters.FingerContourMargin), Parameters.FingerContourColor);
  212. OutputImages[1].drawLineSegment(f.LineSegment, Parameters.FingerTrackedColor);
  213. OutputImages[1].drawText(f.MidPoint.IntX, f.MidPoint.IntY, f.TrackID.ToString(), Parameters.FingerIDColor);
  214. }
  215. //image2
  216. foreach (Hand h in handTracker.Hands)
  217. {
  218. OutputImages[2].drawImage(h.Mask.ThresholdBinary(new Gray(0), new Gray(255)), Parameters.HandColor[h.TrackID % Parameters.HandNumColors]);
  219. OutputImages[2].fillCircle(h.Centroid.IntX, h.Centroid.IntY, 5, Parameters.HandCentroidColor);
  220. OutputImages[2].drawText(h.Centroid.IntX, h.Centroid.IntY, h.TrackID.ToString(), Parameters.HandIDColor);
  221. if (h.Palm != null)
  222. OutputImages[2].drawDefect(h.Palm.ThumbDefect, Parameters.HandThumbDefectPointColor, Parameters.HandThumbDefectLineColor);
  223. }
  224. //image3
  225. OutputImages[3].drawImage((depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image.Or(255 - handDetector.HandMask.ThresholdBinary(new Gray(0), new Gray(255))), Parameters.DepthImageColor);
  226. foreach (TrackedTouchEvent tte in touchTracker.TrackedObjects)
  227. {
  228. Vector2D position = tte.AbsolutePositionPrediction;
  229. OutputImages[3].fillCircle(position.IntX, position.IntY, 5, Parameters.TouchEventTrackedColor);
  230. }
  231. foreach (Palm p in palmTracker.OptimizedPalms)
  232. {
  233. OutputImages[3].drawQuadrangleGrid(p.Quad, Parameters.PalmQuadColor, Parameters.PalmGridColor, Parameters.PalmGridNumRows, Parameters.PalmGridNumColumns);
  234. }
  235. //image4
  236. touchEventVisualizer.updateImage();
  237. OutputImages[4] = touchEventVisualizer.OutputImage;
  238. //borders
  239. for (int i = 0; i < numImages; i++)
  240. {
  241. OutputImages[i].drawRectangle(0, 0, Parameters.ImageWidth - 1, Parameters.ImageHeight - 1, Parameters.OutputImageBorderColor);
  242. }
  243. Timer.stop("createOutputImages");
  244. }
  245. }
  246. }