123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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 projection;
- private List<Vector2D> calibrationPoints;
- private int calibrationPointIndex;
- private bool calibrationImageUpToDate;
- public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler, String name, ImageSize outputSize, int updateInterval)
- {
- InitializeComponent();
- this.inputProvider = inputProvider;
- this.inputHandler = inputHandler;
- this.inputSize = inputHandler.ImageSize;
- this.outputSize = outputSize;
- guiUpToDate = false;
- calibrationImageUpToDate = false;
- calibrationPoints = new List<Vector2D>();
- calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.25f, 0.25f)));
- calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.75f, 0.25f)));
- calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.75f, 0.75f)));
- calibrationPoints.Add(outputSize.getAbsolutePoint(new Vector2D(0.25f, 0.75f)));
- calibrationPointIndex = 0;
- projection = new Projection2DTo2D(inputSize, outputSize, calibrationPoints.Count);
- Name = name;
- Text = name;
- timer = new System.Windows.Forms.Timer();
- timer.Interval = updateInterval;
- timer.Tick += update;
- timer.Start();
- KeyPreview = true;
- }
- 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 (projection.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 = projection.projectPoint(quadInput.TopLeft);
- Vector2D b = projection.projectPoint(quadInput.TopRight);
- Vector2D c = projection.projectPoint(quadInput.BottomRight);
- Vector2D d = projection.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 = projection.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(calibrationPoints[calibrationPointIndex], 50, 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 = calibrationPoints[calibrationPointIndex];
- Vector2D pointInput = frameData.TrackedFingers[0].TipPoint;
- projection.addCalibrationPoints(pointInput, pointOutput);
- calibrationPointIndex = (calibrationPointIndex + 1) % calibrationPoints.Count;
- calibrationImageUpToDate = false;
- }
- }
- }
- }
- else if (e.KeyCode == Keys.R)
- {
- projection.reset();
- calibrationImageUpToDate = false;
- calibrationPointIndex = 0;
- }
- }
- }
- }
|