summaryrefslogtreecommitdiffhomepage
path: root/tracker-kinect-face/camera_kinect_ir.cpp
blob: e170b15a2996620457d612eebe4b3f7414cab528 (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
/* Copyright (c) 2019, Stephane Lenclud <github@lenclud.com>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#include "camera_kinect_ir.h"

#ifdef OTR_HAVE_OPENCV

//#include "frame.hpp"

#include "compat/sleep.hpp"
#include "compat/math-imports.hpp"

#include <opencv2/imgproc.hpp>
#include <cstdlib>

namespace Kinect {

    static const char KKinectIRSensor[] = "Kinect V2 IR Sensor";

    CamerasProvider::CamerasProvider() = default;

    std::unique_ptr<video::impl::camera> CamerasProvider::make_camera(const QString& name)
    {
        if (name.compare(KKinectIRSensor) == 0)
        {
            return std::make_unique<CameraKinectIr>();
        }

        return nullptr;
    }

    std::vector<QString> CamerasProvider::camera_names() const
    {
        return { KKinectIRSensor };
    }

    bool CamerasProvider::can_show_dialog(const QString& camera_name)
    {
        return false;
    }

    bool CamerasProvider::show_dialog(const QString& camera_name)
    {
        return false;
    }

// Register our camera provider thus making sure Point Tracker can use Kinect V2 IR Sensor
OTR_REGISTER_CAMERA(CamerasProvider)


CameraKinectIr::CameraKinectIr() 
{
}


CameraKinectIr::~CameraKinectIr()
{
    stop();
}

bool CameraKinectIr::show_dialog()
{
    return false;
}

bool CameraKinectIr::is_open()
{
    return iInfraredFrameReader!=nullptr;
}

///
/// Wait until we get a first frame
///
void CameraKinectIr::WaitForFirstFrame()
{
    bool new_frame = false;
    int attempts = 200; // Kinect cold start can take a while
    while (!new_frame && attempts>0)
    {
        new_frame = get_frame_(iMatFrame);
        portable::sleep(100);
        --attempts;
    }
}



std::tuple<const video::impl::frame&, bool> CameraKinectIr::get_frame()
{
    bool new_frame = false;
    new_frame = get_frame_(iMatFrame);
        
    iFrame.data = iMatFrame.ptr();
    iFrame.width = iWidth;
    iFrame.height = iHeight;
    iFrame.stride = cv::Mat::AUTO_STEP;
    iFrame.channels = iMatFrame.channels();
    iFrame.channelSize = iMatFrame.elemSize1();
    return { iFrame, new_frame };
}

// Safe release for interfaces
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
    if (pInterfaceToRelease != NULL)
    {
        pInterfaceToRelease->Release();
        pInterfaceToRelease = NULL;
    }
}

///
///
///
bool CameraKinectIr::start(info& aInfo)
{
    stop();

    HRESULT hr;

    // Get and open Kinect sensor
    hr = GetDefaultKinectSensor(&iKinectSensor);
    if (SUCCEEDED(hr))
    {
        hr = iKinectSensor->Open();
    }

    // Create infrared frame reader	
    if (SUCCEEDED(hr))
    {
        // Initialize the Kinect and get the infrared reader
        IInfraredFrameSource* pInfraredFrameSource = NULL;

        hr = iKinectSensor->Open();

        if (SUCCEEDED(hr))
        {
            hr = iKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource);
        }

        if (SUCCEEDED(hr))
        {
            hr = pInfraredFrameSource->OpenReader(&iInfraredFrameReader);
        }

        SafeRelease(pInfraredFrameSource);

        if (SUCCEEDED(hr))
        {
            iKinectSensor->get_CoordinateMapper(&iCoordinateMapper);
        }
    }


    if (SUCCEEDED(hr))
    {
        WaitForFirstFrame();
        bool success = iMatFrame.ptr() != nullptr;
        if (success)
        {
            // Provide frame info
            aInfo.width = iWidth;
            aInfo.height = iHeight;

            CameraIntrinsics intrinsics;
            hr = iCoordinateMapper->GetDepthCameraIntrinsics(&intrinsics);
            if (SUCCEEDED(hr))
            {
                aInfo.focalLengthX = intrinsics.FocalLengthX;
                aInfo.focalLengthY = intrinsics.FocalLengthY;
                aInfo.principalPointX = intrinsics.PrincipalPointX;
                aInfo.principalPointY = intrinsics.PrincipalPointY;
                aInfo.radialDistortionFourthOrder = intrinsics.RadialDistortionFourthOrder;
                aInfo.radialDistortionSecondOrder = intrinsics.RadialDistortionSecondOrder;
                aInfo.radialDistortionSixthOrder = intrinsics.RadialDistortionSixthOrder;                
            }

        }

        return success;
    }

    stop();
    return false;
}

void CameraKinectIr::stop()
{
    // done with infrared frame reader
    SafeRelease(iInfraredFrame);
    SafeRelease(iInfraredFrameReader);

    // close the Kinect Sensor
    if (iKinectSensor)
    {
        iKinectSensor->Close();
    }

    SafeRelease(iCoordinateMapper);
    SafeRelease(iKinectSensor);

    // Free up our memory buffer if any
    iMatFrame = cv::Mat();
}

bool CameraKinectIr::get_frame_(cv::Mat& aFrame)
{

    if (!iInfraredFrameReader)
    {
        return false;
    }

    bool success = false;

    // Release previous frame if any
    SafeRelease(iInfraredFrame);

    HRESULT hr = iInfraredFrameReader->AcquireLatestFrame(&iInfraredFrame);

    if (SUCCEEDED(hr))
    {
        if (iFirstFrame)
        {
            IFrameDescription* frameDescription = NULL;

            if (SUCCEEDED(hr))
            {
                hr = iInfraredFrame->get_FrameDescription(&frameDescription);
            }

            if (SUCCEEDED(hr))
            {
                hr = frameDescription->get_Width(&iWidth);
            }

            if (SUCCEEDED(hr))
            {
                hr = frameDescription->get_Height(&iHeight);
            }

            if (SUCCEEDED(hr))
            {
                hr = frameDescription->get_DiagonalFieldOfView(&iFov);
            }

            if (SUCCEEDED(hr))
            {
                iFirstFrame = false;
            }
                
            SafeRelease(frameDescription);
        }


        UINT nBufferSize = 0;
        UINT16 *pBuffer = NULL;

        if (SUCCEEDED(hr))
        {
            hr = iInfraredFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
        }

        if (SUCCEEDED(hr))
        {
            // Create an OpenCV matrix with our 16-bits IR buffer
            aFrame = cv::Mat(iHeight, iWidth, CV_16UC1, pBuffer, cv::Mat::AUTO_STEP);
            // Any processing of the frame is left to the user
            success = true;
        }
    }


    return success;
}



}

#endif