11using UnityEngine ;
2+ using System ;
23using System . Collections ;
34using System . Collections . Generic ;
5+ using System . IO ;
46
57using PDollarGestureRecognizer ;
68using PDollarDemo ;
@@ -26,16 +28,22 @@ public class CapturePoints : MonoBehaviour {
2628 //GUI
2729 private string message ;
2830 private bool recognized ;
31+ private string newGestureName = "" ;
2932
3033 void Start ( ) {
3134
3235 platform = Application . platform ;
3336 drawArea = new Rect ( 0 , 0 , Screen . width - Screen . width / 3 , Screen . height ) ;
3437
38+ //Load pre-made gestures
3539 TextAsset [ ] gesturesXml = Resources . LoadAll < TextAsset > ( "GestureSet/10-stylus-MEDIUM/" ) ;
36-
3740 foreach ( TextAsset gestureXml in gesturesXml )
38- trainingSet . Add ( GestureIO . ReadGesture ( gestureXml . text ) ) ;
41+ trainingSet . Add ( GestureIO . ReadGestureFromXML ( gestureXml . text ) ) ;
42+
43+ //Load user custom gestures
44+ string [ ] filePaths = Directory . GetFiles ( Application . persistentDataPath , "*.xml" ) ;
45+ foreach ( string filePath in filePaths )
46+ trainingSet . Add ( GestureIO . ReadGestureFromFile ( filePath ) ) ;
3947 }
4048
4149 void Update ( ) {
@@ -57,6 +65,7 @@ void Update () {
5765 if ( recognized ) {
5866
5967 recognized = false ;
68+ strokeId = - 1 ;
6069
6170 points . Clear ( ) ;
6271
@@ -100,15 +109,25 @@ void OnGUI() {
100109 recognized = true ;
101110
102111 Gesture candidate = new Gesture ( points . ToArray ( ) ) ;
103-
104112 Result gestureResult = PointCloudRecognizer . Classify ( candidate , trainingSet . ToArray ( ) ) ;
105113
106114 message = gestureResult . GestureClass + " " + gestureResult . Score ;
107-
108- strokeId = - 1 ;
109115 }
110- }
111116
117+ GUI . Label ( new Rect ( Screen . width - 200 , 150 , 70 , 30 ) , "Add as: " ) ;
118+ newGestureName = GUI . TextField ( new Rect ( Screen . width - 150 , 150 , 100 , 30 ) , newGestureName ) ;
119+
120+ if ( GUI . Button ( new Rect ( Screen . width - 50 , 150 , 50 , 30 ) , "Add" ) && points . Count > 0 && newGestureName != "" ) {
121+
122+ string fileName = String . Format ( "{0}/{1}-{2}.xml" , Application . persistentDataPath , newGestureName , DateTime . Now . ToFileTime ( ) ) ;
123+ GestureIO . WriteGesture ( points . ToArray ( ) , newGestureName , fileName ) ;
124+
125+ trainingSet . Add ( new Gesture ( points . ToArray ( ) , newGestureName ) ) ;
126+
127+ newGestureName = "" ;
128+ }
129+ }
130+
112131 private Vector3 WorldCoordinateForGesturePoint ( Vector3 gesturePoint ) {
113132
114133 Vector3 worldCoordinate = new Vector3 ( gesturePoint . x , gesturePoint . y , 10 ) ;
0 commit comments