123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using bbiwarg.Input.InputHandling;
- using bbiwarg.Input.InputProviding;
- using bbiwarg.Utility;
- using bbiwarg.Recognition.FingerRecognition;
- using bbiwarg.Recognition.PalmRecognition;
- using Emgu.CV.UI;
- namespace bbiwarg.Output.GlassesOutput
- {
- public partial class GlassesWindow : Form
- {
- private InputProvider inputProvider;
- private InputHandler inputHandler;
- private System.Windows.Forms.Timer timer;
- private int currentFrameID;
- private bool guiUpToDate;
- private ImageSize inputSize;
- private ImageSize outputSize;
- private OutputImage image;
- private Projection2DTo2D projection1;
- private Projection3DTo3D projection2;
- private Vector2D currentCalibrationPoint;
- private bool calibrationImageUpToDate;
- private Random rand;
- public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler, String name, Screen screen, int updateInterval)
- {
- InitializeComponent();
- this.inputProvider = inputProvider;
- this.inputHandler = inputHandler;
- this.inputSize = inputHandler.ImageSize;
- this.outputSize = new ImageSize(screen.Bounds.Width, screen.Bounds.Height);
- guiUpToDate = false;
- calibrationImageUpToDate = false;
- rand = new Random();
- currentCalibrationPoint = getRandomOutputPoint();
- projection1 = new Projection2DTo2D(inputSize, outputSize, Parameters.GlassesWindowNumCalibrationPoints);
- projection2 = new Projection3DTo3D(inputSize, outputSize, Parameters.GlassesWindowNumCalibrationPoints);
- Name = name;
- Text = name;
- timer = new System.Windows.Forms.Timer();
- timer.Interval = updateInterval;
- timer.Tick += update;
- timer.Start();
- KeyPreview = true;
- //fullscreen
- FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
- Location = screen.Bounds.Location;
- Size = screen.Bounds.Size;
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- base.OnClosing(e);
- inputProvider.stop();
- }
- private void update(object sender, EventArgs e)
- {
- Utility.Timer.start("GlassesWindow.update");
- if (!inputProvider.IsActive)
- Close();
- if (projection1.IsCalibrated)
- {
- FrameData frameData = inputHandler.FrameData;
- if (frameData != null)
- {
- lock (frameData)
- {
- if (currentFrameID != frameData.FrameID)
- {
- currentFrameID = frameData.FrameID;
- Utility.Timer.start("GlassesWindow.update::updateImage");
- updateImage(frameData);
- Utility.Timer.stop("GlassesWindow.update::updateImage");
- }
- }
- }
- }
- else
- {
- if (!calibrationImageUpToDate)
- updateCalibrationImage();
- }
- if (!guiUpToDate)
- {
- Utility.Timer.start("GlassesWindow.update::updateGUI");
- updateGUI();
- Utility.Timer.stop("GlassesWindow.update::updateGUI");
- }
- Utility.Timer.stop("GlassesWindow.update");
- }
- private void updateImage(FrameData frameData)
- {
- guiUpToDate = false;
- if (image != null)
- image.Dispose();
- image = new OutputImage(outputSize);
- foreach (Palm palm in frameData.TrackedPalms)
- {
- Quadrangle quadInput = palm.Quad;
- Vector2D a = projection1.projectPoint(quadInput.TopLeft);
- Vector2D b = projection1.projectPoint(quadInput.TopRight);
- Vector2D c = projection1.projectPoint(quadInput.BottomRight);
- Vector2D d = projection1.projectPoint(quadInput.BottomLeft);
- Quadrangle quadOutput = new Quadrangle(a, b, c, d);
- image.drawQuadrangleGrid(quadOutput, Color.Yellow, Color.Orange, 3, 4);
- }
- foreach (Finger finger in frameData.TrackedFingers)
- {
- Vector2D tipProjected = projection1.projectPoint(finger.TipPoint);
- image.fillCircle(tipProjected, 10, Color.Yellow);
- }
- }
- private void updateCalibrationImage()
- {
- guiUpToDate = false;
- if (image != null)
- image.Dispose();
- image = new OutputImage(outputSize);
- image.fillCircle(currentCalibrationPoint, 25, Color.Orange);
- }
- private void updateGUI()
- {
- // update image boxes
- imageBox.Image = image;
- guiUpToDate = true;
- }
- private void GlassesWindow_OnKeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.K)
- {
- FrameData frameData = inputHandler.FrameData;
- if (frameData != null)
- {
- lock (frameData)
- {
- if (frameData.TrackedFingers.Count == 1)
- {
- Vector2D pointOutput = currentCalibrationPoint;
- Vector2D pointInput = frameData.TrackedFingers[0].TipPoint;
- Vector3D point3D = inputHandler.CoordinateConverter.convertCoordinate2Dto3D(pointInput, frameData.DepthImage.getDepthAt(pointInput));
- projection1.addCalibrationPoints(pointInput, pointOutput);
- projection2.addCalibrationPoints(point3D, pointInput, pointOutput);
- currentCalibrationPoint = getRandomOutputPoint();
- }
- }
- }
- }
- else if (e.KeyCode == Keys.R)
- {
- projection1.reset();
- currentCalibrationPoint = getRandomOutputPoint();
- }
- }
- private Vector2D getRandomOutputPoint()
- {
- return outputSize.getAbsolutePoint(new Vector2D((float)rand.NextDouble(), (float)rand.NextDouble()));
- }
- }
- }
|