forked from microsoft/psi-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (149 loc) · 7.23 KB
/
Program.cs
File metadata and controls
167 lines (149 loc) · 7.23 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace AzureKinectSample
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Spatial.Euclidean;
using Microsoft.Azure.Kinect.BodyTracking;
using Microsoft.Azure.Kinect.Sensor;
using Microsoft.Psi;
using Microsoft.Psi.AzureKinect;
using Microsoft.Psi.Calibration;
using Microsoft.Psi.Imaging;
/// <summary>
/// Azure Kinect sample program.
/// </summary>
public class Program
{
/// <summary>
/// Main entry point.
/// </summary>
public static void Main()
{
// camera resolution settings
const ColorResolution resolution = ColorResolution.R720p;
const int widthSource = 1280;
const int heightSource = 720;
// down sampled resolution
const int widthOutput = 80;
const int heightOutput = 45;
const double scaleFactorWidth = (double)widthOutput / widthSource;
const double scaleFactorHeight = (double)heightOutput / heightSource;
// background subtraction beyond this depth
const double maxDepth = 1.0; // meters
const SensorOrientation initialOrientation = SensorOrientation.Default;
using (var pipeline = Pipeline.Create("AzureKinectSample", DeliveryPolicy.LatestMessage))
{
var azureKinect = new AzureKinectSensor(
pipeline,
new AzureKinectSensorConfiguration()
{
OutputImu = true,
ColorResolution = resolution,
DepthMode = DepthMode.WFOV_Unbinned,
CameraFPS = FPS.FPS15,
BodyTrackerConfiguration = new AzureKinectBodyTrackerConfiguration()
{
CpuOnlyMode = true, // false if CUDA supported GPU available
SensorOrientation = initialOrientation,
},
});
StringBuilder sb = new StringBuilder();
SensorOrientation lastOrientation = (SensorOrientation)(-1); // detect orientation changes
// consuming color, depth, IMU, body tracking, calibration
azureKinect.ColorImage.Resize(widthOutput, heightOutput)
.Join(azureKinect.DepthImage)
.Join(azureKinect.Imu, TimeSpan.FromMilliseconds(10))
.Pair(azureKinect.Bodies)
.Pair(azureKinect.DepthDeviceCalibrationInfo)
.Do(message =>
{
var (color, depth, imu, bodies, calib) = message;
// determine camera orientation from IMU
static SensorOrientation ImuOrientation(ImuSample imu)
{
const double halfGravity = 9.8 / 2;
return
(imu.AccelerometerSample.Z > halfGravity) ? SensorOrientation.Flip180 :
(imu.AccelerometerSample.Y > halfGravity) ? SensorOrientation.Clockwise90 :
(imu.AccelerometerSample.Y < -halfGravity) ? SensorOrientation.CounterClockwise90 :
SensorOrientation.Default; // upright
}
// enumerate image coordinates while correcting for orientation
static (IEnumerable<int>, IEnumerable<int>, bool) EnumerateCoordinates(SensorOrientation orientation)
{
var w = Enumerable.Range(0, widthOutput);
var h = Enumerable.Range(0, heightOutput);
return orientation switch
{
SensorOrientation.Clockwise90 => (h.Reverse(), w, true),
SensorOrientation.Flip180 => (w.Reverse(), h.Reverse(), false),
SensorOrientation.CounterClockwise90 => (h, w.Reverse(), true),
_ => (w, h, false), // normal
};
}
// render color frame as "ASCII art"
sb.Clear();
var bitmap = color.Resource.ToBitmap();
var orientation = ImuOrientation(imu);
var (horizontal, vertical, swap) = EnumerateCoordinates(orientation);
foreach (var j in vertical.Where(n => n % 2 == 0))
{
foreach (var i in horizontal)
{
var (x, y) = swap ? (j, i) : (i, j);
// subtract background beyond max depth
var d = CalibrationExtensions.ProjectToCameraSpace(calib, new Point2D(x / scaleFactorWidth, y / scaleFactorHeight), depth);
if (!d.HasValue || d.Value.Z < maxDepth)
{
var p = bitmap.GetPixel(x, y);
sb.Append(" .:-=+*#%@"[(int)((p.R + p.G + p.B) / 76.5)]);
}
else
{
sb.Append(' '); // subtract background
}
}
sb.Append(Environment.NewLine);
}
// clear console when orientation changes
if (orientation != lastOrientation)
{
Console.Clear();
lastOrientation = orientation;
}
Console.SetCursorPosition(0, 0);
Console.WriteLine(sb.ToString());
// overlay head tracking
if (orientation == initialOrientation)
{
// body tracking works only in initially configured orientation
Console.BackgroundColor = ConsoleColor.Red;
foreach (var body in bodies)
{
if (calib.TryGetPixelPosition(body.Joints[JointId.Head].Pose.Origin, out var p))
{
var x = (int)(p.X * scaleFactorWidth);
var y = (int)(p.Y * scaleFactorHeight / 2);
if (x > 0 && x < widthOutput && y > 0 && y < heightOutput)
{
Console.SetCursorPosition(x, y / 2);
Console.Write(' ');
}
}
}
Console.BackgroundColor = ConsoleColor.Black;
}
});
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
pipeline.RunAsync();
Console.ReadLine(); // press Enter to end
}
}
}
}