-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sequence.cs
executable file
·132 lines (98 loc) · 3.85 KB
/
Sequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Xml.Serialization;
using Accord.Math;
using System.Drawing.Drawing2D;
namespace Gestures
{
[Serializable]
public class Sequence : ICloneable
{
[XmlIgnore]
[NonSerialized]
private double[][] input;
[XmlIgnore]
[NonSerialized]
private Bitmap bitmap;
public BindingList<String> Classes { get; set; }
public GestureData[] SourcePath { get; set; } //point[] sourcepath
public int Output { get; set; }
public int RecognizedAs { get; set; }
public Sequence()
{
RecognizedAs = -1;
}
public string OutputName
{
get { return Classes[Output]; }
}
public string RecognizedAsName
{
get { return RecognizedAs >= 0 ? Classes[RecognizedAs] : "-"; }
}
public double[][] Input
{
get
{
if (input == null)
input = Preprocess(SourcePath);
return input;
}
}
public Bitmap Bitmap
{
get
{
if (bitmap == null && SourcePath != null)
bitmap = ToBitmap(SourcePath);
return bitmap;
}
}
public static double[][] Preprocess(GestureData[] sequence) //point[] seq
{
double[][] result = new double[sequence.Length][];
for (int i = 0; i < sequence.Length; i++)
//using hand right x,yz; hand left x,yz; elbow right x,yz; elbow left x,yz; wrist right x,y; wrist left x,y
result[i] = new double[] { sequence[i].HandR.X, sequence[i].HandR.Y, sequence[i].HandR.Z, sequence[i].HandL.X, sequence[i].HandL.Y, sequence[i].HandL.Z, sequence[i].ElbowL.X, sequence[i].ElbowL.Y, sequence[i].ElbowL.Z, sequence[i].ElbowR.X, sequence[i].ElbowR.Y, sequence[i].ElbowR.Z, sequence[i].WristR.X, sequence[i].WristR.Y, sequence[i].WristL.X, sequence[i].WristL.Y }; //using only handRight for now to paint gesture
double[][] zscores = Accord.Statistics.Tools.ZScores(result);
return zscores.Add(10);
}
public static Bitmap ToBitmap(GestureData[] sequence) //point[] seq
{
if (sequence.Length == 0)
return null;
int xmax = (int)sequence.Max(x => x.HandR.X);
int xmin = (int)sequence.Min(x => x.HandR.X);
int ymax = (int)sequence.Max(x => x.HandR.Y);
int ymin = (int)sequence.Min(x => x.HandR.Y);
int width = xmax - xmin;
int height = ymax - ymin;
Bitmap bmp = new Bitmap(width + 16, height + 16);
Graphics g = Graphics.FromImage(bmp);
for (int i = 1; i < sequence.Length; i++)
{
int x = (int)sequence[i].HandR.X - xmin;
int y = (int)sequence[i].HandR.Y - ymin;
int p = (int)Accord.Math.Tools.Scale(0, sequence.Length, 0, 255, i);
int prevX = (int)sequence[i - 1].HandR.X - xmin;
int prevY = (int)sequence[i - 1].HandR.Y - ymin;
using (Brush brush = new SolidBrush(Color.FromArgb(255 - p, 0, p)))
using (Pen pen = new Pen(brush, 16))
{
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
g.DrawLine(pen, prevX, prevY, x, y);
}
}
return bmp;
}
public object Clone()
{
return MemberwiseClone();
}
}
}