forked from AnilOsmanTur/SignRecorderAU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBodyIndexHandler.cs
212 lines (187 loc) · 7.31 KB
/
BodyIndexHandler.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
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;
namespace KinectRecorder
{
class BodyIndexHandler
{
//Bodyindex variables
private Bitmap bBitmap;
static BodyIndexHandler instance = new BodyIndexHandler();
/// Size of the RGB pixel in the bitmap
private const int BytesPerPixel = 4;
public long readerFrameCount = 0;
/// Collection of colors to be used to display the BodyIndexFrame data.
private static readonly uint[] BodyColor =
{
0x0000FF00,
0x00FF0000,
0xFFFF4000,
0x40FFFF00,
0xFF40FF00,
0xFF808000,
};
/// Intermediate storage for frame data converted to color
public uint[] bodyIndexPixels = null;
private FrameDescription bodyIndexFrameDescription = null;
private Queue<System.Drawing.Bitmap> bodyBitmapBuffer = new Queue<System.Drawing.Bitmap>();
public byte[] bodyPixelBuffer;
private String BodyIndexPath;
private VideoFileWriter bodyWriter = new VideoFileWriter();
private VideoFileReader bodyReader = new VideoFileReader();
private int bitRate = 1200000;
public UInt32 frameCount = 0;
public int Width, Height;
private bool bodyRecording = false;
public static BodyIndexHandler Instance
{
get { return instance; }
}
public void BodyIndexHandlerSet(FrameDescription fd)
{
bodyIndexFrameDescription = fd;
Width = fd.Width;
Height = fd.Height;
bodyIndexPixels = new uint[Width * Height];
// to save to a video helper buffer
bodyPixelBuffer = new byte[Width * Height];
}
public void openReader()
{
bodyReader.Open(BodyIndexPath);
readerFrameCount = bodyReader.FrameCount;
}
public void closeReader()
{
bodyReader.Close();
readerFrameCount = 0;
}
public void Write()
{
while (true)
{
//Console.WriteLine("Body");
if (bodyBitmapBuffer.Count > 0)
{
//Console.WriteLine(bodyBitmapBuffer.Count);
this.bodyWriter.WriteVideoFrame(bodyBitmapBuffer.Dequeue());
}
else if (!bodyRecording)
{
bodyWriter.Close();
Console.WriteLine("body writer closed.");
break;
}
else
{
Thread.Sleep(1000);
}
}
}
public ImageSource Read()
{
return UtilityClass.BitmapToImageSource(bodyReader.ReadVideoFrame());
}
public void SetVideoPath(string path, int br)
{
BodyIndexPath = path;
bitRate = br;
openVideoWriter();
}
public void openVideoWriter()
{
Accord.Math.Rational rationalFrameRate = new Accord.Math.Rational(30);
bodyWriter.Open(BodyIndexPath, Width, Height, rationalFrameRate, VideoCodec.MPEG4, bitRate);
frameCount = 0;
}
public void setRecordingState(bool state)
{
bodyRecording = state;
frameCount = 0;
}
public void BodyIndexFrameArrival(BodyIndexFrame bif, ref bool frameProcessed, double fps, WriteableBitmap bodyIndexBitmap)
{
// the fastest way to process the body index data is to directly access
// the underlying buffer
using (Microsoft.Kinect.KinectBuffer bodyIndexBuffer = bif.LockImageBuffer())
{
int width = bif.FrameDescription.Width;
int height = bif.FrameDescription.Height;
// verify data and write the color data to the display bitmap
if (((width * height) == bodyIndexBuffer.Size) &&
(width == bodyIndexBitmap.PixelWidth) && (height == bodyIndexBitmap.PixelHeight))
{
ProcessBodyIndexFrameData(bodyIndexBuffer.UnderlyingBuffer, bodyIndexBuffer.Size);
frameProcessed = true;
}
if (bodyRecording)
{
Bitmap bitmapFrame;
try
{
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
}
catch(Exception e)
{
Console.WriteLine("Body Exception");
Console.WriteLine(e);
System.GC.Collect();
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
}
UtilityClass.ByteArrayToBitmap(ref bitmapFrame, bodyPixelBuffer, width, height);
bBitmap = bitmapFrame;
bodyBitmapBuffer.Enqueue(bBitmap);
//System.GC.Collect();
frameCount++;
if (fps < 16.0)
{
Console.WriteLine("fps drop yaşandı");
bodyBitmapBuffer.Enqueue(bBitmap);
frameCount++;
}
}
}
}
/// <summary>
/// Directly accesses the underlying image buffer of the BodyIndexFrame to
/// create a displayable bitmap.
/// This function requires the /unsafe compiler option as we make use of direct
/// access to the native memory pointed to by the bodyIndexFrameData pointer.
/// </summary>
/// <param name="bodyIndexFrameData">Pointer to the BodyIndexFrame image data</param>
/// <param name="bodyIndexFrameDataSize">Size of the BodyIndexFrame image data</param>
private unsafe void ProcessBodyIndexFrameData(IntPtr bodyIndexFrameData, uint bodyIndexFrameDataSize)
{
byte* frameData = (byte*)bodyIndexFrameData;
// convert body index to a visual representation
for (int i = 0; i < (int)bodyIndexFrameDataSize; ++i)
{
// the BodyColor array has been sized to match
// BodyFrameSource.BodyCount
if (frameData[i] < BodyColor.Length)
{
// this pixel is part of a player,
// display the appropriate color
this.bodyIndexPixels[i] = BodyColor[frameData[i]];
this.bodyPixelBuffer[i] = (byte)255;
}
else
{
// this pixel is not part of a player
// display black
this.bodyIndexPixels[i] = 0x00000000;
this.bodyPixelBuffer[i] = (byte)0;
}
}
}
}
}