Issue
I am making a gesture detection app in which if you perform the ASL it detects the sign you are performing and shows the result like below.
I have also implemented a
EditText
field in which the result is retrieved. I am trying to toText-To-Speech
orTTS
.I am using
Log.i("Recognitionss", String.valueOf(results.get(0).getTitle()));
to log the result inDetectorActivity.java
Log result is generated like below which gives me the gesture, and I have passed it to the
XML
file
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
- I have added
Button
andEditText
to mybottom sheet
navigation.
<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"/>
- Java code for
TTS
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();
}
}
- The problem is when I click on the button there is no voice feedback and there is no error in the console or logcat.
Solution
- SOLUTION
- Added the below code in this in which have added a
textview
and aimageview
you can use button or whatever you like to use, which gave me the below output. - Then under
CameraActivity.java
define theImageView
private speakImageView;
speakImageView = findViewById(R.id.speak1);
// set onClick listener
speakImageView.setOnClickListener(this);
- Then under onClick method I am defined if the particular letter is encountered play that letter sound. I have used
mp3
sound file as I was not able to implementTTS
so I opted to go withmp3
.
@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
}
}
- Not the ideal approach but as ```TTS`` didn't worked for me, used this method after almost searching for a week or 2 week as I am new to android.
Answered By - Lav Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.