50c*_*ver 3 android speech-recognition cmusphinx recognizer-intent
我创建了一个简单的 android 应用程序来控制连接到我的 Raspberry Pi 的继电器。我使用按钮以及基本的语音识别来触发这些按钮并打开/关闭相应的中继通道。
到目前为止,语音识别部分由 RecognizerIntent 处理,其中我需要按下我的应用程序上的一个按钮来打开一个谷歌语音提示,它会听取我的语音命令并激活/停用控制继电器开关的相应按钮。
我想通过连续语音识别来做同样的事情,它允许应用程序连续收听我的命令,而无需用户按下应用程序上的按钮,从而实现免提操作。
这是我现有的代码,这是一种非常简单的语音识别方法,可以让我打开和关闭连接到继电器的各种设备的按钮:
public void micclick(View view) {
if(view.getId()==R.id.mic)
{promptSpeechInput();}
}
private void promptSpeechInput() {
Intent i= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak!");
try{
startActivityForResult(i,100);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this,"Sorry your device doesn't support",Toast.LENGTH_SHORT).show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent i) {
super.onActivityResult(requestCode, resultCode, i);
String voicetxt;
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK && i != null) {
ArrayList<String> result2 = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voicetxt = result2.get(0);
if (voicetxt.equals("fan on")) {
StringBuffer result=new StringBuffer();
toggleButton1.setChecked(true);
result.append("Fan: ").append(toggleButton1.getText());
sc.onRelayNumber="a";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("fan of")) {
StringBuffer result=new StringBuffer();
toggleButton1.setChecked(false);
result.append("Fan: ").append(toggleButton1.getText());
sc.onRelayNumber = "a_off";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("light on")) {
StringBuffer result=new StringBuffer();
toggleButton2.setChecked(true);
result.append("Light: ").append(toggleButton2.getText());
sc.onRelayNumber = "b";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("light off")) {
StringBuffer result=new StringBuffer();
toggleButton2.setChecked(false);
result.append("Light: ").append(toggleButton2.getText());
sc.onRelayNumber = "b_off";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("air conditioner on")) {
StringBuffer result=new StringBuffer();
toggleButton3.setChecked(true);
result.append("AC: ").append(toggleButton3.getText());
sc.onRelayNumber = "c";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("air conditioner of")) {
StringBuffer result=new StringBuffer();
toggleButton3.setChecked(false);
result.append("AC: ").append(toggleButton3.getText());
sc.onRelayNumber = "c_off";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("heater on")) {
StringBuffer result=new StringBuffer();
toggleButton4.setChecked(true);
result.append("Heater: ").append(toggleButton4.getText());
sc.onRelayNumber = "d";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
if (voicetxt.equals("heater off")) {
StringBuffer result=new StringBuffer();
toggleButton4.setChecked(false);
result.append("Heater: ").append(toggleButton4.getText());
sc.onRelayNumber = "d_off";
new Thread(sc).start();
Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
}
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在无需按下按钮的情况下实现相同的功能。请注意,我是 Android 应用程序开发的新手。如果可能,请描述外部库的使用,如果需要,因为我认为使用 Google 的 RecognizerIntent 无法进行连续识别。我推测我可能需要包含像CMUSphinx这样的库,但我不确定如何去做。
小智 5
您可以为连续识别/听写模式做几件事。您可以使用 android 本身的 google 语音识别,不建议连续识别(如https://developer.android.com/reference/android/speech/SpeechRecognizer.html 所述)
此 API 的实现可能将音频流式传输到远程服务器以执行语音识别。因此,此 API 不打算用于连续识别,这会消耗大量电池和带宽。
但是如果你真的需要它,你可以通过创建你自己的类并继承 IRecognitionListener 来做一个解决方法。(我是在 xamarin-android 上写的,语法和原生 android 非常相似)
public class CustomRecognizer : Java.Lang.Object, IRecognitionListener, TextToSpeech.IOnInitListener
{
private SpeechRecognizer _speech;
private Intent _speechIntent;
public string Words;
public CustomRecognizer(Context _context)
{
this._context = _context;
Words = "";
_speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
_speech.SetRecognitionListener(this);
_speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
_speechIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
_speechIntent.PutExtra(RecognizerIntent.ActionRecognizeSpeech, RecognizerIntent.ExtraPreferOffline);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
}
void startover()
{
_speech.Destroy();
_speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
_speech.SetRecognitionListener(this);
_speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
StartListening();
}
public void StartListening()
{
_speech.StartListening(_speechIntent);
}
public void StopListening()
{
_speech.StopListening();
}
public void OnBeginningOfSpeech()
{
}
public void OnBufferReceived(byte[] buffer)
{
}
public void OnEndOfSpeech()
{
}
public void OnError([GeneratedEnum] SpeechRecognizerError error)
{
Words = error.ToString();
startover();
}
public void OnEvent(int eventType, Bundle @params)
{
}
public void OnPartialResults(Bundle partialResults)
{
}
public void OnReadyForSpeech(Bundle @params)
{
}
public void OnResults(Bundle results)
{
var matches = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition);
if (matches == null)
Words = "Null";
else
if (matches.Count != 0)
Words = matches[0];
else
Words = "";
//do anything you want for the result
}
startover();
}
public void OnRmsChanged(float rmsdB)
{
}
public void OnInit([GeneratedEnum] OperationResult status)
{
if (status == OperationResult.Error)
txtspeech.SetLanguage(Java.Util.Locale.Default);
}
}
Run Code Online (Sandbox Code Playgroud)
在活动中调用它:
void StartRecording()
{
string rec = PackageManager.FeatureMicrophone;
if (rec != "android.hardware.microphone")
{
// no microphone, no recording. Disable the button and output an alert
Toast.MakeText(this, "NO MICROPHONE", ToastLength.Short);
}
else
{
//you can pass any object you want to connect to your recognizer here (I am passing the activity)
CustomRecognizer voice = new CustomRecognizer(this);
voice.StartListening();
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记请求使用麦克风的许可!
解释 :
- 这将消除烦人的“点击开始录制”
- 这将始终记录您调用 StartListening() 的那一刻并且永远不会停止,因为每次完成记录时我总是调用 startover() 或 StartListening()
- 这是一个非常糟糕的解决方法,因为在处理您的录音时,录音机在调用 StartListening() 之前不会获得任何声音输入(没有解决方法)
- 谷歌识别对于语音命令来说并不是很好,因为语言模型是“[lang] 句子”,所以你不能限制这个词,谷歌总是会尝试制作一个“好句子”。
为了更好的结果和UX,我真的建议你使用Google Cloud API(但它必须在线,而且成本高),第二个建议是CMUSphinx / PocketSphinx,它是开源的,可以做离线模式,但你必须做所有事情手动
PocketSphinx 优势:
离线模式兼容
您可以对声学模型(语音等)进行自己的训练,因此您可以根据您的环境和发音对其进行配置
PocketSphinx 的缺点:您必须手动完成所有事情,从设置声学模型、字典、语言模型、阈值等(如果您想要简单的东西,那就太过分了)。
| 归档时间: |
|
| 查看次数: |
1798 次 |
| 最近记录: |