This repository was archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
105 lines (84 loc) · 3.54 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
105 lines (84 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using DIM_Kinect7.Drawing;
using DIM_Kinect7.Kinect;
using DIM_Kinect7.Kinect.Consumers;
using DIM_Kinect7.Kinect.Processors;
using DIM_Kinect7.Model;
using Microsoft.Kinect;
using Microsoft.Kinect.VisualGestureBuilder;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace DIM_Kinect7
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string StatusBarText {
get => statusBarText;
private set
{
if (statusBarText != value)
{
statusBarText = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StatusBarText)));
}
}
}
string statusBarText;
const string GestureDatabasePath = @"data\Kinect7.gbd";
readonly KinectSensor sensor;
readonly MultiSourceFrameReader frameReader;
readonly WpfColorFrameAdapter colorFrameAdapter;
readonly GameCanvas canvas;
readonly GestureDetector gestureDetector;
readonly GameState gameState;
public MainWindow()
{
DataContext = this;
gameState = new GameState();
sensor = KinectSensor.GetDefault();
if (!sensor.IsAvailable)
{
StatusBarText = Properties.Resources.NoSensorStatusText;
}
frameReader = sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Body);
frameReader.MultiSourceFrameArrived += (_sender, _args) => canvas.Render();
var colorFrameConsumer = new ColorFrameConsumer(frameReader, ColorImageFormat.Bgra, 4);
//colorFrameAdapter = new WpfColorFrameAdapter(colorFrameConsumer, PixelFormats.Bgra32);
var depthFrameConsumer = new DepthFrameConsumer(frameReader);
var depthFrameColorMapper = new DataToGrayScalePixelMapper(depthFrameConsumer);
colorFrameAdapter = new WpfColorFrameAdapter(depthFrameColorMapper, PixelFormats.Bgra32);
var frameDescription = sensor.DepthFrameSource.FrameDescription;
canvas = new GameCanvas(gameState, new DrawingGroup(), frameDescription.Width, frameDescription.Height);
using (var database = new VisualGestureBuilderDatabase(GestureDatabasePath))
{
gestureDetector = new GestureDetector(sensor, database.AvailableGestures.ToArray());
}
gestureDetector.GestureDetected += GestureDetector_GestureDetected;
InitializeComponent();
}
void GestureDetector_GestureDetected(GestureDetector.GestureDetectionResult detection)
{
if (detection.FirstFrameDetected)
{
StatusBarText = $"{detection.GestureName} (C: {detection.Confidence})";
gameState.CheckCut(CutGestureAssociation.CutFromGesture(detection.GestureName));
}
}
void Window_Loaded(object _sender, RoutedEventArgs _args)
{
canvasOverlayImage.Source = canvas.ImageSource;
sensorFeedbackImage.Source = colorFrameAdapter.ImageSource;
sensor.Open();
gameState.Timer.Start();
}
void Window_Closing(object _sender, CancelEventArgs _args)
{
colorFrameAdapter?.Dispose();
frameReader?.Dispose();
sensor?.Close();
gameState.Timer.Stop();
}
}
}