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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| using UnityEngine; using System.IO; using System.Collections; using System.Collections.Generic; using Windows.Kinect; using Microsoft.Kinect.VisualGestureBuilder;
public class GestureSourceManager_right : MonoBehaviour { //没用到,事件我觉得自定义比较好,看个人吧 public struct EventArgs { public string name; public float confidence;
public EventArgs(string _name, float _confidence) { name = _name; confidence = _confidence; } } //定义手势状态 private enum gestureState { Unknown = 0, moving = 1, gribing = 2
} public BodySourceManager _BodySource; public string databasePath; private KinectSensor _Sensor; private VisualGestureBuilderFrameSource _Source; private VisualGestureBuilderFrameReader _Reader; private VisualGestureBuilderDatabase _Database;
// Gesture Detection Events public delegate void GestureAction(EventArgs e); public event GestureAction OnGesture;
private gestureState currentGesture;
// Use this for initialization void Start() { // 获取 Kinect 传感器: 获取默认的 Kinect 传感器实例。 _Sensor = KinectSensor.GetDefault(); if (_Sensor != null) { //如果传感器存在且未打开,则将其打开。 if (!_Sensor.IsOpen) { _Sensor.Open(); } // Set up Gesture Source //创建一个 VisualGestureBuilderFrameSource 实例来处理手势识别。 _Source = VisualGestureBuilderFrameSource.Create(_Sensor, 0); // open the reader for the vgb frames // 创建并配置一个 VisualGestureBuilderFrameReader 实例,设置为暂停状态,并注册 FrameArrived 事件处理器 _Reader = _Source.OpenReader(); if (_Reader != null) { _Reader.IsPaused = true; _Reader.FrameArrived += GestureFrameArrived; } // 构造手势数据库的路径,检查文件是否存在,并创建 VisualGestureBuilderDatabase 实例。 string path = Path.Combine(Application.streamingAssetsPath, "woquan_Right.gba"); //string path = Path.Combine(Application.streamingAssetsPath, "text.txt"); if (File.Exists(path)) { Debug.Log("纯正"); } else { Debug.Log("不纯正");
} // 单个动作叫gba 整个叫gbd //path += "/woquan_Right.gbd"; Debug.Log("database path is " + path);
_Database = VisualGestureBuilderDatabase.Create(path); // 从数据库中获取所有手势,并将其添加到手势源中进行识别。 IList<Gesture> gesturesList = _Database.AvailableGestures; for (int x = 0; x < gesturesList.Count; x++) { Gesture g = gesturesList[x]; //if (g.Name.Equals("Squat")) //{ _Source.AddGesture(g); //} }
} // 设置当前手势状态为“未知”。 currentGesture = gestureState.Unknown; }
// Public setter for Body ID to track public void SetBody(ulong id) { if (id > 0) { _Source.TrackingId = id; _Reader.IsPaused = false; Debug.Log("id is " + id); } else { _Source.TrackingId = 0; _Reader.IsPaused = true; } //gameObject.GetComponent<GestureController>().AddGesture(); }
// Update Loop, set body if we need one void Update() {
if (!_Source.IsTrackingIdValid) { //print ("found"); FindValidBody(); } }
// Check Body Manager, grab first valid body void FindValidBody() {
if (_BodySource != null) {
Body[] bodies = _BodySource.GetData(); if (bodies != null) { foreach (Body body in bodies) { if (body.IsTracked) { SetBody(body.TrackingId); break; } } } }
} /// Handles gesture detection results arriving from the sensor for the associated body tracking Id // 这是一个事件处理方法,它在手势识别系统中的每一帧到达时被调用。 // 处理来自相关身体跟踪Id的传感器的手势检测结果
private void GestureFrameArrived(object sender, VisualGestureBuilderFrameArrivedEventArgs e) { //从事件参数中获取当前帧的引用。 VisualGestureBuilderFrameReference frameReference = e.FrameReference; // 使用 AcquireFrame 方法获取帧的实例,并确保帧对象不为 null。 using (VisualGestureBuilderFrame frame = frameReference.AcquireFrame()) { if (frame != null) { // 从帧中获取最新帧到达的离散手势结果 IDictionary<Gesture, DiscreteGestureResult> discreteResults = frame.DiscreteGestureResults; //定义手势字典 // Dictionary<string, double> DataOnOneFrame = new Dictionary<string, double>(); if (discreteResults != null) { // 检查 discreteResults 是否为 null。遍历所有手势源中的手势。 DiscreteGestureResult result = null; foreach (Gesture gesture in _Source.Gestures) { //print("G: " + gesture.Name); // 只处理类型为 Discrete(离散)的手势。 if (gesture.GestureType == GestureType.Discrete) { //print("G: " + gesture.Name); // result.Confidence 表示为相似度 // gesture.Name为 动作名称 discreteResults.TryGetValue(gesture, out result); Debug.Log("Detected Gesture " + gesture.Name + " with Confidence " + result.Confidence); //将所有手势存在字典里,用来判断当前手势 //DataOnOneFrame.Add(gesture.Name, result.Confidence); //if (result != null) //{ // // Fire Event // //OnGesture (new EventArgs (gesture.Name, result.Confidence)); // Debug.Log("Detected Gesture " +gesture.Name + " with Confidence "+result.Confidence); //} //else //{ // Debug.Log("result is NULL@: "+result); //} } } //判断手势 //JudgeGesture(DataOnOneFrame); //DataOnOneFrame.Clear(); } } } } //这个函数用来判断手势,具体判断方法自定义,其中包括手势状态切换 private void JudgeGesture(Dictionary<string, double> OneFrameData) { if (OneFrameData["grib_Right"] < 0.1 && OneFrameData["move_Right"] < 0.1) { currentGesture = gestureState.Unknown; print("current stage is unknown!"); } else if (OneFrameData["grib_Right"] > OneFrameData["move_Right"] || OneFrameData["grib_Right"] == OneFrameData["move_Right"]) { if (currentGesture != gestureState.gribing) { currentGesture = gestureState.gribing; print("gesture stage has changed, current stage is gribing!"); }
} else if (OneFrameData["grib_Right"] < OneFrameData["move_Right"]) { if (currentGesture != gestureState.moving) { currentGesture = gestureState.moving; print("gesture stage has changed, current stage is moving!"); }
} } }
|