GlassesWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using bbiwarg.Input.InputHandling;
  11. using bbiwarg.Input.InputProviding;
  12. using bbiwarg.Utility;
  13. using bbiwarg.Recognition.FingerRecognition;
  14. using bbiwarg.Recognition.PalmRecognition;
  15. using Emgu.CV.UI;
  16. namespace bbiwarg.Output.GlassesOutput
  17. {
  18. public partial class GlassesWindow : Form
  19. {
  20. private InputProvider inputProvider;
  21. private InputHandler inputHandler;
  22. private System.Windows.Forms.Timer timer;
  23. private int currentFrameID;
  24. private bool guiUpToDate;
  25. private ImageSize inputSize;
  26. private ImageSize outputSize;
  27. private OutputImage image;
  28. private Projection2DTo2D projection1;
  29. private Projection3DTo3D projection2;
  30. private Vector2D currentCalibrationPoint;
  31. private bool calibrationImageUpToDate;
  32. private Random rand;
  33. public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler, String name, Screen screen, int updateInterval)
  34. {
  35. InitializeComponent();
  36. this.inputProvider = inputProvider;
  37. this.inputHandler = inputHandler;
  38. this.inputSize = inputHandler.ImageSize;
  39. this.outputSize = new ImageSize(screen.Bounds.Width, screen.Bounds.Height);
  40. guiUpToDate = false;
  41. calibrationImageUpToDate = false;
  42. rand = new Random();
  43. currentCalibrationPoint = getRandomOutputPoint();
  44. projection1 = new Projection2DTo2D(inputSize, outputSize, Parameters.GlassesWindowNumCalibrationPoints);
  45. projection2 = new Projection3DTo3D(inputSize, outputSize, Parameters.GlassesWindowNumCalibrationPoints);
  46. Name = name;
  47. Text = name;
  48. timer = new System.Windows.Forms.Timer();
  49. timer.Interval = updateInterval;
  50. timer.Tick += update;
  51. timer.Start();
  52. KeyPreview = true;
  53. //fullscreen
  54. FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  55. Location = screen.Bounds.Location;
  56. Size = screen.Bounds.Size;
  57. }
  58. protected override void OnClosing(CancelEventArgs e)
  59. {
  60. base.OnClosing(e);
  61. inputProvider.stop();
  62. }
  63. private void update(object sender, EventArgs e)
  64. {
  65. Utility.Timer.start("GlassesWindow.update");
  66. if (!inputProvider.IsActive)
  67. Close();
  68. if (projection1.IsCalibrated)
  69. {
  70. FrameData frameData = inputHandler.FrameData;
  71. if (frameData != null)
  72. {
  73. lock (frameData)
  74. {
  75. if (currentFrameID != frameData.FrameID)
  76. {
  77. currentFrameID = frameData.FrameID;
  78. Utility.Timer.start("GlassesWindow.update::updateImage");
  79. updateImage(frameData);
  80. Utility.Timer.stop("GlassesWindow.update::updateImage");
  81. }
  82. }
  83. }
  84. }
  85. else
  86. {
  87. if (!calibrationImageUpToDate)
  88. updateCalibrationImage();
  89. }
  90. if (!guiUpToDate)
  91. {
  92. Utility.Timer.start("GlassesWindow.update::updateGUI");
  93. updateGUI();
  94. Utility.Timer.stop("GlassesWindow.update::updateGUI");
  95. }
  96. Utility.Timer.stop("GlassesWindow.update");
  97. }
  98. private void updateImage(FrameData frameData)
  99. {
  100. guiUpToDate = false;
  101. if (image != null)
  102. image.Dispose();
  103. image = new OutputImage(outputSize);
  104. foreach (Palm palm in frameData.TrackedPalms)
  105. {
  106. Quadrangle quadInput = palm.Quad;
  107. Vector2D a = projection1.projectPoint(quadInput.TopLeft);
  108. Vector2D b = projection1.projectPoint(quadInput.TopRight);
  109. Vector2D c = projection1.projectPoint(quadInput.BottomRight);
  110. Vector2D d = projection1.projectPoint(quadInput.BottomLeft);
  111. Quadrangle quadOutput = new Quadrangle(a, b, c, d);
  112. image.drawQuadrangleGrid(quadOutput, Color.Yellow, Color.Orange, 3, 4);
  113. }
  114. foreach (Finger finger in frameData.TrackedFingers)
  115. {
  116. Vector2D tipProjected = projection1.projectPoint(finger.TipPoint);
  117. image.fillCircle(tipProjected, 10, Color.Yellow);
  118. }
  119. }
  120. private void updateCalibrationImage()
  121. {
  122. guiUpToDate = false;
  123. if (image != null)
  124. image.Dispose();
  125. image = new OutputImage(outputSize);
  126. image.fillCircle(currentCalibrationPoint, 25, Color.Orange);
  127. }
  128. private void updateGUI()
  129. {
  130. // update image boxes
  131. imageBox.Image = image;
  132. guiUpToDate = true;
  133. }
  134. private void GlassesWindow_OnKeyDown(object sender, KeyEventArgs e)
  135. {
  136. if (e.KeyCode == Keys.K)
  137. {
  138. FrameData frameData = inputHandler.FrameData;
  139. if (frameData != null)
  140. {
  141. lock (frameData)
  142. {
  143. if (frameData.TrackedFingers.Count == 1)
  144. {
  145. Vector2D pointOutput = currentCalibrationPoint;
  146. Vector2D pointInput = frameData.TrackedFingers[0].TipPoint;
  147. Vector3D point3D = inputHandler.CoordinateConverter.convertCoordinate2Dto3D(pointInput, frameData.DepthImage.getDepthAt(pointInput));
  148. projection1.addCalibrationPoints(pointInput, pointOutput);
  149. projection2.addCalibrationPoints(point3D, pointInput, pointOutput);
  150. currentCalibrationPoint = getRandomOutputPoint();
  151. }
  152. }
  153. }
  154. }
  155. else if (e.KeyCode == Keys.R)
  156. {
  157. projection1.reset();
  158. currentCalibrationPoint = getRandomOutputPoint();
  159. }
  160. }
  161. private Vector2D getRandomOutputPoint()
  162. {
  163. return outputSize.getAbsolutePoint(new Vector2D((float)rand.NextDouble(), (float)rand.NextDouble()));
  164. }
  165. }
  166. }