Compare commits

..

No commits in common. "ecaa884861f3dcb7d9f51b112baa6de332beac67" and "5ce0a50ae14642024c597120c5686165fa861df0" have entirely different histories.

10 changed files with 347 additions and 285 deletions

View File

@ -61,11 +61,12 @@
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />
<Compile Include="Model\CstApplication.cs" /> <Compile Include="Model\CstApplication.cs" />
<Compile Include="Model\GestionFichiersSorties.cs" /> <Compile Include="Model\GestionFichiersSorties.cs" />
<Compile Include="Model\GestionKinect.cs" />
<Compile Include="Model\IGestionFichiers.cs" /> <Compile Include="Model\IGestionFichiers.cs" />
<Compile Include="Model\Perceptron.cs" /> <Compile Include="Model\Perceptron.cs" />
<Compile Include="Model\GestionPerceptrons.cs" /> <Compile Include="Model\GestionPerceptrons.cs" />
<Compile Include="Model\ROGER.cs" />
<Compile Include="Model\Squelette.cs" /> <Compile Include="Model\Squelette.cs" />
<Compile Include="Model\GestionWEntrainement.cs" />
<Compile Include="Presenter\JouerMp3.cs" /> <Compile Include="Presenter\JouerMp3.cs" />
<Compile Include="Presenter\JouerSon.cs" /> <Compile Include="Presenter\JouerSon.cs" />
<Compile Include="App.xaml.cs"> <Compile Include="App.xaml.cs">

View File

@ -1,67 +1,21 @@
using JeuHoy_WPF; using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
namespace JeuHoy_WPF_Natif.Model { namespace JeuHoy_WPF_Natif.Model
public class GestionFichiersSorties : IGestionFichiers { {
public GestionFichiersSorties(){} public class GestionFichiersSorties : IGestionFichiers
{
public GestionFichiersSorties()
{
}
public List<Squelette> ChargerCoordonnees(string fichier) { public List<Squelette> ChargerCoordonnees(string fichier)
List<Squelette> lstSql = new List<Squelette>(); {
string sLigne = ""; throw new System.NotImplementedException();
string[] sTabAttributs = null; }
if (!File.Exists(fichier)) public int SauvegarderCoordonnees(string fichier, List<Squelette> lstData)
return lstSql; {
throw new System.NotImplementedException();
StreamReader lecteur = new StreamReader(fichier);
if (!lecteur.EndOfStream) {
sLigne = lecteur.ReadLine();
int nbElements = Convert.ToInt32(sLigne);
sLigne = lecteur.ReadLine();
int nbAttributs = Convert.ToInt32(sLigne);
for (int i = 0; i < nbElements; i++) {
double[] doubles = new double[nbAttributs];
sLigne = lecteur.ReadLine();
sTabAttributs = sLigne.Split('\t');
for (int j = 0; j < sTabAttributs.Length - 1; j++) {
doubles[j] = Convert.ToDouble(sTabAttributs[j]);
}
string rep = Convert.ToInt32(sTabAttributs[sTabAttributs.Length - 1]).ToString();
lstSql.Add(new Squelette(doubles, rep));
}
}
return lstSql;
}
public int SauvegarderCoordonnees(string fichier, List<Squelette> lstData) {
StreamWriter sw = null;
try {
sw = new StreamWriter(fichier);
} catch {
return CstApplication.ERREUR;
}
try {
sw.WriteLine(lstData.Count);
sw.WriteLine(CstApplication.SKELETONCOUNT * 2);
foreach (Squelette s in lstData) {
foreach (double point in s.Points) {
sw.Write(point.ToString() + "\t");
}
sw.Write(s.Reponse + "\r\n");
}
} catch {
sw.Close();
return CstApplication.ERREUR;
}
sw.Flush();
sw.Close();
return CstApplication.OK;
} }
} }
} }

View File

@ -1,189 +0,0 @@
using JeuHoy_WPF;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
namespace JeuHoy_WPF_Natif.Model {
public class GestionKinect {
public Image Image { get; set; }
public string Console { get; set; }
public Canvas Canvas { get; set; }
public double[] Body { get; set; }
private KinectSensor _sensor;
private MultiSourceFrameReader _multiSourceFrameReader;
private BodyFrameReader _bodyFrameReader;
private WriteableBitmap _bitmap;
private byte[] _picPixels = null;
public GestionKinect(Image img, Canvas cnv) {
Image = img;
Canvas = cnv;
_sensor = KinectSensor.GetDefault();
if (_sensor != null) {
_sensor.Open();
//Lecture des images
_multiSourceFrameReader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color);
_multiSourceFrameReader.MultiSourceFrameArrived += MultiSourceFrameReader_MultiSourceFrameArrived;
FrameDescription frameDescription = _sensor.ColorFrameSource.FrameDescription;
_picPixels = new byte[frameDescription.Width * frameDescription.Height * 4];
//Lecture des squelettes détectés
_bodyFrameReader = _sensor.BodyFrameSource.OpenReader();
_bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived;
}
}
private void MultiSourceFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e) {
MultiSourceFrame multiSource = e.FrameReference.AcquireFrame();
if (multiSource == null)
return;
using (ColorFrame colorFrame = multiSource.ColorFrameReference.AcquireFrame()) {
if (colorFrame != null) {
FrameDescription frameDescription = colorFrame.FrameDescription;
if (_bitmap == null)
_bitmap = new WriteableBitmap(frameDescription.Width,
frameDescription.Height,
96.0,
96.0,
PixelFormats.Bgra32,
null);
colorFrame.CopyConvertedFrameDataToArray(_picPixels, ColorImageFormat.Bgra);
_bitmap.Lock();
Marshal.Copy(_picPixels, 0, _bitmap.BackBuffer, _picPixels.Length);
_bitmap.AddDirtyRect(new Int32Rect(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight));
_bitmap.Unlock();
Image.Source = _bitmap;
}
}
}
public WriteableBitmap GetImage() => _bitmap;
private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) {
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) {
if (bodyFrame == null)
return;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
Body body = bodies.FirstOrDefault(b => b.IsTracked);
if (body != null) {
DessinerSquelette(body, _sensor);
}
}
}
/// <summary>
/// Dessine un ellipse pour chacune des jointure du squelette détecté.
/// </summary>
/// <param name="joueur">Le joueur détecté</param>
/// <param name="sensor">Le sensor Kinect</param>
private void DessinerSquelette(Body body, KinectSensor sensor) {
try {
if (body != null) {
Canvas.Children.Clear();
Joint[] joints = body.Joints.Values.ToArray();
for (int i = 0; i < joints.Count(); i++)
DrawJoint(sensor, joints[i], CstApplication.BODY_ELLIPSE_SIZE, Canvas);
}
} catch (Exception ex) {
Console = ex.Message;
}
if (Body is null)
Body = new double[CstApplication.SKELETONCOUNT * 2];
Vector trans = (Vector)GetPoint(sensor, body.Joints[JointType.Head].Position, new Vector(0, 0));
for (int i = 0; i < CstApplication.SKELETONCOUNT; ++i) {
Point po = GetPoint(sensor, body.Joints[Squelette.Joints[i]].Position, trans);
Body[i * 2] = po.X;
Body[i * 2 + 1] = po.Y;
}
}
/// <summary>
/// Dessine le joint d'un squellete d'un senseur Kinect sur le canvas passé en paramètre
/// </summary>
/// <param name="sensor"></param>
/// <param name="joint"></param>
/// <param name="size"></param>
/// <param name="canvas"></param>
private void DrawJoint(KinectSensor sensor, Joint joint, int size, Canvas canvas) {
if (joint.Position.X != 0 && joint.Position.Y != 0 && joint.Position.Z != 0) {
// Convertir la position du joint en coordonnées d'écran
System.Windows.Point point = GetPoint(sensor, joint.Position, canvas.Height, canvas.Width);
// Créer un cercle à la position du joint
Ellipse ellipse = new Ellipse();
ellipse.Fill = new SolidColorBrush(Colors.Yellow);
ellipse.Width = size;
ellipse.Height = size;
// Positionner le cercle sur l'élément de dessin Canvas
Canvas.SetLeft(ellipse, point.X - size / 2);
Canvas.SetTop(ellipse, point.Y - size / 2);
// Ajouter le cercle à l'élément de dessin Canvas
canvas.Children.Add(ellipse);
}
}
/// <summary>
/// Retourne le point x,y d'un joint par rapport à la taille d'un canvas.
/// J'ai permis de dépasser le canvas car je trouvais ça drole :-)
/// </summary>
/// <param name="sensor"></param>
/// <param name="position"></param>
/// <param name="iCanvasHeight"></param>
/// <param name="iCanvasWidth"></param>
/// <returns></returns>
public System.Windows.Point GetPoint(KinectSensor sensor, CameraSpacePoint position, double iCanvasHeight, double iCanvasWidth) {
System.Windows.Point point = new System.Windows.Point();
DepthSpacePoint depthPoint = sensor.CoordinateMapper.MapCameraPointToDepthSpace(position);
point.X = float.IsInfinity(depthPoint.X) ? 0.0 : depthPoint.X;
point.Y = float.IsInfinity(depthPoint.Y) ? 0.0 : depthPoint.Y;
// La Kinect pour Xbox One utilise également le SDK 2 de Microsoft, et sa résolution de profondeur est de 512x424 pixels.
//// Ainsi, la résolution de la carte de profondeur pour la Kinect pour Xbox One est également de 512x424 pixels.
point.X = point.X / 512 * iCanvasHeight;
point.Y = point.Y / 424 * iCanvasWidth;
return point;
}
private Point GetPoint(KinectSensor sensor, CameraSpacePoint position, Vector trans) {
Point point = new System.Windows.Point();
DepthSpacePoint depthPoint = sensor.CoordinateMapper.MapCameraPointToDepthSpace(position);
point.X = float.IsInfinity(depthPoint.X) ? 0.0 : depthPoint.X;
point.Y = float.IsInfinity(depthPoint.Y) ? 0.0 : depthPoint.Y;
// La Kinect pour Xbox One utilise également le SDK 2 de Microsoft, et sa résolution de profondeur est de 512x424 pixels.
//// Ainsi, la résolution de la carte de profondeur pour la Kinect pour Xbox One est également de 512x424 pixels.
point.X = point.X / 512;
point.Y = point.Y / 424;
return Point.Subtract(point, trans);
}
}
}

View File

@ -4,25 +4,19 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JeuHoy_WPF_Natif.Model;
namespace JeuHoy_WPF_Natif.Model { namespace JeuHoy_WPF_Natif.Model {
public class GestionPerceptrons { public class GestionPerceptrons {
private Dictionary<string, Perceptron> _lstPerceptrons = new Dictionary<string, Perceptron>(); private Dictionary<string, Perceptron> _lstPerceptrons = new Dictionary<string, Perceptron>();
private IGestionFichiers _gestionSortie = new GestionFichiersSorties(); private Model.IGestionFichiers _gestionSortie = new GestionFichiersSorties();
private List<Squelette> _lstData; private List<Squelette> _lstData = new List<Squelette>();
/// <summary> /// <summary>
/// Constructeur /// Constructeur
/// </summary> /// </summary>
public GestionPerceptrons() { public GestionPerceptrons() {
_lstData = _gestionSortie.ChargerCoordonnees("train.txt");
for (char x = '0'; x <= '9'; ++x) for (char x = '0'; x <= '9'; ++x)
_lstPerceptrons.Add(x.ToString(), new Perceptron(x.ToString())); _lstPerceptrons.Add(x.ToString(), new Perceptron(x.ToString()));
foreach (Squelette sql in _lstData)
Entrainement(sql, "");
} }
/// <summary> /// <summary>
@ -50,7 +44,6 @@ namespace JeuHoy_WPF_Natif.Model {
if (reponse != "") { if (reponse != "") {
coordo.Reponse = reponse; coordo.Reponse = reponse;
_lstData.Add(coordo); _lstData.Add(coordo);
SauvegarderCoordonnees("train.txt");
} }
if (_lstData is null) if (_lstData is null)

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JeuHoy_WPF_Natif.Model
{
/// <summary>
/// Description: Logique pour la vue Entrainement
/// </summary>
public class GestionWEntrainement
{
}
}

View File

@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Presentation3Tiers.DAL
{
/// <summary>
/// Auteur : Hugo St-Louis
/// Description : Classe concrète qui gère un fichier texte.
/// Date : 2023-03-29
/// </summary>
public class GestionFichierTexte : ILectureFichier
{
public BDApprentissageAuto BD { get; set; }
/// <summary>
/// permet de remplir la structure de données pour l'apprentissage automatique à partir dun fichier texte
/// </summary>
/// <param name="sNomFichier"></param>
public BDApprentissageAuto RemplirDonneesBD(string sNomFichier)
{
StreamReader lecteur = new StreamReader(sNomFichier);
string sLigne = "";
BD = new BDApprentissageAuto();
string[] sTabElements = null;
if (!lecteur.EndOfStream)
{
sLigne = lecteur.ReadLine();
BD.NbElements = Convert.ToInt32(sLigne);
sLigne = lecteur.ReadLine();
BD.NbAttributs = Convert.ToInt32(sLigne);
BD.Elements = new double[BD.NbElements, BD.NbAttributs - 1];
BD.Resultats = new int[BD.NbElements];
for (int i = 0; i < BD.NbElements; i++)
{
sLigne = lecteur.ReadLine();
sTabElements = sLigne.Split('\t');
for (int j = 0; j < sTabElements.Length - 1; j++)
{
BD.Elements[i, j] = Convert.ToDouble(sTabElements[j]);
}
BD.Resultats[i] = Convert.ToInt32(sTabElements[sTabElements.Length - 1]);
}
}
return BD;
}
/// <summary>
/// rempli et affiche les resultats dun fichier dapprentissage auto
/// </summary>
/// <param name="sNomFichier"></param>
/// <returns></returns>
public string RemplirEtAfficherDonneesBD(string sNomFichier)
{
RemplirDonneesBD(sNomFichier);
string sResultat = "";
sResultat = "nb elements : " + BD.NbElements + "\r\n";
sResultat += "nb attribut : " + BD.NbAttributs + "\r\n";
sResultat += "Voici les attribut et valeur de verité" + "\r\n";
for (int i = 0; i < BD.NbElements; i++)
{
for (int j = 0; j < BD.NbAttributs - 1; j++)
{
sResultat += BD.Elements[i, j] + "\t";
}
sResultat += "\r\nValeur de verite : " + BD.Resultats[i] + "\r\n";
}
return sResultat;
}
/// <summary>
/// Lit et retourne le contenu d'un fichier texte
/// pour lequel le nom est passé en paramètre.
/// </summary>
/// <param name="sNomFichier"></param>
/// <returns></returns>
public string LireFichierTexte(string sNomFichier)
{
bool bFichierExiste = File.Exists(sNomFichier);
string sContenuFichier = "";
StreamReader sr = null;
if (!bFichierExiste)
return "Le fichier n'existe pas ....";
try
{
sr = new StreamReader(sNomFichier);
sContenuFichier = sr.ReadToEnd();
return sContenuFichier;
}
catch (Exception ex)
{
return "Erreur lors de la lecture du fichier : " +
ex.Message;
}
}
}
}

View File

@ -9,7 +9,7 @@ using System.Windows;
namespace JeuHoy_WPF_Natif.Model { namespace JeuHoy_WPF_Natif.Model {
public class Squelette { public class Squelette {
static public readonly JointType[] Joints = new JointType[CstApplication.SKELETONCOUNT] { static private JointType[] _joints = new JointType[CstApplication.SKELETONCOUNT] {
JointType.HandLeft, JointType.HandLeft,
JointType.HandRight, JointType.HandRight,
JointType.ElbowLeft, JointType.ElbowLeft,
@ -22,20 +22,42 @@ namespace JeuHoy_WPF_Natif.Model {
JointType.Neck JointType.Neck
}; };
private double[] _sque; private double[] _sque = new double[CstApplication.SKELETONCOUNT * 2];
private string _rep = "?"; private string _rep = "?";
public string Reponse { get => _rep; set => _rep = value; } public string Reponse { get => _rep; set => _rep = value; }
public double[] Points => _sque; public double[] Points => _sque;
public Squelette(double[] body, string reponse) { public Squelette(KinectSensor kin, Body body, string reponse) {
if (body.Length != CstApplication.SKELETONCOUNT * 2) { Vector trans = (Vector)GetPoint(kin, body.Joints[JointType.Head].Position, new Vector(0,0));
_sque = new double[CstApplication.SKELETONCOUNT * 2];
} else { for (int i = 0; i < CstApplication.SKELETONCOUNT; ++i) {
_sque = body; Point po = GetPoint(kin, body.Joints[_joints[i]].Position, trans);
_rep = reponse;
_sque[i * 2] = po.X;
_sque[i * 2 + 1] = po.Y;
} }
if (reponse.Length == 1)
_rep = reponse;
} }
private Point GetPoint(KinectSensor sensor, CameraSpacePoint position, Vector trans) {
Point point = new System.Windows.Point();
DepthSpacePoint depthPoint = sensor.CoordinateMapper.MapCameraPointToDepthSpace(position);
point.X = float.IsInfinity(depthPoint.X) ? 0.0 : depthPoint.X;
point.Y = float.IsInfinity(depthPoint.Y) ? 0.0 : depthPoint.Y;
// La Kinect pour Xbox One utilise également le SDK 2 de Microsoft, et sa résolution de profondeur est de 512x424 pixels.
//// Ainsi, la résolution de la carte de profondeur pour la Kinect pour Xbox One est également de 512x424 pixels.
point.X = point.X / 512;
point.Y = point.Y / 424;
return Point.Subtract(point, trans);
}
} }
} }

View File

@ -14,22 +14,21 @@ namespace JeuHoy_WPF_Natif.Presenter
public class PresentateurWEntrainement public class PresentateurWEntrainement
{ {
private IwEntrainement _vue; private IwEntrainement _vue;
private GestionPerceptrons _gestionnairePerceptron; private GestionWEntrainement _gestionnaire;
private GestionKinect _gestionnaireKinect;
public PresentateurWEntrainement(IwEntrainement vue) public PresentateurWEntrainement(IwEntrainement vue)
{ {
_vue = vue; _vue = vue;
_gestionnairePerceptron = new GestionPerceptrons(); _gestionnaire = new GestionWEntrainement();
_gestionnaireKinect = new GestionKinect(_vue.Image, _vue.Canvas);
_vue.EntrainementEvt += _vue_EntrainementEvt; _vue.EntrainementEvt += _vue_EntrainementEvt;
} }
private void _vue_EntrainementEvt(object sender, EventArgs e) private void _vue_EntrainementEvt(object sender, EventArgs e)
{ {
// Perceptron!!! // Perceptron!!!
string position = (_vue.GetPositionEnCour() - 1).ToString(); throw new NotImplementedException();
_vue.Console = _gestionnairePerceptron.Entrainement(new Squelette(_gestionnaireKinect.Body, position), position);
} }
} }
} }

View File

@ -16,8 +16,5 @@ namespace JeuHoy_WPF_Natif.View
string Console { get; set; } string Console { get; set; }
Canvas Canvas { get; set; } Canvas Canvas { get; set; }
event EventHandler EntrainementEvt; event EventHandler EntrainementEvt;
}
int GetPositionEnCour();
}
} }

View File

@ -3,7 +3,6 @@ using JeuHoy_WPF_Natif.View;
using Microsoft.Kinect; using Microsoft.Kinect;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
@ -34,23 +33,190 @@ namespace JeuHoy_WPF.View
public string Console { get => txtConsole.Text; set => txtConsole.Text = value; } public string Console { get => txtConsole.Text; set => txtConsole.Text = value; }
public Canvas Canvas { get => pDessinSquelette; set => pDessinSquelette = value; } public Canvas Canvas { get => pDessinSquelette; set => pDessinSquelette = value; }
private KinectSensor _sensor;
private MultiSourceFrameReader _multiSourceFrameReader;
private BodyFrameReader _bodyFrameReader;
private WriteableBitmap _bitmap;
//private DisplayFrameType _displayFrameType = DisplayFrameType.Color;
private byte[] _picPixels = null;
/// <summary> /// <summary>
/// Constructeur /// Constructeur
/// </summary> /// </summary>
public wEntrainement() public wEntrainement()
{ {
_presentateur = new PresentateurWEntrainement(this);
InitializeComponent(); InitializeComponent();
_presentateur = new PresentateurWEntrainement(this);
for (int x = 1; x <= CstApplication.NBFIGURE; ++x) _sensor = KinectSensor.GetDefault();
if (File.Exists("HoyContent/fig" + x.ToString() + ".png")) if (_sensor != null)
_dicImgFigure.Add("fig" + x, new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"./HoyContent/fig" + x.ToString() + ".png", UriKind.Absolute))); {
_sensor.Open();
//Lecture des images
_multiSourceFrameReader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color);
_multiSourceFrameReader.MultiSourceFrameArrived += MultiSourceFrameReader_MultiSourceFrameArrived;
FrameDescription frameDescription = _sensor.ColorFrameSource.FrameDescription;
_picPixels = new byte[frameDescription.Width * frameDescription.Height * 4];
//Lecture des squelettes détectés
_bodyFrameReader = _sensor.BodyFrameSource.OpenReader();
_bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived;
}
for (int i = 1; i <= CstApplication.NBFIGURE; i++)
{
Uri uriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + @"./HoyContent/fig" + i + ".png", UriKind.Absolute);
_dicImgFigure.Add("fig" + i, new BitmapImage(uriSource));
}
lblNbPositions.Content = "/ " + CstApplication.NBFIGURE.ToString();
ChargerFigure(); ChargerFigure();
_son.JouerSonAsync(@"./HoyContent/hoy.wav"); _son.JouerSonAsync(@"./HoyContent/hoy.wav");
} }
private void MultiSourceFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
MultiSourceFrame multiSource = e.FrameReference.AcquireFrame();
if (multiSource == null)
return;
using (ColorFrame colorFrame = multiSource.ColorFrameReference.AcquireFrame())
{
if (colorFrame != null)
{
FrameDescription frameDescription = colorFrame.FrameDescription;
if (_bitmap == null)
_bitmap = new WriteableBitmap(frameDescription.Width,
frameDescription.Height,
96.0,
96.0,
PixelFormats.Bgra32,
null);
colorFrame.CopyConvertedFrameDataToArray(_picPixels, ColorImageFormat.Bgra);
_bitmap.Lock();
Marshal.Copy(_picPixels, 0, _bitmap.BackBuffer, _picPixels.Length);
_bitmap.AddDirtyRect(new Int32Rect(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight));
_bitmap.Unlock();
picKinect.Source = _bitmap;
}
}
}
private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame == null)
return;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
Body body = bodies.FirstOrDefault(b => b.IsTracked);
if (body != null)
{
DessinerSquelette(body, _sensor);
}
}
}
/// <summary>
/// Dessine un ellipse pour chacune des jointure du squelette détecté.
/// </summary>
/// <param name="joueur">Le joueur détecté</param>
/// <param name="sensor">Le sensor Kinect</param>
private void DessinerSquelette(Body body, KinectSensor sensor)
{
try
{
if (body != null)
{
pDessinSquelette.Children.Clear();
Joint[] joints = body.Joints.Values.ToArray();
for (int i = 0; i < joints.Count(); i++)
DrawJoint(sensor, joints[i], CstApplication.BODY_ELLIPSE_SIZE, pDessinSquelette);
}
}
catch (Exception ex)
{
txtConsole.Text = ex.Message;
}
}
/// <summary>
/// Dessine le joint d'un squellete d'un senseur Kinect sur le canvas passé en paramètre
/// </summary>
/// <param name="sensor"></param>
/// <param name="joint"></param>
/// <param name="size"></param>
/// <param name="canvas"></param>
private void DrawJoint(KinectSensor sensor, Joint joint, int size, Canvas canvas)
{
if (joint.Position.X != 0 && joint.Position.Y != 0 && joint.Position.Z != 0)
{
// Convertir la position du joint en coordonnées d'écran
System.Windows.Point point = GetPoint(sensor, joint.Position, canvas.Height, canvas.Width);
// Créer un cercle à la position du joint
Ellipse ellipse = new Ellipse();
ellipse.Fill = new SolidColorBrush(Colors.Yellow);
ellipse.Width = size;
ellipse.Height = size;
// Positionner le cercle sur l'élément de dessin Canvas
Canvas.SetLeft(ellipse, point.X - size / 2);
Canvas.SetTop(ellipse, point.Y - size / 2);
// Ajouter le cercle à l'élément de dessin Canvas
canvas.Children.Add(ellipse);
}
}
/// <summary>
/// Retourne le point x,y d'un joint par rapport à la taille d'un canvas.
/// J'ai permis de dépasser le canvas car je trouvais ça drole :-)
/// </summary>
/// <param name="sensor"></param>
/// <param name="position"></param>
/// <param name="iCanvasHeight"></param>
/// <param name="iCanvasWidth"></param>
/// <returns></returns>
public System.Windows.Point GetPoint(KinectSensor sensor, CameraSpacePoint position, double iCanvasHeight, double iCanvasWidth)
{
System.Windows.Point point = new System.Windows.Point();
DepthSpacePoint depthPoint = sensor.CoordinateMapper.MapCameraPointToDepthSpace(position);
point.X = float.IsInfinity(depthPoint.X) ? 0.0 : depthPoint.X;
point.Y = float.IsInfinity(depthPoint.Y) ? 0.0 : depthPoint.Y;
// La Kinect pour Xbox One utilise également le SDK 2 de Microsoft, et sa résolution de profondeur est de 512x424 pixels.
//// Ainsi, la résolution de la carte de profondeur pour la Kinect pour Xbox One est également de 512x424 pixels.
point.X = point.X / 512 * iCanvasHeight;
point.Y = point.Y / 424 * iCanvasWidth;
return point;
}
/// <summary> /// <summary>
/// Charger la figure de danse en cours. /// Charger la figure de danse en cours.
/// </summary> /// </summary>
@ -132,6 +298,6 @@ namespace JeuHoy_WPF.View
EntrainementEvt(this, e); EntrainementEvt(this, e);
} }
public int GetPositionEnCour() { return _positionEnCours; }
} }
} }