-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
178 lines (145 loc) · 5.81 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;
using System.Net.Http;
using System.Collections.Specialized;
using System.Net;
namespace DepthData
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
const float MaxDepthDistance = 2400; //max value returned
const float MinDepthDistance = 1000;
const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;
int i = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectSensorChooser1.KinectSensorChanged += KinectSensorChooser1_KinectSensorChanged;
}
private void KinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
{
KinectSensor oldSensor = (KinectSensor)e.OldValue;
StopKinect(oldSensor);
KinectSensor newSensor = (KinectSensor)e.NewValue;
if (newSensor == null)
{
return;
}
newSensor.ColorStream.Enable();
newSensor.DepthStream.Enable();
newSensor.SkeletonStream.Enable();
newSensor.AllFramesReady += NewSensor_AllFramesReady;
try
{
newSensor.Start();
}
catch (System.IO.IOException)
{
kinectSensorChooser1.AppConflictOccurred();
}
}
private static readonly HttpClient client = new HttpClient();
private void NewSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame == null)
{
return;
}
byte[] pixels = GenerateColoredBytes(depthFrame);
// Every second send an image to handler
if (i % 30 == 0)
{
//Encode pixels
string pix_str = Convert.ToBase64String(pixels);
string answer;
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["pixels"] = pix_str;
var response = wb.UploadValues("http://localhost:8000", "POST", data);
string responseInString = Encoding.UTF8.GetString(response);
answer = responseInString;
}
// Print decoded comand
text_box1.Text = answer;
}
i++;
int stride = depthFrame.Width * 4;
image1.Source =
BitmapSource.Create(depthFrame.Width, depthFrame.Height,
96, 96, PixelFormats.Bgr32, null, pixels, stride);
}
}
private byte[] GenerateColoredBytes(DepthImageFrame depthFrame)
{
//get the raw data from kinect with the depth for every pixel
short[] rawDepthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(rawDepthData);
//use depthFrame to create the image to dispaly on-screen
//depthFrame contains colour information for all pixels in image
//Height * Width * 4 (Blue, Green, Red, empty byte)
Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];
const int BlueIndex = 0;
const int GreenIndex = 1;
const int RedIndex = 2;
//loop through all distances
//pick a RGB color based on distance
for (int depthIndex = 0, colorIndex = 0;
depthIndex < rawDepthData.Length && colorIndex < pixels.Length;
depthIndex++, colorIndex += 4)
{
//gets the depth value
int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;
//equal coloring for monochromatic histogram
byte intensity = CalculateIntensityFromDepth(depth);
pixels[colorIndex + BlueIndex] = intensity;
pixels[colorIndex + GreenIndex] = intensity;
pixels[colorIndex + RedIndex] = intensity;
}
return pixels;
}
public static byte CalculateIntensityFromDepth(int distance)
{
//formula for calculating monochrome intensity for histogram
return (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0)
/ (MaxDepthDistanceOffset)));
}
void StopKinect(KinectSensor sensor)
{
if (sensor != null)
{
if (sensor.IsRunning)
{
sensor.Stop();
if (sensor.AudioSource != null)
{
sensor.AudioSource.Stop();
}
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
StopKinect(kinectSensorChooser1.Kinect);
}
}
}