summaryrefslogtreecommitdiffhomepage
path: root/tracker-kinect-face/kinect_face_tracker.h
blob: 3ed28bea15568373f063e8e11d124c4c0841cd63 (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



#include <cmath>

#include "api/plugin-api.hpp"
#include "compat/timer.hpp"
#include "compat/macros.hpp"
#include "video/video-widget.hpp"

// Kinect Header files
#include <Kinect.h>
#include <Kinect.Face.h>

#pragma once

// @deprecated Use UniqueInterface instead. Remove it at some point.
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != nullptr)
	{
		pInterfaceToRelease->Release();
		pInterfaceToRelease = nullptr;
	}
}

template<class Interface>
inline void ReleaseInterface(Interface* pInterfaceToRelease)
{
	if (pInterfaceToRelease != nullptr)
	{
		pInterfaceToRelease->Release();
	}
}

// Safely use Microsoft interfaces.
template<typename T>
class UniqueInterface : public std::unique_ptr<T, decltype(&ReleaseInterface<T>)> ///**/
{
public:
	UniqueInterface() : std::unique_ptr<T, decltype(&ReleaseInterface<T>)>(nullptr, ReleaseInterface<T>){}
	// Access pointer, typically for creation
	T** PtrPtr() { return &iPtr; };
	// Called this once the pointer was created
	void Reset() { std::unique_ptr<T, decltype(&ReleaseInterface<T>)>::reset(iPtr); }
	// If ever you want to release that interface before the object is deleted
	void Free() { iPtr = nullptr; Reset(); }	
private:
	T* iPtr = nullptr;
};


//
//
//
class KinectFaceTracker : public ITracker
{
public:
	KinectFaceTracker();
	~KinectFaceTracker() override;
	module_status start_tracker(QFrame* aFrame) override;
	void data(double *data) override;
	bool center() override;

private:
	

	// Kinect stuff
	void Update();
	HRESULT InitializeDefaultSensor();
	void ProcessFaces();
	HRESULT UpdateBodyData(IBody** ppBodies);
	void ExtractFaceRotationInDegrees(const Vector4* pQuaternion, float* pPitch, float* pYaw, float* pRoll);
	static IBody* FindClosestBody(IBody** aBodies);
	static IBody* FindTrackedBodyById(IBody** aBodies,UINT64 aTrackingId);
	
	//
	Timer iTimer;

	// Current Kinect
	IKinectSensor* iKinectSensor = nullptr;

	// Color reader
	IColorFrameReader* iColorFrameReader = nullptr;

	// Body reader
	IBodyFrameReader* iBodyFrameReader = nullptr;

	// Face sources
	IHighDefinitionFaceFrameSource*	iFaceFrameSource = nullptr;

	// Face readers
	IHighDefinitionFaceFrameReader*	iFaceFrameReader = nullptr;

	//
	RGBQUAD* iColorRGBX = nullptr;

	RectI iFaceBox = { 0 };

	// Face position
	CameraSpacePoint iLastFacePosition = { 0 };
	CameraSpacePoint iFacePosition = { 0 };
	CameraSpacePoint iFacePositionCenter = { 0 };

	Vector4 iFaceRotationQuaternion = { 0 };
	// As Yaw, Pitch, Roll
	CameraSpacePoint iLastFaceRotation = { 0 };
	CameraSpacePoint iFaceRotation = { 0 };
	CameraSpacePoint iFaceRotationCenter = { 0 };
	//
	std::unique_ptr<video_widget> iVideoWidget;
	std::unique_ptr<QLayout> iLayout;

	// Id of the body currently being tracked
	UINT64 iTrackingId = 0;
};