root / trunk / tbeta / Windows / addons / ofxNCore / src / Modules / ofxNCoreVision.cpp @ 145

View | Annotate | Download (26.7 KB)

1
/*
2
*  ofxNCoreVision.cpp
3
*  NUI Group Community Core Vision
4
*
5
*  Created by NUI Group Dev Team A on 2/1/09.
6
*  Copyright 2009 NUI Group\Inc. All rights reserved.
7
*
8
*/
9
10
#include "ofxNCoreVision.h"
11
#include "../Controls/gui.h"
12
13
/******************************************************************************
14
* The setup function is run once to perform initializations in the application
15
*****************************************************************************/
16
void ofxNCoreVision::_setup(ofEventArgs &e)
17
{
18
        //set the title
19
        ofSetWindowTitle(" Community Core Vision ");
20
21
//        ofSetBackgroundAuto(false);
22
23
        //create filter
24
        if ( filter == NULL ){filter = new ProcessFilters();}
25
26
        //Load Settings from config.xml file
27
        loadXMLSettings();
28
29
        //Setup Window Properties
30
        ofSetWindowShape(winWidth,winHeight);
31
        ofSetFrameRate(camRate * 1.4);                        //This will be based on camera fps in the future
32
        ofSetVerticalSync(false);                    //Set vertical sync to false for better performance?
33
34
        initDevice();
35
36
        /*****************************************************************************************************
37
        * Allocate images (needed for drawing/processing images)
38
        ******************************************************************************************************/
39
        processedImg.allocate(camWidth, camHeight); //main Image that'll be processed.
40
        processedImg.setUseTexture(false);
41
        sourceImg.allocate(camWidth, camHeight);    //Source Image
42
        sourceImg.setUseTexture(false);                                //We don't need to draw this so don't create a texture
43
        /******************************************************************************************************/
44
45
        //Fonts - Is there a way to dynamically change font size?
46
        verdana.loadFont("verdana.ttf", 8, true, true);           //Font used for small images
47
        sidebarTXT.loadFont("verdana.ttf", 8, true, true);
48
        bigvideo.loadFont("verdana.ttf", 13, true, true);  //Font used for big images.
49
50
        //Static Images
51
        background.loadImage("images/background.jpg"); //Main (Temp?) Background
52
53
        controls = ofxGui::Instance(this);
54
        setupControls();
55
56
        calib.setup(camWidth, camHeight, &tracker);
57
58
        filter->allocate( camWidth, camHeight );
59
60
        if (autoTracker)
61
        {
62
                printf("Starting in tracking mode...\n");
63
                showConfiguration = true;
64
        }
65
66
        if (bMiniMode)
67
        {
68
                bShowInterface = false;
69
                printf("Starting in Mini Mode...\n");
70
                ofSetWindowShape(190, 200); //minimized size
71
                //ofSetWindowTitle("Mini");
72
                filter->bMiniMode = bMiniMode;
73
        }
74
        else{
75
                //full mode
76
                bShowInterface = true;
77
        }
78
        printf("Community Core Vision is setup!\n\n");
79
}
80
81
/****************************************************************
82
*        Load/Save config.xml file Settings
83
****************************************************************/
84
void ofxNCoreVision::loadXMLSettings()
85
{
86
        // TODO: a seperate XML to map keyboard commands to action
87
        message = "Loading config.xml...";
88
        // Can this load via http?
89
        if ( XML.loadFile("config.xml"))
90
                message = "Settings Loaded!\n\n";
91
        else
92
                message = "No Settings Found...\n\n"; //FAIL
93
        
94
        //--------------------------------------------------------------
95
        //  START BINDING XML TO VARS
96
        //--------------------------------------------------------------
97
        winWidth                                        = XML.getValue("CONFIG:WINDOW:WIDTH", 950);
98
        winHeight                                        = XML.getValue("CONFIG:WINDOW:HEIGHT", 600);
99
        bcamera                                                = XML.getValue("CONFIG:CAMERA_0:USECAMERA", 1);
100
        deviceID                                        = XML.getValue("CONFIG:CAMERA_0:DEVICE", 0);
101
        camWidth                                        = XML.getValue("CONFIG:CAMERA_0:WIDTH", 320);
102
        camHeight                                        = XML.getValue("CONFIG:CAMERA_0:HEIGHT", 240);
103
        camRate                                                = XML.getValue("CONFIG:CAMERA_0:FRAMERATE", 0);
104
        videoFileName                                = XML.getValue("CONFIG:VIDEO:FILENAME", "RearDI.m4v");
105
        maxBlobs                                        = XML.getValue("CONFIG:BLOBS:MAXNUMBER", 20);
106
        bShowLabels                                        = XML.getValue("CONFIG:BOOLEAN:LABELS",0);
107
        bDrawOutlines                                = XML.getValue("CONFIG:BOOLEAN:OUTLINES",0);
108
        filter->bLearnBakground                = XML.getValue("CONFIG:BOOLEAN:LEARNBG",0);
109
        filter->bVerticalMirror                = XML.getValue("CONFIG:BOOLEAN:VMIRROR",0);
110
        filter->bHorizontalMirror        = XML.getValue("CONFIG:BOOLEAN:HMIRROR",0);
111
        //Filters
112
        filter->bHighpass                        = XML.getValue("CONFIG:BOOLEAN:HIGHPASS",1);
113
        filter->bAmplify                        = XML.getValue("CONFIG:BOOLEAN:AMPLIFY", 1);
114
        filter->bSmooth                                = XML.getValue("CONFIG:BOOLEAN:SMOOTH", 1);
115
        filter->bDynamicBG                        = XML.getValue("CONFIG:BOOLEAN:DYNAMICBG", 1);
116
        //MODES
117
        bGPUMode                                        = XML.getValue("CONFIG:BOOLEAN:GPU", 0);
118
        bMiniMode                   = XML.getValue("CONFIG:BOOLEAN:MINIMODE",0);
119
        //CONTROLS
120
        tracker.MIN_MOVEMENT_THRESHOLD        = XML.getValue("CONFIG:INT:MINMOVEMENT",0);
121
        MIN_BLOB_SIZE                                = XML.getValue("CONFIG:INT:MINBLOBSIZE",2);
122
        MAX_BLOB_SIZE                                = XML.getValue("CONFIG:INT:MAXBLOBSIZE",100);
123
        backgroundLearnRate                        = XML.getValue("CONFIG:INT:BGLEARNRATE", 0.01f);
124
        //Filter Settings
125
        filter->threshold                        = XML.getValue("CONFIG:INT:THRESHOLD",0);
126
        filter->highpassBlur                = XML.getValue("CONFIG:INT:HIGHPASSBLUR",0);
127
        filter->highpassNoise                = XML.getValue("CONFIG:INT:HIGHPASSNOISE",0);
128
        filter->highpassAmp                        = XML.getValue("CONFIG:INT:HIGHPASSAMP",0);
129
        filter->smooth                                = XML.getValue("CONFIG:INT:SMOOTH",0);
130
        //--------------------------------------------------- TODO XML NETWORK SETTINGS
131
        bTUIOMode                                        = XML.getValue("CONFIG:BOOLEAN:TUIO",0);
132
        myTUIO.bOSCMode                                = XML.getValue("CONFIG:BOOLEAN:OSCMODE",1);
133
        myTUIO.bTCPMode                                = XML.getValue("CONFIG:BOOLEAN:TCPMODE",1);
134
        myTUIO.bHeightWidth                        = XML.getValue("CONFIG:BOOLEAN:HEIGHTWIDTH",0);
135
        tmpLocalHost                                = XML.getValue("CONFIG:NETWORK:LOCALHOST", "localhost");
136
        tmpPort                                                = XML.getValue("CONFIG:NETWORK:TUIOPORT_OUT", 3333);
137
        myTUIO.setup(tmpLocalHost.c_str(), tmpPort); //have to convert tmpLocalHost to a const char*
138
        //--------------------------------------------------------------
139
        //  END XML SETUP
140
}
141
142
void ofxNCoreVision::saveSettings()
143
{
144
        XML.setValue("CONFIG:CAMERA_0:USECAMERA", bcamera);
145
        XML.setValue("CONFIG:CAMERA_0:DEVICE", deviceID);
146
        XML.setValue("CONFIG:CAMERA_0:WIDTH", camWidth);
147
        XML.setValue("CONFIG:CAMERA_0:HEIGHT", camHeight);
148
        XML.setValue("CONFIG:CAMERA_0:FRAMERATE", camRate);
149
        XML.setValue("CONFIG:BOOLEAN:PRESSURE",bShowPressure);
150
        XML.setValue("CONFIG:BOOLEAN:LABELS",bShowLabels);
151
        XML.setValue("CONFIG:BOOLEAN:OUTLINES",bDrawOutlines);
152
        XML.setValue("CONFIG:BOOLEAN:LEARNBG", filter->bLearnBakground);
153
        XML.setValue("CONFIG:BOOLEAN:VMIRROR", filter->bVerticalMirror);
154
        XML.setValue("CONFIG:BOOLEAN:HMIRROR", filter->bHorizontalMirror);
155
        XML.setValue("CONFIG:BOOLEAN:HIGHPASS", filter->bHighpass);
156
        XML.setValue("CONFIG:BOOLEAN:AMPLIFY", filter->bAmplify);
157
        XML.setValue("CONFIG:BOOLEAN:SMOOTH", filter->bSmooth);
158
        XML.setValue("CONFIG:BOOLEAN:DYNAMICBG", filter->bDynamicBG);
159
        XML.setValue("CONFIG:BOOLEAN:GPU", bGPUMode);
160
        XML.setValue("CONFIG:INT:MINMOVEMENT", tracker.MIN_MOVEMENT_THRESHOLD);
161
        XML.setValue("CONFIG:INT:MINBLOBSIZE", MIN_BLOB_SIZE);
162
        XML.setValue("CONFIG:INT:MAXBLOBSIZE", MAX_BLOB_SIZE);
163
        XML.setValue("CONFIG:INT:BGLEARNRATE", backgroundLearnRate);
164
        XML.setValue("CONFIG:INT:THRESHOLD", filter->threshold);
165
        XML.setValue("CONFIG:INT:HIGHPASSBLUR", filter->highpassBlur);
166
        XML.setValue("CONFIG:INT:HIGHPASSNOISE", filter->highpassNoise);
167
        XML.setValue("CONFIG:INT:HIGHPASSAMP", filter->highpassAmp);
168
        XML.setValue("CONFIG:INT:SMOOTH", filter->smooth);
169
        XML.setValue("CONFIG:BOOLEAN:MINIMODE", bMiniMode);
170
        XML.setValue("CONFIG:BOOLEAN:TUIO",bTUIOMode);
171
        XML.setValue("CONFIG:BOOLEAN:HEIGHTWIDTH", myTUIO.bHeightWidth);
172
        XML.setValue("CONFIG:BOOLEAN:OSCMODE", myTUIO.bOSCMode);
173
        XML.setValue("CONFIG:BOOLEAN:TCPMODE", myTUIO.bTCPMode);
174
175
        //        XML.setValue("CONFIG:NETWORK:LOCALHOST", *myTUIO.localHost);
176
        //        XML.setValue("CONFIG:NETWORK:TUIO_PORT_OUT",myTUIO.TUIOPort);
177
178
        XML.saveFile("config.xml");
179
}
180
181
/******************************************************************************
182
* The update function runs continuously. Use it to update states and variables
183
*****************************************************************************/
184
void ofxNCoreVision::_update(ofEventArgs &e)
185
{
186
        if(exited) return;
187
188
        bNewFrame = false;
189
190
        if (activeInput)
191
        {
192
                if (bcamera) //if camera
193
                {
194
                    #ifdef TARGET_WIN32
195
/*                                if(PS3!=NULL)//ps3 camera
196
                                {
197
                                        bNewFrame = PS3->isFrameNew();
198
                                }
199
                                else 
200
*/                                if(ffmv!=NULL)
201
                                 {
202
                                        ffmv->grabFrame();
203
                                         bNewFrame = true;
204
                                }
205
                                else if(vidGrabber !=NULL)
206
                                {
207
                                         vidGrabber->grabFrame();
208
                                         bNewFrame = vidGrabber->isFrameNew();
209
                                 }
210
                                else if(dsvl !=NULL)
211
                                {
212
                                         bNewFrame = dsvl->isFrameNew();
213
                                 }
214
                         #else
215
                vidGrabber->grabFrame();
216
                                 bNewFrame = vidGrabber->isFrameNew();
217
                         #endif
218
                }
219
                else //if video
220
                {
221
                        vidPlayer->idleMovie();
222
                        bNewFrame = vidPlayer->isFrameNew();
223
                }
224
225
                //if no new frame, return
226
                if(!bNewFrame){
227
                        return;
228
                }
229
                else//else process camera frame
230
                {
231
                        ofBackground(0, 0, 0);
232
233
                        // Calculate FPS of Camera
234
                        frames++;
235
                        float time = ofGetElapsedTimeMillis();
236
                        if (time > (lastFPSlog + 1000))
237
                        {
238
                                fps = frames;
239
                                frames = 0;
240
                                lastFPSlog = time;
241
                        }//End calculation
242
243
                        float beforeTime = ofGetElapsedTimeMillis();
244
245
                        if (bGPUMode)
246
                        {
247
                                grabFrameToGPU(filter->gpuSourceTex);
248
                                filter->applyGPUFilters();
249
                                contourFinder.findContours(filter->gpuReadBackImageGS,  (MIN_BLOB_SIZE * 2) + 1, ((camWidth * camHeight) * .4) * (MAX_BLOB_SIZE * .001), maxBlobs, false);
250
                        }
251
                        else
252
                        {
253
                                grabFrameToCPU();
254
                                filter->applyCPUFilters( processedImg );
255
                                contourFinder.findContours(processedImg,  (MIN_BLOB_SIZE * 2) + 1, ((camWidth * camHeight) * .4) * (MAX_BLOB_SIZE * .001), maxBlobs, false);
256
                        }
257
258
                        //Track found contours/blobss
259
                        tracker.track(&contourFinder);
260
                        //get DSP time
261
                        differenceTime = ofGetElapsedTimeMillis() - beforeTime;
262
263
                        //Dynamic Background subtraction LearRate
264
                        if (filter->bDynamicBG)
265
                        {
266
                                filter->fLearnRate = backgroundLearnRate * .0001; //If there are no blobs, add the background faster.
267
                                if (contourFinder.nBlobs > 0) //If there ARE blobs, add the background slower.
268
                                {
269
                                        filter->fLearnRate = backgroundLearnRate * .0001;
270
                                }
271
                        }//End Background Learning rate
272
273
                        if (bTUIOMode)
274
                        {
275
                                //Start sending OSC
276
                                myTUIO.sendTUIO();
277
                        }
278
                }
279
        }
280
}
281
282
/************************************************
283
*                                Init Device
284
************************************************/
285
//Init Device (camera/video)
286
void ofxNCoreVision::initDevice(){
287
288
        //Pick the Source - camera or video
289
        if (bcamera)
290
        {
291
                activeInput = true;
292
293
                //check if a firefly, ps3 camera, or other is plugged in
294
                #ifdef TARGET_WIN32
295
                        /****PS3 - PS3 camera only****/
296
/*                        if(ofxPS3::getDeviceCount() > 0){
297
                                PS3 = new ofxPS3();
298
                                PS3->listDevices();
299
                                PS3->initPS3(camWidth, camHeight, camRate);
300
                                printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, PS3->getCamWidth(), PS3->getCamHeight());
301
                        }
302
                        /****ffmv - firefly camera only****/
303
//                        else
304
                        if(ffmv->getDeviceCount() > 0 && ffmv == NULL){
305
                           ffmv = new ofxffmv();
306
                           ffmv->listDevices();
307
                           ffmv->initFFMV(camWidth,camHeight);
308
                           printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, ffmv->getCamWidth(), ffmv->getCamHeight());
309
                           camWidth = ffmv->getCamWidth();
310
                           camHeight = ffmv->getCamHeight();
311
                        }
312
                        else if( vidGrabber == NULL ) {
313
                                vidGrabber = new ofVideoGrabber();
314
                                vidGrabber->listDevices();
315
                                vidGrabber->setVerbose(true);
316
                                vidGrabber->initGrabber(camWidth,camHeight);
317
                                printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, vidGrabber->width, vidGrabber->height);
318
                                camWidth = vidGrabber->width;
319
                                camHeight = vidGrabber->height;
320
                        }
321
                        else if( dsvl == NULL) {
322
                                dsvl = new ofxDSVL();
323
                                dsvl->initDSVL();
324
                                printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, dsvl->getCamWidth(), dsvl->getCamHeight());
325
                                camWidth = dsvl->getCamWidth();
326
                                camHeight = dsvl->getCamHeight();
327
                        }
328
                #else
329
                if( vidGrabber == NULL ) {
330
            vidGrabber = new ofVideoGrabber();
331
                        vidGrabber->listDevices();
332
            vidGrabber->setVerbose(true);
333
            vidGrabber->initGrabber(camWidth,camHeight);
334
                        int grabW = vidGrabber->width;
335
                        int grabH = vidGrabber->height;
336
                        printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, grabW, grabH);
337
        }
338
                #endif
339
        }else{
340
                if( vidPlayer == NULL ) {
341
                        activeInput = true;
342
            vidPlayer = new ofVideoPlayer();
343
            vidPlayer->loadMovie( videoFileName );
344
            vidPlayer->play();
345
            vidPlayer->setLoopState(OF_LOOP_NORMAL);
346
                        printf("Video Mode\n\n");
347
                        camHeight = vidPlayer->height;
348
                        camWidth = vidPlayer->width;
349
        }
350
        }
351
}
352
353
//get pixels from camera
354
void ofxNCoreVision::getPixels(){
355
356
/*        if(PS3!=NULL){
357
                sourceImg.setFromPixels(PS3->getPixels(), camWidth, camHeight);
358
                //convert to grayscale
359
                processedImg = sourceImg;
360
        }
361
        else 
362
*/        if(ffmv != NULL){
363
                processedImg.setFromPixels(ffmv->fcImage[ffmv->getDeviceID()].pData, camWidth, camHeight);
364
        }
365
        else if(vidGrabber != NULL ) {
366
                sourceImg.setFromPixels(vidGrabber->getPixels(), camWidth, camHeight);
367
                //convert to grayscale
368
                processedImg = sourceImg;
369
        }
370
        if(dsvl!=NULL)
371
        {
372
                //sourceImg.setFromPixels(vidGrabber->getPixels(), camWidth, camHeight);
373
                sourceImg.setFromPixels(dsvl->getPixels(), camWidth, camHeight);
374
                //convert to grayscale
375
                processedImg = sourceImg;
376
        }
377
}
378
379
380
//Grab frame from CPU
381
void ofxNCoreVision::grabFrameToCPU()
382
{
383
        //Set sourceImg as new camera/video frame
384
        if (bcamera)
385
        {
386
            #ifdef TARGET_WIN32
387
                        getPixels();
388
                 #else
389
            sourceImg.setFromPixels(vidGrabber->getPixels(), camWidth, camHeight);
390
                         //convert to grayscale
391
                         processedImg = sourceImg;
392
                 #endif
393
        }
394
        else
395
        {
396
                sourceImg.setFromPixels(vidPlayer->getPixels(), camWidth, camHeight);
397
                //convert to grayscale
398
                processedImg = sourceImg;
399
        }
400
}
401
402
//Grab frame from GPU
403
void ofxNCoreVision::grabFrameToGPU(GLuint target)
404
{
405
        //grab the frame to a raw openGL texture
406
        if (bcamera)
407
        {
408
                glEnable(GL_TEXTURE_2D);
409
                //glPixelStorei(1);
410
                glBindTexture(GL_TEXTURE_2D, target);
411
412
                #ifdef TARGET_WIN32
413
/*                        if(PS3!=NULL)
414
                        {
415
                                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, PS3->getPixels());
416
                        }
417
                        else 
418
*/                        if(vidGrabber!=NULL)
419
                        {
420
                                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidGrabber->getPixels());
421
                        }
422
                        else if(dsvl!=NULL)
423
                        {
424
                                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, dsvl->getPixels());
425
                        }
426
                #else
427
            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidGrabber->getPixels());
428
                #endif
429
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
430
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
431
                glBindTexture(GL_TEXTURE_2D,0);
432
        }
433
        else
434
        {
435
                glEnable(GL_TEXTURE_2D);
436
                glBindTexture(GL_TEXTURE_2D, target);
437
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidPlayer->getPixels());
438
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
439
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
440
                glBindTexture(GL_TEXTURE_2D,0);
441
        }
442
}
443
444
445
/******************************************************************************
446
* The draw function paints the textures onto the screen. It runs after update.
447
*****************************************************************************/
448
void ofxNCoreVision::_draw(ofEventArgs &e)
449
{
450
        if(exited) return;
451
452
        if (showConfiguration) 
453
        {
454
                //if calibration
455
                if (bCalibration)
456
                {
457
                        //Don't draw main interface
458
                        calib.passInContourFinder(contourFinder.nBlobs, contourFinder.blobs);
459
                        calib.doCalibration();
460
                }
461
                //if mini mode
462
                else if (bMiniMode)
463
                {
464
                        drawMiniMode();
465
                }
466
                //if full mode
467
                else if (bShowInterface)
468
                {
469
                        drawFullMode();
470
                        if(bDrawOutlines || bShowLabels) drawFingerOutlines();
471
                }
472
                //draw gui controls
473
                if (!bCalibration && !bMiniMode) {controls->draw();}
474
        }        
475
}
476
477
void ofxNCoreVision::drawFullMode(){
478
479
        ofSetColor(0xFFFFFF);
480
        //Draw Background Image
481
        background.draw(0, 0);
482
        //Draw arrows
483
        ofSetColor(187, 200, 203);
484
        ofFill();
485
        ofTriangle(680, 420, 680, 460, 700, 440);
486
        ofTriangle(70, 420, 70, 460, 50, 440);
487
        ofSetColor(255, 255, 0);
488
489
        ofSetColor(0xFFFFFF);
490
        //Draw Image Filters To Screen
491
        if (bGPUMode) filter->drawGPU();
492
        else filter->draw();
493
494
        ofSetColor(0x000000);
495
        if (bShowPressure)
496
        {
497
                bigvideo.drawString("Pressure Map", 140, 20);
498
        }
499
        else
500
        {
501
                bigvideo.drawString("Source Image", 140, 20);
502
        }
503
        bigvideo.drawString("Tracked Image", 475, 20);
504
505
        //draw link to tbeta website
506
        ofSetColor(79, 79, 79);
507
        ofFill();
508
        ofRect(ofGetWidth() - 228,ofGetHeight() - 14, 228, 14);
509
        ofSetColor(0xFFFFFF);
510
        ofDrawBitmapString("|  ~  |tbeta.nuigroup.com", ofGetWidth() - 228, ofGetHeight() - 2);
511
512
        //Display Application information in bottom right
513
        string str = "Calc. Time [ms]: ";
514
        str+= ofToString(differenceTime, 0)+"\n\n";
515
516
        if (bcamera)
517
        {
518
                string str2 = "Camera [Res]:     ";
519
        str2+= ofToString(camWidth, 0) + " x " + ofToString(camWidth, 0)  + "\n";
520
                string str4 = "Camera [fps]:     ";
521
                str4+= ofToString(fps, 0)+"\n";
522
                ofSetColor(0xFFFFFF);
523
                sidebarTXT.drawString(str + str2 + str4, 740, 410);
524
        }
525
        else
526
        {
527
                string str2 = "Video [Res]:      ";
528
                str2+= ofToString(vidPlayer->width, 0) + " x " + ofToString(vidPlayer->height, 0)  + "\n";
529
                string str4 = "Video [fps]:       ";
530
                str4+= ofToString(fps, 0)+"\n";
531
                ofSetColor(0xFFFFFF);
532
                sidebarTXT.drawString(str + str2 + str4, 740, 410);
533
        }
534
535
        if (bTUIOMode)
536
        {
537
                //Draw Port and IP to screen
538
                ofSetColor(0xffffff);
539
                char buf[256];
540
                sprintf(buf, "Sending TUIO messages to:\nHost: %s\nPort: %i", myTUIO.localHost, myTUIO.TUIOPort);
541
                sidebarTXT.drawString(buf, 740, 480);
542
        }
543
544
        ofSetColor(0xFF0000);
545
        sidebarTXT.drawString("Press spacebar to toggle mini mode", ofGetWidth() - 215, 580);
546
}
547
548
void ofxNCoreVision::drawMiniMode()
549
{
550
        //black background
551
        ofSetColor(0,0,0);
552
        ofRect(0,0,ofGetWidth(), ofGetHeight());
553
        //draw outlines
554
        if (bDrawOutlines){
555
                for (int i=0; i<contourFinder.nBlobs; i++)
556
                {
557
                        contourFinder.blobs[i].drawContours(0,0, camWidth, camHeight+175, ofGetWidth(), ofGetHeight());
558
                }
559
        }
560
561
        //draw grey rectagles for text information
562
        ofSetColor(128,128,128);
563
        ofFill();
564
        ofRect(0,ofGetHeight() - 83, ofGetWidth(), 20);
565
        ofRect(0,ofGetHeight() - 62, ofGetWidth(), 20);
566
        ofRect(0,ofGetHeight() - 41, ofGetWidth(), 20);
567
        ofRect(0,ofGetHeight() - 20, ofGetWidth(), 20);
568
569
        //draw text
570
        ofSetColor(250,250,250);
571
        sidebarTXT.drawString("Calc. Time  [ms]:        " + ofToString(differenceTime,0),10, ofGetHeight() - 70 );
572
        if (bcamera){
573
                sidebarTXT.drawString("Camera [fps]:            " + ofToString(fps,0),10, ofGetHeight() - 50 );
574
        }
575
        else {
576
                sidebarTXT.drawString("Video [fps]:             " + ofToString(fps,0),10, ofGetHeight() - 50 );
577
        }
578
        sidebarTXT.drawString("Blob Count:               " + ofToString(contourFinder.nBlobs,0),10, ofGetHeight() - 29 );
579
        sidebarTXT.drawString("Sending TUIO:  " ,10, ofGetHeight() - 9 );
580
581
        //draw green tuio circle
582
        if (bTUIOMode)
583
        {
584
                //Draw GREEN CIRCLE 'ON' LIGHT
585
                ofSetColor(0x00FF00);
586
                ofFill();
587
                ofCircle(ofGetWidth() - 17 , ofGetHeight() - 10, 5);
588
                ofNoFill();
589
        }
590
}
591
592
void ofxNCoreVision::drawFingerOutlines()
593
{
594
        //Find the blobs for drawing
595
        for (int i=0; i<contourFinder.nBlobs; i++)
596
        {
597
                if (bDrawOutlines)
598
                {
599
                        //Draw contours (outlines) on the source image
600
                        contourFinder.blobs[i].drawContours(40, 30, camWidth, camHeight, MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
601
                }
602
                if (bShowLabels) //Show ID label;
603
                {
604
                        float xpos = contourFinder.blobs[i].centroid.x * (MAIN_WINDOW_WIDTH/camWidth);
605
                        float ypos = contourFinder.blobs[i].centroid.y * (MAIN_WINDOW_HEIGHT/camHeight);
606
607
                        ofSetColor(0xCCFFCC);
608
                        char idStr[1024];
609
                        sprintf(idStr, "id: %i", contourFinder.blobs[i].id);
610
                        verdana.drawString(idStr, xpos + 365, ypos + contourFinder.blobs[i].boundingRect.height/2 + 45);
611
                }
612
        }
613
        ofSetColor(0xFFFFFF);
614
}
615
616
/*****************************************************************************
617
* KEY EVENTS
618
*****************************************************************************/
619
void ofxNCoreVision::_keyPressed(ofKeyEventArgs &e)
620
{
621
        // detect escape key
622
        if(e.key==0x1b)
623
        {
624
                exited=true;
625
        }
626
627
        if (showConfiguration)
628
        {
629
                switch (e.key)
630
                {
631
                        // ughhh!!!!
632
                        // my main problem is that the slider doesn't move....
633
                case 'a':
634
                        filter->threshold++;
635
                        controls->update(appPtr->trackedPanel_threshold, kofxGui_Set_Int, &appPtr->filter->threshold, sizeof(int));
636
                        break;
637
                case 'z':
638
                        filter->threshold--;
639
                        controls->update(appPtr->trackedPanel_threshold, kofxGui_Set_Int, &appPtr->filter->threshold, sizeof(int));
640
                        break;
641
                case 'b':
642
                        filter->bLearnBakground = true;
643
                        break;
644
                case 'o':
645
                        bDrawOutlines ? bDrawOutlines = false : bDrawOutlines = true;
646
                        controls->update(appPtr->trackedPanel_outlines, kofxGui_Set_Bool, &appPtr->bDrawOutlines, sizeof(bool));
647
                        break;
648
                case 'h':
649
                        filter->bHorizontalMirror ? filter->bHorizontalMirror = false : filter->bHorizontalMirror = true;
650
                        controls->update(appPtr->propertiesPanel_flipH, kofxGui_Set_Bool, &appPtr->filter->bHorizontalMirror, sizeof(bool));
651
                        break;
652
                case 'j':
653
                        filter->bVerticalMirror ? filter->bVerticalMirror = false : filter->bVerticalMirror = true;
654
                        controls->update(appPtr->propertiesPanel_flipV, kofxGui_Set_Bool, &appPtr->filter->bVerticalMirror, sizeof(bool));
655
                        break;
656
                case 't':
657
                        myTUIO.bOSCMode = !myTUIO.bOSCMode;
658
                        myTUIO.bTCPMode = false;
659
                        bTUIOMode = myTUIO.bOSCMode;
660
                        controls->update(appPtr->optionPanel_tuio_tcp, kofxGui_Set_Bool, &appPtr->myTUIO.bTCPMode, sizeof(bool));
661
                        controls->update(appPtr->optionPanel_tuio_osc, kofxGui_Set_Bool, &appPtr->myTUIO.bOSCMode, sizeof(bool));
662
                        //clear blobs
663
                        myTUIO.blobs.clear();
664
                        break;
665
                case 'f':
666
                        myTUIO.bOSCMode = false;
667
                        myTUIO.bTCPMode = !myTUIO.bTCPMode;
668
                        bTUIOMode = myTUIO.bTCPMode;
669
                        controls->update(appPtr->optionPanel_tuio_tcp, kofxGui_Set_Bool, &appPtr->myTUIO.bTCPMode, sizeof(bool));
670
                        controls->update(appPtr->optionPanel_tuio_osc, kofxGui_Set_Bool, &appPtr->myTUIO.bOSCMode, sizeof(bool));
671
                        //clear blobs
672
                        myTUIO.blobs.clear();
673
                        break;
674
                case 'g':
675
                        bGPUMode ? bGPUMode = false : bGPUMode = true;
676
                        controls->update(appPtr->gpuPanel_use, kofxGui_Set_Bool, &appPtr->bGPUMode, sizeof(bool));
677
                        filter->bLearnBakground = true;
678
                        break;
679
                case 'v':
680
                        if (bcamera)
681
                                vidGrabber->videoSettings();
682
                        break;
683
                case 'l':
684
                        bShowLabels ? bShowLabels = false : bShowLabels = true;
685
                        controls->update(appPtr->trackedPanel_ids, kofxGui_Set_Bool, &appPtr->bShowLabels, sizeof(bool));
686
                        break;
687
                case 'p':
688
                        bShowPressure ? bShowPressure = false : bShowPressure = true;
689
                        break;
690
                case ' ':
691
                        if (bMiniMode && !bCalibration) // NEED TO ADD HERE ONLY GO MINI MODE IF NOT CALIBRATING
692
                        {
693
                                bMiniMode = false;
694
                                bShowInterface = true;
695
                                filter->bMiniMode = bMiniMode;
696
                                ofSetWindowShape(950,600); //default size
697
                        }
698
                        else if(!bCalibration)
699
                        {
700
                                bMiniMode = true;
701
                                bShowInterface = false;
702
                                filter->bMiniMode = bMiniMode;
703
                                ofSetWindowShape(190,200); //minimized size
704
                        }
705
                        break;
706
                case 'x': //Exit Calibrating
707
                        if (bCalibration)
708
                        {        bShowInterface = true;
709
                        bCalibration = false;
710
                        calib.calibrating = false;
711
                        tracker.isCalibrating = false;
712
                        if (bFullscreen == true) ofToggleFullscreen();
713
                        bFullscreen = false;
714
//                        ofSetBackgroundAuto(false);
715
                        }
716
                        break;
717
                }
718
        }
719
}
720
721
void ofxNCoreVision::_keyReleased(ofKeyEventArgs &e)
722
{
723
        if (showConfiguration)
724
        {
725
                if ( e.key == 'c' && !bCalibration)
726
                {
727
                        bShowInterface = false;
728
                        //Enter/Exit Calibration
729
                        bCalibration = true;
730
                        calib.calibrating = true;
731
                        tracker.isCalibrating = true;
732
                        if (bFullscreen == false) ofToggleFullscreen();
733
                        bFullscreen = true;
734
//                        ofSetBackgroundAuto(true);
735
                }
736
        }
737
        if ( e.key == '~' || e.key == '`' /*&& !bMiniMode*/ && !bCalibration) showConfiguration = !showConfiguration;
738
}
739
740
/*****************************************************************************
741
*        Touch EVENTS
742
*****************************************************************************/
743
void ofxNCoreVision::TouchDown( Blob b)
744
{
745
        if (bTUIOMode)//If sending TUIO, add the blob to the map list
746
        {
747
                //if blob is not otuside calibration mesh
748
                if (b.centroid.x != 0 && b.centroid.y != 0)
749
                        myTUIO.blobs[b.id] = b;
750
        }
751
}
752
753
void ofxNCoreVision::TouchUp( Blob b)
754
{
755
        if (bTUIOMode)//If sending TUIO delete Blobs from map list
756
        {
757
                std::map<int, Blob>::iterator iter;
758
                for (iter = myTUIO.blobs.begin(); iter != myTUIO.blobs.end(); iter++)
759
                {
760
                        if (iter->second.id == b.id)
761
                        {
762
                                myTUIO.blobs.erase(iter);
763
                                break;
764
                        }
765
                }
766
        }
767
}
768
769
void ofxNCoreVision::TouchMoved( Blob b)
770
{
771
        if (bTUIOMode)//If sending TUIO, add the blob to the map list
772
        {
773
                //if blob is not otuside calibration mesh
774
                if (b.centroid.x != 0 && b.centroid.y != 0)
775
                        myTUIO.blobs[b.id] = b;
776
        }
777
}
778
779
/*****************************************************************************
780
*        MOUSE EVENTS
781
*****************************************************************************/
782
void ofxNCoreVision::_mouseDragged(ofMouseEventArgs &e)
783
{
784
        if (showConfiguration)
785
                controls->mouseDragged(e.x, e.y, e.button); //guilistener
786
}
787
788
void ofxNCoreVision::_mousePressed(ofMouseEventArgs &e)
789
{
790
        if (showConfiguration)
791
        {
792
                controls->mousePressed(e.x, e.y, e.button); //guilistener
793
                if (e.x > ofGetWidth() - 230 && e.y > ofGetHeight() - 14){ofLaunchBrowser("http://www.openframeworks.cc/forum/viewtopic.php?p=9651#9651");}
794
        }
795
}
796
797
void ofxNCoreVision::_mouseReleased(ofMouseEventArgs &e)
798
{
799
        if (showConfiguration)
800
                controls->mouseReleased(e.x, e.y, 0); //guilistener
801
}
802
803
/*****************************************************************************
804
* Getters
805
*****************************************************************************/
806
807
std::vector<pair<int,Blob>> ofxNCoreVision::getBlobs(){
808
809
        return tracker.getTrackedBlobs();
810
}
811
812
/*****************************************************************************
813
* ON EXIT
814
*****************************************************************************/
815
void ofxNCoreVision::_exit(ofEventArgs &e)
816
{
817
    #ifdef TARGET_WIN32
818
//        if(PS3!=NULL) delete PS3;
819
        if(ffmv!=NULL) delete ffmv;
820
        if(dsvl!=NULL) delete dsvl;        
821
        #endif
822
823
        if(vidGrabber!=NULL) delete vidGrabber;
824
        if(vidPlayer !=NULL) delete vidGrabber;
825
        // -------------------------------- SAVE STATE ON EXIT
826
        saveSettings();
827
828
        printf("Vision module has exited!\n");
829
}
830