Skip to content

Commit fce57ab

Browse files
nnegreykurtisvg
authored andcommitted
Model selection (#1074)
* Add model selection to streaming sample * Fix model selection naming
1 parent 8d607cb commit fce57ab

File tree

4 files changed

+88
-37
lines changed

4 files changed

+88
-37
lines changed

speech/cloud-client/README.md

+13-22
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,54 @@ Install [Maven](http://maven.apache.org/).
1919
Build your project with:
2020

2121
```
22-
mvn clean compile assembly:single
22+
mvn clean package
2323
```
2424

2525
## Quickstart
2626
Transcribe a local audio file
2727
```
28-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
29-
com.example.speech.QuickstartSample
28+
mvn exec:java -DQuickstart
3029
```
3130

3231
## Transcribe a audio file
3332
Transcribe a local audio file
3433
```
35-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
36-
com.example.speech.Recognize syncrecognize ./resources/audio.raw
34+
mvn exec:java -DRecognize -Dexec.args="syncrecognize ./resources/audio.raw"
3735
```
3836

3937
Asynchronously transcribe a local audio file
4038
```
41-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
42-
com.example.speech.Recognize asyncrecognize ./resources/audio.raw
39+
mvn exec:java -DRecognize -Dexec.args="asyncrecognize ./resources/audio.raw"
4340
```
4441

4542
Transcribe a remote audio file
4643
```
47-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
48-
com.example.speech.Recognize syncrecognize gs://cloud-samples-tests/speech/brooklyn.flac
44+
mvn exec:java -DRecognize -Dexec.args="syncrecognize gs://cloud-samples-tests/speech/brooklyn.flac"
4945
```
5046

5147
Asynchronously transcribe a remote audio file
5248
```
53-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
54-
com.example.speech.Recognize asyncrecognize gs://cloud-samples-tests/speech/vr.flac
49+
mvn exec:java -DRecognize -Dexec.args="asyncrecognize gs://cloud-samples-tests/speech/vr.flac"
5550
```
5651

5752
## Transcribe a audio file and print word offsets
5853
Synchronously transcribe an audio file and print word offsets
5954
```
60-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
61-
com.example.speech.Recognize wordoffsets ./resources/audio.raw
55+
mvn exec:java -DRecognize -Dexec.args="wordoffsets ./resources/audio.raw"
6256
```
6357

6458
Asynchronously transcribe a remote audio file and print word offsets
6559
```
66-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
67-
com.example.speech.Recognize wordoffsets gs://cloud-samples-tests/speech/vr.flac
60+
mvn exec:java -DRecognize -Dexec.args="wordoffsets gs://cloud-samples-tests/speech/vr.flac"
6861
```
6962

70-
## Transcribe a video file
71-
Synchronously transcribe a video file
63+
## Model Selection
64+
Synchronously transcribe a audio file
7265
```
73-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
74-
com.example.speech.Recognize video ./resources/Google_Gnome.wav
66+
mvn exec:java -DRecognize -Dexec.args="model-selection ./resources/Google_Gnome.wav"
7567
```
7668

77-
Asynchronously transcribe a video file hosted on GCS
69+
Asynchronously transcribe a audio file hosted on GCS
7870
```
79-
java -cp target/speech-google-cloud-samples-1.0.0-jar-with-dependencies.jar \
80-
com.example.speech.Recognize video gs://cloud-samples-tests/speech/Google_Gnome.wav
71+
mvn exec:java -DRecognize -Dexec.args="model-selection gs://cloud-samples-tests/speech/Google_Gnome.wav"
8172
```

speech/cloud-client/pom.xml

+60
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,64 @@
7676
</plugin>
7777
</plugins>
7878
</build>
79+
80+
<profiles>
81+
<profile>
82+
<id>Quickstart</id>
83+
<activation>
84+
<property>
85+
<name>Quickstart</name>
86+
</property>
87+
</activation>
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.codehaus.mojo</groupId>
92+
<artifactId>exec-maven-plugin</artifactId>
93+
<version>1.6.0</version>
94+
<executions>
95+
<execution>
96+
<goals>
97+
<goal>java</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
<configuration>
102+
<mainClass>com.example.speech.QuickstartSample</mainClass>
103+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
104+
</configuration>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</profile>
109+
110+
<profile>
111+
<id>Recognize</id>
112+
<activation>
113+
<property>
114+
<name>Recognize</name>
115+
</property>
116+
</activation>
117+
<build>
118+
<plugins>
119+
<plugin>
120+
<groupId>org.codehaus.mojo</groupId>
121+
<artifactId>exec-maven-plugin</artifactId>
122+
<version>1.6.0</version>
123+
<executions>
124+
<execution>
125+
<goals>
126+
<goal>java</goal>
127+
</goals>
128+
</execution>
129+
</executions>
130+
<configuration>
131+
<mainClass>com.example.speech.Recognize</mainClass>
132+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
133+
</configuration>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</profile>
138+
</profiles>
79139
</project>

speech/cloud-client/src/main/java/com/example/speech/Recognize.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void main(String... args) throws Exception {
5252
System.out.printf(
5353
"\tjava %s \"<command>\" \"<path-to-image>\"\n"
5454
+ "Commands:\n"
55-
+ "\tsyncrecognize | asyncrecognize | streamrecognize | wordoffsets\n"
55+
+ "\tsyncrecognize | asyncrecognize | streamrecognize | wordoffsets | model-selection\n"
5656
+ "Path:\n\tA file path (ex: ./resources/audio.raw) or a URI "
5757
+ "for a Cloud Storage resource (gs://...)\n",
5858
Recognize.class.getCanonicalName());
@@ -82,11 +82,11 @@ public static void main(String... args) throws Exception {
8282
}
8383
} else if (command.equals("streamrecognize")) {
8484
streamingRecognizeFile(path);
85-
} else if (command.equals("video")) {
85+
} else if (command.equals("model-selection")) {
8686
if (path.startsWith("gs://")) {
87-
transcribeGcsVideoFile(path);
87+
transcribeModelSelectionGcs(path);
8888
} else {
89-
transcribeVideoFile(path);
89+
transcribeModelSelection(path);
9090
}
9191
}
9292
}
@@ -420,10 +420,10 @@ public SettableFuture<List<T>> future() {
420420
// [START speech_transcribe_model_selection]
421421
/**
422422
* Performs transcription of the given audio file synchronously with
423-
* video as the original media type.
424-
* @param fileName the path to a video file to transcribe
423+
* the selected model.
424+
* @param fileName the path to a audio file to transcribe
425425
*/
426-
public static void transcribeVideoFile(String fileName) throws Exception {
426+
public static void transcribeModelSelection(String fileName) throws Exception {
427427
Path path = Paths.get(fileName);
428428
byte[] content = Files.readAllBytes(path);
429429

@@ -456,11 +456,11 @@ public static void transcribeVideoFile(String fileName) throws Exception {
456456

457457
// [START speech_transcribe_model_selection_gcs]
458458
/**
459-
* Performs transcription on remote video file and prints the transcription.
460-
*
461-
* @param gcsUri the path to the remote video file to transcribe.
459+
* Performs transcription of the remote audio file asynchronously with
460+
* the selected model.
461+
* @param gcsUri the path to the remote audio file to transcribe.
462462
*/
463-
public static void transcribeGcsVideoFile(String gcsUri) throws Exception {
463+
public static void transcribeModelSelectionGcs(String gcsUri) throws Exception {
464464
try (SpeechClient speech = SpeechClient.create()) {
465465

466466
// Configure request with video media type

speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ public void testStreamRecognize() throws Exception {
110110
}
111111

112112
@Test
113-
public void testVideoTranscription() throws Exception {
114-
Recognize.transcribeVideoFile(videoFileName);
113+
public void testModelSelection() throws Exception {
114+
Recognize.transcribeModelSelection(videoFileName);
115115
String got = bout.toString();
116116
assertThat(got).contains("OK Google");
117117
assertThat(got).contains("the weather outside is sunny");
118118
}
119119

120120
@Test
121-
public void testGcsVideoTranscription() throws Exception {
122-
Recognize.transcribeGcsVideoFile(gcsVideoPath);
121+
public void testGcsModelSelection() throws Exception {
122+
Recognize.transcribeModelSelectionGcs(gcsVideoPath);
123123
String got = bout.toString();
124124
assertThat(got).contains("OK Google");
125125
assertThat(got).contains("the weather outside is sunny");

0 commit comments

Comments
 (0)