-
Notifications
You must be signed in to change notification settings - Fork 11
/
Program.cs
169 lines (146 loc) · 5.31 KB
/
Program.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
using System;
using System.Threading;
using System.Windows.Forms;
using CommandLine;
using CommandLine.Text;
using DumpKinectSkeletonLib;
using Timer = System.Threading.Timer;
namespace DumpKinectSkeleton
{
internal class Program
{
private const string BodyDataOutputFileSuffix = "_body.csv";
private const string ColorDataOutputFileSuffix = "_color.yuy2";
private KinectSource _kinectSource;
/// <summary>
/// Dump body to file.
/// </summary>
private BodyFrameDumper _bodyFrameDumper;
/// <summary>
/// Dump body to file.
/// </summary>
private ColorFrameDumper _colorFrameDumper;
/// <summary>
/// Status display timer
/// </summary>
private Timer _timer;
[Option( 'v', "video", HelpText = "Dump color video stream data as a yuy2 raw format." )]
public bool DumpVideo { get; set; }
[Option( 's', "synchronize", HelpText = "Synchronize streams." )]
public bool Synchronize { get; set; }
[Option( "prefix", DefaultValue = "output", MetaValue = "PREFIX", HelpText = "Output files prefix." )]
public string BaseOutputFile { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild( this, current => HelpText.DefaultParsingErrorsHandler( this, current ) );
}
public void Run()
{
try
{
_kinectSource = new KinectSource();
_kinectSource.FrameSync = Synchronize;
_kinectSource.FrameProcessExceptionEvent += e =>
{
Console.Error.WriteLine( "Error: " + e.Message );
Terminate();
};
}
catch ( Exception e )
{
Console.Error.WriteLine( "Error initializing Kinect: " + e.Message );
Cleanup();
return;
}
// initialize dumpers
try
{
_bodyFrameDumper = new BodyFrameDumper( _kinectSource, BaseOutputFile + BodyDataOutputFileSuffix );
if ( DumpVideo )
{
_colorFrameDumper = new ColorFrameDumper( _kinectSource, BaseOutputFile + ColorDataOutputFileSuffix );
}
}
catch ( Exception e )
{
Console.Error.WriteLine( "Error preparing dumpers: " + e.Message );
Cleanup();
return;
}
Console.WriteLine( "Starting capture" );
Console.WriteLine( $"Ouput skeleton data in file {BaseOutputFile + BodyDataOutputFileSuffix}" );
if ( DumpVideo )
{
Console.WriteLine( $"Video stream @{_kinectSource.ColorFrameDescription.Width}x{_kinectSource.ColorFrameDescription.Height} outputed in file {BaseOutputFile + ColorDataOutputFileSuffix}" );
}
Console.WriteLine( "Press X, Q or Control + C to stop capture" );
Console.WriteLine();
Console.WriteLine( "Capture rate(s):" );
// write status in console every seconds
_timer = new Timer( o =>
{
Console.Write( $"{_bodyFrameDumper.BodyCount} Skeleton(s) @ { _kinectSource.BodySourceFps:F1} Fps" );
if ( DumpVideo )
{
Console.Write( $" - Color Frames @ { _kinectSource.ColorSourceFps:F1} Fps" );
}
Console.Write( "\r" );
}, null, 1000, 1000 );
// start capture
_kinectSource.Start();
// wait for X, Q or Ctrl+C events to exit
Console.CancelKeyPress += (sender, args) => Cleanup();
while ( true )
{
// Start a console read operation. Do not display the input.
var cki = Console.ReadKey( true );
// Exit if the user pressed the 'X', 'Q' or ControlC key.
if ( cki.Key == ConsoleKey.X || cki.Key == ConsoleKey.Q )
{
break;
}
}
Cleanup();
}
private void Cleanup()
{
Console.WriteLine( Environment.NewLine + $"Stopping capture" );
Close();
}
/// <summary>
/// Execute shutdown tasks
/// </summary>
private void Close()
{
_timer?.Change( Timeout.Infinite, Timeout.Infinite );
if ( _kinectSource != null )
{
_kinectSource.Close();
_kinectSource = null;
}
if ( _bodyFrameDumper != null )
{
_bodyFrameDumper.Close();
_bodyFrameDumper = null;
}
if ( _colorFrameDumper != null )
{
_colorFrameDumper.Close();
_colorFrameDumper = null;
}
}
private void Terminate()
{
SendKeys.SendWait( "Q" );
}
public static void Main( string[] args )
{
var main = new Program();
if ( Parser.Default.ParseArguments( args, main ) )
{
main.Run();
}
}
}
}