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

时间:2024-10-09 10:49:07

kinect SDK2 比起 Kinect SDK1 多了两个大杀器 Kinect Studio 和 Visual Gesture Builder,这两个软件可以让我们很简单的使用微软自带的机器学习模型,建立自己的姿势库。简单来说就是Kinect Studio 录制和剪辑视频(可以选择自己想要Stream ),然后用Visual Gesture Builder标记视频中的动作,然后生成gba数据库文件。
具体操作过程见 微软发布在 Microsoft Visual Academy 的官方教程视频

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


准备过程

  • 添加 和 的引用

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

这里写图片描述

代码部分

  • 变量声明
//路径不对
        private readonly string gestureDatabase = "WpfApplication1/WpfApplication1/DataBase/";
        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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 变量初始化
 // 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. += this.Source_TrackingIdLost;

            // 打开 vgb frames和写入FrameArrive 事件
            this.vgbFrameReader = this.();
            if (this.vgbFrameReader != null)
            {
                this. = false;
                this. += 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 (), 
                // 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 )
                {

                    if ((this.cleanGestureName))
                    {

                          this.(gesture);

                    }
                }
            }

            using (VisualGestureBuilderDatabase photodatabase = new VisualGestureBuilderDatabase(this.photoDatabase))
            {
                // we could load all available gestures in the photodatabase with a call to (), 
                // 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 )
                {

                    if ((this.photoGestureName) )
                    {

                        this.(gesture);

                    }
                }
     }
  • 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

处理gesture_arrive 事件

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

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

                    if (discreteResults != null)
                    {

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

                                if (result != null)
                                {

                                    this.cleanClothesByGesture(true, , );
                                }
                            }
                            else if((this.photoGestureName) &&  == )
                            {

                                DiscreteGestureResult result = null;
                                (gesture, out result);

                                if (result != null)
                                {

                                    this.takePhotoByGesture(true, , );
                                }
                            }
                        }
                    }
                }

            }
        }
  • 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
  • 清空衣物操作,即把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)
            {
                 = ;

            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 执行拍照操作,使用前面编写的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();

            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14