Update(Gesture): Body frame avec les os

pull/14/head
Louis DUFOUR 1 year ago
parent ce82e5dadc
commit 50a988422c

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApp", "WpfApp\WpfApp.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib", "Lib\Lib.csproj", "{0751C83E-7845-4E5F-A5D3-E11ABA393ACA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMyGesturesBank", "LibMyGesturesBank\LibMyGesturesBank.csproj", "{2496DFB1-EB55-47A1-A780-211E079B289D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{0751C83E-7845-4E5F-A5D3-E11ABA393ACA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0751C83E-7845-4E5F-A5D3-E11ABA393ACA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0751C83E-7845-4E5F-A5D3-E11ABA393ACA}.Release|Any CPU.Build.0 = Release|Any CPU
{2496DFB1-EB55-47A1-A780-211E079B289D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2496DFB1-EB55-47A1-A780-211E079B289D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2496DFB1-EB55-47A1-A780-211E079B289D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2496DFB1-EB55-47A1-A780-211E079B289D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using Microsoft.Kinect;
namespace LibMyGesturesBank
{
public abstract class BaseGesture
{
// Événement déclenché lorsque le geste est reconnu
public event EventHandler GestureRecognized;
// Nom du geste - marqué comme virtual pour permettre la substitution
public virtual string GestureName { get; protected set; }
// Méthode abstraite pour tester le geste
public abstract void TestGesture(Body body);
// Méthode protégée pour déclencher l'événement GestureRecognized
protected virtual void OnGestureRecognized()
{
GestureRecognized?.Invoke(this, EventArgs.Empty);
}
}
}

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2496DFB1-EB55-47A1-A780-211E079B289D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LibMyGesturesBank</RootNamespace>
<AssemblyName>LibMyGesturesBank</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Kinect, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Kinect.2.0.1410.19000\lib\net45\Microsoft.Kinect.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BaseGesture.cs" />
<Compile Include="Posture.cs" />
<Compile Include="PostureHandsOnHead .cs" />
<Compile Include="PostureHandUp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lib\Lib.csproj">
<Project>{0751c83e-7845-4e5f-a5d3-e11aba393aca}</Project>
<Name>Lib</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using System.Threading.Tasks;
namespace LibMyGesturesBank
{
public abstract class Posture : BaseGesture
{
protected abstract bool TestPosture(Body body);
public override void TestGesture(Body body)
{
if (TestPosture(body))
{
// Posture is recognized
OnGestureRecognized();
}
}
// Ajoutez ici d'autres méthodes ou propriétés nécessaires
}
}

@ -0,0 +1,21 @@
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibMyGesturesBank
{
public class PostureHandUp : Posture
{
protected override bool TestPosture(Body body)
{
// Exemple de condition : la main droite est plus haute que l'épaule droite
return body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.ShoulderRight].Position.Y;
}
public override string GestureName => "Hand Up";
}
}

@ -0,0 +1,37 @@
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibMyGesturesBank
{
public class PostureHandsOnHead : Posture
{
protected override bool TestPosture(Body body)
{
// Condition pour la main droite proche de la tête
bool rightHandNearHead = IsHandNearHead(body.Joints[JointType.HandRight], body.Joints[JointType.Head]);
// Condition pour la main gauche proche de la tête
bool leftHandNearHead = IsHandNearHead(body.Joints[JointType.HandLeft], body.Joints[JointType.Head]);
return rightHandNearHead && leftHandNearHead;
}
private bool IsHandNearHead(Joint hand, Joint head)
{
// Exemple de condition : la main est à moins de 30 cm de la tête
return Math.Sqrt(
Math.Pow(hand.Position.X - head.Position.X, 2) +
Math.Pow(hand.Position.Y - head.Position.Y, 2) +
Math.Pow(hand.Position.Z - head.Position.Z, 2)) < 0.3;
}
public override string GestureName => "Hands On Head";
}
}

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Les informations générales relatives à un assembly dépendent de
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
// associées à un assembly.
[assembly: AssemblyTitle("LibMyGesturesBank")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LibMyGesturesBank")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
[assembly: ComVisible(false)]
// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
[assembly: Guid("2496dfb1-eb55-47a1-a780-211e079b289d")]
// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
//
// Version principale
// Version secondaire
// Numéro de build
// Révision
//
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Kinect" version="2.0.1410.19000" targetFramework="net472" />
</packages>

@ -26,8 +26,12 @@
<Button Content="Filtre 5" Margin="5"/>
</StackPanel>
<Image Grid.Row="2" Source="{Binding InfraredBitmap}" />
<Canvas Grid.Row="2" x:Name="skeletonCanvas" />
<Viewbox Grid.Row="2" Stretch="Uniform">
<Grid>
<Image Source="{Binding ColorBitmap}" />
<Canvas x:Name="skeletonCanvas" />
</Grid>
</Viewbox>
</Grid>
</Window>

@ -69,7 +69,7 @@ namespace WpfApp
// Initialiser la Kinect
this.kinectSensor = KinectSensor.GetDefault();
/* Capteur couleur
// Capteur couleur
// Ouvrir le lecteur de flux de couleur
this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
@ -81,7 +81,6 @@ namespace WpfApp
// Gérer l'événement FrameArrived pour le flux de couleur
this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
*/
// Initialisation du BodyFrameReader
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
@ -102,8 +101,8 @@ namespace WpfApp
// Initialisez depthBitmap pour afficher les données de profondeur
this.depthBitmap = new WriteableBitmap(depthFrameDescription.Width, depthFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
*/
// Capteur infra
// Initialisation du InfraredFrameReader
this.infraredFrameReader = this.kinectSensor.InfraredFrameSource.OpenReader();
this.infraredFrameReader.FrameArrived += this.Reader_InfraredFrameArrived;
@ -115,6 +114,7 @@ namespace WpfApp
// Initialisez infraredBitmap pour afficher les données infrarouges
this.infraredBitmap = new WriteableBitmap(infraredFrameDescription.Width, infraredFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
*/
// Ouvrir la Kinect
this.kinectSensor.Open();
@ -187,25 +187,54 @@ namespace WpfApp
}
}
private void DrawSkeleton(Body body)
{
// Tête et cou
DrawBone(body, JointType.Head, JointType.Neck);
DrawBone(body, JointType.Neck, JointType.SpineShoulder);
// Torse
DrawBone(body, JointType.SpineShoulder, JointType.SpineMid);
DrawBone(body, JointType.SpineMid, JointType.SpineBase);
DrawBone(body, JointType.SpineShoulder, JointType.ShoulderRight);
DrawBone(body, JointType.SpineShoulder, JointType.ShoulderLeft);
DrawBone(body, JointType.SpineBase, JointType.HipRight);
DrawBone(body, JointType.SpineBase, JointType.HipLeft);
// Bras droit
DrawBone(body, JointType.ShoulderRight, JointType.ElbowRight);
DrawBone(body, JointType.ElbowRight, JointType.WristRight);
DrawBone(body, JointType.WristRight, JointType.HandRight);
DrawBone(body, JointType.HandRight, JointType.HandTipRight);
DrawBone(body, JointType.WristRight, JointType.ThumbRight);
// Bras gauche
DrawBone(body, JointType.ShoulderLeft, JointType.ElbowLeft);
DrawBone(body, JointType.ElbowLeft, JointType.WristLeft);
DrawBone(body, JointType.WristLeft, JointType.HandLeft);
DrawBone(body, JointType.HandLeft, JointType.HandTipLeft);
DrawBone(body, JointType.WristLeft, JointType.ThumbLeft);
// Jambe droite
DrawBone(body, JointType.HipRight, JointType.KneeRight);
DrawBone(body, JointType.KneeRight, JointType.AnkleRight);
DrawBone(body, JointType.AnkleRight, JointType.FootRight);
// Jambe gauche
DrawBone(body, JointType.HipLeft, JointType.KneeLeft);
DrawBone(body, JointType.KneeLeft, JointType.AnkleLeft);
DrawBone(body, JointType.AnkleLeft, JointType.FootLeft);
// Dessinez les joints
foreach (JointType jointType in body.Joints.Keys)
{
Joint joint = body.Joints[jointType];
if (joint.TrackingState == TrackingState.Tracked)
{
// Convertir les coordonnées du joint en coordonnées de l'écran
Point point = new Point();
ColorSpacePoint colorPoint = this.kinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
point.X = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X;
point.Y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y;
// Dessiner le joint
DrawJoint(point);
DrawJoint(MapJointToScreen(joint));
}
}
// Dessinez les os ici si nécessaire
}
private void DrawJoint(Point point)
@ -223,6 +252,42 @@ namespace WpfApp
skeletonCanvas.Children.Add(ellipse);
}
private void DrawBone(Body body, JointType jointType0, JointType jointType1)
{
Joint joint0 = body.Joints[jointType0];
Joint joint1 = body.Joints[jointType1];
// Ne dessinez que si les deux joints sont suivis
if (joint0.TrackingState == TrackingState.Tracked && joint1.TrackingState == TrackingState.Tracked)
{
Line bone = new Line
{
Stroke = new SolidColorBrush(Colors.LightBlue),
StrokeThickness = 4,
X1 = MapJointToScreen(joint0).X,
Y1 = MapJointToScreen(joint0).Y,
X2 = MapJointToScreen(joint1).X,
Y2 = MapJointToScreen(joint1).Y
};
skeletonCanvas.Children.Add(bone);
}
}
private Point MapJointToScreen(Joint joint)
{
ColorSpacePoint colorPoint = this.kinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
// Gestion des coordonnées infinies
float x = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X;
float y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y;
return new Point(x, y);
}
private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e)
{
using (DepthFrame depthFrame = e.FrameReference.AcquireFrame())

Loading…
Cancel
Save