0

Trying to develop a speech to text application using Google's API with below code

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.testng.annotations.Test;

public class Speech2Text_Test {
@Test
public void f() {

  try{
  Path path = Paths.get("out.flac");
  byte[] data = Files.readAllBytes(path);

  String request = "https://www.google.com/"+
       "speech-api/v2/recognize?"+
       "xjerr=1&client=speech2text&lang=en-US&maxresults=10"+
       "output=json&key=<My Key>";

  URL url = new URL(request);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();          
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setInstanceFollowRedirects(false);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
  connection.setRequestProperty("User-Agent", "speech2text");
  connection.setConnectTimeout(60000);
  connection.setUseCaches (false);

  DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
  wr.write(data);
  wr.flush();
  wr.close();
  connection.disconnect();

  System.out.println("Done");

  BufferedReader in = new BufferedReader(
      new InputStreamReader(
      connection.getInputStream()));
       String decodedString;
       while ((decodedString = in.readLine()) != null) {
       System.out.println(decodedString);
       }

  }
  catch(Exception e){
  e.printStackTrace();
  }

  }
}

however after running the class (which sends .flac file to Google api) am getting as "{"result":[]}" Instead of the utterances of the audio file converted to text, what could be the cases Google returns the result as "{"result":[]}"?

enter image description here

Vinod
  • 318
  • 2
  • 9
  • 31
  • Possible duplicate of [Google Speech Recognition API Result is Empty](http://stackoverflow.com/questions/38906527/google-speech-recognition-api-result-is-empty) – Nikolay Shmyrev Feb 16 '17 at 19:50

1 Answers1

3

Ran into the same issue my self. I found that is was the format of the flac file. It needs to be 16-bit PCM and mono otherwise you get the null result back. I use http://www.audacityteam.org/ to check/convert my files.

Kristofer Källsbo
  • 877
  • 1
  • 8
  • 21
  • Kallsbo: I did as advised however still the same issue, added the changed screen shot. – Vinod Feb 12 '16 at 14:26
  • 1
    From what I can see in your screenshot the file is 44100hz but you state in the code that it's 16000, that might be your issue. I would also verrify that you actually send data to the server. I'm not to good at java but here is a C# example: http://www.hackviking.com/development/google-speech-api-returns-no-result/ – Kristofer Källsbo Feb 12 '16 at 20:40
  • Will change the frequency and check – Vinod Feb 13 '16 at 05:03
  • Thanks Kristofer its working, however i am getting two results: {"result":[]} {"result":[{"alternative":[{"transcript":"good morning Google how are you feeling today","confidence":0.88946682}],"final":true}],"result_index":0} any idea why? – Vinod Feb 13 '16 at 07:54
  • Hi, yes I have seen that as well in many cases and as far as I can tell a lot of other developers as well. Sorry, don't have a clue why it's behaving that way. – Kristofer Källsbo Feb 13 '16 at 17:45