TTS 无法在 tensorflow 中用于手势检测 android 应用

TTS not working in tensorflow for gesture detection android app

2021-04-14 19:34:24.826 29814-29924/org.tensorflow.lite.examples.detection I/Recognitionss: L
2021-04-14 19:34:24.835 29814-29924/org.tensorflow.lite.examples.detection I/tensorflow: MultiBoxTracker: Processing 1 results from 123
2021-04-14 19:34:24.855 29814-29814/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Preparing image 128 for detection in bg thread.
2021-04-14 19:34:24.876 29814-29924/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Running detection on image 128
        <Button
            android:id="@+id/btnSpeak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="10dp"
            android:text="Voice Conversion" />

        <EditText
            android:id="@+id/txtText"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:inputType="text"/>
package org.tensorflow.lite.examples.detection;

import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class AndroidTextToSpeechActivity extends AppCompatActivity {

    Button btnSpeak;
    EditText editText;

    TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tfe_od_layout_bottom_sheet);

        // Init TextToSpeech
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.ENGLISH);
                    if (result == TextToSpeech.LANG_MISSING_DATA ||
                            result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(getApplicationContext(), "This language is not supported!",
                                Toast.LENGTH_SHORT);
                    } else {
                        btnSpeak.setEnabled(true);
                        textToSpeech.setPitch(0.6f);
                        textToSpeech.setSpeechRate(1.0f);

                        speak();
                    }
                }
            }
        });

        // Init View
        btnSpeak = (Button)findViewById(R.id.btnSpeak);
        editText = (EditText)findViewById(R.id.txtText);

        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak();
            }
        });
    }

    private void speak() {
        String text = editText.getText().toString();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
        } else {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    @Override
    protected void onDestroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }
}

  • 解决方案
  • 添加了下面的代码,其中添加了一个 textview 和一个 imageview 你可以使用按钮或任何你喜欢使用的东西,这给了我下面的输出。
  • 然后在CameraActivity.java下定义ImageView
  private speakImageView;
  speakImageView = findViewById(R.id.speak1);
  // set onClick listener
  speakImageView.setOnClickListener(this);
  • 然后在onClick方法下我定义了如果遇到特定的字母播放那个字母的声音。我使用了 mp3 声音文件,因为我无法实现 TTS,所以我选择使用 mp3
@Override
  public void onClick(View v) {
   // some code
    ...
    // voice for the detected alphabet
    else if (v.getId() == R.id.speak1) {
      // this is the value we are showing
      EditText mEdit = findViewById(R.id.editText);
      String letter = mEdit.getText().toString();
      if (letter.equalsIgnoreCase("A")) {
        final MediaPlayer mp100 = MediaPlayer.create(this, R.raw.letter_a);
        mp100.start();
      }
      else if (letter.equalsIgnoreCase("B")) {
        final MediaPlayer mp100 = MediaPlayer.create(this, R.raw.letter_b);
        mp100.start();
      }
      // similarly you can do the same for other alphabets also
    }
  }
  • 这不是理想的方法,但由于 ```TTS`` 对我不起作用,因为我是 android.[=30 的新手,几乎搜索了一周或两周后使用了此方法=]