-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
526 lines (466 loc) · 19.6 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace InCarConversationRecorder
{
public partial class MainForm : Form
{
private PXCMSession session;
private volatile bool closing = false;
public volatile bool stop = false;
private PXCMGesture.GeoNode[][] nodes = null;
private Bitmap bitmap = null;
private Hashtable pictures;
private Timer timer = new Timer();
private AudioRecorder audioRecorder;
private DateTime time;
private GPSManager gpsmanager;
public MainForm(PXCMSession session)
{
InitializeComponent();
this.session = session;
PopulateDeviceMenu();
PopulateModuleMenu();
FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
Panel2.Paint += new PaintEventHandler(Panel_Paint);
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 2000;
timer.Start();
pictures = new Hashtable();
pictures[PXCMGesture.Gesture.Label.LABEL_HAND_CIRCLE]=Properties.Resources.circle;
pictures[PXCMGesture.Gesture.Label.LABEL_HAND_WAVE]=Properties.Resources.wave;
pictures[PXCMGesture.Gesture.Label.LABEL_NAV_SWIPE_DOWN] = Properties.Resources.swipe_down;
pictures[PXCMGesture.Gesture.Label.LABEL_NAV_SWIPE_LEFT] = Properties.Resources.swipe_left;
pictures[PXCMGesture.Gesture.Label.LABEL_NAV_SWIPE_RIGHT] = Properties.Resources.swipe_right;
pictures[PXCMGesture.Gesture.Label.LABEL_NAV_SWIPE_UP] = Properties.Resources.swipe_up;
pictures[PXCMGesture.Gesture.Label.LABEL_POSE_BIG5] = Properties.Resources.big5;
pictures[PXCMGesture.Gesture.Label.LABEL_POSE_PEACE] = Properties.Resources.peace;
pictures[PXCMGesture.Gesture.Label.LABEL_POSE_THUMB_DOWN] = Properties.Resources.thumb_down;
pictures[PXCMGesture.Gesture.Label.LABEL_POSE_THUMB_UP] = Properties.Resources.thumb_up;
//gps receiver
gpsmanager = new GPSManager();
gpsPort.Items.AddRange(gpsmanager.ListAvailablePorts());
if (gpsPort.Items.Count > 0)
{
gpsPort.SelectedIndex = 0;
}
else
{
gps.Checked = false;
gps.Enabled = false;
}
}
private void PopulateDeviceMenu()
{
PXCMSession.ImplDesc desc = new PXCMSession.ImplDesc();
desc.group = PXCMSession.ImplGroup.IMPL_GROUP_SENSOR;
desc.subgroup = PXCMSession.ImplSubgroup.IMPL_SUBGROUP_VIDEO_CAPTURE;
ToolStripMenuItem sm = new ToolStripMenuItem("Device");
for (uint i = 0; ; i++)
{
PXCMSession.ImplDesc desc1;
if (session.QueryImpl(ref desc, i, out desc1) < pxcmStatus.PXCM_STATUS_NO_ERROR) break;
PXCMCapture capture;
if (session.CreateImpl<PXCMCapture>(ref desc1, PXCMCapture.CUID, out capture) < pxcmStatus.PXCM_STATUS_NO_ERROR) continue;
for (uint j = 0; ; j++)
{
PXCMCapture.DeviceInfo dinfo;
if (capture.QueryDevice(j, out dinfo) < pxcmStatus.PXCM_STATUS_NO_ERROR) break;
ToolStripMenuItem sm1 = new ToolStripMenuItem(dinfo.name.get(), null, new EventHandler(Device_Item_Click));
sm.DropDownItems.Add(sm1);
}
capture.Dispose();
}
if (sm.DropDownItems.Count > 0)
(sm.DropDownItems[0] as ToolStripMenuItem).Checked = true;
MainMenu.Items.RemoveAt(0);
MainMenu.Items.Insert(0, sm);
}
private void PopulateModuleMenu()
{
PXCMSession.ImplDesc desc = new PXCMSession.ImplDesc();
desc.cuids[0] = PXCMGesture.CUID;
ToolStripMenuItem mm = new ToolStripMenuItem("Module");
for (uint i = 0; ; i++)
{
PXCMSession.ImplDesc desc1;
if (session.QueryImpl(ref desc, i, out desc1) < pxcmStatus.PXCM_STATUS_NO_ERROR) break;
ToolStripMenuItem mm1 = new ToolStripMenuItem(desc1.friendlyName.get(), null, new EventHandler(Module_Item_Click));
mm.DropDownItems.Add(mm1);
}
if (mm.DropDownItems.Count > 0)
(mm.DropDownItems[0] as ToolStripMenuItem).Checked = true;
MainMenu.Items.RemoveAt(1);
MainMenu.Items.Insert(1, mm);
}
private void RadioCheck(object sender, string name)
{
foreach (ToolStripMenuItem m in MainMenu.Items)
{
if (!m.Text.Equals(name)) continue;
foreach (ToolStripMenuItem e1 in m.DropDownItems)
{
e1.Checked = (sender == e1);
}
}
}
private void Device_Item_Click(object sender, EventArgs e)
{
RadioCheck(sender, "Device");
}
private void Module_Item_Click(object sender, EventArgs e)
{
RadioCheck(sender, "Module");
}
private void Start_Click(object sender, EventArgs e)
{
MainMenu.Enabled = false;
Start.Enabled = false;
Stop.Enabled = true;
stop = false;
time = DateTime.Now;
string timeformat = "yyyyMMddHHmm";
//audio recording
if (audioRec.Checked)
{
startAudioRecording(time.ToString(timeformat));
}
if (gps.Checked && gps.Enabled)
{
startGPS(time.ToString(timeformat));
}
System.Threading.Thread thread = new System.Threading.Thread(DoRecognition);
thread.Start();
System.Threading.Thread.Sleep(5);
}
private void startGPS(string timestamp)
{
gpsmanager.setPort((string)gpsPort.SelectedItem);
gpsmanager.start(timestamp);
}
private void stopGPS()
{
gpsmanager.stop();
}
private void startAudioRecording(string timestamp)
{
this.audioRecorder = new AudioRecorder(session, timestamp);
audioRecorder.startRecording();
}
private void stopAudioRecording()
{
if(audioRecorder != null){
audioRecorder.stopRecording();
}
audioRecorder = null;
}
delegate void DoRecognitionCompleted();
private void DoRecognition()
{
GestureRecognition gr = new GestureRecognition(this);
if (simpleToolStripMenuItem.Checked)
{
gr.SimplePipeline();
}
else
{
gr.AdvancedPipeline();
}
this.Invoke(new DoRecognitionCompleted(
delegate
{
Start.Enabled = true;
Stop.Enabled = false;
MainMenu.Enabled = true;
if (closing) Close();
}
));
}
public string GetCheckedDevice()
{
foreach (ToolStripMenuItem m in MainMenu.Items)
{
if (!m.Text.Equals("Device")) continue;
foreach (ToolStripMenuItem e in m.DropDownItems)
{
if (e.Checked) return e.Text;
}
}
return null;
}
public string GetCheckedModule()
{
foreach (ToolStripMenuItem m in MainMenu.Items)
{
if (!m.Text.Equals("Module")) continue;
foreach (ToolStripMenuItem e in m.DropDownItems)
{
if (e.Checked) return e.Text;
}
}
return null;
}
public bool GetDepthState()
{
return Depth.Checked;
}
public bool GetLabelmapState()
{
return Labelmap.Checked;
}
public bool GetGeoNodeState()
{
return GeoNode.Checked;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
stop = true;
e.Cancel = Stop.Enabled;
closing = true;
}
private delegate void UpdateStatusDelegate(string status);
public void UpdateStatus(string status)
{
Status2.Invoke(new UpdateStatusDelegate(delegate(string s) { StatusLabel.Text = s; }), new object[] { status });
}
private void Stop_Click(object sender, EventArgs e)
{
if(audioRec.Checked)
stopAudioRecording();
if(gps.Checked)
stopGPS();
stop = true;
}
public void DisplayBitmap(Bitmap picture)
{
lock (this)
{
bitmap = new Bitmap(picture);
}
}
private void Panel_Paint(object sender, PaintEventArgs e)
{
lock (this)
{
if (bitmap == null) return;
if (Scale2.Checked)
{
e.Graphics.DrawImage(bitmap, Panel2.ClientRectangle);
}
else
{
e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
}
}
}
public void DisplayGeoNodes(PXCMGesture.GeoNode[][] nodes)
{
lock(this) {
if (bitmap == null) return;
if (nodes == null) return;
Graphics g = Graphics.FromImage(bitmap);
using (Pen red = new Pen(Color.Red, 3.0f), green = new Pen(Color.Green, 3.0f), cyan = new Pen(Color.Cyan, 3.0f))
{
for (int i = 0; i < 2; i++)
{
//left
PXCMPoint3DF32 hand_left3d = new PXCMPoint3DF32();
hand_left3d.x = 0; hand_left3d.y = 0;
PXCMPoint3DF32 finger_left3d = new PXCMPoint3DF32();
finger_left3d.x = 0; finger_left3d.y = 0;
int flag_left = 0;
//right
PXCMPoint3DF32 hand_right3d = new PXCMPoint3DF32();
hand_right3d.x = 0; hand_right3d.y = 0;
PXCMPoint3DF32 finger_right3d = new PXCMPoint3DF32();
finger_right3d.x = 0; finger_right3d.y = 0;
int flag_right = 0;
for (int j = 0; j < 10; j++)
{
if (nodes[i][j].body <= 0){
continue;
}
else if (nodes[i][j].body == PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_LEFT)
{
//hand_left = new Point((int)nodes[i][j].positionImage.x, (int)nodes[i][j].positionImage.y);
hand_left3d = nodes[i][j].positionImage;
flag_left++;
}
else if (nodes[i][j].body == PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_RIGHT)
{
//hand_right = new Point((int)nodes[i][j].positionImage.x, (int)nodes[i][j].positionImage.y);
hand_right3d = nodes[i][j].positionImage;
flag_right++;
}
else if (nodes[i][j].body == (PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_LEFT | PXCMGesture.GeoNode.Label.LABEL_FINGER_THUMB))
{
//due to a limitation of the sdk, we store the position of the thumb finger instead of the index finger.
if (finger_left3d.x == 0 && finger_left3d.y==0)
finger_left3d = nodes[i][j].positionImage;
flag_left++;
}
else if (nodes[i][j].body == (PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_RIGHT | PXCMGesture.GeoNode.Label.LABEL_FINGER_THUMB))
{
//due to a limitation of the sdk, we store the position of the thumb finger instead of the index finger.
if (finger_right3d.x == 0 && finger_right3d.y == 0)
finger_right3d = nodes[i][j].positionImage;
flag_right++;
}
else if (nodes[i][j].body == (PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_LEFT | PXCMGesture.GeoNode.Label.LABEL_FINGER_INDEX))
{
//indexfinger_left = new Point((int)nodes[i][j].positionImage.x, (int)nodes[i][j].positionImage.y);
finger_left3d = nodes[i][j].positionImage;
flag_left++;
}
else if (nodes[i][j].body == (PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_RIGHT | PXCMGesture.GeoNode.Label.LABEL_FINGER_INDEX))
{
//indexfinger_right = new Point((int)nodes[i][j].positionImage.x, (int)nodes[i][j].positionImage.y);
finger_right3d = nodes[i][j].positionImage;
flag_right++;
}
float sz = (j == 0) ? 10 : ((nodes[i][j].radiusImage>5)?nodes[i][j].radiusImage:5);
g.DrawEllipse(j > 5 ? red : green, nodes[i][j].positionImage.x - sz / 2, nodes[i][j].positionImage.y - sz / 2, sz, sz);
}
if (flag_left > 1)
{
if(gps.Checked){
gpsmanager.addHandCoordinations(hand_left3d, finger_left3d, "left");
}
Point hand = new Point((int)hand_left3d.x, (int)hand_left3d.y);
Point finger = new Point((int)finger_left3d.x, (int)finger_left3d.y);
g.DrawLine(new Pen(Color.Blue, 3.0f), hand, finger);
}
if (flag_right > 1)
{
if (gps.Checked)
{
gpsmanager.addHandCoordinations(hand_right3d, finger_right3d, "right");
}
Point hand = new Point((int)hand_right3d.x, (int)hand_right3d.y);
Point finger = new Point((int)finger_right3d.x, (int)finger_right3d.y);
g.DrawLine(new Pen(Color.Blue, 3.0f), hand, finger);
}
}
if (Params.Checked)
{
if (nodes[0][0].body > 0)
g.DrawLine(cyan, 0, bitmap.Height - 1, 0, (100 - nodes[0][0].openness) * (bitmap.Height - 1) / 100);
if (nodes[1][0].body > 0)
g.DrawLine(cyan, bitmap.Width - 1, bitmap.Height - 1, bitmap.Width - 1, (100 - nodes[1][0].openness) * (bitmap.Height - 1) / 100);
}
}
g.Dispose();
}
}
private delegate void DisplayGesturesDelegate(PXCMGesture.Gesture[] gestures);
public void DisplayGestures(PXCMGesture.Gesture[] gestures)
{
if (!Gesture.Checked) return;
Gesture1.Invoke(new DisplayGesturesDelegate(delegate(PXCMGesture.Gesture[] data)
{
if (data != null) if (data[0].label > 0)
{
Gesture1.Image = (Bitmap)pictures[data[0].label];
Gesture1.Invalidate();
timer.Start();
}
}), new object[] { gestures });
Gesture2.Invoke(new DisplayGesturesDelegate(delegate(PXCMGesture.Gesture[] data)
{
if (data != null) if (data[1].label > 0)
{
Gesture2.Image = (Bitmap)pictures[data[1].label];
Gesture2.Invalidate();
timer.Start();
}
}), new object[] { gestures });
}
private delegate void UpdatePanelDelegate();
public void UpdatePanel()
{
Panel2.Invoke(new UpdatePanelDelegate(delegate()
{
if (Mirror.Checked)
{
lock (this)
{
if (bitmap!=null)
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
}
Panel2.Invalidate();
}));
}
private void simpleToolStripMenuItem_Click(object sender, EventArgs e)
{
RadioCheck(sender, "Pipeline");
}
private void advancedToolStripMenuItem_Click(object sender, EventArgs e)
{
RadioCheck(sender, "Pipeline");
}
private void timer_Tick(object sender, EventArgs e)
{
Gesture1.Image = Gesture2.Image= null;
Gesture1.Invalidate();
Gesture2.Invalidate();
}
private void Live_Click(object sender, EventArgs e)
{
Playback.Checked = Record.Checked = false;
Live.Checked = true;
}
private void Playback_Click(object sender, EventArgs e)
{
Live.Checked = Record.Checked = false;
Playback.Checked = true;
}
public bool GetPlaybackState()
{
return Playback.Checked;
}
private void Record_Click(object sender, EventArgs e)
{
Live.Checked = Playback.Checked = false;
Record.Checked = true;
}
public bool GetRecordState()
{
return Record.Checked;
}
private delegate string GetFileDelegate();
public string GetPlaybackFile()
{
return this.Invoke(new GetFileDelegate(delegate () {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files (*.*)|*.*";
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
if (ofd.ShowDialog() == DialogResult.OK) return ofd.FileName;
return null;
})) as string;
}
public string GetRecordFile()
{
return this.Invoke(new GetFileDelegate(delegate()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*";
sfd.CheckPathExists = true;
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() == DialogResult.OK) return sfd.FileName;
return null;
})) as string;
}
private void gps_CheckedChanged(object sender, EventArgs e)
{
gpsPort.Enabled = gps.Checked;
}
}
}