-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.cpp
595 lines (523 loc) · 16.9 KB
/
main.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
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <GL/gl.h>
#include <GL/freeglut.h>
#include <iostream>
#include <stdio.h>
/** Constants */
const int minFaceSize = 80; // in pixel. The smaller it is, the further away you can go
const float f = 500; //804.71
const float eyesGap = 6.5; //cm
const float pixelNbrPerCm = 50.0;
const float far = 60.0;
const float near = 1.0;
/** Global variables */
//-- capture opencv
const cv::String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
cv::CascadeClassifier face_cascade;
cv::VideoCapture *capture = NULL;
cv::Mat frame;
//-- display
bool bPause = false; //- press ' ' to change
bool bFullScreen = true; //- press 'f' to change
bool bInvertCam = false; //- press 'i' to change
bool bDisplayCam = true; //- press 'c' to change
bool bDisplayDetection = true; //- press 'd' to change
bool bDisplayBox = true; //- press 'b' to change
bool bPolygonMode = false; //- press 'm' to change
bool bProjectionMode = true; //- press 'p' to change
float camRatio = 0.3; //- press '+/-' to change
//-- dimensions
int windowWidth;
int windowHeight;
float cx;
float cy;
int camWidth;
int camHeight;
//-- opengl camera
GLdouble glCamX;
GLdouble glCamY;
GLdouble glCamZ;
/** Functions */
void redisplay();
cv::Mat detectEyes(cv::Mat image);
void displayCam(cv::Mat camImage);
void setGlCamera();
void draw3dScene();
void drawFrame(float z);
void drawLineToInf(float x, float y, float z);
void drawAxes(float length);
float pixelToCm(int size);
void onReshape( int w, int h );
void onMouse( int button, int state, int x, int y );
void onKeyboard( unsigned char key, int x, int y );
void onIdle();
/**
* @function main
*/
int main( int argc, char **argv )
{
// OPENCV INIT
//-- Load the cascades
if( !face_cascade.load( face_cascade_name ) )
{
fprintf(stderr,"-- (!) ERROR loading 'haarcascade_frontalface_alt.xml'\n");
fprintf(stderr,"Please edit 'face_cascade_name' path in main.cpp:22 and recompile the project.\n");
return -1;
};
// VIDEO CAPTURE
//-- start video capture from camera
capture = new cv::VideoCapture(0);
//-- check that video is working
if ( capture == NULL || !capture->isOpened() ) {
fprintf( stderr, "Could not start video capture\n" );
return 1;
}
// CAMERA IMAGE DIMENSIONS
camWidth = (int) capture->get( CV_CAP_PROP_FRAME_WIDTH );
camHeight = (int) capture->get( CV_CAP_PROP_FRAME_HEIGHT );
// GLUT INIT
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
if(!bFullScreen){
windowWidth = camWidth*1.5;
windowHeight = camHeight*1.5;
cx = pixelToCm(windowWidth);
cy = pixelToCm(windowHeight);
glutInitWindowPosition( 200, 80 );
glutInitWindowSize( windowWidth, windowHeight );
}
glutCreateWindow( "ScreenReality - Vision 3D" );
// RENDERING INIT
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
// SCREEN DIMENSIONS
if(bFullScreen)
{
glutFullScreen();
windowWidth = glutGet(GLUT_SCREEN_WIDTH);
windowHeight = glutGet(GLUT_SCREEN_HEIGHT);
cx = pixelToCm(windowWidth);
cy = pixelToCm(windowHeight);
glViewport( 0, 0, windowWidth, windowHeight );
}
// GUI CALLBACK FUNCTIONS
glutDisplayFunc( redisplay );
glutReshapeFunc( onReshape );
glutMouseFunc( onMouse );
glutKeyboardFunc( onKeyboard );
glutIdleFunc( onIdle );
// GUI LOOP
glutMainLoop();
return 0;
}
/**
* @function redisplay
* (Called at each openGL step)
* - Processes the webcam frame to detect the eyes with OpenCV,
* - Creates a 3D scene with OpenGL,
* - Render the scene and the webcam image.
*/
void redisplay()
{
if(frame.empty()) return;
if(!bPause)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// OPENCV
//-- flip frame image
cv::Mat tempimage;
if(bInvertCam) cv::flip(frame, tempimage, 0);
else cv::flip(frame, tempimage, 1);
//-- detect eyes
tempimage = detectEyes(tempimage);
// OPENGL
//-- scene
setGlCamera();
draw3dScene();
//-- cam
if(bDisplayCam) displayCam(tempimage);
// RENDER
glutSwapBuffers();
}
//-- post the next redisplay
glutPostRedisplay();
}
/**
* @function detectEyes
* - Uses OpenCV to detect face
* - Interpolate eyes position on image
* - Computes eyes position in space
* - Add some display for the detection
*/
cv::Mat detectEyes(cv::Mat image)
{
// INIT
std::vector<cv::Rect> faces;
cv::Mat image_gray;
cv::cvtColor( image, image_gray, CV_BGR2GRAY );
cv::equalizeHist( image_gray, image_gray );
// DETECT FACE
//-- Find bigger face (opencv documentation)
face_cascade.detectMultiScale( image_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(minFaceSize, minFaceSize) );
for( size_t i = 0; i < faces.size(); i++ )
{
// DETECT EYES
//-- points in pixel
cv::Point leftEyePt( faces[i].x + faces[i].width*0.30, faces[i].y + faces[i].height*0.37 );
cv::Point rightEyePt( faces[i].x + faces[i].width*0.70, faces[i].y + faces[i].height*0.37 );
cv::Point eyeCenterPt( faces[i].x + faces[i].width*0.5, leftEyePt.y );
//-- normalize with webcam internal parameters
GLdouble normRightEye = (rightEyePt.x - camWidth/2)/f;
GLdouble normLeftEye = (leftEyePt.x - camWidth/2)/f;
GLdouble normCenterX = (eyeCenterPt.x - camWidth/2)/f;
GLdouble normCenterY = (eyeCenterPt.y - camHeight/2)/f;
//-- get space coordinates
float tempZ = eyesGap/(normRightEye-normLeftEye);
float tempX = normCenterX*glCamZ;
float tempY = -normCenterY*glCamZ;
//-- update cam coordinates (smoothing)
glCamZ = (glCamZ*0.5) + (tempZ*0.5);
glCamX = (glCamX*0.5) + (tempX*0.5);
glCamY = (glCamY*0.5) + (tempY*0.5);
// DISPLAY
if(bDisplayCam && bDisplayDetection)
{
//-- face rectangle
cv::rectangle(image, faces[i], 1234);
//-- face lines
cv::Point leftPt( faces[i].x, faces[i].y + faces[i].height*0.37 );
cv::Point rightPt( faces[i].x + faces[i].width, faces[i].y + faces[i].height*0.37 );
cv::Point topPt( faces[i].x + faces[i].width*0.5, faces[i].y);
cv::Point bottomPt( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height);
cv::line(image, leftPt, rightPt, cv::Scalar( 0, 0, 0 ), 1, 1, 0);
cv::line(image, topPt, bottomPt, cv::Scalar( 0, 0, 0 ), 1, 1, 0);
//-- eyes circles
cv::circle(image, rightEyePt, 0.06*faces[i].width, cv::Scalar( 255, 255, 255 ), 1, 8, 0);
cv::circle(image, leftEyePt, 0.06*faces[i].width, cv::Scalar( 255, 255, 255 ), 1, 8, 0);
//-- eyes line & center
cv::line(image, leftEyePt, rightEyePt, cv::Scalar( 0, 0, 255 ), 1, 1, 0);
cv::circle(image, eyeCenterPt, 2, cv::Scalar( 0, 0, 255 ), 3, 1, 0);
}
}
return image;
}
/**
* @function setGlCamera
* Set OpenGL camera parameters.
* The off-axis projection is what gives
* the feeling of augmented reality.
*/
void setGlCamera()
{
if(bProjectionMode)
{
/* SKEWED FRUSTRUM / OFF-AXIS PROJECTION
** My implementation is based on the following paper:
** Name: Generalized Perspective Projection
** Author: Robert Kooima
** Date: August 2008, revised June 2009
*/
//-- space corners coordinates
float pa[3]={-cx,-cy,0};
float pb[3]={cx,-cy,0};
float pc[3]={-cx,cy,0};
float pe[3]={glCamX,glCamY,glCamZ};
//-- space points
cv::Vec3f Pa(pa);
cv::Vec3f Pb(pb);
cv::Vec3f Pc(pc);
cv::Vec3f Pe(pe);
//-- Compute an orthonormal basis for the screen.
cv::Vec3f Vr = Pb-Pa;
Vr /= cv::norm(Vr);
cv::Vec3f Vu = Pc-Pa;
Vu /= cv::norm(Vu);
cv::Vec3f Vn = Vr.cross(Vu);
Vn /= cv::norm(Vn);
//-- Compute the screen corner vectors.
cv::Vec3f Va = Pa-Pe;
cv::Vec3f Vb = Pb-Pe;
cv::Vec3f Vc = Pc-Pe;
//-- Find the distance from the eye to screen plane.
float d = -Va.dot(Vn);
//-- Find the extent of the perpendicular projection.
float l = Va.dot(Vr) * near / d;
float r = Vr.dot(Vb) * near / d;
float b = Vu.dot(Va) * near / d;
float t = Vu.dot(Vc) * near / d;
//-- Load the perpendicular projection.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(l, r, b, t, near, far+d);
//-- Rotate the projection to be non-perpendicular.
float M[16];
memset(M, 0, 16 * sizeof (float));
M[0] = Vr[0]; M[1] = Vu[0]; M[2] = Vn[0];
M[4] = Vr[1]; M[5] = Vu[1]; M[6] = Vn[1];
M[8] = Vr[2]; M[9] = Vu[2]; M[10] = Vn[2];
M[15] = 1.0f;
glMultMatrixf(M);
//-- Move the apex of the frustum to the origin.
glTranslatef(-pe[0], -pe[1], -pe[2]);
//-- Reset modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
else
{
//-- intrinsic camera params
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)windowWidth/(float)windowHeight, 1, 250);
//-- extrinsic camera params
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(glCamX, glCamY, glCamZ, 0, 0, 0, 0, 1, 0);
}
}
/**
* @function draw3dScene
* Draws OpenGL 3D scene
*/
void draw3dScene()
{
// DISPLAY MODE
if(bPolygonMode){
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawAxes(10.0);
}else{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
// BOUNDING BOX
if(bDisplayBox){
for(int i = 0; i <= 10; i++){
float j = (float)i/10.0;
//-- lines
drawLineToInf((2*j*cx)-cx, cy, 0.0);//top lines
drawLineToInf(cx, cy-(2*j*cy), 0.0);//right lines
drawLineToInf(cx-(2*j*cx), -cy, 0.0);//bottom lines
drawLineToInf(-cx, (2*j*cy)-cy, 0.0);//left lines
//-- frames
glColor3f(1.0-j,1.0-j,1.0-j);
if((i%2) == 0) drawFrame(-j*far);
}
}
// LIGHTING
glEnable(GL_LIGHTING);
//-- Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//-- Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//-- Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f}; //Positioned at (-1, 0.5, 0.5)
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
// GEOMETRY
//-- TeaPot
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(-1, -3, -10);
glutSolidTeapot(5);
glTranslatef(1, 3, 10);
drawLineToInf( -1, -3, -10);
//-- Cube 1
glColor3f(1.0f, 1.0f, 0.0f);
glTranslatef(-10, -5, 30);
glutSolidCube(3);
glTranslatef(10, 5, -30);
drawLineToInf(-10, -5, 30);
//-- Cube 2
glColor3f(1.0f, 0.0f, 1.0f);
glTranslatef(-20, 0, -40);
glutSolidCube(3);
glTranslatef(20, 0, 40);
drawLineToInf(-20, 0, -40);
//-- Cube 3
glColor3f(0.0f, 1.0f, 1.0f);
glTranslatef(5, 5, 10);
glutSolidCube(3);
glTranslatef(-5, -5, -10);
drawLineToInf(5, 5, 10);
glDisable(GL_LIGHTING); //Disable lighting
}
/**
* @function displayCam
* Draws the webcam image in window + detection info
*/
void displayCam(cv::Mat camImage)
{
//-- Save matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
//-- Display Coordinates
if(bDisplayDetection)
{
//-- Coord text
std::stringstream sstm;
sstm << "(x,y,z) = (" << (int)glCamX << "," << (int)glCamY << "," << (int)glCamZ << ")";
std::string s = sstm.str();
//std::cout<<s<<std::endl;
//-- Display text
glColor3f(1.0, 1.0, 1.0);
glRasterPos2i(10, windowHeight-(camRatio*camImage.size().height)-20);
void * font = GLUT_BITMAP_9_BY_15;
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
glutBitmapCharacter(font, c);
}
}
//-- Display image
glRasterPos2i(0, windowHeight-(camRatio*camImage.size().height));
cv::flip(camImage, camImage, 0);
cv::resize(camImage, camImage, cv::Size(camRatio*camWidth, camRatio*camHeight), 0, 0, cv::INTER_CUBIC);
glDrawPixels( camImage.size().width, camImage.size().height, GL_BGR, GL_UNSIGNED_BYTE, camImage.ptr() );
//-- Load matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
/**
* @function drawFrame
* Draws lines between the corners of what we consider to be the screen
*/
void drawFrame(float z)
{
glBegin(GL_LINE_LOOP);
glVertex3f(cx, cy, z);
glVertex3f(cx, -cy, z);
glVertex3f(-cx, -cy, z);
glVertex3f(-cx, cy, z);
glEnd();
}
/**
* @function drawLineToInf
* Draws line from the (x,y,z) coordinate given to (x,y,far)
*/
void drawLineToInf(float x, float y, float z)
{
glBegin(GL_LINES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(x, y, z);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(x, y, -far);
glEnd();
}
/**
* @function drawAxes
** Display the coordinate system
*/
void drawAxes(float length)
{
glBegin(GL_LINES) ;
glColor3f(1,0,0) ;
glVertex3f(0,0,0) ;
glVertex3f(length,0,0);
glColor3f(0,1,0) ;
glVertex3f(0,0,0) ;
glVertex3f(0,length,0);
glColor3f(0,0,1) ;
glVertex3f(0,0,0) ;
glVertex3f(0,0,length);
glEnd() ;
}
float pixelToCm(int size)
{
return (float)size/pixelNbrPerCm;
}
/**
* @function onReshape;
** Adapts the viewport to the window size when the window is reshaped
*/
void onReshape( int w, int h )
{
windowWidth = w;
windowHeight = h;
cx = pixelToCm(windowWidth);
cy = pixelToCm(windowHeight);
glViewport( 0, 0, windowWidth, windowHeight );
}
/**
* @function onMouse
*/
void onMouse( int button, int state, int x, int y )
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
{
}
}
/**
* @function onKeyboard
*/
void onKeyboard( unsigned char key, int x, int y )
{
if( bFullScreen && ((key == 'f' || key == 'F') || ((int)key == 27)))
{
glutReshapeWindow(camWidth*1.5, camHeight*1.5);
glutPositionWindow( 200, 80);
bFullScreen = false;
}
else if(!bFullScreen && (key == 'f' || key == 'F'))
{
glutFullScreen();
bFullScreen = true;
}
else switch ( key )
{
// invert cam ortientation
case 'i': bInvertCam = !bInvertCam; break;
case 'I': bInvertCam = !bInvertCam; break;
// change cam display
case 'c': bDisplayCam = !bDisplayCam; break;
case 'C': bDisplayCam = !bDisplayCam; break;
// change detection display
case 'd': bDisplayDetection = !bDisplayDetection; break;
case 'D': bDisplayDetection = !bDisplayDetection; break;
// change cam ratio
case '+': if(camRatio < 1.8) camRatio += 0.1 ; break;
case '-': if(camRatio > 0.2) camRatio -= 0.1; break;
// change homography correction
case 'b': bDisplayBox = !bDisplayBox; break;
case 'B': bDisplayBox = !bDisplayBox; break;
// change axes display
case 'm': bPolygonMode = !bPolygonMode; break;
case 'M': bPolygonMode = !bPolygonMode; break;
// change projection mode
case 'p': bProjectionMode = !bProjectionMode; break;
case 'P': bProjectionMode = !bProjectionMode; break;
// change projection mode
case ' ': bPause = !bPause; break;
// quit app
case 'q': exit(0); break;
case 'Q': exit(0); break;
default: break;
}
}
/**
* @function onIdle
* (Called at each openGL step)
* Updates the 'frame' image from the captured video
*/
void onIdle()
{
if(!bPause)
{
(*capture) >> frame;
}
}