Skip to content

Commit 768c7d5

Browse files
lab09
1 parent c85c025 commit 768c7d5

34 files changed

+101
-3
lines changed

Kinect2Sample/Kinect2Sample.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
<None Include="Kinect2Sample_TemporaryKey.pfx" />
9393
</ItemGroup>
9494
<ItemGroup>
95+
<Reference Include="DirectXSceneStore">
96+
<HintPath>Libraries\x64\DirectXSceneStore.winmd</HintPath>
97+
</Reference>
9598
<Reference Include="KinectFaceStore">
9699
<HintPath>Libraries\x64\KinectFaceStore.dll</HintPath>
97100
</Reference>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Kinect2Sample/MainPage.xaml

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:local="using:Kinect2Sample"
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:dx="using:DirectXSceneStore"
89
mc:Ignorable="d">
910
<Page.Resources>
1011
<LinearGradientBrush x:Key="ButtonGradientBrush" StartPoint="0,0" EndPoint="0,1" >
@@ -53,6 +54,10 @@
5354
<Viewbox Grid.Row="1" HorizontalAlignment="Center">
5455
<Grid x:Name="BodyJointsGrid" Background="Transparent" Width="512" Height="414"/>
5556
</Viewbox>
57+
<dx:ScenePanel x:Name="DXScenePanel" Grid.Row="1" Margin="20"
58+
Visibility="{Binding CurrentDisplayFrameType,
59+
Converter={StaticResource DisplayTypeToVisibilityConverter},
60+
ConverterParameter=FaceGame }" />
5661
<Viewbox Grid.Row="1" HorizontalAlignment="Center">
5762
<Canvas x:Name="FacePointsCanvas"/>
5863
</Viewbox>
@@ -90,9 +95,13 @@
9095
<TextBlock Text="Color Face" TextWrapping="Wrap" />
9196
</Button>
9297
<Button Style="{StaticResource FrameSelectorButtonStyle}"
93-
Click="InfraredFaceButton_Click" x:Name="InfraredFaceButton">
98+
Click="InfraredFaceButton_Click">
9499
<TextBlock Text="Infrared Face" TextWrapping="Wrap" />
95100
</Button>
101+
<Button Style="{StaticResource FrameSelectorButtonStyle}"
102+
Click="FaceGameButton_Click">
103+
<TextBlock Text="Face Game" TextWrapping="Wrap" />
104+
</Button>
96105
</StackPanel>
97106
</ScrollViewer>
98107
</Grid>

Kinect2Sample/MainPage.xaml.cs

+88-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ public enum DisplayFrameType
3434
BodyJoints,
3535
BackgroundRemoved,
3636
FaceOnColor,
37-
FaceOnInfrared
37+
FaceOnInfrared,
38+
FaceGame
3839
}
3940

4041
public sealed partial class MainPage : Page, INotifyPropertyChanged
4142
{
4243
private const DisplayFrameType DEFAULT_DISPLAYFRAMETYPE = DisplayFrameType.Infrared;
4344

45+
private const double FACE_AIMING_ACCURACY= 1.0;
46+
private const double FACE_AIMING_SENSITIVITY = 0.01;
47+
4448
/// <summary>
4549
/// The highest value that can be returned in the InfraredFrame.
4650
/// It is cast to a float for readability in the visualization code.
@@ -113,6 +117,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
113117
//Cat assets
114118
private Image[] catEyeRightOpen, catEyeRightClosed, catEyeLeftOpen, catEyeLeftClosed, catNose;
115119

120+
//Face Orientation Shaping
121+
private double prevPitch = 0.0f;
122+
private double prevYaw = 0.0f;
123+
116124
public event PropertyChangedEventHandler PropertyChanged;
117125
public string StatusText
118126
{
@@ -391,6 +399,13 @@ private void SetupCurrentDisplay(DisplayFrameType newDisplayFrameType)
391399
this.FacePointsCanvas.Height = infraredFrameDescription.Height;
392400
break;
393401

402+
case DisplayFrameType.FaceGame:
403+
colorFrameDescription = this.kinectSensor.ColorFrameSource.FrameDescription;
404+
this.CurrentFrameDescription = colorFrameDescription;
405+
this.FacePointsCanvas.Width = colorFrameDescription.Width;
406+
this.FacePointsCanvas.Height = colorFrameDescription.Height;
407+
break;
408+
394409
default:
395410
break;
396411
}
@@ -540,16 +555,69 @@ private void Reader_MultiSourceFrameArrived(MultiSourceFrameReader sender, Multi
540555
DrawFaceOnInfrared();
541556
}
542557
break;
558+
case DisplayFrameType.FaceGame:
559+
FaceGameLookUpdate();
560+
break;
543561
default:
544562
break;
545563
}
546564
}
547565

566+
private void FaceGameLookUpdate()
567+
{
568+
this.FacePointsCanvas.Children.Clear();
569+
FaceFrameResult[] results = faceManager.GetLatestFaceFrameResults();
570+
for (int i = 0; i < results.Count(); i++)
571+
{
572+
if (results[i] != null)
573+
{
574+
foreach (KeyValuePair<FacePointType, Point> facePointKVP in
575+
results[i].FacePointsInColorSpace)
576+
{
577+
if (facePointKVP.Value.X == 0.0 || facePointKVP.Value.Y == 0.0)
578+
{
579+
break;
580+
}
581+
Size ellipseSize = new Size(10, 10);
582+
Ellipse ellipse = new Ellipse();
583+
ellipse.Width = ellipseSize.Width;
584+
ellipse.Height = ellipseSize.Height;
585+
ellipse.Fill = new SolidColorBrush(Colors.Red);
586+
Canvas.SetLeft(ellipse, facePointKVP.Value.X - (ellipseSize.Width / 2));
587+
Canvas.SetTop(ellipse, facePointKVP.Value.Y - (ellipseSize.Height / 2));
588+
this.FacePointsCanvas.Children.Add(ellipse);
589+
}
590+
591+
double pitch, roll, yaw = 0;
592+
593+
ExtractFaceRotationInDegrees(results[i].FaceRotationQuaternion, out pitch, out yaw, out roll);
594+
595+
double pitchDiff = Math.Abs(pitch - prevPitch);
596+
double yawDiff = Math.Abs(yaw - prevYaw);
597+
if (pitchDiff > FACE_AIMING_ACCURACY ||
598+
yawDiff > FACE_AIMING_ACCURACY)
599+
{
600+
this.DXScenePanel.SetYawPitch(
601+
-(float)(yaw * FACE_AIMING_SENSITIVITY),
602+
(float)(pitch * FACE_AIMING_SENSITIVITY));
603+
prevPitch = pitch;
604+
prevYaw = yaw;
605+
}
606+
607+
if (results[i].FaceProperties[FaceProperty.MouthOpen] == DetectionResult.Yes)
608+
{
609+
this.DXScenePanel.Fire();
610+
}
611+
break;
612+
}
613+
}
614+
}
615+
548616
private void DrawFaceOnInfrared()
549617
{
550618
FacePointsCanvas.Children.Clear();
551619
FaceFrameResult[] results = faceManager.GetLatestFaceFrameResults();
552-
for (int i = 0; i < results.Count(); i++ )
620+
for (int i = 0; i < results.Count(); i++)
553621
{
554622
if (results[i] != null)
555623
{
@@ -869,6 +937,19 @@ private void RenderPixelArray(byte[] pixels)
869937
this.FrameDisplayImage.Source = this.bitmap;
870938
}
871939

940+
private static void ExtractFaceRotationInDegrees(Vector4 rotQuaternion, out double pitch, out double yaw, out double roll)
941+
{
942+
double x = rotQuaternion.X;
943+
double y = rotQuaternion.Y;
944+
double z = rotQuaternion.Z;
945+
double w = rotQuaternion.W;
946+
947+
// convert face rotation quaternion to Euler angles in degrees
948+
pitch = Math.Atan2(2 * ((y * z) + (w * x)), (w * w) - (x * x) - (y * y) + (z * z)) / Math.PI * 180.0;
949+
yaw = Math.Asin(2 * ((w * y) - (x * z))) / Math.PI * 180.0;
950+
roll = Math.Atan2(2 * ((x * y) + (w * z)), (w * w) + (x * x) - (y * y) - (z * z)) / Math.PI * 180.0;
951+
}
952+
872953
private void InfraredButton_Click(object sender, RoutedEventArgs e)
873954
{
874955
SetupCurrentDisplay(DisplayFrameType.Infrared);
@@ -914,5 +995,10 @@ interface IBufferByteAccess
914995
{
915996
unsafe void Buffer(out byte* pByte);
916997
}
998+
999+
private void FaceGameButton_Click(object sender, RoutedEventArgs e)
1000+
{
1001+
SetupCurrentDisplay(DisplayFrameType.FaceGame);
1002+
}
9171003
}
9181004
}

0 commit comments

Comments
 (0)