java-google-speech-api icon indicating copy to clipboard operation
java-google-speech-api copied to clipboard

Official Google Cloud Speech API code

Open goxr3plus opened this issue 7 years ago • 6 comments

@DeathStrokeAlpha @Twister21 Hello my friends , i am working with Google Cloud Speech Library so :

Here is working code Google Cloud Speech Official

Any problems you might have about setting the credentials check this stackoverflow question i did :

For some reason it has the same problem as this library , stopping after 65 seconds , google has made it like this .... gonna find a work around soon

Check this -> https://github.com/GoogleCloudPlatform/google-cloud-java/issues/3188

package googleSpeech;

import java.io.IOException;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;

import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.StreamingRecognitionConfig;
import com.google.cloud.speech.v1.StreamingRecognizeRequest;
import com.google.cloud.speech.v1.StreamingRecognizeResponse;
import com.google.protobuf.ByteString;

public class GoogleSpeechTest {
	
	public GoogleSpeechTest() {
		
		//Set credentials?
		//	GoogleCredentials credentials = GoogleCredentials.create(new AccessToken("AIzaSyCtrBlhBiqNd7kI4BiOn2kWiCYlwp1azVM",Date.valueOf(LocalDate.now())));
		//	System.out.print(credentials.getAccessToken());
		
		//Target data line
		TargetDataLine microphone;
		AudioInputStream audio = null;
		
		//Check if Microphone is Supported
		checkMicrophoneAvailability();
		
		//Print available mixers
		//printAvailableMixers();
		
		//Capture Microphone Audio Data
		try {
			
			// Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
			AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
			DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
			
			//Check if Microphone is Supported
			if (!AudioSystem.isLineSupported(info)) {
				System.out.println("Microphone is not available");
				System.exit(0);
			}
			
			//Get the target data line
			microphone = (TargetDataLine) AudioSystem.getLine(info);
			microphone.open(format);
			microphone.start();
			
			//Audio Input Stream
			audio = new AudioInputStream(microphone);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		//Send audio from Microphone to Google Servers and return Text
		try (SpeechClient client = SpeechClient.create()) {
			
			ResponseObserver<StreamingRecognizeResponse> responseObserver = new ResponseObserver<StreamingRecognizeResponse>() {
				
				public void onStart(StreamController controller) {
					System.out.println("Started....");
				}
				
				public void onResponse(StreamingRecognizeResponse response) {
					System.out.println(response.getResults(0));
				}
				
				public void onComplete() {
					System.out.println("Complete");
				}
				
				public void onError(Throwable t) {
					System.err.println(t);
				}
			};
			
			ClientStream<StreamingRecognizeRequest> clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
			
			RecognitionConfig recConfig = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000)
					.build();
			StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder().setConfig(recConfig).build();
			
			StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().setStreamingConfig(config).build(); // The first request in a streaming call has to be a config
			
			clientStream.send(request);
			
			//Infinity loop from microphone
			while (true) {
				byte[] data = new byte[10];
				try {
					audio.read(data);
				} catch (IOException e) {
					System.out.println(e);
				}
				request = StreamingRecognizeRequest.newBuilder().setAudioContent(ByteString.copyFrom(data)).build();
				clientStream.send(request);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	/**
	 * Checks if the Microphone is available
	 */
	public static void checkMicrophoneAvailability() {
		enumerateMicrophones().forEach((string , info) -> {
			System.out.println("Name :" + string);
		});
	}
	
	/**
	 * Generates a hashmap to simplify the microphone selection process. The keyset is the name of the audio device's Mixer The value is the first
	 * lineInfo from that Mixer.
	 * 
	 * @author Aaron Gokaslan (Skylion)
	 * @return The generated hashmap
	 */
	public static HashMap<String,Line.Info> enumerateMicrophones() {
		HashMap<String,Line.Info> out = new HashMap<String,Line.Info>();
		Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
		for (Mixer.Info info : mixerInfos) {
			Mixer m = AudioSystem.getMixer(info);
			Line.Info[] lineInfos = m.getTargetLineInfo();
			if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class))//Only adds to hashmap if it is audio input device
				out.put(info.getName(), lineInfos[0]);//Please enjoy my pun
		}
		return out;
	}
	
	/**
	 * Print available mixers
	 */
	public void printAvailableMixers() {
		
		//Get available Mixers
		Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
		
		//Print available Mixers
		Arrays.asList(mixerInfos).forEach(info -> {
			System.err.println("\n-----------Mixer--------------");
			
			Mixer mixer = AudioSystem.getMixer(info);
			
			System.err.println("\nSource Lines");
			
			//SourceLines
			Arrays.asList(mixer.getSourceLineInfo()).forEach(lineInfo -> {
				//Line Name
				System.out.println(info.getName() + "---" + lineInfo);
				Line line = null;
				try {
					line = mixer.getLine(lineInfo);
				} catch (LineUnavailableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("\t-----" + line);
			});
			
			System.err.println("\nTarget Lines");
			//TargetLines
			Arrays.asList(mixer.getTargetLineInfo()).forEach(lineInfo -> {
				
				//Line Name
				System.out.println(mixer + "---" + lineInfo);
				Line line = null;
				try {
					line = mixer.getLine(lineInfo);
				} catch (LineUnavailableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("\t-----" + line);
				
			});
			
		});
	}
	
	public static void main(String[] args) {
		new GoogleSpeechTest();
	}
	
}

goxr3plus avatar Jul 01 '18 23:07 goxr3plus

How to use Google Speech API on Java Servlet? I need to use this for a web app (on browser). I'm using Eclipse and Tomcat as localhost. Appreciate your help.

ghshpro avatar Jul 02 '18 04:07 ghshpro

Hmmmm you want yo stream audio from thr microphone ? The credentials will need to be configured someway too for the Servlet. To tell the truth i am experimenting these days too, so :)

goxr3plus avatar Jul 02 '18 23:07 goxr3plus

@goxr3plus I've built the JAR files from your maven project. Is there anyway to just call these JAR files from a simple Servlet code? or the whole code must be modified to be compatible with Servlet? I'm still new to Servlet. Thanks :)

ghshpro avatar Jul 03 '18 03:07 ghshpro

Of course you can call directly the library, there is no incompatibility issues as far as is calling Google Speech API and the access token is specified as i have shown in the small example of the README file.

Hey you can use Maven to add the libraries eaaasy on your project no need to rebuild them.

As now for the Official Google Speech API. Well this library doesn't support it, it supports only Google Speech API PRIVATE which is the same with the difference that it can't be used for commercial projects.

The code i have given on this issue can be easily used with Servlet. Try it, firstly create your own credentials on Google Cloud. Check the links i have provided on the first answer of the issue.

On Tue, Jul 3, 2018, 06:24 ghshpro [email protected] wrote:

@goxr3plus https://github.com/goxr3plus I've built the JAR files from your maven project. Is there anyway to just call these JAR files from a simple Servlet code? or the whole code must be modified to be compatible with Servlet? I'm still new to Servlet. Thanks :)

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/goxr3plus/java-google-speech-api/issues/4#issuecomment-402001813, or mute the thread https://github.com/notifications/unsubscribe-auth/ATbiwLWf8xz8kBh92wN_zXAVohf6HiJGks5uCuQDgaJpZM4U-hmu .

goxr3plus avatar Jul 03 '18 11:07 goxr3plus

  1. How can I call your JAVA code from SERVLET then? Let's say here's my code, what should I add to call your JAVA library? (Let's say I have all your code as a JAR file in my build path)

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
  1. In your YouTube tutorial, you said we can search for "Speech API 'Private'" on GCP. But there is no such API on the platform anymore. There's just one Google Speech API. I found a way to obtain a JSON file from GCP which contains "private_key", which is a very veryyyy long (1000+ characters) and "private_key_id" which is a 40-character key. Both don't seem to work. Maybe I'm entering it at the wrong place? Maybe you can help update the tutorial please?

FYI, here's how to get the above JSON file:

  1. In "Create Credentials" drop down, select "Service account key".
  2. Create a "service account" if you haven't yet.
  3. Select JSON and click "Create". Then you'll get a JSON file containing some info.

Thanks for your help, mate.

ghshpro avatar Jul 17 '18 07:07 ghshpro

Hello my friend sorry if the tutorials are not so professional, it's been time, i ave to remake them. Look :

I have not so. much experience eith the Servlets but :

Look this discussion i have on this issue on how to enable Google Speech API private :

EVERYTHIN YOU NEED is on the discussion i have in this issue :

https://github.com/goxr3plus/java-google-speech-api/issues/2

Also you can use the official Google Speech API as i have posted description, links and code in this issue :

https://github.com/goxr3plus/java-google-speech-api/issues/4

Please read the above carefuly, they are the answer to your questions 🙋

On Tue, Jul 17, 2018, 10:27 ghshpro [email protected] wrote:

  1. How can I call your JAVA code from SERVLET then? Let's say here's my code, what should I add to call your JAVA library? (Let's say I have all your code as a JAR file in my build path)

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

/**

  • Servlet implementation class Test */ @WebServlet("/Test") public class Test extends HttpServlet { private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet() */ public Test() { super(); // TODO Auto-generated constructor stub }

/**

  • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }

/**

  • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }

}

  1. In your YouTube tutorial, you said we can search for "Speech API 'Private'" on GCP. But there is no such API on the platform anymore. There's just one Google Speech API. I found a way to obtain a JSON file from GCP which contains "private_key", which is a very veryyyy long (1000+ characters) and "private_key_id" which is a 40-character key. Both don't seem to work. Maybe I'm entering it at the wrong place? Maybe you can help update the tutorial please?

FYI, here's how to get the above JSON file:

  1. In "Create Credentials" drop down, select "Service account key".
  2. Create a "service account" if you haven't yet.
  3. Select JSON and click "Create". Then you'll get a JSON file containing some info.

Thanks for your help, mate.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/goxr3plus/java-google-speech-api/issues/4#issuecomment-405486556, or mute the thread https://github.com/notifications/unsubscribe-auth/ATbiwMzdCeTZWmsgZtdARsEt-trL1a_8ks5uHZHWgaJpZM4U-hmu .

goxr3plus avatar Jul 17 '18 13:07 goxr3plus