summaryrefslogtreecommitdiffhomepage
path: root/faceAPI/main.cpp
blob: f1c5f768082734c8878070536ee15ea62112d4da (plain)
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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	FaceAPI2FSX program implementation
//	Merges the old CockpitCamera.cpp and TestAppConsole.cpp into a single file
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Precompiled header
#include "stdafx.h"

//FaceAPI headers
#include "sm_api.h"
#include "ftnoir_tracker_sm_types.h"
#include "utils.h"

//local headers
#include "build_options.h"

//namespaces
using namespace std;
using namespace sm::faceapi::samplecode;

//
// global variables
//
HANDLE					hSMMemMap = NULL;
SMMemMap				*pMemData;
HANDLE					hSMMutex;
smEngineHeadPoseData	new_head_pose;
bool					stopCommand = false;

//enums
enum GROUP_ID
{
    GROUP0=0,
};

enum EVENT_ID
{
	EVENT_PING=0,
	EVENT_INIT,
};

enum INPUT_ID
{
    INPUT0=0,
};

//function definitions
void	updateHeadPose(smEngineHeadPoseData* temp_head_pose);
bool	SMCreateMapping();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//FaceAPI function implementations
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void STDCALL receiveLogMessage(void *, const char *buf, int /*buf_len*/)
{
    Lock lock(g_mutex); // serialize logging calls from different threads to avoid garbled output.
    //cout << string(buf);
}

// Callback function for face-data
void STDCALL receiveFaceData(void *, smEngineFaceData face_data, smCameraVideoFrame video_frame)
{   
    Lock lock(g_mutex);

    // Get info including data pointer to original image from camera
    smImageInfo video_frame_image_info;
    THROW_ON_ERROR(smImageGetInfo(video_frame.image_handle, &video_frame_image_info)); // reentrant, so ok

    // video_frame_image_info.plane_addr[*] now point to the image memory planes. 
    // The memory is only valid until the end of this routine unless you call smImageAddRef(video_frame.image_handle).
    // So you can deep copy the image data here, or use smImageAddRef() and just copy the pointer.
    // If you use smImageAddRef() you are responsible for calling smImageDestroy() to avoid a memory leak later.

    // In this callback you will typically want to copy the smEngineFaceData data into your own data-structure.
    // Since the smEngineFaceData contains multiple pod types copying it is not atomic and 
    // a mutex is required to avoid the race-condition with any thread simultaneously 
    // reading from your data-structure.
    // Such a race condition will not crash your code but will create weird noise in the tracking data.

    if (g_do_face_data_printing)
    {
        //cout << video_frame << " " << face_data;

        // Save any face texture to a PNG file
        if (face_data.texture)
        {
            // Create a unique filename
            std::stringstream filename;
            filename << "face_" << video_frame.frame_num << ".png";        
            // Try saving to a file
            if (saveToPNGFile(filename.str(), face_data.texture->image_info) == SM_API_OK)
            {
                cout << "Saved face-texture to " << filename.str() << std::endl;
            }
            else
            {
                cout << "Error saving face-texture to " << filename.str() << std::endl;
            }
        }
    }
}

// Callback function for head-pose
void STDCALL receiveHeadPose(void *,smEngineHeadPoseData head_pose, smCameraVideoFrame video_frame)
{
    Lock lock(g_mutex);

    // Get info including data pointer to original image from camera
    smImageInfo video_frame_image_info;
    THROW_ON_ERROR(smImageGetInfo(video_frame.image_handle, &video_frame_image_info)); // reentrant, so ok

    // video_frame_image_info.plane_addr[*] now point to the image memory planes. 
    // The memory is only valid until the end of this routine unless you call smImageAddRef(video_frame.image_handle).
    // So you can deep copy the image data here, or use smImageAddRef() and just copy the pointer.
    // If you use smImageAddRef() you are responsible for calling smImageDestroy() to avoid a memory leak later.

    // In this callback you will typically want to copy the smEngineFaceData data into your own data-structure.
    // Since the smEngineFaceData contains multiple pod types copying it is not atomic and 
    // a mutex is required to avoid the race-condition with any thread simultaneously 
    // reading from your data-structure.
    // Such a race condition will not crash your code but will create weird noise in the tracking data.

    if (g_do_head_pose_printing)
    {
        //cout << video_frame << " " << head_pose << std::endl;
    }

	//make a copy of the new head pose data and send it to simconnect
	//when we get a simmconnect frame event the new offset will be applied to the camera
	updateHeadPose(&head_pose);
}

// Create the first available camera detected on the system, and return its handle
smCameraHandle createFirstCamera()
{
    // Detect cameras
    smCameraInfoList info_list;
    THROW_ON_ERROR(smCameraCreateInfoList(&info_list));

    if (info_list.num_cameras == 0)
    {
        throw runtime_error("No cameras were detected");
    }
    else
    {
        cout << "The followings cameras were detected: " << endl;
        for (int i=0; i<info_list.num_cameras; ++i)
        {
            char buf[1024];
            cout << "    " << i << ". Type: " << info_list.info[i].type;
            THROW_ON_ERROR(smStringWriteBuffer(info_list.info[i].model,buf,1024));
            cout << " Model: " << string(buf);
            cout << " Instance: " << info_list.info[i].instance_index << endl;
            // Print all the possible formats for the camera
            for (int j=0; j<info_list.info[i].num_formats; j++)
            {
                smCameraVideoFormat video_format = info_list.info[i].formats[j];
                cout << "     - Format: ";
                cout << " res (" << video_format.res.w << "," << video_format.res.h << ")";
                cout << " image code " << video_format.format;
                cout << " framerate " << video_format.framerate << "(hz)";
                cout << " upside-down? " << (video_format.is_upside_down ? "y":"n") << endl;
            }
        }
    }

    // Create the first camera detected on the system
    smCameraHandle camera_handle = 0;
    THROW_ON_ERROR(smCameraCreate(&info_list.info[0],   // Use first camera
                                  0,                    // Use default settings for lens
                                  &camera_handle));

    // Destroy the info list
    smCameraDestroyInfoList(&info_list);

    return camera_handle;
}

// The main function: setup a tracking engine and show a video window, then loop on the keyboard.
void run()
{
    // Capture control-C
//    signal(SIGINT, CtrlCHandler);

    // Make the console window a bit bigger (see utils.h)
    initConsole();

	#ifdef _DEBUG
    // Log API debugging information to a file
    THROW_ON_ERROR(smLoggingSetFileOutputEnable(SM_API_TRUE));

    // Hook up log message callback
    THROW_ON_ERROR(smLoggingRegisterCallback(0,receiveLogMessage));
	#endif

    // Get the version
    int major, minor, maint;
    THROW_ON_ERROR(smAPIVersion(&major, &minor, &maint));
    cout << endl << "API VERSION: " << major << "." << minor << "." << maint << "." << endl << endl;
    // Print detailed license info
    char *buff;
    int size;
    THROW_ON_ERROR(smAPILicenseInfoString(0,&size,SM_API_TRUE));
    buff = new char[size];
    THROW_ON_ERROR(smAPILicenseInfoString(buff,&size,SM_API_TRUE));
    cout << "LICENSE: " << buff << endl << endl;
    // Determine if non-commercial restrictions apply
    const bool non_commercial_license = smAPINonCommercialLicense() == SM_API_TRUE;

    // Initialize the API
    THROW_ON_ERROR(smAPIInit());

	#ifdef _DEBUG
    // Get the path to the logfile
    smStringHandle logfile_path_handle = 0;
    THROW_ON_ERROR(smStringCreate(&logfile_path_handle));
    THROW_ON_ERROR(smLoggingGetPath(logfile_path_handle));
    int buf_len = 0;
    unsigned short *buf = 0;
    THROW_ON_ERROR(smStringGetBufferW(logfile_path_handle,(wchar_t **)&buf,&buf_len));
    wcout << "Writing log to file: " << wstring((wchar_t *)buf) << endl;
    THROW_ON_ERROR(smStringDestroy(&logfile_path_handle));
	#endif

    // Register the WDM category of cameras
    THROW_ON_ERROR(smCameraRegisterType(SM_API_CAMERA_TYPE_WDM));

    smEngineHandle engine_handle = 0;
    smCameraHandle camera_handle = 0;
    if (non_commercial_license)
    {
        // Create a new Head-Tracker engine that uses the camera
        THROW_ON_ERROR(smEngineCreate(SM_API_ENGINE_LATEST_HEAD_TRACKER,&engine_handle));
    }
    else
    {
        // Print out a list of connected cameras, and choose the first camera on the system
        camera_handle = createFirstCamera();
        // Create a new Head-Tracker engine that uses the camera
        THROW_ON_ERROR(smEngineCreateWithCamera(SM_API_ENGINE_LATEST_HEAD_TRACKER,camera_handle,&engine_handle));
    }

    // Check license for particular engine version (always ok for non-commercial license)
    const bool engine_licensed = smEngineIsLicensed(engine_handle) == SM_API_OK;

    cout << "-----------------------------------------------------" << endl;
    cout << "Press 'r' to restart tracking" << endl;
    cout << "Press 'a' to toggle auto-restart mode" << endl;
    if (!non_commercial_license)
    {
        cout << "Press 'l' to toggle lip-tracking" << endl;
        cout << "Press 'e' to toggle eyebrow-tracking" << endl;
    }
    if (engine_licensed)
    {
        cout << "Press 'h' to toggle printing of head-pose data" << endl;
        cout << "Press 'f' to toggle printing of face-landmark data" << endl;
    }
    cout << "Press '1' to toggle face coordinate frame axes" << endl;
    cout << "Press '2' to toggle performance info" << endl;
    cout << "Press '3' to toggle face mask" << endl;
    cout << "Press '4' to toggle face landmarks" << endl;
    cout << "Press CTRL-C or 'q' to quit" << endl;
    cout << "-----------------------------------------------------" << endl;

    // Hook up callbacks to receive output data from engine.
    // These functions will return errors if the engine is not licensed.
    if (engine_licensed)
    {
		#if (USE_HEADPOSE_CALLBACK==1)
		#pragma message("Using Headpose Callback")
        THROW_ON_ERROR(smHTRegisterHeadPoseCallback(engine_handle,0,receiveHeadPose));
		#endif
        if (!non_commercial_license)
        {
            THROW_ON_ERROR(smHTRegisterFaceDataCallback(engine_handle,0,receiveFaceData));
        }
    }
    else
    {
        cout << "Engine is not licensed, cannot obtain any output data." << endl;
    }

    if (!non_commercial_license)
    {
        // Enable lip and eyebrow tracking
        THROW_ON_ERROR(smHTSetLipTrackingEnabled(engine_handle,SM_API_TRUE));
        THROW_ON_ERROR(smHTSetEyebrowTrackingEnabled(engine_handle,SM_API_TRUE));
    }

    // Create and show a video-display window
    smVideoDisplayHandle video_display_handle = 0;
    THROW_ON_ERROR(smVideoDisplayCreate(engine_handle,&video_display_handle,0,TRUE));

    // Setup the VideoDisplay
    THROW_ON_ERROR(smVideoDisplaySetFlags(video_display_handle,g_overlay_flags));

    // Get the handle to the window and change the title to "Hello World"
    smWindowHandle win_handle = 0;
    THROW_ON_ERROR(smVideoDisplayGetWindowHandle(video_display_handle,&win_handle));    
    SetWindowText(win_handle, _T("faceAPI Video-widget"));

    // Start tracking
    THROW_ON_ERROR(smEngineStart(engine_handle));

    // Loop on the keyboard
    while (processKeyPress(engine_handle, video_display_handle) && !stopCommand)
    {
        // Read and print the current head-pose (if not using the callback mechanism)
		#if (USE_HEADPOSE_CALLBACK==0)
		#pragma message("Polling Headpose Manually")
		if (engine_licensed)
		{
			smEngineHeadPoseData head_pose;
			Lock lock(g_mutex);
			
			THROW_ON_ERROR(smHTCurrentHeadPose(engine_handle,&head_pose));
			if (g_do_head_pose_printing)
			{
				std::cout << head_pose << std::endl;
			}

		}
		#endif

        // NOTE: If you have a windows event loop in your program you 
        // will not need to call smAPIProcessEvents(). This manually redraws the video window.
        THROW_ON_ERROR(smAPIProcessEvents());

        // Prevent CPU overload in our simple loop.
        const int frame_period_ms = 10;
        Sleep(frame_period_ms); 
    }

    // Destroy engine
    THROW_ON_ERROR(smEngineDestroy(&engine_handle));
    // Destroy video display
    THROW_ON_ERROR(smVideoDisplayDestroy(&video_display_handle));

	if ( pMemData != NULL ) {
		UnmapViewOfFile ( pMemData );
	}
	
	if (hSMMutex != 0) {
		CloseHandle( hSMMutex );
	}
	hSMMutex = 0;
	
	if (hSMMemMap != 0) {
		CloseHandle( hSMMemMap );
	}
	hSMMemMap = 0;

} // run()

// Application entry point
int _tmain(int /*argc*/, _TCHAR** /*argv*/)
{
	OutputDebugString(_T("_tmain() says: Starting Function\n"));

	try
    {
		if (SMCreateMapping()) {
			run();
		}
    }
    catch (exception &e)
    {
        cerr << e.what() << endl;
    }

    return smAPIQuit();
}

//
// This is called exactly once for each FaceAPI callback and must be within an exclusive lock
//
void updateHeadPose(smEngineHeadPoseData* temp_head_pose)
{
	char msg[100];

	OutputDebugString(_T("updateHeadPose() says: Starting Function\n"));

	//
	// Check if the pointer is OK and wait for the Mutex.
	//
	if ( (pMemData != NULL) && (WaitForSingleObject(hSMMutex, 100) == WAIT_OBJECT_0) ) {
		
		OutputDebugString(_T("updateHeadPose() says: Writing Data\n"));

		//
		// Copy the Raw measurements directly to the client.
		//
		if (temp_head_pose->confidence > 0.0f)
		{
			memcpy(&pMemData->data.new_pose,temp_head_pose,sizeof(smEngineHeadPoseData));
			sprintf(msg, "HeadPose Yaw: %.2f\n", pMemData->data.new_pose.head_rot.x_rads);
			OutputDebugStringA(msg);
		}

		if (pMemData->command == 100) {
			stopCommand = TRUE;
		}
		ReleaseMutex(hSMMutex);
	}

};

//
// Create a memory-mapping to the faceAPI data.
// It contains the tracking data, a command-code from FaceTrackNoIR
//
//
bool SMCreateMapping()
{
	OutputDebugString(_T("FTCreateMapping says: Starting Function\n"));

	//
	// A FileMapping is used to create 'shared memory' between the faceAPI and FaceTrackNoIR.
	//
	// Try to create a FileMapping to the Shared Memory.
	// If one already exists: close it.
	//
	hSMMemMap = CreateFileMappingA( INVALID_HANDLE_VALUE , 00 , PAGE_READWRITE , 0 , 
		                           sizeof( TFaceData ) + sizeof( HANDLE ) + 100, 
								   (LPCSTR) SM_MM_DATA );

	if ( hSMMemMap != 0 ) {
		OutputDebugString(_T("FTCreateMapping says: FileMapping Created!\n"));
	}

	if ( ( hSMMemMap != 0 ) && ( (long) GetLastError == ERROR_ALREADY_EXISTS ) ) {
		CloseHandle( hSMMemMap );
		hSMMemMap = 0;
	}

	//
	// Create a new FileMapping, Read/Write access
	//
	hSMMemMap = OpenFileMappingA( FILE_MAP_ALL_ACCESS , false , (LPCSTR) SM_MM_DATA );
	if ( ( hSMMemMap != 0 ) ) {
		OutputDebugString(_T("FTCreateMapping says: FileMapping Created again...\n"));
		pMemData = (SMMemMap *) MapViewOfFile(hSMMemMap, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(TFaceData));
		if (pMemData != NULL) {
			OutputDebugString(_T("FTCreateMapping says: MapViewOfFile OK.\n"));
//			pMemData->handle = handle;	// The game uses the handle, to send a message that the Program-Name was set!
		}
	    hSMMutex = CreateMutexA(NULL, false, SM_MUTEX);
	}
	else {
		OutputDebugString(_T("Error creating Shared Memory for faceAPI\n"));
        cerr << "Error creating Shared Memory for faceAPI" << endl;
		return false;
	}

	//if (pMemData != NULL) {
	//	pMemData->data.DataID = 1;
	//	pMemData->data.CamWidth = 100;
	//	pMemData->data.CamHeight = 250;
	//}

	return true;
}