-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMakeItUnderstandGestures.java
More file actions
243 lines (200 loc) · 7.24 KB
/
MakeItUnderstandGestures.java
File metadata and controls
243 lines (200 loc) · 7.24 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
package com.androidmontreal.gesturevoicecommander.practice;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.os.Environment;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.androidmontreal.gesturevoicecommander.GestureBuilderActivity;
import com.androidmontreal.gesturevoicecommander.R;
import com.androidmontreal.gesturevoicecommander.robots.RoverLexicon;
/**
*
* Building on what we saw in MakeItListenAndRepeat, now lets make it understand
* gestures, or speech (sometimes its too noisy or too public to speak to your
* Android). Here is some super simple code that builds on the GestureBuilder
* sample code to recognize what the user wants the Android to do, and then use
* Text To Speech to tell the user what it might have understood.
*
* @author cesine
*
*/
public class MakeItUnderstandGestures extends Activity implements
OnInitListener, OnGesturePerformedListener {
//Constants used in mTestPath.
public static final String CONFIGURATION_FILENAME = "my.config";
public static final String LOG_TAG = "MyApplication-";
private static final String TAG = "MakeItUnderstandGestures";
private static final int RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE = 341;
private static final boolean D = true;
/** Talk to the user */
private TextToSpeech mTts;
/*
* A gesture library we created with the GestureBuilder, saved on the SDCard
* and then imported into the res/raw folder of this project
*/
private GestureLibrary gestureLib;
/* A little lexicon we made for the DFR Rover at Cloud Robotics Hackathon */
private RoverLexicon lexicon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTts = new TextToSpeech(this, this);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.commander, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
// Raw file in the app, text file in the SD card folder
String rawGestureFilePath = Environment.getExternalStorageDirectory().getAbsoluteFile().getAbsolutePath();
String txtFileName = "gestures.txt";
String fileOnYourSDCard = rawGestureFilePath.concat("/").concat(txtFileName); //"/storage/sdcard0/gestures.txt";
// Copy the raw file to the phone
copyRawGesturesToSDCard(fileOnYourSDCard);
// Now from the destination file
gestureLib = GestureLibraries.fromFile(fileOnYourSDCard);
// gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load()) {
finish();
}
setContentView(gestureOverlayView);
lexicon = new RoverLexicon();
}
protected void promptTheUserToTalk() {
mTts.speak(getString(R.string.im_listening), TextToSpeech.QUEUE_ADD, null);
}
/**
* Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.im_listening));
startActivityForResult(intent, RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE
&& resultCode == RESULT_OK) {
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
/* try to find a robot command in the first match */
if (matches.size() > 0) {
sendRobotThisCommand(matches.get(0));
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e(TAG, "Language is not available.");
Toast.makeText(
this,
"The " + Locale.getDefault().getDisplayLanguage()
+ " TextToSpeech isn't installed, you can go into the "
+ "\nAndroid's settings in the "
+ "\nVoice Input and Output menu to turn it on. ",
Toast.LENGTH_LONG).show();
} else {
// everything is working.
}
} else {
Toast.makeText(
this,
"Sorry, I can't talk to you because "
+ "I could not initialize TextToSpeech.", Toast.LENGTH_LONG)
.show();
}
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 3.0) {
Log.d(TAG, "Detected this gesture " + prediction.name
+ " with a score of " + prediction.score);
}
}
if (predictions.size() > 0) {
sendRobotThisCommand(predictions.get(0).name);
}
}
public String sendRobotThisCommand(String command) {
String guessedCommand = lexicon.guessWhatToDo(command);
Toast.makeText(this, guessedCommand, Toast.LENGTH_SHORT).show();
if (Locale.getDefault().getLanguage().contains("fr")) {
mTts.speak(lexicon.FR_CARRIER_PHRASE + guessedCommand,
TextToSpeech.QUEUE_ADD, null);
} else {
mTts.speak(lexicon.EN_CARRIER_PHRASE + guessedCommand,
TextToSpeech.QUEUE_ADD, null);
}
return lexicon.executeGuess();
}
public void onCommandByVoiceClick(View v) {
promptTheUserToTalk();
startVoiceRecognitionActivity();
}
public void onViewGesturesClick(View v) {
Intent i = new Intent(this, GestureBuilderActivity.class);
startActivity(i);
}
/**
* Copy the raw gestures file of the app to the SD card.
* @param fileOnYourSDCard a String containing the path to the destination file.
*/
public void copyRawGesturesToSDCard(String fileOnYourSDCard){
InputStream inStream = null;
File sdCardFile = null;
OutputStream outStream = null;
try{
inStream = getApplicationContext().getResources().openRawResource(R.raw.gestures);
sdCardFile = new File(fileOnYourSDCard);
outStream = new FileOutputStream(sdCardFile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("The file was successfully copied!");
}catch(IOException e){
e.printStackTrace();
}
}
}