抽象姿勢偵測類別(AbstractPoseDetector)主要是實作detect()函式,供外界呼叫使用,而在detect()函式中,會呼叫onDetectPre()、isPosing()、onPoseDetected()、onDetectPost()等4個函式,這4個函式採用掛勾方式供子類別來實作,換言之,在子類別中不需要修改偵測函式的動作流程,只需要負責將這些掛勾函式實現出來即可。這四個函式作用分述如下:
onDetectPre():偵測前要什那些工作。
isPosing():只否為我們要的姿勢 ?
onPoseDetected():姿勢偵測出來後要做什麼。
onDetectPost():偵測後要做那些工作。
m_userDetector物件是用來紀錄使用者的資訊。
AbstractPoseDetector.h 程式表列
#ifndef _ABSTRACT_POSE_DETECTOR_H_
#define _ABSTRACT_POSE_DETECTOR_H_
#include "common.h"
#include "UserDetector.h"
#include "TimeTicker.h"
class AbstractPoseDetector
{
protected:
UserDetector* m_userDetector;
private:
// TODO: should be time instead of frame count
float m_requiredPosingStability;
float m_posingTime;
TimeTicker m_ticker;
public:
AbstractPoseDetector(UserDetector* userDetector);
virtual ~AbstractPoseDetector();
virtual void detect();
protected:
void setRequiredPosingStability(float value) { m_requiredPosingStability = value; }
virtual bool isPosing(float dt);
virtual void onPoseDetected(float dt);
virtual void onDetectPre(float dt);
virtual void onDetectPost(float dt);
};
#endif
#include "AbstractPoseDetector.h"
AbstractPoseDetector::AbstractPoseDetector(UserDetector* userDetector)
{
m_userDetector = userDetector;
m_requiredPosingStability = 0;
m_posingTime = 0;
}
AbstractPoseDetector::~AbstractPoseDetector()
{
}
void AbstractPoseDetector::detect()
{
XuUserID userID = m_userDetector->getTrackedUserID();
if (!userID) {
return;
}
float dt = m_ticker.tick();
onDetectPre(dt);
if (isPosing(dt)) {
if (m_posingTime < m_requiredPosingStability) {
m_posingTime += dt;
}
if (m_posingTime >= m_requiredPosingStability) {
onPoseDetected(dt); }
} else {
if (m_posingTime > 0) {
m_posingTime = std::max(m_posingTime - dt, 0.0f);
}
}
onDetectPost(dt);}
bool AbstractPoseDetector::isPosing(float dt)
{
return false;
}
void AbstractPoseDetector::onPoseDetected(float dt)
{
}
void AbstractPoseDetector::onDetectPre(float dt)
{
}
void AbstractPoseDetector::onDetectPost(float dt)
{
}
沒有留言:
張貼留言