-1

任何人都可以帮助我或给我有关阿拉伯语语音识别的线索 - Android 离线?

我想开发一个简单的应用程序来做语音识别 - 离线做这个在线我知道如何......如果有人有建议或链接到一个开源项目甚至图书馆购买,它会有效地做到这一点,这将是惊人的。

提前致谢。

4

2 回答 2

3

利用

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"ar-JO");

下面的代码对我来说很好用:

package tk.oryx.voice;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {

    private TextView mText;
    private MediaRecorder recorder;
    private SpeechRecognizer sr;
    private static final String TAG = "MyStt3Activity";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mText = (TextView) findViewById(R.id.textView1);
        speakButton.setOnClickListener(this);
        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(new listener());
    }

    class listener implements RecognitionListener
    {
        public void onReadyForSpeech(Bundle params){ }
        public void onBeginningOfSpeech(){ }
        public void onRmsChanged(float rmsdB){ }
        public void onBufferReceived(byte[] buffer) { }
        public void onEndOfSpeech(){ }
        public void onError(int error)
        {
            mText.setText("error " + error);
        }
        public void onResults(Bundle results)
        {
            String str = new String();
            Log.d(TAG, "onResults " + results);
            ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
         /*   for (int i = 0; i < data.size(); i++)
            {
                Log.d(TAG, "result " + data.get(i));
                str += data.get(i);
            }
         */
          //  mText.setText("results: "+str+" "+String.valueOf(data.size()));
            mText.setText("results: "+data.get(0));
        }
        public void onPartialResults(Bundle partialResults)
        {
            Log.d(TAG, "onPartialResults");
        }
        public void onEvent(int eventType, Bundle params)
        {
            Log.d(TAG, "onEvent " + eventType);
        }
    }
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak)
        {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"ar-JO");
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"tk.oryx.voice");
         //   intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 20000); // value to wait

            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);  // 1 is the maximum number of results to be returned.
            sr.startListening(intent);
        }
    }
}

布局非常简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tk.oryx.voice.MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/progressBar1"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/progressBar1"
        android:layout_alignStart="@+id/progressBar1"
        android:layout_marginTop="220dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_speak"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="Button"
        tools:layout_editor_absoluteY="295dp"
        tools:layout_editor_absoluteX="148dp"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_alignStart="@+id/progressBar1"
        android:layout_marginTop="18dp" />

</RelativeLayout>

清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tk.oryx.voice">

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
于 2017-03-19T11:36:04.060 回答
0

在这个地址有一个多语言语音到文本的项目

https://github.com/MaryamAzhdari/speechToTextMultiLanguage

通过summery,您应该添加以下代码:

imv_arabic.setOnClickListener{
        val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        intent.putExtra(
            RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
        )
        //For some county you can use both of the below lines
        //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.FRANCE)
        //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fr-FR")//French (France)

        //Some countries not define in Locale
        //You can use this page for finding your language
        //https://cloud.google.com/speech-to-text/docs/languages
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-AE")//Arabic (United Arab Emirates)
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak")

        try {
            startActivityForResult(intent, REQ_CODE)
        } catch (a: ActivityNotFoundException) {
            Toast.makeText(
                applicationContext,
                "Sorry! Your device not supported",
                Toast.LENGTH_SHORT
            ).show()
        }
    }


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if(requestCode==REQ_CODE){
        if (resultCode == RESULT_OK && data!=null) {
            val result = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            tv_result?.text=result[0].toString()
        }
    }
}

还在清单中添加此权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

以及 6 以上的 android 代码:

  private fun setupPermissions() {
    val permission = ContextCompat.checkSelfPermission(this,
        Manifest.permission.RECORD_AUDIO)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        //Log.i(Tag, "Permission to record denied")
    }
}

要查找任何语言,请参阅此地址:

https://cloud.google.com/speech-to-text/docs/languages

于 2019-07-22T04:55:25.077 回答