-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainForm.cs
executable file
·1003 lines (802 loc) · 36.4 KB
/
MainForm.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Accord.NET Sample Applications
// http://accord.googlecode.com
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Accord.Statistics.Distributions.Fitting;
using Accord.Statistics.Distributions.Multivariate;
using Accord.Statistics.Models.Fields;
using Accord.Statistics.Models.Fields.Functions;
using Accord.Statistics.Models.Fields.Learning;
using Accord.Statistics.Models.Markov;
using Accord.Statistics.Models.Markov.Learning;
using Accord.Statistics.Models.Markov.Topology;
using Gestures.Native;
using Microsoft.Kinect; //for Kinect libraries
using System.Web.UI.DataVisualization.Charting; //for Point3D class
using System.Collections.Generic; //for List Collection
using System.Drawing.Imaging;
using System.Runtime.InteropServices;//for Marshal
using System.Speech.Synthesis; //for speech.
using System.Threading; //for background threads.
//import CCT
using CCT.NUI.KinectSDK;
using CCT.NUI.Core;
using CCT.NUI.HandTracking;
using CCT.NUI.Visual;
using CCT.NUI.Core.Clustering;
using CCT.NUI.Core.Shape;
using CCT.NUI.Core.Video;
namespace Gestures
{
public partial class MainForm : Form
{
private Database database;
private HiddenMarkovClassifier<MultivariateNormalDistribution> hmm;
private HiddenConditionalRandomField<double[]> hcrf;
//Kinect code
KinectSensor _kinectDevice;
Skeleton[] skeletonData;
byte[] pixeldata;
SpeechSynthesizer ttsout; //for Speech synth
bool ShouldSpeakOut = true;
//private ViterbiLearning<MultivariateNormalDistribution> vtl;
Boolean flagRecording = false;
Boolean isGestureOver = false;
bool TestingOn = false;
List<GestureData> sequence;
static List<GestureData> passableSequence;
//global label strin
string label;
bool detectionDone = false;
//to limit the no of frames being captured in skeletonTracker. currently at FPS/3. (24FPS)
int frameCount = 0;
//for CCT func
private ClusterDataSourceSettings clusteringSettings = new ClusterDataSourceSettings();
private ShapeDataSourceSettings shapeSettings = new ShapeDataSourceSettings();
private HandDataSourceSettings handDetectionSettings = new HandDataSourceSettings();
Thread th_restPosCheck; //added.
bool isRestPos = false;
Skeleton skel;
public MainForm()
{
InitializeComponent();
th_restPosCheck = new Thread(checkResting);
th_restPosCheck.IsBackground = true;
database = new Database();
gridSamples.AutoGenerateColumns = false;
cbClasses.DataSource = database.Classes;
gridSamples.DataSource = database.Samples;
openDataDialog.InitialDirectory = Path.Combine(Application.StartupPath, "Resources");
sequence= new List<GestureData>(); //init the GestureData list
InitKinect(); //initialize Kinect sensor and its various data streams
ttsout = new SpeechSynthesizer(); //init the speech synth object
passableSequence = sequence;
SpeakOut("Hello!");
btnLearnHCRF.Visible = false;
}
void InitKinect()
{
foreach (var potentialSensor in KinectSensor.KinectSensors)
{
if (potentialSensor.Status == KinectStatus.Connected)
{
this._kinectDevice = potentialSensor;
break;
}
}
if( null != this._kinectDevice)
{
//_kinectDevice = KinectSensor.KinectSensors[0];
_kinectDevice.SkeletonStream.Enable();
_kinectDevice.ColorStream.Enable();
_kinectDevice.DepthStream.Enable();
_kinectDevice.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(myKinect_ColorFrameReady);
_kinectDevice.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(SkeletonHandler);
try
{
_kinectDevice.Start();
skeletonData = new Skeleton[_kinectDevice.SkeletonStream.FrameSkeletonArrayLength];
EnableNearModeSkeletalTracking();
// restPosCheck.Start(); //start restPos check thread.
}
catch (IOException)
{
this._kinectDevice = null;
}
System.Diagnostics.Debug.WriteLine("detected:========="+ _kinectDevice.Status);
//button for gesture recording.ADD.
btnRecord.Enabled = true;
btnRecord.Click += new EventHandler(btnRecord_Click);
BtnCameraUp.Enabled = true;
BtnCameraDown.Enabled = true;
BtnCameraUp.Click += new EventHandler(BtnCameraUpClick);
BtnCameraDown.Click += new EventHandler(BtnCameraDownClick);
btnTestbegin.Enabled = true;
btnTestbegin.Click += new EventHandler(Testingbegin_click);
}
#region CCT code
//CCT hand tracking code:
try
{
this.clusteringSettings.MaximumDepthThreshold = 2000; //added threshold. as per CCT eg
IDataSourceFactory dataSourceFactory = new SDKDataSourceFactory(useNearMode: false); //trying new modded src
var handDataSource = new HandDataSource(dataSourceFactory.CreateShapeDataSource(this.clusteringSettings, this.shapeSettings), this.handDetectionSettings);
handDataSource.NewDataAvailable += new NewDataHandler<HandCollection>(handDataSource_NewDataAvailable);
handDataSource.Start();
//System.Diagnostics.Debug.WriteLine("hand data src started");
}
catch (ArgumentOutOfRangeException exc)
{
//Cursor.Current = Cursors.Default;
//MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Diagnostics.Debug.WriteLine("===Error using CCT !!=========== " + exc.Message);
//return;
}
#endregion
}
void Testingbegin_click(object sender, EventArgs e)
{
flagRecording = true;
TestingOn = true;
}
void handDataSource_NewDataAvailable(HandCollection data)
{
for (int index = 0; index < data.Count; index++)
{
var hand = data.Hands[index];
// Console.WriteLine(string.Format("Fingers on hand {0}: {1}", index, hand.FingerCount));
//HINT : the current contet when running is on a separate thread, need to use begininvoke or a backgroundworker to change the UItext
//uncomment the following 1 line, and get it working if you can.
//updateHandLabels(data.Count, hand.FingerCount);
//offloaded to a separate method.
//handLabel.Text = "Hands: " + data.Count;
//fingerLabel.Text = "Fingers: " + hand.FingerCount;
}
}
//void updateHandLabels(int hands, int fingers)
//{
// handLabel.Text = "Hands: " + hands.ToString();
// fingerLabel.Text = "Fingers: " + fingers.ToString();
//}
private void SkeletonHandler(object sender, SkeletonFrameReadyEventArgs e)
{
using (SkeletonFrame sF = e.OpenSkeletonFrame())
{
// check that a frame is available
if (sF != null && this.skeletonData != null)
{
// get the skeletal information in this frame
sF.CopySkeletonDataTo(this.skeletonData);
foreach (Skeleton sk in this.skeletonData)
{
if (sk.TrackingState == SkeletonTrackingState.Tracked)
{
TraceTrackedSkeletonJoints(sk.Joints);
updateSkeletonData(sk);
//System.Diagnostics.Debug.WriteLine("@@@skel updated@@@~~~");
}
}
}
}
}
void updateSkeletonData(Skeleton s)
{
if (!th_restPosCheck.IsAlive)
th_restPosCheck.Start();
this.skel = s;
}
void TraceTrackedSkeletonJoints(JointCollection jCollection)
{
Point3D shl, shr, elbl, elbr, wrstl, wrstr, handl, handr, spine, head;
if (flagRecording && frameCount >= 6)
{
//add check if jointdata is non null
if (jCollection[JointType.ShoulderLeft].TrackingState == JointTrackingState.Tracked)
{
shl = new Point3D(jCollection[JointType.ShoulderLeft].Position.X, jCollection[JointType.ShoulderLeft].Position.Y, jCollection[JointType.ShoulderLeft].Position.Z);
}
else
{
shl = new Point3D(0,0,0);
}
if (jCollection[JointType.ShoulderRight].TrackingState == JointTrackingState.Tracked)
{
shr = new Point3D(jCollection[JointType.ShoulderRight].Position.X, jCollection[JointType.ShoulderRight].Position.Y, jCollection[JointType.ShoulderRight].Position.Z);
}
else
{
shr = new Point3D(0,0,0);
}
if(jCollection[JointType.ElbowLeft].TrackingState == JointTrackingState.Tracked)
{
elbl = new Point3D(jCollection[JointType.ElbowLeft].Position.X, jCollection[JointType.ElbowLeft].Position.Y, jCollection[JointType.ElbowLeft].Position.Z);
}
else
{
elbl = new Point3D(0,0,0);
}
if(jCollection[JointType.ElbowRight].TrackingState == JointTrackingState.Tracked )
{
elbr = new Point3D(jCollection[JointType.ElbowRight].Position.X, jCollection[JointType.ElbowRight].Position.Y, jCollection[JointType.ElbowRight].Position.Z);
}
else
{
elbr = new Point3D(0,0,0);
}
if(jCollection[JointType.WristLeft].TrackingState == JointTrackingState.Tracked)
{
wrstl = new Point3D(jCollection[JointType.WristLeft].Position.X, jCollection[JointType.WristLeft].Position.Y, jCollection[JointType.WristLeft].Position.Z);
}
else
{
wrstl = new Point3D(0,0,0);
}
if(jCollection[JointType.WristRight].TrackingState == JointTrackingState.Tracked)
{
wrstr = new Point3D(jCollection[JointType.WristRight].Position.X, jCollection[JointType.WristRight].Position.Y, jCollection[JointType.WristRight].Position.Z);
}
else
{
wrstr = new Point3D(0,0,0);
}
if(jCollection[JointType.HandLeft].TrackingState == JointTrackingState.Tracked)
{
handl = new Point3D(jCollection[JointType.HandLeft].Position.X, jCollection[JointType.HandLeft].Position.Y, jCollection[JointType.HandLeft].Position.Z);
}
else
{
handl = new Point3D(0,0,0);
}
if(jCollection[JointType.HandRight].TrackingState == JointTrackingState.Tracked )
{
handr = new Point3D(jCollection[JointType.HandRight].Position.X, jCollection[JointType.HandRight].Position.Y, jCollection[JointType.HandRight].Position.Z);
}
else
{
handr = new Point3D(0,0,0);
}
if(jCollection[JointType.Spine].TrackingState == JointTrackingState.Tracked )
{
spine = new Point3D(jCollection[JointType.Spine].Position.X, jCollection[JointType.Spine].Position.Y, jCollection[JointType.Spine].Position.Z);
}
else
{
spine = new Point3D(0,0,0);
}
if(jCollection[JointType.Head].TrackingState == JointTrackingState.Tracked)
{
head = new Point3D(jCollection[JointType.Head].Position.X, jCollection[JointType.Head].Position.Y, jCollection[JointType.Head].Position.Z);
}
else
{
head = new Point3D(0,0,0);
}
//frameCount++;
GestureData gd = new GestureData(shl, shr, elbl, elbr, wrstl, wrstr, handl, handr, spine, head);
sequence.Add(gd);
System.Diagnostics.Debug.WriteLine("Seq added===========");
frameCount = 0;
}
else if(flagRecording)
{
frameCount++;
System.Diagnostics.Debug.Write("~");
}
}
private void EnableNearModeSkeletalTracking()
{
if (this._kinectDevice != null && this._kinectDevice.DepthStream != null && this._kinectDevice.SkeletonStream != null)
{
// this._kinectDevice.DepthStream.Range = DepthRange.Near; // Depth in near range enabled
this._kinectDevice.SkeletonStream.EnableTrackingInNearRange = true; // enable returning skeletons while depth is in Near Range
//this._kinectDevice.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated; // Use seated tracking
System.Diagnostics.Debug.WriteLine("============enabled near mode=============");
}
}
private void btnRecord_Click(object sender, EventArgs e) //this is a gesture recording toggle button.
{
if (flagRecording) //current recording, stopping now
{
flagRecording = false;//stopeed
inputCanvas_GestureDone();
btnRecord.Text = "Start Recording!"; //button toggled, can be used to start now
btnRecord.ForeColor = Color.Black;
//call func to get 3d point data, keep adding points as hand moves
//with func recording data if skel is moving and flag is true.
}
else
{
//start recording
flagRecording = true;
canvas_GestureBegin();
btnRecord.Text = "Stop Recording!"; //button toggled, can be used to stop recording
btnRecord.ForeColor = Color.Red; //visual indication.
}
}
byte[] colorData = null;
Bitmap kinectVideoBitmap = null;
IntPtr colorPtr;
void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null) return;
if (colorData == null)
colorData = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(colorData);
Marshal.FreeHGlobal(colorPtr);
colorPtr = Marshal.AllocHGlobal(colorData.Length);
Marshal.Copy(colorData, 0, colorPtr, colorData.Length);
kinectVideoBitmap = new Bitmap(
colorFrame.Width,
colorFrame.Height,
colorFrame.Width * colorFrame.BytesPerPixel,
PixelFormat.Format32bppRgb,
colorPtr);
kinImage.Image = kinectVideoBitmap;
//kinectVideoBitmap.Dispose();
}
}
//void ColorImageFrameReady(object sender, ColorImageFrameReadyEventArgs e)
//{
// // PlanarImage Image = e.ImageFrame.Image;
// ColorImageFrame Image;
// //BitmapImage bitmap;
// using (Image = e.OpenColorImageFrame())
// {
// if(Image == null) return;
// {
// Bitmap bm = ImageToBitmap(Image);
// kinImage = Image;
// //CalculateFps();
// }
// {
// System.Diagnostics.Debug.WriteLine("bitmap empty :/");
// }
// }
//}
//Bitmap ImageToBitmap(ColorImageFrame Image)
// {
// if (Image != null)
// {
// if (pixeldata == null)
// {
// pixeldata = new byte[Image.PixelDataLength];
// }
// Image.CopyPixelDataTo(pixeldata);
// bmap = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
// BitmapData bmapdata = bmap.LockBits(
// new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
// ImageLockMode.WriteOnly,
// bmap.PixelFormat);
// IntPtr ptr = bmapdata.Scan0;
// Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
// bmap.UnlockBits(bmapdata);
// return bmap;
// }
// // byte[] pixeldata = new byte[Image.PixelDataLength];
// // Image.CopyPixelDataTo(pixeldata);
// else
// {
// return null;
// }
// }
private void btnLearnHMM_Click(object sender, EventArgs e)
{
if (gridSamples.Rows.Count == 0)
{
MessageBox.Show("Please load or insert some data first.");
return;
}
BindingList<Sequence> samples = database.Samples;
BindingList<String> classes = database.Classes;
double[][][] inputs = new double[samples.Count][][];
int[] outputs = new int[samples.Count];
for (int i = 0; i < inputs.Length; i++)
{
inputs[i] = samples[i].Input; //fine.
outputs[i] = samples[i].Output;
}
int states = 4;//def:5,should change this.
int iterations = 0;
double tolerance = 0.001; //reqd to change?
bool rejection = false;
hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(classes.Count,
new Forward(states), new MultivariateNormalDistribution(16) , classes.ToArray()); //!UPDATE!, no of dimension/features2 stand for?, and forward states?
//vtl = new ViterbiLearning<MultivariateNormalDistribution>(
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenMarkovClassifierLearning<MultivariateNormalDistribution>(hmm,
// Train each model using the selected convergence criteria
i => new BaumWelchLearning<MultivariateNormalDistribution>(hmm.Models[i])
{
Tolerance = tolerance,
Iterations = iterations,
FittingOptions = new NormalOptions()
{
Regularization = 1e-5
}
}
);
teacher.Empirical = true;
teacher.Rejection = rejection;
// Run the learning algorithm
double error = teacher.Run(inputs, outputs);
// Classify all training instances
foreach (var sample in database.Samples)
{
sample.RecognizedAs = hmm.Compute(sample.Input);
}
foreach (DataGridViewRow row in gridSamples.Rows)
{
var sample = row.DataBoundItem as Sequence;
row.DefaultCellStyle.BackColor = (sample.RecognizedAs == sample.Output) ?
Color.LightGreen : Color.White;
}
btnLearnHCRF.Enabled = true;
}
private void btnLearnHCRF_Click(object sender, EventArgs e)
{
if (gridSamples.Rows.Count == 0)
{
MessageBox.Show("Please load or insert some data first.");
return;
}
var samples = database.Samples;
var classes = database.Classes;
double[][][] inputs = new double[samples.Count][][];
int[] outputs = new int[samples.Count];
for (int i = 0; i < inputs.Length; i++)
{
inputs[i] = samples[i].Input;
outputs[i] = samples[i].Output;
}
int iterations = 100;
double tolerance = 0.01;
hcrf = new HiddenConditionalRandomField<double[]>(
new MarkovMultivariateFunction(hmm));
// Create the learning algorithm for the ensemble classifier
var teacher = new HiddenResilientGradientLearning<double[]>(hcrf)
{
Iterations = iterations,
Tolerance = tolerance
};
// Run the learning algorithm
double error = teacher.Run(inputs, outputs);
foreach (var sample in database.Samples)
{
sample.RecognizedAs = hcrf.Compute(sample.Input);
}
foreach (DataGridViewRow row in gridSamples.Rows)
{
var sample = row.DataBoundItem as Sequence;
row.DefaultCellStyle.BackColor = (sample.RecognizedAs == sample.Output) ?
Color.LightGreen : Color.White;
}
}
// Load and save database methods
private void openDataStripMenuItem_Click(object sender, EventArgs e)
{
openDataDialog.ShowDialog();
}
private void saveDataStripMenuItem_Click(object sender, EventArgs e)
{
saveDataDialog.ShowDialog();
}
private void openDataDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
hmm = null;
hcrf = null;
using (var stream = openDataDialog.OpenFile())
database.Load(stream);
btnLearnHMM.Enabled = true;
btnLearnHCRF.Enabled = false;
panelClassification.Visible = false;
panelUserLabeling.Visible = false;
}
private void saveDataDialog_FileOk(object sender, CancelEventArgs e)
{
using (var stream = saveDataDialog.OpenFile())
database.Save(stream);
}
private void btnFile_MouseDown(object sender, MouseEventArgs e)
{
menuFile.Show(button4, button4.PointToClient(Cursor.Position));
}
// Top user interaction panel box events
private void btnYes_Click(object sender, EventArgs e)
{
//addGesture();
detectionDone = true; //modified
add3DGesture();
panelClassification.Visible = false;//hide after confirmation
}
private void btnNo_Click(object sender, EventArgs e)
{
panelClassification.Visible = false;
panelUserLabeling.Visible = true;
}
// Bottom user interaction panel box events
private void btnClear_Click(object sender, EventArgs e)
{
canvas.Clear();
panelUserLabeling.Visible = false;
}
private void btnInsert_Click(object sender, EventArgs e)
{
//addGesture();
add3DGesture();
}
public GestureData[] Get3DSequence()
{
return sequence.ToArray();
}
//add GEsture -- obsolete
//private void addGesture()
//{
// //get Text Lable for the performed gesture
// string selectedItem = cbClasses.SelectedItem as String;
// string classLabel = String.IsNullOrEmpty(selectedItem) ?
// cbClasses.Text : selectedItem;
// //add labelled gesture to database
// if (database.Add(canvas.GetSequence(), classLabel) != null)
// {
// canvas.Clear();
// if (database.Classes.Count >= 2 &&
// database.SamplesPerClass() >= 3)
// btnLearnHMM.Enabled = true;
// panelUserLabeling.Visible = false;
// }
//}
//add 3D gesture event (based on addGEsture) to database, adds the sequence(list of GestureData objects) and classLabel
private void add3DGesture()
{
//get Text Label for the performed gesture
string selectedItem = cbClasses.SelectedItem as String;
string classLabel;
if (detectionDone)
{
classLabel = label; //from global label (detected)
detectionDone = false;
}
else
{
classLabel = String.IsNullOrEmpty(selectedItem) ?
cbClasses.Text : selectedItem;
}
//add labelled gesture to database
// canvas.setSequence();
if (database.Add(Get3DSequence(), classLabel) != null) //!modify! canvas.get3Dsequnce
{
canvas.Clear();
if (database.Classes.Count >= 2 &&
database.SamplesPerClass() >= 3)
btnLearnHMM.Enabled = true;
panelUserLabeling.Visible = false;
}
System.Diagnostics.Debug.WriteLine("Sequence complete");
//clear the sequence list, so it may be ready to receive data for next gesture
sequence.Clear(); // = new List<GestureData>(); //!added. check
}
// Canvas events - obsolete
private void inputCanvas_MouseUp(object sender, MouseEventArgs e)
{
double[][] input = Sequence.Preprocess(Get3DSequence());
if (input.Length < 5)
{
panelUserLabeling.Visible = false;
panelClassification.Visible = false;
return;
}
if (hmm == null && hcrf == null)
{
panelUserLabeling.Visible = true;
panelClassification.Visible = false;
}
else
{
int index = (hcrf != null) ? hcrf.Compute(input) : hmm.Compute(input);
string label = database.Classes[index];
//int[] path = ;
lbHaveYouDrawn.Text = String.Format("Have you painted a {0}?", label);
panelClassification.Visible = true;
panelUserLabeling.Visible = false;
}
}
private void canvas_MouseDown(object sender, MouseEventArgs e)
{
canvas.Clear();
lbIdle.Visible = false;
}
//Gesture events based on Canvas events, modded for 3D space-time gestures
private void inputCanvas_GestureDone()
{
double[][] input = Sequence.Preprocess(Get3DSequence());
if (input.Length < 5) //length of input, might incr this because now gestures not screen drawing.
{
System.Diagnostics.Debug.WriteLine("----input gesture was too short---!");
panelUserLabeling.Visible = false;
panelClassification.Visible = false;
return;
}
if (hmm == null && hcrf == null)
{
panelUserLabeling.Visible = true;
panelClassification.Visible = false;
}
else
{
int index = (hcrf != null) ?
hcrf.Compute(input) : hmm.Compute(input); //check if input is well formed from sequence preprocessing, only xy breakpoint
label = database.Classes[index]; //modified
lbHaveYouDrawn.Text = String.Format("Did you say: {0}?", label);
SpeakOut(label); //speak the label
panelClassification.Visible = true;
panelUserLabeling.Visible = false;
}
}
private void canvas_GestureBegin()
{
lbIdle.Visible = false;
}
// Aero Glass settings
//
private void MainForm_Load(object sender, EventArgs e)
{
// Perform special processing to enable aero
if (SafeNativeMethods.IsAeroEnabled)
{
ThemeMargins margins = new ThemeMargins();
margins.TopHeight = canvas.Top;
margins.LeftWidth = canvas.Left;
margins.RightWidth = ClientRectangle.Right - gridSamples.Right;
margins.BottomHeight = ClientRectangle.Bottom - canvas.Bottom;
// Extend the Frame into client area
SafeNativeMethods.ExtendAeroGlassIntoClientArea(this, margins);
}
}
/// <summary>
/// Paints the background of the control.
/// </summary>
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (SafeNativeMethods.IsAeroEnabled)
{
// paint background black to enable include glass regions
e.Graphics.Clear(Color.FromArgb(0, this.BackColor));
}
}
/// <summary>
/// Camera Angle Button Handlers
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnCameraUpClick(object sender, EventArgs e)
{
try
{
_kinectDevice.ElevationAngle = _kinectDevice.ElevationAngle + 5;
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentOutOfRangeException outOfRangeException)
{
//Elevation angle must be between Elevation Minimum/Maximum"
MessageBox.Show(outOfRangeException.Message);
}
}
private void BtnCameraDownClick(object sender, EventArgs e)
{
try
{
_kinectDevice.ElevationAngle = _kinectDevice.ElevationAngle - 5;
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentOutOfRangeException outOfRangeException)
{
//Elevation angle must be between Elevation Minimum/Maximum"
MessageBox.Show(outOfRangeException.Message);
}
}
public static List<GestureData> GetSeq()
{
return passableSequence;
}
private void SpeakOut(String s)
{
//Speech TTS demo
if (null != ttsout)
ttsout.Dispose();
if (ShouldSpeakOut && null != s)
{
System.Diagnostics.Debug.WriteLine("Speaking things~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
ttsout = new SpeechSynthesizer();
ttsout.SpeakAsync(s);
}
}
void checkResting()
{
int restPosFrames = 0;
while (null != _kinectDevice && _kinectDevice.Status == KinectStatus.Connected && _kinectDevice.SkeletonStream.IsEnabled == true && skel !=null)
{
// System.Diagnostics.Debug.WriteLine(skel.Joints[JointType.HandRight].Position.Y +" "+ skel.Joints[JointType.HipCenter].Position.Y+ " "+ skel.Joints[JointType.HandLeft].Position.Y);
//add code for rest pos check.
if (skel.Joints[JointType.HandRight].Position.Y < skel.Joints[JointType.HipCenter].Position.Y && skel.Joints[JointType.HandLeft].Position.Y < skel.Joints[JointType.HipCenter].Position.Y)
{
if (isRestPos)
continue;
if (restPosFrames < 3)
{
restPosFrames++;
}
else
{
isRestPos = true;
System.Diagnostics.Debug.Write("Rest pos !! ~~~~~~~~~~");
//ops
if (TestingOn)
{
double[][] input = Sequence.Preprocess(Get3DSequence());
if (input.Length < 5) //length of input, might incr this because now gestures not screen drawing.
{
System.Diagnostics.Debug.WriteLine("----input gesture was too short---!");
//panelUserLabeling.Visible = false;
//panelClassification.Visible = false;
//return;
}
if (hmm == null && hcrf == null)
{
System.Diagnostics.Debug.WriteLine("----hmm not declared---!");
//panelUserLabeling.Visible = true;
//panelClassification.Visible = false;
}
else
{
int index = (hcrf != null) ?
hcrf.Compute(input) : hmm.Compute(input); //check if input is well formed from sequence preprocessing, only xy breakpoint
label = database.Classes[index]; //modified
// lbHaveYouDrawn.Text = String.Format("Have you drawn a {0}?", label);
SpeakOut(label); //speak the label
// panelClassification.Visible = true;
// panelUserLabeling.Visible = false;
}
sequence.Clear();
}
restPosFrames = 0;
}
}
else
{
isRestPos = false;
}
}
}