您的位置:首页 > 其它

Kinect虚拟试衣间开发(5)-动作识别

2017-08-10 17:45 926 查看
kinect SDK2 比起 Kinect SDK1 多了两个大杀器 Kinect Studio 和 Visual Gesture Builder,这两个软件可以让我们很简单的使用微软自带的机器学习模型,建立自己的姿势库。简单来说就是Kinect Studio 录制和剪辑视频(可以选择自己想要Stream ),然后用Visual Gesture Builder标记视频中的动作,然后生成gba数据库文件。

具体操作过程见 微软发布在 Microsoft Visual Academy 的官方教程视频

这里我用两个动作执行拍照和脱衣的操作

准备过程

添加AdaBoostRech.dll 和 RFRProgressTech.dll 的引用

在解决方案中添加DataBase文件夹,并在文件夹中加入之前软件生产的gba数据库文件



代码部分

变量声明

//路径不对
private readonly string gestureDatabase = "WpfApplication1/WpfApplication1/DataBase/clean.gba";
private readonly string photoDatabase = "C:/Users/Wen/Desktop/实验DataBase/take_photo.gba";
private readonly string cleanGestureName = "clean";
private string photoGestureName = "take_photo";
private VisualGestureBuilderFrameSource vgbFrameSource = null;
private VisualGestureBuilderFrameReader vgbFrameReader = null;


变量初始化

// create the vgb source. The associated body tracking ID will be set when a valid body frame arrives from the sensor.
this.vgbFrameSource = new VisualGestureBuilderFrameSource(kinectSensor, 0);
this.vgbFrameSource.TrackingIdLost += this.Source_TrackingIdLost;

// 打开 vgb frames和写入FrameArrive 事件
this.vgbFrameReader = this.vgbFrameSource.OpenReader();
if (this.vgbFrameReader != null)
{
this.vgbFrameReader.IsPaused = false;
this.vgbFrameReader.FrameArrived += this.Reader_GestureFrameArrived;
}

// 从 gesture database 导入clean gesture
using (VisualGestureBuilderDatabase database = new VisualGestureBuilderDatabase(this.gestureDatabase))
{
// we could load all available gestures in the database with a call to vgbFrameSource.AddGestures(database.AvailableGestures),
// but for this program, we only want to track one discrete gesture from the database, so we'll load it by name
foreach (Gesture gesture in database.AvailableGestures)
{

if (gesture.Name.Equals(this.cleanGestureName))
{

this.vgbFrameSource.AddGesture(gesture);

}
}
}

using (VisualGestureBuilderDatabase photodatabase = new VisualGestureBuilderDatabase(this.photoDatabase))
{
// we could load all available gestures in the photodatabase with a call to vgbFrameSource.AddGestures(photodatabase.AvailableGestures),
// but for this program, we only want to track one discrete gesture from the database, so we'll load it by name
foreach (Gesture gesture in photodatabase.AvailableGestures)
{

if (gesture.Name.Equals(this.photoGestureName) )
{

this.vgbFrameSource.AddGesture(gesture);

}
}
}


处理gesture_arrive 事件

//处理gesture事件
private void Reader_GestureFrameArrived(object sender, VisualGestureBuilderFrameArrivedEventArgs e)
{

VisualGestureBuilderFrameReference frameReference = e.FrameReference;
using (VisualGestureBuilderFrame frame = frameReference.AcquireFrame())
{
if (frame != null)
{
// get the discrete gesture results which arrived with the latest frame
IReadOnlyDictionary<Gesture, DiscreteGestureResult> discreteResults = frame.DiscreteGestureResults;

if (discreteResults != null)
{

// we only have one gesture in this source object, but you can get multiple gestures
foreach (Gesture gesture in this.vgbFrameSource.Gestures)
{
if (gesture.Name.Equals(this.cleanGestureName) && gesture.GestureType == GestureType.Discrete)
{
DiscreteGestureResult result = null;
discreteResults.TryGetValue(gesture, out result);

if (result != null)
{

this.cleanClothesByGesture(true, result.Detected, result.Confidence);
}
}
else if(gesture.Name.Equals(this.photoGestureName) && gesture.GestureType == GestureType.Discrete)
{

DiscreteGestureResult result = null;
discreteResults.TryGetValue(gesture, out result);

if (result != null)
{

this.takePhotoByGesture(true, result.Detected, result.Confidence);
}
}
}
}
}

}
}


清空衣物操作,即把image 控件隐藏

/// <summary>
/// 识别动作,清空衣服
/// </summary>
/// <param name="isBodyTrackingIdValid">True, if the body associated with the GestureResultView object is still being tracked</param>
/// <param name="isGestureDetected">True, if the discrete gesture is currently detected for the associated body</param>
/// <param name="detectionConfidence">Confidence value for detection of the discrete gesture</param>
public  void  cleanClothesByGesture(bool isBodyTrackingIdValid,bool isGestureDetected,float detectionConfidence)
{
if (isGestureDetected&&detectionConfidence>0.5)
{
clth.Visibility = Visibility.Hidden;

}
}


执行拍照操作,使用前面编写的takephoto函数

/// <summary>
/// 识别动作,拍照
/// </summary>
/// <param name="isBodyTrackingIdValid">True, if the body associated with the GestureResultView object is still being tracked</param>
/// <param name="isGestureDetected">True, if the discrete gesture is currently detected for the associated body</param>
/// <param name="detectionConfidence">Confidence value for detection of the discrete gesture</param>
public void takePhotoByGesture(bool isBodyTrackingIdValid, bool isGestureDetected, float detectionConfidence)
{
if (isGestureDetected && detectionConfidence > 0.5)
{
takephoto();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: