-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoOpenFace.cpp
2710 lines (2114 loc) · 96.5 KB
/
moOpenFace.cpp
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
/*******************************************************************************
moOpenFace.cpp
****************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code 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 *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
****************************************************************************
Copyright(C) 2006 Fabricio Costa
Authors:
Fabricio Costa
*******************************************************************************/
#include "moOpenFace.h"
#include "moArray.h"
moDefineDynamicArray( moOpenFaceSystems )
/*********************
NOTAS:
el tracker podr� funcionar como un thread que vaya calculando en funcion de q va llegando la info,
o mejor aun, que trate de calcular, y cuando llega a un resultado el efecto en cuestion tome ese valor.
//para que el tracker funcione sin shaders, debemos hacer el calculo antes de que se pase la informacion a la textura,
para ello quizas no sea conveniente trabajar con threads, sino bloquear la ejecucion hasta terminar de tomar los valores q nos interesan.
***********************/
//========================
// Factory
//========================
moOpenFaceFactory *m_OpenFaceFactory = NULL;
MO_PLG_API moResourceFactory* CreateResourceFactory(){
if (m_OpenFaceFactory==NULL)
m_OpenFaceFactory = new moOpenFaceFactory();
return (moResourceFactory*) m_OpenFaceFactory;
}
MO_PLG_API void DestroyResourceFactory(){
delete m_OpenFaceFactory;
m_OpenFaceFactory = NULL;
}
moResource* moOpenFaceFactory::Create() {
return new moOpenFace();
}
void moOpenFaceFactory::Destroy(moResource* fx) {
delete fx;
}
//===========================================
//
// Class: moOpenFace
//
//===========================================
moOpenFace::moOpenFace() {
SetName(moText("openface"));
m_pCVSourceTexture = NULL;
m_pCVResultTexture = NULL;
m_pCVResult2Texture = NULL;
m_pCVResult3Texture = NULL;
m_pCVBlobs = NULL;
m_pCVThresh = NULL;
m_OutletDataMessage = NULL;
m_OutletDataVectorMessage = NULL;
m_pDataMessage = NULL;
m_pDataVectorMessage = NULL;
m_Blob1X = NULL;
m_Blob1Y = NULL;
m_Blob1Size = NULL;
m_Blob1Vx = NULL;
m_Blob1Vy = NULL;
m_Blob2X = NULL;
m_Blob2Y = NULL;
m_Blob2Size = NULL;
m_Blob2Vx = NULL;
m_Blob2Vy = NULL;
m_Blob3X = NULL;
m_Blob3Y = NULL;
m_Blob3Size = NULL;
m_Blob3Vx = NULL;
m_Blob3Vy = NULL;
m_Blob4X = NULL;
m_Blob4Y = NULL;
m_Blob4Size = NULL;
m_Blob4Vx = NULL;
m_Blob4Vy = NULL;
p_clnf_model = NULL;
m_keepAspectRatio = true;
}
moOpenFace::~moOpenFace() {
Finish();
}
moConfigDefinition * moOpenFace::GetDefinition( moConfigDefinition *p_configdefinition ) {
//default: alpha, color, syncro
p_configdefinition = moMoldeoObject::GetDefinition( p_configdefinition );
p_configdefinition->Add( moText("texture"), MO_PARAM_TEXTURE, OPENFACE_TEXTURE, moValue( "default", "TXT") );
p_configdefinition->Add( moText("color"), MO_PARAM_COLOR, OPENFACE_COLOR );
/*
enum moOpenFaceRecognitionMode {
OPENFACE_RECOGNITION_MODE_UNDEFINED=-1,
OPENFACE_RECOGNITION_MODE_FACE=0,
OPENFACE_RECOGNITION_MODE_GPU_MOTION=1,
OPENFACE_RECOGNITION_MODE_CONTOUR=2,
OPENFACE_RECOGNITION_MODE_COLOR=3,
OPENFACE_RECOGNITION_MODE_MOTION=4,
OPENFACE_RECOGNITION_MODE_BLOBS=5,
OPENFACE_RECOGNITION_MODE_THRESHOLD=6
};*/
p_configdefinition->Add( moText("recognition_mode"), MO_PARAM_NUMERIC, OPENFACE_RECOGNITION_MODE, moValue( "0", "NUM"),
moText("Face,Gpu Motion,Contour,Color,Motion Detection,Blobs,Threshold,Body,Face Recognition Memorize,Face Recognition Remember") );
p_configdefinition->Add( moText("reduce_width"), MO_PARAM_NUMERIC, OPENFACE_REDUCE_WIDTH, moValue( "64","INT").Ref() );
p_configdefinition->Add( moText("reduce_height"), MO_PARAM_NUMERIC, OPENFACE_REDUCE_HEIGHT, moValue( "64","INT").Ref() );
p_configdefinition->Add( moText("threshold"), MO_PARAM_FUNCTION, OPENFACE_THRESHOLD, moValue( "50", "FUNCTION").Ref() );
p_configdefinition->Add( moText("threshold_max"), MO_PARAM_FUNCTION, OPENFACE_THRESHOLD_MAX, moValue( "255", "FUNCTION").Ref() );
p_configdefinition->Add( moText("threshold_type"), MO_PARAM_NUMERIC, OPENFACE_THRESHOLD_TYPE, moValue( "0", "NUM"), moText("Binary,Binary Inverted,Threshold Truncated,To Zero,To Zero Inverted") );
p_configdefinition->Add( moText("line_thickness"), MO_PARAM_FUNCTION, OPENFACE_LINE_THICKNESS, moValue( "2", "FUNCTION").Ref() );
p_configdefinition->Add( moText("line_color"), MO_PARAM_COLOR, OPENFACE_LINE_COLOR );
p_configdefinition->Add( moText("line_offset_x"), MO_PARAM_FUNCTION, OPENFACE_LINE_OFFSET_X, moValue( "0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("line_offset_y"), MO_PARAM_FUNCTION, OPENFACE_LINE_OFFSET_Y, moValue( "0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("line_steps"), MO_PARAM_FUNCTION, OPENFACE_LINE_STEPS, moValue( "0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("crop_min_x"), MO_PARAM_FUNCTION, OPENFACE_CROP_MIN_X, moValue( "0.0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("crop_max_x"), MO_PARAM_FUNCTION, OPENFACE_CROP_MAX_X, moValue( "1.0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("crop_min_y"), MO_PARAM_FUNCTION, OPENFACE_CROP_MIN_Y, moValue( "0.0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("crop_max_y"), MO_PARAM_FUNCTION, OPENFACE_CROP_MAX_Y, moValue( "1.0", "FUNCTION").Ref() );
p_configdefinition->Add( moText("motion_pixels"), MO_PARAM_NUMERIC, OPENFACE_MOTION_PIXELS, moValue( "5", "INT").Ref() );
p_configdefinition->Add( moText("motion_deviation"), MO_PARAM_NUMERIC, OPENFACE_MOTION_DEVIATION, moValue( "20", "INT").Ref() );
p_configdefinition->Add( moText("blob_min_area"), MO_PARAM_FUNCTION, OPENFACE_BLOB_MIN_AREA, moValue( "1000", "FUNCTION").Ref() );
p_configdefinition->Add( moText("blob_max_area"), MO_PARAM_FUNCTION, OPENFACE_BLOB_MAX_AREA, moValue( "50000", "FUNCTION").Ref() );
p_configdefinition->Add( moText("blob_min_distance"), MO_PARAM_FUNCTION, OPENFACE_BLOB_MIN_DISTANCE, moValue( "10", "FUNCTION").Ref() );
p_configdefinition->Add( moText("training_images_folder"), MO_PARAM_TEXT, OPENFACE_TRAINING_IMAGES_FOLDER );
p_configdefinition->Add( moText("saving_images_folder"), MO_PARAM_TEXT, OPENFACE_SAVING_IMAGES_FOLDER );
p_configdefinition->Add( moText("saving_images_mode"), MO_PARAM_NUMERIC, OPENFACE_SAVING_IMAGES_MODE, moValue( "0", "NUM"),
moText("Time,Still,Smile") );
p_configdefinition->Add( moText("saving_images_time"), MO_PARAM_FUNCTION, OPENFACE_SAVING_IMAGES_TIME, moValue( "5000", "FUNCTION").Ref());
p_configdefinition->Add( moText("saving_images_size_width"), MO_PARAM_FUNCTION, OPENFACE_SAVING_IMAGES_SIZE_WIDTH, moValue( "30", "FUNCTION").Ref());
p_configdefinition->Add( moText("saving_images_size_height"), MO_PARAM_FUNCTION, OPENFACE_SAVING_IMAGES_SIZE_HEIGHT, moValue( "30", "FUNCTION").Ref());
p_configdefinition->Add( moText("debug_on"), MO_PARAM_NUMERIC, OPENFACE_DEBUG_ON, moValue( "0", "NUM").Ref(), moText("Off,On,Full") );
return p_configdefinition;
}
MOboolean moOpenFace::Init() {
moText configname;
MOint nvalues;
MOint trackersystems;
if ( GetConfigName().Length()==0 ) return false;
if (!moResource::Init()) return false;
moDefineParamIndex( OPENFACE_TEXTURE, "texture" );
moDefineParamIndex( OPENFACE_COLOR, "color" );
moDefineParamIndex( OPENFACE_RECOGNITION_MODE, "recognition_mode" );
moDefineParamIndex( OPENFACE_REDUCE_WIDTH, moText("reduce_width") );
moDefineParamIndex( OPENFACE_REDUCE_HEIGHT, moText("reduce_height") );
moDefineParamIndex( OPENFACE_THRESHOLD, moText("threshold") );
moDefineParamIndex( OPENFACE_THRESHOLD_MAX, moText("threshold_max") );
moDefineParamIndex( OPENFACE_THRESHOLD_TYPE, moText("threshold_type") );
moDefineParamIndex( OPENFACE_LINE_THICKNESS, moText("line_thickness") );
moDefineParamIndex( OPENFACE_LINE_COLOR, moText("line_color") );
moDefineParamIndex( OPENFACE_LINE_OFFSET_X, moText("line_offset_x") );
moDefineParamIndex( OPENFACE_LINE_OFFSET_Y, moText("line_offset_y") );
moDefineParamIndex( OPENFACE_LINE_STEPS, moText("line_steps") );
moDefineParamIndex( OPENFACE_CROP_MIN_X, "crop_min_x" );
moDefineParamIndex( OPENFACE_CROP_MAX_X, "crop_max_x" );
moDefineParamIndex( OPENFACE_CROP_MIN_Y, "crop_min_y" );
moDefineParamIndex( OPENFACE_CROP_MAX_Y, "crop_max_y" );
moDefineParamIndex( OPENFACE_MOTION_PIXELS, "motion_pixels" );
moDefineParamIndex( OPENFACE_MOTION_DEVIATION, "motion_deviation" );
moDefineParamIndex( OPENFACE_BLOB_MIN_AREA, "blob_min_area" );
moDefineParamIndex( OPENFACE_BLOB_MAX_AREA, "blob_max_area" );
moDefineParamIndex( OPENFACE_BLOB_MIN_DISTANCE, "blob_min_distance" );
moDefineParamIndex( OPENFACE_TRAINING_IMAGES_FOLDER, "training_images_folder" );
moDefineParamIndex( OPENFACE_SAVING_IMAGES_FOLDER, "saving_images_folder" );
moDefineParamIndex( OPENFACE_SAVING_IMAGES_MODE, "saving_images_mode" );
moDefineParamIndex( OPENFACE_SAVING_IMAGES_TIME, "saving_images_time" );
moDefineParamIndex( OPENFACE_SAVING_IMAGES_SIZE_WIDTH, "saving_images_size_width" );
moDefineParamIndex( OPENFACE_SAVING_IMAGES_SIZE_HEIGHT, "saving_images_size_height" );
moDefineParamIndex( OPENFACE_DEBUG_ON, "debug_on" );
/*
img = cvCreateImage( cvSize(w,w), 8, 1 );
cvZero( img );
levels = 0;
threshold = 150;
threshold_max = 3;
contours = NULL;
storage = cvCreateMemStorage(0);
idopencvout = -1;
//contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
*/
m_pBuffer = NULL;
m_BufferSize = 0;
m_pIplImage = NULL;
m_RecognitionMode = OPENFACE_RECOGNITION_MODE_UNDEFINED;
m_bReInit = true;
m_pSrcTexture = NULL;
m_pDest0Texture = NULL;
m_pDest1Texture = NULL;
m_pDest2Texture = NULL;
m_pDestDiff1Texture = NULL;
m_pDestDiff2Texture = NULL;
m_pTFDest0Texture = NULL;
m_pTFDest1Texture = NULL;
m_pTFDest2Texture = NULL;
m_pTFDestDiff1Texture = NULL;
m_pTFDestDiff2Texture = NULL;
m_pTrackerSystemData = NULL;
m_pBucketDiff1 = NULL;
m_pBucketDiff2 = NULL;
switch_texture = -1;
m_FacePositionX = NULL;
m_FacePositionY = NULL;
m_FaceSizeWidth = NULL;
m_FaceSizeHeight = NULL;
m_FaceDetection = NULL;
m_FaceDetectionCertainty = NULL;
m_GazeDirectionL_X = NULL;
m_GazeDirectionL_Y = NULL;
m_GazeDirectionL_Z = NULL;
m_GazeDirectionR_X = NULL;
m_GazeDirectionR_Y = NULL;
m_GazeDirectionR_Z = NULL;
m_FacePositionZ = NULL;
m_FaceSizeDepth = NULL;
m_FaceAngleX = NULL;
m_FaceAngleY = NULL;
m_FaceAngleZ = NULL;
m_MotionDetection = NULL;
m_MotionDetectionX = NULL;
m_MotionDetectionY = NULL;
m_EyeLeftX = NULL;
m_EyeLeftY = NULL;
m_EyeRightX = NULL;
m_EyeRightY = NULL;
m_OutTracker = NULL;
m_OutletDataMessage = NULL;
m_OutletDataVectorMessage = NULL;
m_pDataMessage = NULL;
m_pDataMessage = new moDataMessage();
m_pDataVectorMessage = NULL;
m_pDataVectorMessage = new moDataMessage();
moTexParam tparam = MODefTex2DParams;
//tparam.internal_format = GL_RGBA32F_ARB;
tparam.internal_format = GL_RGB;
int Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVSOURCE", 128, 128, tparam );
if (Mid>0) {
m_pCVSourceTexture = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVSourceTexture->BuildEmpty(128, 128);
if (m_debug_on) MODebug2->Message("CVSOURCE texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVSOURCE");
}
Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVRESULT", 128, 128, tparam );
if (Mid>0) {
m_pCVResultTexture = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVResultTexture->BuildEmpty(128, 128);
if (m_debug_on) MODebug2->Message("CVRESULT texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVRESULT");
}
Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVRESULT2", 128, 128, tparam );
if (Mid>0) {
m_pCVResult2Texture = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVResult2Texture->BuildEmpty(128, 128);
if (m_debug_on) MODebug2->Message("CVRESULT2 texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVRESULT2");
}
Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVRESULT3", 128, 128, tparam );
if (Mid>0) {
m_pCVResult3Texture = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVResult3Texture->BuildEmpty(128, 128);
if (m_debug_on) MODebug2->Message("CVRESULT3 texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVRESULT3");
}
Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVBLOBS", 512, 512, tparam );
if (Mid>0) {
m_pCVBlobs = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVBlobs->BuildEmpty(512, 512);
if (m_debug_on) MODebug2->Message("CVBLOBS texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVBLOBS");
}
Mid = GetResourceManager()->GetTextureMan()->AddTexture( "CVTHRESH", 512, 512, tparam );
if (Mid>0) {
m_pCVThresh = GetResourceManager()->GetTextureMan()->GetTexture(Mid);
m_pCVThresh->BuildEmpty(512, 512);
if (m_debug_on) MODebug2->Message("CVTHRESH texture created!!");
} else {
MODebug2->Error("Couldn't create texture: CVTHRESH");
}
m_pContourIndex = new moInlet();
if (m_pContourIndex) {
((moConnector*)m_pContourIndex)->Init( moText("contourindex"), m_Inlets.Count(), MO_DATA_NUMBER_LONG );
m_Inlets.Add(m_pContourIndex);
}
m_pLineIndex = new moInlet();
if (m_pLineIndex) {
((moConnector*)m_pLineIndex)->Init( moText("lineindex"), m_Inlets.Count(), MO_DATA_NUMBER_LONG );
m_Inlets.Add(m_pLineIndex);
}
UpdateParameters();
return true;
}
void moOpenFace::UpdateParameters() {
moOpenFaceRecognitionMode mode = (moOpenFaceRecognitionMode) m_Config.Int( moR(OPENFACE_RECOGNITION_MODE) );
if (m_RecognitionMode != mode ) {
m_bReInit = true;
m_RecognitionMode = mode;
}
m_debug_on = m_Config.Int(moR(OPENFACE_DEBUG_ON ));
int reduce_width = m_Config.Int(moR(OPENFACE_REDUCE_WIDTH ));
int reduce_height = m_Config.Int(moR(OPENFACE_REDUCE_HEIGHT ));
if ( m_reduce_width!=reduce_width || m_reduce_height!=reduce_height ) m_bReInit = true;
m_reduce_width = reduce_width;
m_reduce_height = reduce_height;
m_threshold = m_Config.Eval( moR(OPENFACE_THRESHOLD));
m_threshold_max = m_Config.Eval( moR(OPENFACE_THRESHOLD_MAX));
m_threshold_type = (moOpenFaceThresholdType) min( (int)OPENFACE_THRESHOLD_TYPE_MAX, m_Config.Int( moR(OPENFACE_THRESHOLD_TYPE)));
m_crop_min_x = m_Config.Eval( moR(OPENFACE_CROP_MIN_X));
m_crop_max_x = m_Config.Eval( moR(OPENFACE_CROP_MAX_X));
m_crop_min_y = m_Config.Eval( moR(OPENFACE_CROP_MIN_Y));
m_crop_max_y = m_Config.Eval( moR(OPENFACE_CROP_MAX_Y));
m_line_thickness = fabs( m_Config.Eval( moR(OPENFACE_LINE_THICKNESS)) );
m_line_offset_x = m_Config.Eval( moR(OPENFACE_LINE_OFFSET_X));
m_line_offset_y = m_Config.Eval( moR(OPENFACE_LINE_OFFSET_Y));
m_line_steps = m_Config.Eval( moR(OPENFACE_LINE_STEPS));
m_line_color = m_Config.EvalColor(moR( OPENFACE_LINE_COLOR ));
m_motion_pixels = m_Config.Eval( moR(OPENFACE_MOTION_PIXELS));
m_motion_deviation = m_Config.Eval( moR(OPENFACE_MOTION_DEVIATION));
m_blob_min_area = m_Config.Eval( moR(OPENFACE_BLOB_MIN_AREA));
m_blob_max_area = m_Config.Eval( moR(OPENFACE_BLOB_MAX_AREA));
m_blob_min_distance = m_Config.Eval( moR(OPENFACE_BLOB_MIN_DISTANCE));
m_trainingImagesPath = m_pResourceManager->GetDataMan()->GetDataPath() + moSlash+m_Config.Text( moR(OPENFACE_TRAINING_IMAGES_FOLDER) );
m_savingImagesPath = m_Config.Text( moR(OPENFACE_SAVING_IMAGES_FOLDER) );
m_savingImagesMode = m_Config.Int( moR(OPENFACE_SAVING_IMAGES_MODE) );
m_savingImagesTime = m_Config.Eval( moR(OPENFACE_SAVING_IMAGES_TIME) );
m_savingImagesSizeWidth = m_Config.Eval( moR(OPENFACE_SAVING_IMAGES_SIZE_WIDTH) );
m_savingImagesSizeHeight = m_Config.Eval( moR(OPENFACE_SAVING_IMAGES_SIZE_HEIGHT) );
m_keepAspectRatio = true;
m_goalSize = cvSize(m_savingImagesSizeWidth, m_savingImagesSizeHeight);
moData* pTexData = m_Config[moR( OPENFACE_TEXTURE )].GetData();
//moTexture rTexture = m_Config.Texture( moR( OPENFACE_TEXTURE );
if (pTexData
&& pTexData->Texture()) {
//MODebug2->Message( "moOpenFace::UpdateParameters() > " + pTexData->Texture()->GetName() );
} else {
MODebug2->Error("moOpenFace::UpdateParameters() > no TexData !! Trying updating the connectors..." );
moMoldeoObject::UpdateConnectors();
pTexData = m_Config[moR( OPENFACE_TEXTURE )].GetData();
if ( pTexData)
if (pTexData->Texture())
if (m_debug_on) MODebug2->Message( "moOpenFace::UpdateParameters() > " + pTexData->Texture()->GetName() );
}
moTexture* m_pRecTexture = NULL;
if (m_pSrcTexture==NULL) {
m_bReInit = true;
}
if (m_pDataMessage)
m_pDataMessage->Empty();
if (m_pDataVectorMessage)
m_pDataVectorMessage->Empty();
if (pTexData) {
///segun el modelo aplicamos...
pTexData->GetGLId();
switch(pTexData->Type()) {
case MO_DATA_IMAGESAMPLE:
m_pRecTexture = pTexData->Texture();
break;
case MO_DATA_IMAGESAMPLE_FILTERED:
m_pRecTexture = pTexData->TextureDestination();
break;
default:
m_pRecTexture = NULL;
break;
}
} else {
}
if (m_pRecTexture) {
if (m_pSrcTexture) {
if (m_pRecTexture->GetName()!=m_pSrcTexture->GetName()) {
if (m_debug_on) MODebug2->Message("moOpenFace::UpdateParameters() > m_bReInit TRUE! for " + m_pRecTexture->GetName());
m_bReInit = true;
}
} else {
if (m_debug_on) MODebug2->Message("moOpenFace::UpdateParameters() > set texture to " + m_pRecTexture->GetName());
}
m_pSrcTexture = m_pRecTexture;
}
if (m_pSrcTexture==NULL) {
MODebug2->Error("moOpenFace::UpdateParameters() > texture is null ");
return;
}
switch( m_RecognitionMode ) {
case OPENFACE_RECOGNITION_MODE_FACE:
{
//int stepN = 5;
//( m_steps<0 || m_steps>stepN )? m_steps = 0 : m_steps++;
//if (m_steps==stepN) { FaceDetection(); }
FaceDetection();
}
break;
case OPENFACE_RECOGNITION_MODE_FACERECOGNITION_REM:
case OPENFACE_RECOGNITION_MODE_FACERECOGNITION_MEM:
{
int stepN = 5;
( m_steps<0 || m_steps>stepN )? m_steps = 0 : m_steps++;
if (m_steps==stepN) { FaceRecognition(); }
}
break;
case OPENFACE_RECOGNITION_MODE_BODY:
{
int stepN = 5;
( m_steps<0 || m_steps>stepN )? m_steps = 0 : m_steps++;
if (m_steps==stepN) { BodyDetection(); }
}
break;
case OPENFACE_RECOGNITION_MODE_GPU_MOTION:
GpuMotionRecognition();
break;
case OPENFACE_RECOGNITION_MODE_CONTOUR:
ContourRecognition();
break;
case OPENFACE_RECOGNITION_MODE_BLOBS:
BlobRecognition();
break;
case OPENFACE_RECOGNITION_MODE_COLOR:
ColorRecognition();
break;
case OPENFACE_RECOGNITION_MODE_MOTION:
MotionRecognition();
break;
case OPENFACE_RECOGNITION_MODE_THRESHOLD:
ThresholdRecognition();
break;
default:
m_bReInit = false;
break;
}
if (!m_OutletDataMessage) {
m_OutletDataMessage = m_Outlets.GetRef( GetOutletIndex( moText("DATAMESSAGE") ) );
//MODebug2->Message("moOpenFace::FaceDetection > outlet FACE_POSITION_X: "+IntToStr((int)m_FacePositionX));
} else {
//m_OutletDataMessage->GetData()->SetInt( (int)(number_of_sequence>0) );
if (m_pDataMessage) {
m_OutletDataMessage->GetData()->SetMessage(m_pDataMessage);
m_OutletDataMessage->Update(true);
}
}
if (!m_OutletDataVectorMessage) {
m_OutletDataVectorMessage = m_Outlets.GetRef( GetOutletIndex( moText("DATAVECTORMESSAGE") ) );
//MODebug2->Message("moOpenFace::FaceDetection > outlet DATAVECTORMESSAGE: "+IntToStr((int)m_OutletDataVectorMessage));
} else {
//m_OutletDataMessage->GetData()->SetInt( (int)(number_of_sequence>0) );
if (m_pDataVectorMessage) {
m_OutletDataVectorMessage->GetData()->SetMessage(m_pDataVectorMessage);
m_OutletDataVectorMessage->Update(true);
}
}
}
#include "moDebugManager.h"
// Check if there is motion in the result matrix
// count the number of changes and return.
int moOpenFace::detectMotion(const Mat & motion, Mat & result, Mat & result_cropped,
int x_start, int x_stop, int y_start, int y_stop,
int max_deviation,
Scalar & color)
{
Scalar mean, stddev;
meanStdDev(motion, mean, stddev);
// if not to much changes then the motion is real (neglect agressive snow, temporary sunlight)
if(stddev[0] < max_deviation)
{
//MODebug2->Message("max_deviation ok");
int number_of_changes = 0;
int min_x = motion.cols, max_x = 0;
int min_y = motion.rows, max_y = 0;
// loop over image and detect changes
for(int j = y_start; j < y_stop; j+=2){ // height
for(int i = x_start; i < x_stop; i+=2){ // width
// check if at pixel (j,i) intensity is equal to 255
// this means that the pixel is different in the sequence
// of images (prev_frame, current_frame, next_frame)
int pi = static_cast<int>(motion.at<uchar>(j,i));
if( pi == 255)
{
number_of_changes++;
if(min_x>i) min_x = i;
if(max_x<i) max_x = i;
if(min_y>j) min_y = j;
if(max_y<j) max_y = j;
}
/*
if (pi>100)
MODebug2->Message( "i,j: (" + IntToStr(i)
+ "," + IntToStr(j)
+ "): " + IntToStr(pi) );
*/
}
}
if(number_of_changes){
//check if not out of bounds
if(min_x-10 > 0) min_x -= 10;
if(min_y-10 > 0) min_y -= 10;
if(max_x+10 < result.cols-1) max_x += 10;
if(max_y+10 < result.rows-1) max_y += 10;
// draw rectangle round the changed pixel
Point x(min_x,min_y);
Point y(max_x,max_y);
Rect rect(x,y);
Mat cropped = result(rect);
cropped.copyTo(result_cropped);
rectangle(result,rect,color,1);
}
return number_of_changes;
}
return 0;
}
void
moOpenFace::MotionRecognition() {
Mat d1, d2, motion;
Mat motioncolor;
Scalar mean_, color(0,255,255); // yellow
Mat kernel_ero = getStructuringElement(MORPH_RECT, Size(2,2));
//MODebug2->Message("MotionRecognition");
// Detect motion in window
int x_start = (int)((float)m_reduce_width*m_crop_min_x), x_stop = (int)((float)m_reduce_width*m_crop_max_x);
int y_start = (int)((float)m_reduce_height*m_crop_min_y), y_stop = (int)((float)m_reduce_height*m_crop_max_y);
// If more than 'there_is_motion' pixels are changed, we say there is motion
// and store an image on disk
int there_is_motion = m_motion_pixels;
// Maximum deviation of the image, the higher the value, the more motion is allowed
int max_deviation = m_motion_deviation;
if (m_pSrcTexture==NULL) {
if (m_debug_on) MODebug2->Message("moOpenFace::MotionRecognition >> no src texture");
return;
}
moVector2i resizer( m_reduce_width, m_reduce_height );
IplImage* srcframe = TextureToCvImage( m_pSrcTexture, resizer );
if (srcframe==NULL) {
MODebug2->Error("Error TextureToCvImage() : " + m_pSrcTexture->GetName() );
return;
}
//Mat frame( srcframe );
Mat frame = cv::cvarrToMat(srcframe);
Mat framecol;
CvMatToTexture( frame, 0 , 0, 0, m_pCVSourceTexture );
if (m_bReInit) {
if (m_debug_on) MODebug2->Message("MotionRecognition INIT");
number_of_sequence = 0;
cvtColor(frame, next_frame, COLOR_BGR2GRAY);
next_frame.copyTo(prev_frame);
next_frame.copyTo(current_frame);
}
/**CONVERT AND ENHANCE TO GRAYSCALE*/
current_frame.copyTo(prev_frame);
next_frame.copyTo(current_frame);
cvtColor(frame, next_frame, COLOR_BGR2GRAY);
Mat result, result_cropped;
result = next_frame;
absdiff(prev_frame, next_frame, d1);
absdiff(next_frame, current_frame, d2);
/*
Mat motioncolor1;
cvtColor( next_frame, motioncolor1, COLOR_GRAY2RGB);
CvMatToTexture( motioncolor1, 0 , 0, 0, m_pCVResultTexture );
Mat motioncolor2;
cvtColor( current_frame, motioncolor2, COLOR_GRAY2RGB);
CvMatToTexture( motioncolor2, 0 , 0, 0, m_pCVResult2Texture );
Mat motioncolor3;
cvtColor( prev_frame, motioncolor3, COLOR_GRAY2RGB);
CvMatToTexture( motioncolor3, 0 , 0, 0, m_pCVResult3Texture );
*/
bitwise_and(d1, d2, motion);
threshold(motion, motion, 35, 255, CV_THRESH_BINARY);
erode(motion, motion, kernel_ero);
Mat motioncolor1;
cvtColor( motion, motioncolor1, COLOR_GRAY2RGB);
CvMatToTexture( motioncolor1, 0 , 0, 0, m_pCVResultTexture );
number_of_changes = detectMotion(motion, result, result_cropped, x_start, x_stop, y_start, y_stop, max_deviation, color);
//MODebug2->Push( "number_of_changes: " + IntToStr(number_of_changes)
// + "there_is_motion: " + IntToStr(there_is_motion) );
// If a lot of changes happened, we assume something changed.
if(number_of_changes>=there_is_motion)
{
if (m_debug_on) MODebug2->Message( "number_of_changes: " + IntToStr(number_of_changes)
+ "there_is_motion: " + IntToStr(there_is_motion) );
number_of_sequence++;
if(number_of_sequence>0){
if (m_debug_on) MODebug2->Message("Motion Detected!");
//saveImg(result,DIR,EXT,DIR_FORMAT.c_str(),FILE_FORMAT.c_str());
//saveImg(result_cropped,DIR,EXT,DIR_FORMAT.c_str(),CROPPED_FILE_FORMAT.c_str());
}
}
else
{
number_of_sequence = 0;
}
if (!m_MotionDetection) {
m_MotionDetection = m_Outlets.GetRef( GetOutletIndex( moText("MOTION_DETECTION") ) );
//MODebug2->Message("moOpenFace::FaceDetection > outlet FACE_POSITION_X: "+IntToStr((int)m_FacePositionX));
} else {
m_MotionDetection->GetData()->SetInt( (int)(number_of_sequence>0) );
m_MotionDetection->Update(true);
}
if (!m_MotionDetectionX) {
m_MotionDetectionX = m_Outlets.GetRef( GetOutletIndex( moText("MOTION_DETECTION_X") ) );
//MODebug2->Message("moOpenFace::FaceDetection > outlet FACE_POSITION_X: "+IntToStr((int)m_FacePositionX));
} else {
m_MotionDetectionX->GetData()->SetInt( (int)(number_of_sequence>0) );
m_MotionDetectionX->Update(true);
}
if (!m_MotionDetectionY) {
m_MotionDetectionY = m_Outlets.GetRef( GetOutletIndex( moText("MOTION_DETECTION_Y") ) );
//MODebug2->Message("moOpenFace::FaceDetection > outlet FACE_POSITION_X: "+IntToStr((int)m_FacePositionX));
} else {
m_MotionDetectionY->GetData()->SetInt( (int)(number_of_sequence>0) );
m_MotionDetectionY->Update(true);
}
//SENDING DATAMESSAGE
if (m_pDataMessage) {
m_pDataMessage->Empty();//EMPTY IN UpdateParameters()
moData pData;
pData.SetText( moText("opencv") );
m_pDataMessage->Add(pData);
pData.SetText( moText("MOTION_DETECTION") );
m_pDataMessage->Add(pData);
pData.SetInt( (int)(number_of_sequence>0) );
m_pDataMessage->Add(pData);
pData.SetText( moText("MOTION_DETECTION_X") );
m_pDataMessage->Add(pData);
pData.SetInt( (int)(number_of_sequence>0) );
m_pDataMessage->Add(pData);
pData.SetText( moText("MOTION_DETECTION_Y") );
m_pDataMessage->Add(pData);
pData.SetInt( (int)(number_of_sequence>0) );
m_pDataMessage->Add(pData);
/*
moText ccc = "";
for( int c=0; c<m_pDataMessage->Count(); c++) {
ccc = ccc + m_pDataMessage->Get(c).ToText();
}
//MODebug2->Push(ccc);
*/
}
m_bReInit = false;
}
void
moOpenFace::GpuMotionRecognition() {
if (m_pSrcTexture==NULL) {
if (m_debug_on) MODebug2->Error("moOpenFace::GpuMotionRecognition >> no src texture");
return;
}
moText reduce_resolution = IntToStr(m_reduce_width)+moText("x")+IntToStr(m_reduce_height);
if (m_bReInit) {
if (m_debug_on) MODebug2->Message("moOpenFace::GpuMotionRecognition >> REINIT");
m_pTFDest0Texture = NULL;
m_pTFDest1Texture = NULL;
m_pTFDest2Texture = NULL;
m_pTFDestDiff1Texture = NULL;
m_pTFDestDiff2Texture = NULL;
}
//crea textura que recibe frame0 desde el SrcTexture
//crea textura que recibe frame1
//crea textura que recibe frame_diff
int idt = -1;
///creamos la textura de destino de la copia:
/// y su shader asociado
if (!m_pTFDest2Texture && m_pSrcTexture->GetWidth()>0 ) {
moTextArray copy_filter_0;
copy_filter_0.Add( m_pSrcTexture->GetName()+moText(" shaders/Copy.cfg res:"+reduce_resolution+" "+m_pSrcTexture->GetName()+"dest2") );
int idx = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->LoadFilters( ©_filter_0 );
if (idx>-1) {
m_pTFDest2Texture = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->Get(idx-1);
if (m_debug_on) MODebug2->Message( moText("filter loaded m_pTFDest2Texture") );
}
}
///creamos la textura de destino de la copia del cuadro anterior
/// y su shader asociado
if (!m_pTFDest1Texture && m_pSrcTexture->GetWidth()>0 ) {
moTextArray copy_filter_0;
copy_filter_0.Add( moText("livein0dest2 shaders/Copy.cfg res:"+reduce_resolution+" "+m_pSrcTexture->GetName()+"dest1") );
int idx = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->LoadFilters( ©_filter_0 );
if (idx>-1) {
m_pTFDest1Texture = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->Get(idx-1);
if (m_debug_on) MODebug2->Message( moText("filter loaded m_pTFDest1Texture") );
}
}
///creamos la textura de destino de la copia del cuadro anterior
/// y su shader asociado
if (!m_pTFDest0Texture && m_pSrcTexture->GetWidth()>0 ) {
moTextArray copy_filter_0;
copy_filter_0.Add( moText("livein0dest1 shaders/Copy.cfg res:"+reduce_resolution+" "+m_pSrcTexture->GetName()+"dest0") );
int idx = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->LoadFilters( ©_filter_0 );
if (idx>-1) {
m_pTFDest0Texture = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->Get(idx-1);
if (m_debug_on) MODebug2->Message( moText("filter loaded m_pTFDest0Texture") );
}
}
///creamos la textura final, que contendra la diferencia entre el cuadro actual y el anterior..
/// y su shader asociado
if (!m_pTFDestDiff2Texture && m_pSrcTexture->GetWidth()>0 ) {
moTextArray copy_filter_0;
copy_filter_0.Add( moText("livein0dest1 livein0dest2 shaders/Diff.cfg res:"+reduce_resolution+" "+m_pSrcTexture->GetName()+"diff2") );
int idx = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->LoadFilters( ©_filter_0 );
if (idx>-1) {
m_pTFDestDiff2Texture = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->Get(idx-1);
if (m_pTFDestDiff2Texture) {
m_pDestDiff2Texture = m_pTFDestDiff2Texture->GetDestTex()->GetTexture(0);
if (m_debug_on) MODebug2->Message( moText("filter loaded m_pTFDestDiff2Texture") );
}
}
}
///creamos la textura final, que contendra la diferencia entre el cuadro anterior y el ante-ultimo..
/// y su shader asociado
if (!m_pTFDestDiff1Texture && m_pSrcTexture->GetWidth()>0 ) {
moTextArray copy_filter_0;
copy_filter_0.Add( moText("livein0dest0 livein0dest1 shaders/Diff.cfg res:"+reduce_resolution+" "+m_pSrcTexture->GetName()+"diff1") );
int idx = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->LoadFilters( ©_filter_0 );
if (idx>-1) {
m_pTFDestDiff1Texture = m_pResourceManager->GetShaderMan()->GetTextureFilterIndex()->Get(idx-1);
if (m_pTFDestDiff1Texture) {
m_pDestDiff1Texture = m_pTFDestDiff1Texture->GetDestTex()->GetTexture(0);
if (m_debug_on) MODebug2->Message( moText("filter loaded m_pTFDestDiff1Texture") );
}
}
}
/*
pFBO->AddTexture( 160,
120,
m_pSrcTexture->GetTexParam(),
m_pSrcTexture->GetGLId(),
attach_point );
m_pSrcTexture->SetFBO( pFBO );
m_pSrcTexture->SetFBOAttachPoint( attach_point );
*/
///Guarda el frame ante-anterior....
if (m_pTFDest0Texture) {
m_pTFDest0Texture->Apply( (GLuint)0 );
}
///Guarda el frame anterior....
if (m_pTFDest1Texture) {
m_pTFDest1Texture->Apply( (GLuint)0 );
}
///Guarda el frame actual.... (esto lo hacemos ultimo para no perder los anteriores.
if (m_pTFDest2Texture) {
m_pTFDest2Texture->Apply( (GLuint)0 );
}
///Calcular la diferencia del frame anterior con el anterior a este
if (m_pTFDestDiff1Texture) {
m_pTFDestDiff1Texture->Apply( (GLuint)0 );
}
///Calcular la diferencia con el frame anterior
if (m_pTFDestDiff2Texture) {
m_pTFDestDiff2Texture->Apply( (GLuint)0 );
}
if (m_pDestDiff2Texture) {
/// BAJAR A BUFFER Y PASAR A ARRAY DE TRACKING FEATURES.... (BLOBS CON OPENCV ?)
if (!m_pBucketDiff2) {
if (m_debug_on) MODebug2->Message("moOpenFace::UpdateParameters > moBucket Object created to receive TextureFilters Data.");
m_pBucketDiff2 = new moBucket();
}
if (m_pBucketDiff2) {
///ATENCION!!! al hacer un BuildBucket, hay que hacer un EmptyBucket!!!
//pBucket->BuildBucket( pTexSample->m_VideoFormat.m_BufferSize, 0);
m_pBucketDiff2->BuildBucket( m_pDestDiff2Texture->GetWidth()*m_pDestDiff2Texture->GetHeight()*4, 0);
m_pDestDiff2Texture->GetBuffer( (void*) m_pBucketDiff2->GetBuffer(), GL_RGBA, GL_UNSIGNED_BYTE );
///asignamos el bucket al videosample
//pTexSample->m_pSampleBuffer = pBucket;
}
if (!m_pTrackerSystemData) {
if (m_debug_on) MODebug2->Message("moOpenFace::UpdateParameters > creating moTrackerSystemData() Object.");
m_pTrackerSystemData = new moTrackerSystemData();
if (m_pTrackerSystemData) {
if (m_debug_on) MODebug2->Message( "moOpenFace::UpdateParameters > moTrackerSystemData() Object OK.");
if (m_debug_on) MODebug2->Message( moText("moOpenFace::UpdateParameters > m_pDestDiff2Texture->GetWidth().")
+ IntToStr( m_pDestDiff2Texture->GetWidth() )
+ moText(" m_pDestDiff2Texture->GetHeight().")
+ IntToStr( m_pDestDiff2Texture->GetHeight() )
);
}
}