t1 / TFDContents / Assets / KinectScripts / Samples / GetJointPositionDemo.cs @ 9
이력 | 보기 | 이력해설 | 다운로드 (2.55 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | using System.IO; |
||
4 | |||
5 | public class GetJointPositionDemo : MonoBehaviour |
||
6 | { |
||
7 | [Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
||
8 | public int playerIndex = 0; |
||
9 | |||
10 | [Tooltip("The Kinect joint we want to track.")] |
||
11 | public KinectInterop.JointType joint = KinectInterop.JointType.HandRight; |
||
12 | |||
13 | [Tooltip("Current joint position in Kinect coordinates (meters).")] |
||
14 | public Vector3 jointPosition; |
||
15 | |||
16 | [Tooltip("Whether we save the joint data to a CSV file or not.")] |
||
17 | public bool isSaving = false; |
||
18 | |||
19 | [Tooltip("Path to the CSV file, we want to save the joint data to.")] |
||
20 | public string saveFilePath = "joint_pos.csv"; |
||
21 | |||
22 | [Tooltip("How many seconds to save data to the CSV file, or 0 to save non-stop.")] |
||
23 | public float secondsToSave = 0f; |
||
24 | |||
25 | |||
26 | // start time of data saving to csv file |
||
27 | private float saveStartTime = -1f; |
||
28 | |||
29 | |||
30 | void Start() |
||
31 | { |
||
32 | if(isSaving && File.Exists(saveFilePath)) |
||
33 | { |
||
34 | File.Delete(saveFilePath); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | |||
39 | void Update() |
||
40 | { |
||
41 | if(isSaving) |
||
42 | { |
||
43 | // create the file, if needed |
||
44 | if(!File.Exists(saveFilePath)) |
||
45 | { |
||
46 | using(StreamWriter writer = File.CreateText(saveFilePath)) |
||
47 | { |
||
48 | // csv file header |
||
49 | string sLine = "time,joint,pos_x,pos_y,poz_z"; |
||
50 | writer.WriteLine(sLine); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | // check the start time |
||
55 | if(saveStartTime < 0f) |
||
56 | { |
||
57 | saveStartTime = Time.time; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // get the joint position |
||
62 | KinectManager manager = KinectManager.Instance; |
||
63 | |||
64 | if(manager && manager.IsInitialized()) |
||
65 | { |
||
66 | if(manager.IsUserDetected(playerIndex)) |
||
67 | { |
||
68 | long userId = manager.GetUserIdByIndex(playerIndex); |
||
69 | |||
70 | if(manager.IsJointTracked(userId, (int)joint)) |
||
71 | { |
||
72 | // output the joint position for easy tracking |
||
73 | Vector3 jointPos = manager.GetJointPosition(userId, (int)joint); |
||
74 | jointPosition = jointPos; |
||
75 | |||
76 | if(isSaving) |
||
77 | { |
||
78 | if((secondsToSave == 0f) || ((Time.time - saveStartTime) <= secondsToSave)) |
||
79 | { |
||
80 | #if !UNITY_WSA |
||
81 | using(StreamWriter writer = File.AppendText(saveFilePath)) |
||
82 | { |
||
83 | string sLine = string.Format("{0:F3},{1},{2:F3},{3:F3},{4:F3}", Time.time, ((KinectInterop.JointType)joint).ToString(), jointPos.x, jointPos.y, jointPos.z); |
||
84 | writer.WriteLine(sLine); |
||
85 | } |
||
86 | #else |
||
87 | string sLine = string.Format("{0:F3},{1},{2:F3},{3:F3},{4:F3}", Time.time, ((KinectInterop.JointType)joint).ToString(), jointPos.x, jointPos.y, jointPos.z); |
||
88 | Debug.Log(sLine); |
||
89 | #endif |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | } |
||
97 | |||
98 | } |