Skip to content
Permalink
16e941848f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
402 lines (331 sloc) 7.92 KB
/** \file dab_tracker_azure_camera.cpp
*/
#include "dab_tracker_azure_camera.h"
#include "dab_tracker_azure_tools.h"
#include "ofxCvGrayscaleImage.h"
#include "ofxCvColorImage.h"
#include "ofxCvShortImage.h"
#include "dab_tracker_pointcloud.h"
#include <thread>
#include <future>
using namespace dab;
using namespace dab::tracker;
AzureCamera::AzureCamera(bool pColorMode, bool pDepthMode, bool pPointCloudMode, int pDeviceNr, WiredMode pWireMode) throw (dab::Exception)
: Camera()
, mColorMode(pColorMode)
, mDepthMode(pDepthMode)
, mPointCloudMode(pPointCloudMode)
, mColorFrame(nullptr)
, mDepthFrame(nullptr)
, mPointCloud(nullptr)
, mWiredMode(pWireMode)
, mDeviceNr(pDeviceNr)
, mSerialNr("")
, mNativeConfig(K4A_DEVICE_CONFIG_INIT_DISABLE_ALL)
{
mMoviePlaying = false;
mMovieRecordFileSuffix = "mkv";
mFrameSize = { 0, 0 };
try
{
init();
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: Failed to create Azure camera", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
mCapturing = false;;
}
AzureCamera::AzureCamera(bool pColorMode, bool pDepthMode, bool pPointCloudMode, const std::string& pString, WiredMode pWireMode) throw (dab::Exception)
: Camera()
, mColorMode(pColorMode)
, mDepthMode(pDepthMode)
, mPointCloudMode(pPointCloudMode)
, mColorFrame(nullptr)
, mDepthFrame(nullptr)
, mPointCloud(nullptr)
, mWiredMode(pWireMode)
, mDeviceNr(-1)
, mSerialNr("")
, mNativeConfig(K4A_DEVICE_CONFIG_INIT_DISABLE_ALL)
{
// chech if the strings represents the camera's serial number of a movie file name
int stringLength = pString.length();
if (stringLength >= 4 && pString.substr(stringLength - 4, 4) == ".mkv")
{
mMoviePlayFileName = pString;
mMoviePlaying = true;
}
else
{
mSerialNr = pString;
mMoviePlaying = false;
}
mMovieRecordFileSuffix = "mkv";
mFrameSize = { 0, 0 };
try
{
init();
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: Failed to create Azure camera", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
mCapturing = false;
}
AzureCamera::AzureCamera(k4a_device_configuration_t pNativeConfig, int pDeviceNr) throw (dab::Exception)
: Camera()
, mColorMode(false)
, mDepthMode(false)
, mPointCloudMode(false)
, mColorFrame(nullptr)
, mDepthFrame(nullptr)
, mPointCloud(nullptr)
, mDeviceNr(pDeviceNr)
, mSerialNr("")
, mNativeConfig(pNativeConfig)
{
mMoviePlaying = false;
mMovieRecordFileSuffix = "mkv";
mFrameSize = { 0, 0 };
try
{
init();
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: Failed to create Azure camera", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
mCapturing = false;;
}
AzureCamera::AzureCamera(k4a_device_configuration_t pNativeConfig, const std::string& pSerialNr) throw (dab::Exception)
: Camera()
, mColorMode(false)
, mDepthMode(false)
, mPointCloudMode(false)
, mColorFrame(nullptr)
, mDepthFrame(nullptr)
, mPointCloud(nullptr)
, mDeviceNr(-1)
, mSerialNr(pSerialNr)
, mNativeConfig(pNativeConfig)
{
mMoviePlaying = false;
mFrameSize = { 0, 0 };
try
{
init();
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: Failed to create Azure camera", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
mCapturing = false;;
}
AzureCamera::~AzureCamera()
{
// TODO
}
void
AzureCamera::init() throw (dab::Exception)
{
mSelf = std::shared_ptr<AzureCamera>(this);
AzureTools& azureTools = AzureTools::get();
try
{
azureTools.initCamera(mSelf);
azureTools.initControls(mSelf);
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: failed to initialize azure camera\n", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
}
bool
AzureCamera::isColorMode() const
{
return mColorMode;
}
bool
AzureCamera::isDepthMode() const
{
return mDepthMode;
}
bool
AzureCamera::isPointCloudMode() const
{
return mPointCloudMode;
}
ofxCvImage*
AzureCamera::colorFrame()
{
return mColorFrame;
}
ofxCvImage*
AzureCamera::depthFrame()
{
return mDepthFrame;
}
PointCloud*
AzureCamera::pointCloud()
{
return mPointCloud;
}
const k4a::image
AzureCamera::nativeColorFrame()
{
mLock.lock();
k4a::image _nativeColorImage = mNativeColorImage;
mLock.unlock();
return _nativeColorImage;
}
const k4a::image
AzureCamera::nativeDepthFrame()
{
mLock.lock();
k4a::image _nativeDepthImage = mNativeDepthImage;
mLock.unlock();
return _nativeDepthImage;
}
const k4a::image
AzureCamera::nativeDepthColorFrame()
{
mLock.lock();
k4a::image _nativeColorDepthImage = mNativeDepthColorImage;
mLock.unlock();
return _nativeColorDepthImage;
}
const k4a::image
AzureCamera::nativePointCloudFrame()
{
mLock.lock();
k4a::image _nativePointCloudImage = mNativePointCloudImage;
mLock.unlock();
return _nativePointCloudImage;
}
void
AzureCamera::setControlValue(const std::string& pControlName) throw (dab::Exception)
{
// TODO
}
void
AzureCamera::setControlValue(const std::string& pControlName, const AbstractValue& pValue) throw (dab::Exception)
{
//std::cout << "AzureCamera::setControlValue(const " << pControlName << ", " << pValue << " ) throw (dab::Exception)\n";
auto controlIter = mControls.find(pControlName);
if (controlIter == mControls.end()) throw dab::Exception("TRACKER ERROR: Camera has no control named " + pControlName, __FILE__, __FUNCTION__, __LINE__);
try
{
Camera::setControlValue(pControlName, pValue);
AzureTools::get().setControlValue(pControlName, pValue, mSelf);
}
catch (dab::Exception& e)
{
e += dab::Exception("TRACKER ERROR: failed to set camera control value " + pControlName, __FILE__, __FUNCTION__, __LINE__);
throw;
}
}
void
AzureCamera::start() throw (dab::Exception)
{
// TODO
}
void
AzureCamera::stop() throw (dab::Exception)
{
// TODO
}
void
AzureCamera::capture() throw (dab::Exception)
{
try
{
mLock.lock();
AzureTools::get().capture(mSelf);
bool success;
if (mMovieRecording == true && mMovieRecorder != nullptr)
{
if (mChannelCount == 1)
{
if (mColorConversionFrame == nullptr)
{
mColorConversionFrame = new ofxCvColorImage();
mColorConversionFrame->allocate(mFrameSize[0], mFrameSize[1]);
}
*mColorConversionFrame = *static_cast<ofxCvGrayscaleImage*>(mFrame);
success = mMovieRecorder->addFrame(mColorConversionFrame->getPixels());
}
else
{
success = mMovieRecorder->addFrame(mFrame->getPixels());
}
if (success == false) throw dab::Exception("TRACKER ERROR: failed to record camera frame", __FUNCTION__, __FUNCTION__, __LINE__);
//if( mMovieRecorder->hasVideoError() ) throw dab::Exception("TRACKER ERROR: failed to record camera frame", __FUNCTION__, __FUNCTION__, __LINE__);
}
if (mCaptureStill == true)
{
if (mColorMode == true)
{
saveStill(mFrame, "color_" + mCaptureStillFileName + ".bmp");
}
if (mDepthMode == true)
{
saveStill(mDepthFrame, "depth_" + mCaptureStillFileName + ".bmp");
}
mCaptureStill = false;
}
mLock.unlock();
}
catch (dab::Exception& e)
{
mLock.unlock();
e += dab::Exception("TRACKER ERROR: failed to capture camera frame", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
}
void
AzureCamera::startRecording() throw (dab::Exception)
{
if (mMovieRecording == true) return;
std::string fileName = mMovieRecordFileName + "_" + ofGetTimestampString() + "." + mMovieRecordFileSuffix;
try
{
AzureTools::get().startMovieRecording(mSelf, fileName);
mMovieRecording = true;
}
catch (dab::Exception& e)
{
e += dab::Exception("Camera Error: failed to create movie recording", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
}
void
AzureCamera::stopRecording() throw (dab::Exception)
{
if (mMovieRecording == false) return;
try
{
AzureTools::get().stopMovieRecording(mSelf);
mMovieRecording = false;
}
catch (dab::Exception& e)
{
e += dab::Exception("Camera Error: failed to create movie recording", __FILE__, __FUNCTION__, __LINE__);
throw e;
}
}
void
AzureCamera::saveSettings() throw (dab::Exception)
{
// TODO
}
void
AzureCamera::restoreSettings() throw (dab::Exception)
{
// TODO
}