-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.cs
83 lines (73 loc) · 4.63 KB
/
.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Kinect;
using Microsoft.Kinect.Face;
namespace KinectFaceDetection
{
class Program
{
static void Main(string[] args)
{
using (var kinectSensor = KinectSensor.GetDefault())
{
kinectSensor.Open();
using (var frameReader = kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Infrared | FrameSourceTypes.Body))
{
var faceFrameSources = new FaceFrameSource[6];
var faceFrameReaders = new FaceFrameReader[6];
for (int i = 0; i < 6; i++)
{
faceFrameSources[i] = new FaceFrameSource(kinectSensor, 0, FaceFrameFeatures.BoundingBoxInColorSpace);
faceFrameReaders[i] = faceFrameSources[i].OpenReader();
}
frameReader.MultiSourceFrameArrived += (s, e) =>
{
using (var multiSourceFrame = e.FrameReference.AcquireFrame())
{
if (multiSourceFrame == null)
{
return;
}
using (var colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame())
using (var infraredFrame = multiSourceFrame.InfraredFrameReference.AcquireFrame())
using (var bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame())
{
if (colorFrame == null || infraredFrame == null || bodyFrame == null)
{
return;
}
var colorFrameDescription = colorFrame.FrameDescription;
var colorPixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * 4];
colorFrame.CopyConvertedFrameDataToArray(colorPixels, ColorImageFormat.Bgra);
var infraredFrameDescription = infraredFrame.FrameDescription;
var infraredPixels = new ushort[infraredFrameDescription.Width * infraredFrameDescription.Height];
infraredFrame.CopyFrameDataToArray(infraredPixels);
var bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
for (int i = 0; i < 6; i++)
{
if (faceFrameSources[i].IsTrackingIdValid)
{
using (var faceFrame = faceFrameReaders[i].AcquireLatestFrame())
{
if (faceFrame != null)
{
var faceBoundingBoxInColorSpace = faceFrame.FaceBoundingBoxInColorSpace;
var x = (int)faceBoundingBoxInColorSpace.Left;
var y = (int)faceBoundingBoxInColorSpace.Top;
var width = (int)faceBoundingBoxInColorSpace.Width;
var height = (int)faceBoundingBoxInColorSpace.Height;
if (x >= 0 && y >= 0 && width > 0 && height > 0 && x + width < colorFrameDescription.Width && y + height < colorFrameDescription.Height)
{
// Draw rectangle around face
for (int j = 0; j < width; j++)
{
var index1 = ((y * colorFrameDescription.Width) + (x + j)) * 4;
var index2 = (((y + height - 1) * colorFrameDescription.Width) + (x + j)) * 4;
colorPixels[index1] = 0;
colorPixels[index1 + 1] = 255;
colorPixels[index1 + 2] = 0;
colorPixels[index2]