Skip to content

Commit

Permalink
Improving the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lasp73 committed Mar 11, 2019
1 parent 4511a99 commit 8bf84fc
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;

import br.com.cpqd.asr.recognizer.AudioSource;
Expand All @@ -42,14 +43,21 @@ public class BatchRecognizer {

private SpeechRecognizer recognizer;

public BatchRecognizer(String serverUrl, String user, String pwd)
public BatchRecognizer(Properties pa)
throws URISyntaxException, IOException, RecognitionException {

String serverUrl = pa.getProperty("server");
String user = pa.getProperty("user");
String pwd = pa.getProperty("pwd");

RecognitionConfig config = RecognitionConfig.builder()
.maxSentences(1)
.continuousMode(true)
.recognitionTimeoutEnabled(false)
.noInputTimeoutEnabled(false)
.endPointerLevelThreshold(Integer.parseInt(pa.getProperty("endpointer.levelThreshold", "2")))
.confidenceThreshold(Integer.parseInt(pa.getProperty("decoder.confidenceThreshold", "30")))
.waitEndMilis(Integer.parseInt(pa.getProperty("endpointer.waitEnd", "2000")))
.build();

recognizer = SpeechRecognizer.builder().serverURL(serverUrl).userAgent("client=JavaSE;app=BatchRecognizer")
Expand Down Expand Up @@ -135,16 +143,16 @@ public void close() {

public static void main(String[] args) throws IOException, URISyntaxException, RecognitionException {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: BatchRecognizer --server <Server URL> --lm <LM URI> --audio <Audio Path> [--user <username> --pwd <password>]");
System.err.println(" e.g.: BatchRecognizer --server ws://127.0.0.1:8025/asr-server/asr --lm builtin:slm/general --audio audio/pt-br/87431_8k.wav");
return;
}

BatchRecognizer recognizer = new BatchRecognizer(pa.getArg("server"), pa.getArg("user"), pa.getArg("pwd"));
recognizer.recognize(pa.getArg("audio"), pa.getArg("lm"));
BatchRecognizer recognizer = new BatchRecognizer(pa);
recognizer.recognize(pa.getProperty("audio"), pa.getProperty("lm"));
recognizer.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package br.com.cpqd.asr.recognizer.example;

import java.io.FileInputStream;
import java.util.Properties;

import br.com.cpqd.asr.recognizer.BufferAudioSource;
import br.com.cpqd.asr.recognizer.LanguageModelList;
Expand All @@ -35,7 +36,7 @@ public class BufferRecognizer {

public static void main(String[] args) throws Exception {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: BufferRecognizer --server <Server URL> --lm <LM URI> --audio <Audio Path> [--user <username> --pwd <password>]");
Expand All @@ -44,17 +45,17 @@ public static void main(String[] args) throws Exception {
}

RecognitionConfig config = RecognitionConfig.builder().maxSentences(1).confidenceThreshold(70).build();
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();
SpeechRecognizer asr = SpeechRecognizer.builder()
.serverURL(pa.getArg("server"))
.credentials(pa.getArg("user"), pa.getArg("pwd"))
.serverURL(pa.getProperty("server"))
.credentials(pa.getProperty("user"), pa.getProperty("pwd"))
.recogConfig(config).build();

// cria um objeto BufferAudioSource para receber os bytes de audio
BufferAudioSource audio = new BufferAudioSource();

// Usando outra thead para enviar o áudio para o ASR
ReadAudioThread thread = new ReadAudioThread(audio, pa.getArg("audio"));
ReadAudioThread thread = new ReadAudioThread(audio, pa.getProperty("audio"));
thread.start();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package br.com.cpqd.asr.recognizer.example;

import java.io.File;
import java.util.Properties;

import br.com.cpqd.asr.recognizer.AudioSource;
import br.com.cpqd.asr.recognizer.FileAudioSource;
Expand All @@ -32,7 +33,7 @@ public class ContinuousModeRecognizer {

public static void main(String[] args) throws Exception {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: ContinuousModeRecognizer --server <Server URL> --lm <LM URI> --audio <Audio Path> [--user <username> --pwd <password>]");
Expand All @@ -45,14 +46,15 @@ public static void main(String[] args) throws Exception {
.continuousMode(true)
.noInputTimeoutEnabled(false)
.recognitionTimeoutEnabled(false)
.endPointerLevelThreshold(Integer.parseInt(pa.getProperty("endpointer.levelThreshold", "2")))
.confidenceThreshold(70).build();

AudioSource audio = new FileAudioSource(new File(pa.getArg("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
AudioSource audio = new FileAudioSource(new File(pa.getProperty("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();

SpeechRecognizer asr = SpeechRecognizer.builder()
.serverURL(pa.getArg("server"))
.credentials(pa.getArg("user"), pa.getArg("pwd")).recogConfig(config)
.serverURL(pa.getProperty("server"))
.credentials(pa.getProperty("user"), pa.getProperty("pwd")).recogConfig(config)
.addListener(new SimpleRecognizerListener() {
@Override
public void onRecognitionResult(RecognitionResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package br.com.cpqd.asr.recognizer.example;

import java.io.File;
import java.util.Properties;

import br.com.cpqd.asr.recognizer.AudioSource;
import br.com.cpqd.asr.recognizer.FileAudioSource;
Expand All @@ -31,7 +32,7 @@ public class FileRecognizer {

public static void main(String[] args) throws Exception {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: FileRecognizer --server <Server URL> --lm <LM URI> --audio <Audio Path> [--user <username> --pwd <password>]");
Expand All @@ -40,9 +41,9 @@ public static void main(String[] args) throws Exception {
}

RecognitionConfig config = RecognitionConfig.builder().maxSentences(1).confidenceThreshold(70).build();
AudioSource audio = new FileAudioSource(new File(pa.getArg("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getArg("server")).credentials(pa.getArg("user"), pa.getArg("pwd")).recogConfig(config).build();
AudioSource audio = new FileAudioSource(new File(pa.getProperty("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getProperty("server")).credentials(pa.getProperty("user"), pa.getProperty("pwd")).recogConfig(config).build();

try {
asr.recognize(audio, lm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

import javax.websocket.DeploymentException;

Expand Down Expand Up @@ -47,14 +48,14 @@ public static void main(String[] args) throws DeploymentException, IOException,
return;
}

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

RecognitionConfig config = RecognitionConfig.builder().maxSentences(1).confidenceThreshold(70).build();
System.out.println(pa.getArg("audio"));
AudioSource audio = new FileAudioSource(new File(pa.getArg("audio")));
String grammarBody = new String(Files.readAllBytes(Paths.get(pa.getArg("lm"))));
System.out.println(pa.getProperty("audio"));
AudioSource audio = new FileAudioSource(new File(pa.getProperty("audio")));
String grammarBody = new String(Files.readAllBytes(Paths.get(pa.getProperty("lm"))));
LanguageModelList lm = LanguageModelList.builder().addInlineGrammar("mygram", grammarBody).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getArg("server")).credentials(pa.getArg("user"), pa.getArg("pwd")).recogConfig(config).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getProperty("server")).credentials(pa.getProperty("user"), pa.getProperty("pwd")).recogConfig(config).build();

try {
asr.recognize(audio, lm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.LineUnavailableException;
Expand All @@ -44,7 +45,7 @@ public class MicContinuousRecognizer {
public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException,
RecognitionException, LineUnavailableException {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: MicContinuousRecognizer --server <Server URL> --lm <LM URI> [--user <username> --pwd <password>]");
Expand All @@ -60,11 +61,11 @@ public static void main(String[] args) throws DeploymentException, IOException,
.confidenceThreshold(70).build();

MicAudioSource audio = new MicAudioSource(new AudioFormat(8000F, 16, 1, true, false));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();

SpeechRecognizer asr = SpeechRecognizer.builder()
.serverURL(pa.getArg("server"))
.credentials(pa.getArg("user"), pa.getArg("pwd"))
.serverURL(pa.getProperty("server"))
.credentials(pa.getProperty("user"), pa.getProperty("pwd"))
.recogConfig(config)
.addListener(new SimpleRecognizerListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.LineUnavailableException;
Expand Down Expand Up @@ -50,15 +51,15 @@ public static void main(String[] args) throws DeploymentException, IOException,
return;
}

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

RecognitionConfig config = RecognitionConfig.builder().maxSentences(1).confidenceThreshold(70).build();
MicAudioSource audio = new MicAudioSource(new AudioFormat(8000F, 16, 1, true, false));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();

SpeechRecognizer asr = SpeechRecognizer.builder()
.serverURL(pa.getArg("server"))
.credentials(pa.getArg("user"), pa.getArg("pwd"))
.serverURL(pa.getProperty("server"))
.credentials(pa.getProperty("user"), pa.getProperty("pwd"))
.addListener(new SimpleRecognizerListener() {
@Override
public void onSpeechStart(Integer time) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package br.com.cpqd.asr.recognizer.example;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ProgramArguments {

private Map<String, String> arguments = new HashMap<>();
public static Properties parseFrom(String[] args) {

public static ProgramArguments from(String[] args) {

ProgramArguments pa = new ProgramArguments();
Properties arguments = new Properties();

String name = null;
for (String arg : args) {
Expand All @@ -20,21 +17,12 @@ public static ProgramArguments from(String[] args) {
throw new RuntimeException("Invalid argument: " + arg);
}
} else {
pa.arguments.put(name, arg);
arguments.put(name, arg);
name = null;
}
}

return pa;
}

public String getArg(String name) {
return arguments.get(name);
}

@Override
public String toString() {
return "Arguments[" + arguments.toString() + "]";
return arguments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.websocket.DeploymentException;

Expand All @@ -36,7 +37,7 @@ public class WordDetailRecognizer {

public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException, RecognitionException {

ProgramArguments pa = ProgramArguments.from(args);
Properties pa = ProgramArguments.parseFrom(args);

if (args.length == 0) {
System.err.println("Usage: WordDetailRecognizer --server <Server URL> --lm <LM URI> --audio <Audio Path> [--user <username> --pwd <password>]");
Expand All @@ -45,9 +46,9 @@ public static void main(String[] args) throws DeploymentException, IOException,
}

RecognitionConfig config = RecognitionConfig.builder().maxSentences(1).confidenceThreshold(70).build();
AudioSource audio = new FileAudioSource(new File(pa.getArg("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getArg("lm")).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getArg("server")).credentials(pa.getArg("user"), pa.getArg("pwd")).recogConfig(config).build();
AudioSource audio = new FileAudioSource(new File(pa.getProperty("audio")));
LanguageModelList lm = LanguageModelList.builder().addFromURI(pa.getProperty("lm")).build();
SpeechRecognizer asr = SpeechRecognizer.builder().serverURL(pa.getProperty("server")).credentials(pa.getProperty("user"), pa.getProperty("pwd")).recogConfig(config).build();

try {
asr.recognize(audio, lm);
Expand Down

0 comments on commit 8bf84fc

Please sign in to comment.