33 #include "objects/KinectDevice.h" 34 #include "generator/Depth.h" 35 #include "generator/Image.h" 36 #include "generator/User.h" 37 #include "DeviceManager.h" 38 #include "openNiScolPlugin.h" 39 #include <scolplugin.h> 42 boost::mutex enumerateMutex;
44 KinectDevice::KinectDevice(
unsigned int id) :
Thread()
57 mframeSize = cv::Size(640, 480);
58 mfingersBgr = cv::Mat(mframeSize, CV_8UC3);
64 openni::Status rc = openni::STATUS_OK;
67 openni::Array<openni::DeviceInfo> deviceList;
68 openni::OpenNI::enumerateDevices(&deviceList);
69 const char* deviceURI = openni::ANY_DEVICE;
71 if (deviceList.getSize() > mId)
72 deviceURI = deviceList[mId].getUri();
74 rc = NiDevice.open(deviceURI);
76 if (rc == openni::STATUS_OK && NiDevice.isValid())
78 if (!NiDevice.hasSensor(openni::SENSOR_DEPTH) || !CreateAndStartGenerators())
85 OBJpostEvent(KINECT_CONNECTED_CB, SCOL_PTR
this, 0);
89 StartThreading(boost::bind(&KinectDevice::GoThread,
this));
92 KinectDevice::KinectDevice() :
Thread()
96 KinectDevice::~KinectDevice()
100 openni::OpenNI::removeDeviceConnectedListener(
this);
101 openni::OpenNI::removeDeviceDisconnectedListener(
this);
102 openni::OpenNI::removeDeviceStateChangedListener(
this);
108 StopAndDestroyGenerators();
112 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
113 while (iSearchedUser != listOfUsers.end())
115 (*iSearchedUser)->SetId(-1);
116 (*iSearchedUser)->SetCalibrated(
false);
122 void KinectDevice::onDeviceStateChanged(
const openni::DeviceInfo* pInfo, openni::DeviceState state)
124 MMechostr(MSKDEBUG,
">> Kinect state changed");
127 void KinectDevice::onDeviceConnected(
const openni::DeviceInfo* pInfo)
129 MMechostr(MSKDEBUG,
">> Kinect connected");
132 void KinectDevice::onDeviceDisconnected(
const openni::DeviceInfo* pInfo)
135 boost::mutex::scoped_lock l(enumerateMutex);
136 boost::mutex::scoped_lock lg(uGeneratorsCreate);
140 StopAndDestroyGenerators();
143 boost::mutex::scoped_lock l(uListMutex);
144 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
145 while (iSearchedUser != listOfUsers.end())
147 (*iSearchedUser)->SetId(-1);
148 (*iSearchedUser)->SetCalibrated(
false);
154 OBJpostEvent(KINECT_DISCONNECTED_CB, SCOL_PTR
this, 0);
156 MMechostr(MSKDEBUG,
">> Kinect disconnected");
159 openni::Device& KinectDevice::GetDevice()
165 void KinectDevice::GetGeneratorsSize(
int &width,
int &height)
167 width = mframeSize.width;
168 height = mframeSize.height;
171 void KinectDevice::SetGeneratorsSize(
int width,
int height)
173 mframeSize = cv::Size(width, height);
174 mfingersBgr = cv::Mat(mframeSize, CV_8UC3);
177 bool KinectDevice::IsConnected()
185 void KinectDevice::SetMirrorMode(
bool state)
189 mDepthGenerator->SetMirror(state);
191 mImageGenerator->SetMirror(state);
194 bool KinectDevice::GetMirrorMode()
199 void KinectDevice::DestroyKinectUser(
KinectUser* existingKinectUser)
201 boost::mutex::scoped_lock l(uListMutex);
202 boost::mutex::scoped_lock lh(updateHandsMutex);
203 listOfUsers.remove(existingKinectUser);
204 SAFE_DELETE(existingKinectUser);
209 boost::mutex::scoped_lock l(uListMutex);
210 boost::mutex::scoped_lock lh(updateHandsMutex);
212 listOfUsers.push_back(newKinectUser);
213 return newKinectUser;
219 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
220 while ((iSearchedUser != listOfUsers.end()) && (userFound == 0))
222 if ((*iSearchedUser)->GetId() == userId)
223 userFound = *iSearchedUser;
230 void KinectDevice::GetValidUsersMap(
KinectUser** tUsers)
232 boost::mutex::scoped_lock l(uListMutex);
233 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
234 while (iSearchedUser != listOfUsers.end())
236 int uid = (*iSearchedUser)->GetId();
237 if ((uid > 0) && (uid < 64))
238 tUsers[uid] = *iSearchedUser;
244 void KinectDevice::GoThread()
250 boost::mutex::scoped_lock l(enumerateMutex);
251 openni::Status rc = openni::STATUS_OK;
254 openni::Array<openni::DeviceInfo> deviceList;
255 openni::OpenNI::enumerateDevices(&deviceList);
256 const char* deviceURI = openni::ANY_DEVICE;
258 if (deviceList.getSize() > mId)
259 deviceURI = deviceList[mId].getUri();
261 rc = NiDevice.open(deviceURI);
263 if (rc == openni::STATUS_OK && NiDevice.isValid())
265 if (!NiDevice.hasSensor(openni::SENSOR_DEPTH) || !CreateAndStartGenerators())
272 OBJpostEvent(KINECT_CONNECTED_CB, SCOL_PTR
this, 0);
283 int tick = GetTickCount();
285 boost::mutex::scoped_lock l(updateMutex);
286 if (mImageGenerator && mImageGenerator->UpdateData())
288 if (mUserGenerator && mDepthGenerator)
290 boost::mutex::scoped_lock l(uListMutex);
291 if (mDepthGenerator->UpdateData(mUserGenerator->UpdateData()))
301 int elapsedTime = 16 - (GetTickCount() - tick);
308 unsigned int KinectDevice::GetId()
313 int KinectDevice::GetExistingAvailableId()
316 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.begin();
317 if (iuser != listOfAvalaibleUsers.end())
320 listOfAvalaibleUsers.erase(iuser);
326 void KinectDevice::AddExistingId(
unsigned int id)
328 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.find(
id);
329 if (iuser == listOfAvalaibleUsers.end())
331 listOfAvalaibleUsers.insert(
id);
335 void KinectDevice::RemoveExistingId(
unsigned int id)
337 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.find(
id);
338 if (iuser != listOfAvalaibleUsers.end())
340 listOfAvalaibleUsers.erase(iuser);
344 bool KinectDevice::CreateAndStartGenerators()
346 boost::mutex::scoped_lock l(uGeneratorsCreate);
347 SAFE_DELETE(mDepthGenerator);
348 SAFE_DELETE(mImageGenerator);
349 SAFE_DELETE(mUserGenerator);
352 mImageGenerator =
new Image(
this);
355 if (!mImageGenerator->UpdateData())
357 SAFE_DELETE(mImageGenerator);
361 SAFE_DELETE(mImageGenerator);
363 NiDevice.setDepthColorSyncEnabled(
true);
366 mUserGenerator =
new User(
this);
369 mImageGenerator =
new Image(
this);
370 mDepthGenerator =
new Depth(
this);
373 mUserGenerator->SetSmoothing(msklSmoothing);
376 mDepthGenerator->SetDistCutoff(iDistCutoff);
377 mDepthGenerator->SetAngCutoff(fAngCutoff);
379 if (NiDevice.isImageRegistrationModeSupported(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR))
380 NiDevice.setImageRegistrationMode(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR);
382 SetMirrorMode(bMirror);
386 void KinectDevice::StopAndDestroyGenerators()
389 boost::mutex::scoped_lock l(uGeneratorsCreate);
391 SAFE_DELETE(mDepthGenerator);
392 SAFE_DELETE(mImageGenerator);
393 SAFE_DELETE(mUserGenerator);
394 mfingersBgr.setTo(0);
395 listOfAvalaibleUsers.clear();
398 Depth* KinectDevice::GetDepthGenerator()
400 return mDepthGenerator;
403 Image* KinectDevice::GetImageGenerator()
405 return mImageGenerator;
408 User* KinectDevice::GetUserGenerator()
410 return mUserGenerator;
413 void KinectDevice::SetSkeletonSmoothing(
float value)
415 boost::mutex::scoped_lock l(updateMutex);
416 msklSmoothing = value;
418 if (mUserGenerator && mUserGenerator->IsValid())
419 mUserGenerator->SetSmoothing(msklSmoothing);
422 float KinectDevice::GetSkeletonSmoothing()
424 return msklSmoothing;
427 void KinectDevice::SetDistCutoff(
int dist)
429 boost::mutex::scoped_lock l(updateMutex);
432 mDepthGenerator->SetDistCutoff(dist);
435 int KinectDevice::GetDistCutoff()
440 void KinectDevice::SetAngCutoff(
float ang)
442 boost::mutex::scoped_lock l(updateMutex);
445 mDepthGenerator->SetAngCutoff(ang);
448 float KinectDevice::GetAngCutoff()
453 void KinectDevice::DetectHands()
455 if (mDepthGenerator->IsValid())
457 cv::Mat mat(mframeSize, CV_16SC1, (
unsigned char*)mDepthGenerator->GetBuffer());
458 cv::Mat matRGB(mframeSize, CV_8UC3);
470 boost::mutex::scoped_lock l(updateHandsMutex);
471 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
472 while (iSearchedUser != listOfUsers.end())
474 if ((*iSearchedUser)->GetId() != -1)
475 (*iSearchedUser)->DetectUserHands(mat, matRGB);
481 boost::mutex::scoped_lock lf(uFingersUpdate);
483 matRGB.copyTo(mfingersBgr);
488 void KinectDevice::DetectUsers()
490 if (!mUserGenerator || !mDepthGenerator)
502 if (user && mUserGenerator->IsValid() && mDepthGenerator->IsValid())
504 if (listOfAvalaibleUsers.size() > 0)
506 const nite::UserId* ubuf = mUserGenerator->GetBuffer();
507 const unsigned short* dbuf = mDepthGenerator->GetCuttedBuffer();
511 if ((ubuf != 0) && (dbuf != 0))
513 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.begin();
516 mDepthGenerator->GetDepthSize(width, height);
519 while ((iuser != listOfAvalaibleUsers.end()) && (user != 0))
522 int uavailable = (int)*iuser;
525 for (
int i=0; i < (width * height); i++)
527 if (ubuf[i] == uavailable)
529 if ((ubuf[i] * dbuf[i]) > 0)
534 if ((ucnt > 0) && (ucnt > maxcnt))
546 RemoveExistingId(bestid);
548 OBJpostEvent(KINECT_NEW_USER_CB, (
int)user, 0);
554 void KinectDevice::GetHandsPixels(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
556 boost::mutex::scoped_lock l(uGeneratorsCreate);
560 int sbpl = Bcolor->TailleW * 3;
561 int dbpl = Bcolor->BPL;
563 cv::Mat mat(mframeSize, CV_8UC3);
567 boost::mutex::scoped_lock lf(uFingersUpdate);
568 mfingersBgr.copyTo(mat);
570 catch(std::exception &e)
572 MMechostr(MSKDEBUG,
"KinectDevice::GetHandsPixels : %s", e.what());
576 if (mframeSize != cv::Size(Bcolor->TailleW, Bcolor->TailleH))
580 memcpy(Bcolor->bits, mat.data, Bcolor->TailleW * Bcolor->TailleH * Bcolor->BytesPP);
582 for (
long y=0; y<Bcolor->TailleH; y++)
584 for (
long x=0; x<Bcolor->TailleW; x++)
586 unsigned long srcByte = (x * 3) + (sbpl * y);
587 unsigned long destByte = (x * Bcolor->BytesPP) + (dbpl * y);
589 Bcolor->bits[destByte] = mat.data[srcByte];
590 Bcolor->bits[destByte + 1] = mat.data[srcByte + 1];
591 Bcolor->bits[destByte + 2] = mat.data[srcByte + 2];
597 int dabpl = Balpha->BPL;
600 for (
long y=0; y<Balpha->TailleH; y++)
602 for (
long x=0; x<Balpha->TailleW; x++)
604 unsigned long sr = mat.data[(x * 3) + (sbpl * y)];
605 unsigned long sg = mat.data[((x * 3) + (sbpl * y)) + 1];
606 unsigned long sb = mat.data[((x * 3) + (sbpl * y)) + 2];
607 unsigned long destByte = (x * Balpha->BytesPP) + (dabpl * y);
609 Balpha->bits[destByte] = (sr + sg + sb) == 0 ? 0 : 0xFF;
616 void KinectDevice::GetDepthBuffer(PtrObjBitmap Bcolor)
618 boost::mutex::scoped_lock l(uGeneratorsCreate);
620 mDepthGenerator->GetBuffer(Bcolor);
623 void KinectDevice::GetImageBuffer(PtrObjBitmap Bcolor)
625 boost::mutex::scoped_lock l(uGeneratorsCreate);
627 mImageGenerator->GetBuffer(Bcolor);
630 void KinectDevice::GetImageGreyBuffer(PtrObjBitmap Bcolor)
632 boost::mutex::scoped_lock l(uGeneratorsCreate);
634 mImageGenerator->GetGreyBuffer(Bcolor);
637 void KinectDevice::GetValidUserPixels(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
639 boost::mutex::scoped_lock l(uGeneratorsCreate);
641 mUserGenerator->GetValidUserPixels(Bcolor, Balpha);
644 void KinectDevice::GetValidUserPixelsRGB(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
646 boost::mutex::scoped_lock l(uGeneratorsCreate);
648 mUserGenerator->GetValidUserPixels(Bcolor, Balpha);
651 mImageGenerator->GetBuffer(Bcolor);
654 bool KinectDevice::GetSklMutexLock()
656 return uSklMutex.try_lock();
659 void KinectDevice::ReleaseSklMutex()
User generator handling. .
Image generator handling. .
Depth generator handling. .