forked from microcosm/KinectV2-OSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
213 lines (188 loc) · 6.46 KB
/
MainWindow.xaml.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
namespace KinectV2OSC
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using Model.Drawing;
using Model.Network;
/// <summary>
/// Interaction logic for MainWindow
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private DrawingImage imageSource;
private KinectSensor kinectSensor;
private BodyFrameReader bodyFrameReader;
private Body[] bodies;
private FrameTimer timer;
private KinectCanvas kinectCanvas;
private BodySender bodySender;
public event PropertyChangedEventHandler PropertyChanged;
public ImageSource ImageSource
{
get { return this.imageSource; }
}
private string framesText;
public string FramesText
{
get { return this.framesText; }
set
{
this.framesText = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("FramesText"));
}
}
}
private string uptimeText;
public string UptimeText
{
get { return this.uptimeText; }
set
{
this.uptimeText = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("UptimeText"));
}
}
}
private string oscText;
public string OscText
{
get { return this.oscText; }
set
{
this.oscText = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("OscText"));
}
}
}
public MainWindow()
{
this.timer = new FrameTimer();
this.InitKinect();
this.InitNetwork();
this.InitWindowObjectAsViewModel();
}
private void InitKinect()
{
Size displaySize = new Size(0, 0);
this.kinectSensor = KinectSensor.GetDefault();
if (this.kinectSensor != null)
{
this.kinectSensor.Open();
var frameDescription = this.kinectSensor.DepthFrameSource.FrameDescription;
displaySize.Width= frameDescription.Width;
displaySize.Height = frameDescription.Height;
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
this.UptimeText = Properties.Resources.InitializingStatusTextFormat;
}
else
{
this.UptimeText = Properties.Resources.NoSensorFoundText;
}
this.kinectCanvas = new KinectCanvas(this.kinectSensor, displaySize);
}
private void InitNetwork()
{
var ipAddress = ReadIpAddressCsv();
var port = Properties.Resources.PortNumber;
this.bodySender = new BodySender(ipAddress, port);
}
private void InitWindowObjectAsViewModel()
{
this.imageSource = this.kinectCanvas.GetDrawingImage();
this.DataContext = this;
this.InitializeComponent();
}
private string ReadIpAddressCsv()
{
string ipAddressCsv;
try
{
System.IO.TextReader file = new StreamReader(Properties.Resources.IpAddressFileName);
ipAddressCsv = file.ReadLine();
file.Close();
file = null;
}
catch(Exception)
{
ipAddressCsv = Properties.Resources.DefaultIpAddressCsv;
}
return ipAddressCsv;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (this.bodyFrameReader != null)
{
this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
}
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if (this.bodyFrameReader != null)
{
this.bodyFrameReader.Dispose();
this.bodyFrameReader = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
var frameReference = e.FrameReference;
try
{
var frame = frameReference.AcquireFrame();
if (frame != null)
{
using (frame)
{
this.timer.AddFrame(frameReference);
this.setStatusText();
this.updateBodies(frame);
this.kinectCanvas.Draw(this.bodies);
this.bodySender.Send(this.bodies);
}
}
}
catch (Exception)
{
Console.WriteLine("Frame exception encountered...");
}
}
private void setStatusText()
{
var framesPerSecond = timer.GetFramesPerSecond();
var runningTime = timer.GetRunningTime();
this.FramesText = string.Format(Properties.Resources.StandardFramesTextFormat, framesPerSecond);
this.UptimeText = string.Format(Properties.Resources.StandardUptimeTextFormat, runningTime);
this.OscText = bodySender.GetStatusText();
}
private void updateBodies(BodyFrame frame)
{
if (this.bodies == null)
{
this.bodies = new Body[frame.BodyCount];
}
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
// As long as those body objects are not disposed and not set to null in the array,
// those body objects will be re-used.
frame.GetAndRefreshBodyData(this.bodies);
}
}
}