GlassesWindow.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 projection;
  29. private List<Vector2D> calibrationPoints;
  30. private int calibrationPointIndex;
  31. private bool calibrationImageUpToDate;
  32. public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler, String name, ImageSize outputSize, int updateInterval)
  33. {
  34. InitializeComponent();
  35. this.inputProvider = inputProvider;
  36. this.inputHandler = inputHandler;
  37. this.inputSize = inputHandler.ImageSize;
  38. this.outputSize = outputSize;
  39. guiUpToDate = false;
  40. calibrationImageUpToDate = false;
  41. Random rand = new Random();
  42. calibrationPoints = new List<Vector2D>();
  43. for (int i = 0; i < 4; i++)
  44. calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D((float)rand.NextDouble(), (float)rand.NextDouble())));
  45. /*calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.25f, 0.25f)));
  46. calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.75f, 0.25f)));
  47. calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.75f, 0.75f)));
  48. calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.25f, 0.75f)));*/
  49. calibrationPointIndex = 0;
  50. projection = new Projection2DTo2D(inputSize, outputSize, calibrationPoints.Count);
  51. Name = name;
  52. Text = name;
  53. timer = new System.Windows.Forms.Timer();
  54. timer.Interval = updateInterval;
  55. timer.Tick += update;
  56. timer.Start();
  57. KeyPreview = true;
  58. }
  59. protected override void OnClosing(CancelEventArgs e)
  60. {
  61. base.OnClosing(e);
  62. inputProvider.stop();
  63. }
  64. private void update(object sender, EventArgs e)
  65. {
  66. Utility.Timer.start("GlassesWindow.update");
  67. if (!inputProvider.IsActive)
  68. Close();
  69. if (projection.IsCalibrated)
  70. {
  71. FrameData frameData = inputHandler.FrameData;
  72. if (frameData != null)
  73. {
  74. lock (frameData)
  75. {
  76. if (currentFrameID != frameData.FrameID)
  77. {
  78. currentFrameID = frameData.FrameID;
  79. Utility.Timer.start("GlassesWindow.update::updateImage");
  80. updateImage(frameData);
  81. Utility.Timer.stop("GlassesWindow.update::updateImage");
  82. }
  83. }
  84. }
  85. }
  86. else
  87. {
  88. if (!calibrationImageUpToDate)
  89. updateCalibrationImage();
  90. }
  91. if (!guiUpToDate)
  92. {
  93. Utility.Timer.start("GlassesWindow.update::updateGUI");
  94. updateGUI();
  95. Utility.Timer.stop("GlassesWindow.update::updateGUI");
  96. }
  97. Utility.Timer.stop("GlassesWindow.update");
  98. }
  99. private void updateImage(FrameData frameData)
  100. {
  101. guiUpToDate = false;
  102. if (image != null)
  103. image.Dispose();
  104. image = new OutputImage(outputSize);
  105. foreach (Palm palm in frameData.TrackedPalms)
  106. {
  107. Quadrangle quadInput = palm.Quad;
  108. Vector2D a = projection.projectPoint(quadInput.TopLeft);
  109. Vector2D b = projection.projectPoint(quadInput.TopRight);
  110. Vector2D c = projection.projectPoint(quadInput.BottomRight);
  111. Vector2D d = projection.projectPoint(quadInput.BottomLeft);
  112. Quadrangle quadOutput = new Quadrangle(a, b, c, d);
  113. image.drawQuadrangleGrid(quadOutput, Color.Yellow, Color.Orange, 3, 4);
  114. }
  115. foreach (Finger finger in frameData.TrackedFingers)
  116. {
  117. Vector2D tipProjected = projection.projectPoint(finger.TipPoint);
  118. image.fillCircle(tipProjected, 10, Color.Yellow);
  119. }
  120. }
  121. private void updateCalibrationImage()
  122. {
  123. guiUpToDate = false;
  124. if (image != null)
  125. image.Dispose();
  126. image = new OutputImage(outputSize);
  127. image.fillCircle(calibrationPoints[calibrationPointIndex], 25, Color.Orange);
  128. }
  129. private void updateGUI()
  130. {
  131. // update image boxes
  132. imageBox.Image = image;
  133. guiUpToDate = true;
  134. }
  135. private void GlassesWindow_OnKeyDown(object sender, KeyEventArgs e)
  136. {
  137. if (e.KeyCode == Keys.K)
  138. {
  139. FrameData frameData = inputHandler.FrameData;
  140. if (frameData != null)
  141. {
  142. lock (frameData)
  143. {
  144. if (frameData.TrackedFingers.Count == 1)
  145. {
  146. Vector2D pointOutput = calibrationPoints[calibrationPointIndex];
  147. Vector2D pointInput = frameData.TrackedFingers[0].TipPoint;
  148. projection.addCalibrationPoints(pointInput, pointOutput);
  149. calibrationPointIndex = (calibrationPointIndex + 1) % calibrationPoints.Count;
  150. calibrationImageUpToDate = false;
  151. }
  152. }
  153. }
  154. }
  155. else if (e.KeyCode == Keys.R)
  156. {
  157. projection.reset();
  158. calibrationImageUpToDate = false;
  159. calibrationPointIndex = 0;
  160. }
  161. }
  162. }
  163. }