forked from AnilOsmanTur/SignRecorderAU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepthHandler.cs
314 lines (250 loc) · 10.1 KB
/
DepthHandler.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Accord.Video.FFMPEG;
using System.Drawing;
using Microsoft.Kinect;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows;
using System.IO;
namespace KinectRecorder
{
class DepthHandler
{
//Depth variables
private double MapDepthToByte = 8000 / 256;
public byte[] depthPixels = null; // wee have to use byte
/// Size of the RGB pixel in the bitmap
private const int BytesPerPixel = 4;
static DepthHandler instance = new DepthHandler();
private Queue<byte[]> depthBinaryBuffer = new Queue<byte[]>();
public byte[] depthPixelBuffer;
private String depthVideoPath;
private BinaryReader depthReader;
private BinaryWriter binaryWriter;
private int savedBinaryCount = 0;
private int readBinaryCount = 0;
public UInt32 frameCount = 0;
public long readerFrameCount = 0;
private FrameDescription depthFrameDescription = null;
public int Width, Height;
private bool depthRecording = false;
public bool show;
private int garbageCount = 0;
public static DepthHandler Instance
{
get { return instance; }
}
public byte[] depthPreviewPixels;
public void DepthHandlerSet(FrameDescription fd)
{
depthFrameDescription = fd;
Width = fd.Width;
Height = fd.Height;
// to show on screen
depthPixels = new byte[Width * Height * 2];
// to save to a video helper buffer
depthPixelBuffer = new byte[Width * Height * 2];
depthPreviewPixels = new byte[Width * Height * 2];
}
public void SetShowState(bool state)
{
show = state;
}
public void openReader(string path)
{
try
{
depthReader = new BinaryReader(new FileStream(path, FileMode.Open));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot open file.");
return;
}
readerFrameCount = savedBinaryCount - 1; }
public void closeReader()
{
depthReader.Close();
readerFrameCount = 0;
}
public void startReading()
{
readBinaryCount = 0;
}
public void Read(ref WriteableBitmap depthPreview)
{
openReader(depthVideoPath+"/"+readBinaryCount);
this.depthPreviewPixels = depthReader.ReadBytes(Width * Height * 2);
int stride = 2, j, k;
for (int i = 0; i < Width * Height; ++i)
{
k = stride * i;
// Get the depth for this pixel
ushort depth = (ushort)((ushort)depthPreviewPixels[k] + (ushort)(depthPreviewPixels[k + 1] << 8));
// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).
if(depth != 0)
{
depth = (ushort)(((depth - 500) / (float)4000) * (ushort.MaxValue - 1));
this.depthPreviewPixels[k] = (byte)(depth);
this.depthPreviewPixels[k + 1] = (byte)(depth >> 8);
}
}
depthPreview.WritePixels(
new Int32Rect(0, 0, (int)(depthPreview.Width),(int)(depthPreview.Height)),
this.depthPreviewPixels,
depthPreview.PixelWidth*2,
0);
readBinaryCount++;
closeReader();
}
public void Write()
{
while (true)
{
// Console.WriteLine("Depth");
if (depthBinaryBuffer.Count > 0)
{
openBinaryWriter();
//Console.WriteLine(depthBitmapBuffer.Count);
this.binaryWriter.Write(depthBinaryBuffer.Dequeue());
savedBinaryCount++;
closeBinaryWriter();
}
else if (!depthRecording)
{
break;
}
else
{
Thread.Sleep(1000);
}
}
}
public void SetVideoPath(string path, int br)
{
depthVideoPath = path + "/depth";
try
{
Directory.CreateDirectory(depthVideoPath);
}
catch (Exception e)
{
System.Console.WriteLine("The directory creating process failed {0}", e.ToString());
}
frameCount = 0;
savedBinaryCount = 0;
}
public void openBinaryWriter()
{
try
{
binaryWriter = new BinaryWriter(new FileStream(depthVideoPath+"/"+savedBinaryCount, FileMode.Create));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create directory.");
return;
}
}
public void closeBinaryWriter()
{
binaryWriter.Close();
}
public void setRecordingState(bool state)
{
depthRecording = state;
frameCount = 0;
}
public uint getBPP() // bytes per pixel
{
return this.depthFrameDescription.BytesPerPixel;
}
public void DepthFrameArrival(DepthFrame df, ref bool frameProcessed, double fps, WriteableBitmap depthBitmap)
{
// the fastest way to process the body index data is to directly access
// the underlying buffer
using (Microsoft.Kinect.KinectBuffer depthBuffer = df.LockImageBuffer())
{
// verify data and write the color data to the display bitmap
if (((df.FrameDescription.Width * df.FrameDescription.Height) == (depthBuffer.Size / getBPP())) &&
(df.FrameDescription.Width == depthBitmap.PixelWidth) && (df.FrameDescription.Height == depthBitmap.PixelHeight))
{
// Note: In order to see the full range of depth (including the less reliable far field depth)
// we are setting maxDepth to the extreme potential depth threshold
//ushort maxDepth = ushort.MaxValue;
// If you wish to filter by reliable depth distance, uncomment the following line:
ushort maxDepth = df.DepthMaxReliableDistance;
ushort minDepth = df.DepthMinReliableDistance;
ProcessDepthFrameData(depthBuffer.UnderlyingBuffer, depthBuffer.Size, minDepth, maxDepth);
frameProcessed = true;
// depthFrame.CopyFrameDataToArray(this.depthPixelBuffer); done in processing function
if (depthRecording)
{
garbageCount++;
this.depthBinaryBuffer.Enqueue((byte[])(depthPixelBuffer.Clone()));
this.frameCount++;
if (fps < 16.0)
{
garbageCount++;
Console.WriteLine("fps drop yaşandı");
this.depthBinaryBuffer.Enqueue((byte[])(depthPixelBuffer.Clone()));
this.frameCount++;
}
/*if(garbageCount > 500)
{
System.GC.Collect();
garbageCount = 0;
}*/
}
}
}
}
private unsafe void ProcessDepthFrameData(IntPtr depthFrameData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth)
{
this.MapDepthToByte = (maxDepth - minDepth) / 255.0;
//System.Console.WriteLine(MapDepthToByte);
// depth frame data is a 16 bit value
ushort* frameData = (ushort*)depthFrameData;
// convert depth to a visual representation
int stride = 2, strideWrite = 2, j, k;
for (int i = 0; i < (int)(depthFrameDataSize / this.depthFrameDescription.BytesPerPixel); ++i)
{
// Get the depth for this pixel
ushort depth = frameData[i];
j = stride * i;
k = strideWrite * i;
// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).
if (depth >= minDepth && depth <= maxDepth)
{
ushort depthShow = (ushort)(((depth-500) / (float)4000) * (ushort.MaxValue-1));
this.depthPixels[j] = (byte)(depthShow);
this.depthPixels[j + 1] = (byte)(depthShow >> 8);
this.depthPixelBuffer[k] = (byte)(depth);
this.depthPixelBuffer[k + 1] = (byte)(depth >> 8);
}
else if (depth < minDepth)
{
this.depthPixels[j] = (byte)0;
this.depthPixels[j + 1] = (byte)0;
this.depthPixelBuffer[k] = (byte)0; //(frameData[i] / 1000);
this.depthPixelBuffer[k+1] = (byte)0; //((frameData[i] % 1000) / 100);
}
else
{
this.depthPixels[j] = (byte)255;
this.depthPixels[j + 1] = (byte)255;
this.depthPixelBuffer[k] = (byte)255; //(frameData[i] / 1000);
this.depthPixelBuffer[k+1] = (byte)255; //((frameData[i] % 1000) / 100);
}
}
}
}
}