Transfert de la gestion Kinect pour utiliser la classe GestionKinect

This commit is contained in:
Merlin Gélinas 2023-05-13 15:55:34 -04:00
parent fe589a29e7
commit 12d745d1b0
3 changed files with 6 additions and 175 deletions

View File

@ -25,7 +25,9 @@ namespace JeuHoy_WPF_Natif.Model {
private WriteableBitmap _bitmap;
private byte[] _picPixels = null;
public GestionKinect() {
public GestionKinect(Image img, Canvas cnv) {
Image = img;
Canvas = cnv;
_sensor = KinectSensor.GetDefault();
if (_sensor != null) {
_sensor.Open();

View File

@ -15,11 +15,13 @@ namespace JeuHoy_WPF_Natif.Presenter
{
private IwEntrainement _vue;
private GestionPerceptrons _gestionnairePerceptron;
private GestionKinect _gestionnaireKinect;
public PresentateurWEntrainement(IwEntrainement vue)
{
_vue = vue;
_gestionnairePerceptron = new GestionPerceptrons();
_gestionnaireKinect = new GestionKinect(_vue.Image, _vue.Canvas);
_vue.EntrainementEvt += _vue_EntrainementEvt;
}

View File

@ -33,192 +33,19 @@ namespace JeuHoy_WPF.View
public string Console { get => txtConsole.Text; set => txtConsole.Text = value; }
public Canvas Canvas { get => pDessinSquelette; set => pDessinSquelette = value; }
private KinectSensor _sensor;
private MultiSourceFrameReader _multiSourceFrameReader;
private BodyFrameReader _bodyFrameReader;
private WriteableBitmap _bitmap;
private Body _currentBody;
//private DisplayFrameType _displayFrameType = DisplayFrameType.Color;
private byte[] _picPixels = null;
/// <summary>
/// Constructeur
/// </summary>
public wEntrainement()
{
_presentateur = new PresentateurWEntrainement(this);
InitializeComponent();
_presentateur = new PresentateurWEntrainement(this);
_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;
}
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();
_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)
{
_currentBody = body;
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>
/// Charger la figure de danse en cours.
/// </summary>