This commit is contained in:
Merlin Gélinas 2023-05-03 10:56:18 -04:00
commit deb08be6fc
7 changed files with 332 additions and 11 deletions

View File

@ -60,6 +60,9 @@
</ApplicationDefinition>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Model\CstApplication.cs" />
<Compile Include="Model\Perceptron.cs" />
<Compile Include="Model\GestionPerceptrons.cs" />
<Compile Include="Model\Squelette.cs" />
<Compile Include="Model\GestionWEntrainement.cs" />
<Compile Include="Presenter\JouerMp3.cs" />
<Compile Include="Presenter\JouerSon.cs" />
@ -285,8 +288,6 @@
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<Folder Include="Vue\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -11,7 +11,7 @@ namespace JeuHoy_WPF
//Nombre de figure de danse dans l'application.
public const int NBFIGURE = 10;
//Constante pour la Kinect
public const int SKELETONCOUNT = 6;
public const int SKELETONCOUNT = 10;
public const int KINECT_DISPLAY_WIDTH = 320;
public const int KINECT_DISPLAY_HEIGHT = 240;

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JeuHoy_WPF_Natif.Model {
public class GestionPerceptrons {
private Dictionary<string, Perceptron> _lstPerceptrons = new Dictionary<string, Perceptron>();
private IGestionFichiers _gestionSortie = new GestionFichiersSorties();
private List<Squelette> _lstData = new List<Squelette>();
/// <summary>
/// Constructeur
/// </summary>
public GestionPerceptrons() {
for (char x = '0'; x <= '9'; ++x)
_lstPerceptrons.Add(x.ToString(), new Perceptron(x.ToString()));
}
/// <summary>
/// Charge les échantillons d'apprentissage sauvegardé sur le disque.
/// </summary>
/// <param name="fichier">Le nom du fichier</param>
public void ChargerCoordonnees(string fichier) { _lstData = _gestionSortie.ChargerCoordonnees(fichier); }
/// <summary>
/// Sauvegarde les échantillons d'apprentissage sauvegardé sur le disque.
/// </summary>
/// <param name="fichier">Le nom du fichier</param>
/// <returns>En cas d'erreur retourne le code d'erreur</returns>
public int SauvegarderCoordonnees(string fichier) => _gestionSortie.SauvegarderCoordonnees(fichier, _lstData);
/// <summary>
/// Entraine les perceptrons avec un nouveau caractère
/// </summary>
/// <param name="coordo">Les nouvelles coordonnées</param>
/// <param name="reponse">La réponse associé(caractère) aux coordonnées</param>
/// <returns>Le résultat de la console</returns>
public string Entrainement(Squelette coordo, string reponse) {
StringWriter sw = new StringWriter();
if (reponse != "") {
coordo.Reponse = reponse;
_lstData.Add(coordo);
}
if (_lstData is null)
return "";
foreach (Perceptron perc in _lstPerceptrons.Values)
sw.WriteLine(perc.Entrainement(_lstData));
return sw.ToString();
}
/// <summary>
/// Test le perceptron avec de nouvelles coordonnées.
/// </summary>
/// <param name="coord">Les nouvelles coordonnées</param>
/// <returns>Retourne la liste des valeurs possibles du perceptron</returns>
public string Tester(Squelette coord) {
string resultat = "";
foreach (Perceptron perc in _lstPerceptrons.Values)
if (perc.TesterNeurone(coord))
resultat += perc.Reponse;
if (resultat == "")
resultat = "?";
return resultat;
}
/// <summary>
/// Obtient une liste des coordonées.
/// </summary>
/// <returns>Une liste des coordonées.</returns>
public IList<Squelette> ObtenirCoordonnees() => _lstData;
}
}

View File

@ -0,0 +1,90 @@
using JeuHoy_WPF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace JeuHoy_WPF_Natif.Model {
public class Perceptron {
private double _cstApprentissage;
private double[] _poidsSyn;
private string _reponse = "?";
public string Reponse { get => _reponse; }
/// <summary>
/// Constructeur de la classe. Crée un perceptron pour une réponse(caractère) qu'on veut identifier le pattern(modèle)
/// </summary>
/// <param name="reponse">La classe que défini le perceptron</param>
public Perceptron(string reponse) {
Random _rand = new Random();
_reponse = reponse;
_cstApprentissage = CstApplication.CONSTANTEAPPRENTISSAGE;
_poidsSyn = new double[CstApplication.SKELETONCOUNT * 2];
for (int x = 0; x < CstApplication.SKELETONCOUNT - 1; ++x)
_poidsSyn[x] = _rand.NextDouble();
}
/// <summary>
/// Faire l'apprentissage sur un ensemble de coordonnées. Ces coordonnées sont les coordonnées de tous les caractères analysés.
/// </summary>
/// <param name="lstCoord">La liste de coordonnées pour les caractères à analysés.</param>
/// <returns>Les paramètres de la console</returns>
public string Entrainement(List<Squelette> lstCoord) {
int iNbItérations = 0,
iNbErreurs = 0;
double dSum = 0,
dPourSucc = 0;
do {
iNbErreurs = 0;
foreach (Squelette coord in lstCoord) {
dSum = _poidsSyn[0];
for (int j = 1; j < _poidsSyn.Length; ++j)
dSum += _poidsSyn[j] * coord.Points[j - 1];
int iRes = (dSum >= 0) ? 1 : 0;
int iErr = (_reponse == coord.Reponse ? 1 : 0) - iRes;
if (iErr != 0) {
_poidsSyn[0] = _cstApprentissage;
for (int j = 1; j < _poidsSyn.Length; ++j)
_poidsSyn[j] += _cstApprentissage * iErr * coord.Points[j - 1];
++iNbErreurs;
}
}
dPourSucc = (double)(lstCoord.Count - iNbErreurs) / (double)lstCoord.Count * 100.0;
++iNbItérations;
}
while (dPourSucc < CstApplication.POURCENTCONVERGENCE && iNbItérations < CstApplication.MAXITERATION);
return $"Perceptron: {_reponse}; Itérations: {iNbItérations} Taux Réussite: {dPourSucc.ToString("0.00")} %"; ;
}
/// <summary>
/// Calcul la valeur(vrai ou faux) pour un les coordonnées d'un caractère. Permet au perceptron d'évaluer la valeur de vérité.
/// </summary>
/// <param name="vecteurSyn">Les poids synaptiques du perceptron</param>
/// <param name="entree">Le vecteur de bit correspondant aux couleurs du caractère</param>
/// <returns>Vrai ou faux</returns>
public int ValeurEstime(double[] vecteurSyn, double[] entree) {
double dSum = vecteurSyn[0];
for (int j = 1; j < vecteurSyn.Length; ++j)
dSum += vecteurSyn[j] * entree[j - 1];
return (dSum >= 0) ? CstApplication.VRAI : CstApplication.FAUX;
}
/// <summary>
/// Interroge la neuronnes pour un ensembles des coordonnées(d'un caractère).
/// </summary>
/// <param name="squel"></param>
/// <returns></returns>
public bool TesterNeurone(Squelette squel) => ValeurEstime(_poidsSyn, squel.Points) == CstApplication.VRAI ? true : false;
}
}

View File

@ -0,0 +1,62 @@
using JeuHoy_WPF;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace JeuHoy_WPF_Natif.Model {
public class Squelette {
static private JointType[] _joints = new JointType[CstApplication.SKELETONCOUNT] {
JointType.HandLeft,
JointType.HandRight,
JointType.ElbowLeft,
JointType.ElbowRight,
JointType.FootLeft,
JointType.FootRight,
JointType.KneeRight,
JointType.KneeLeft,
JointType.SpineBase,
JointType.Neck
};
private double[] _sque = new double[CstApplication.SKELETONCOUNT * 2];
private string _rep = "?";
public string Reponse { get => _rep; set => _rep = value; }
public double[] Points => _sque;
public Squelette(KinectSensor kin, Body body, string reponse) {
Vector trans = (Vector)GetPoint(kin, body.Joints[JointType.Head].Position, new Vector(0,0));
for (int i = 0; i < CstApplication.SKELETONCOUNT; ++i) {
Point po = GetPoint(kin, body.Joints[_joints[i]].Position, trans);
_sque[i * 2] = po.X;
_sque[i * 2 + 1] = po.Y;
}
_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

@ -22,7 +22,7 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas x:Name="pDessinSquelette" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="0" Height="300px" Margin="0,0,0,0" VerticalAlignment="Top" Width="300px" Background="Black"/>
<Image x:Name="picKinect" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="1" Height="300px" Margin="0,0,0,0" VerticalAlignment="Top" Width="300px" Grid.RowSpan="2" Source="/Resources/téléchargement.jpg" />
<Image x:Name="picKinect" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="1" Height="300px" Margin="0,0,0,0" VerticalAlignment="Top" Width="300px" Grid.RowSpan="2" Source="/Resources/téléchargement.jpg" Stretch="UniformToFill" />
<TextBox x:Name="txtConsole" HorizontalAlignment="Center" Margin="0,0,0,0" Grid.Row="2" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="300" Height="300" Grid.RowSpan="2"/>
<Image x:Name="picPositionAFaire" Grid.Column="1" HorizontalAlignment="Left" Height="822" VerticalAlignment="Top" Width="828" Grid.RowSpan="3"/>

View File

@ -21,7 +21,7 @@ namespace JeuHoy_WPF.View
/// </summary>
public partial class wEntrainement : Window, IwEntrainement
{
private Dictionary<string, BitmapImage> _dicImgFigure = new Dictionary<string, BitmapImage>();
private JouerSon _son = new JouerSon();
private int _positionEnCours = 1;
@ -33,15 +33,47 @@ namespace JeuHoy_WPF.View
public string Console { get => txtConsole.Text; set => txtConsole.Text = value; }
public Canvas Canvas { get => pDessinSquelette; set => pDessinSquelette = value; }
/// <summary>
/// Constructeur
/// </summary>
public wEntrainement()
private KinectSensor _sensor;
private MultiSourceFrameReader _multiSourceFrameReader;
private BodyFrameReader _bodyFrameReader;
private WriteableBitmap _bitmap;
//private DisplayFrameType _displayFrameType = DisplayFrameType.Color;
private byte[] _picPixels = null;
/// <summary>
/// Constructeur
/// </summary>
public wEntrainement()
{
_presentateur = new PresentateurWEntrainement(this);
InitializeComponent();
_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);
@ -53,6 +85,59 @@ namespace JeuHoy_WPF.View
_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>
@ -64,6 +149,7 @@ namespace JeuHoy_WPF.View
{
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);
@ -92,7 +178,7 @@ namespace JeuHoy_WPF.View
// Créer un cercle à la position du joint
Ellipse ellipse = new Ellipse();
ellipse.Fill = new SolidColorBrush(Colors.Green);
ellipse.Fill = new SolidColorBrush(Colors.Yellow);
ellipse.Width = size;
ellipse.Height = size;