Software Archive
Read-only legacy content
17061 Discussions

Java: Voice recognition minimal example not working

Kilian_B_
Beginner
271 Views

Streaming color and depth data using java as well as running the c++ and c# speech recognition samples works without a problem. Once I try to utilize the speech recognition in java my program crashes just before? the public void OnRecognition(RecognitionData data) event handler is called.

If no word is being said the program spits out the ALERT_SPEECH_END and will continue normally but if an actual word has been said the program will terminate after this event has been thrown.

Alert: ALERT_SPEECH_BEGIN 753776210693
Alert: ALERT_VOLUME_LOW 753776210693
Alert: ALERT_SPEECH_END 753788110102

Here is a minimal example I am using to try to get the speech recognition to work. Can someone please tell me if I made any obvious mistakes.

Thank you

public class SpeechRecognition implements PXCMSpeechRecognition.Handler {
	
	PXCMSpeechRecognition sr;
	
	public static void main(String[] args){
		new Thread(new Runnable(){

			@Override
			public void run() {
				new SpeechRecognition();
			}
			
		}).start();
		
		//Keep the program alive
		while(true){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public SpeechRecognition(){
		PXCMSession session = PXCMSession.CreateInstance();
		
		//Audio
		PXCMAudioSource source=session.CreateAudioSource();
		
		//Select the correct microphone
		source.ScanDevices();
		PXCMAudioSource.DeviceInfo dinfo = new PXCMAudioSource.DeviceInfo();
		for (int d=source.QueryDeviceNum()-1;d>=0;d--) {
		    source.QueryDeviceInfo(d,dinfo);
		    System.out.println(d + " " + dinfo.name + " " + dinfo.did);
		    if(dinfo.name.contains("USB"))
		    	break;
		}
		
		source.SetDevice(dinfo);
		
		//Speech recognition module
		sr=new PXCMSpeechRecognition();
		session.CreateImpl(sr);
		
		PXCMSpeechRecognition.ProfileInfo pinfo=new PXCMSpeechRecognition.ProfileInfo();
		sr.QueryProfile(0, pinfo);
		System.out.println(pinfo.speaker);
		
		//Default is english
		sr.SetProfile(pinfo);

		sr.SetDictation(); //No need for grammar if dictation mode is turned on

		// Start recognition
		pxcmStatus pxcmStatus = sr.StartRec(source, this);
		printPxCMStatus(pxcmStatus, "StartRecording");

	}
	
	private void printPxCMStatus(pxcmStatus pxcmStatus, String prefix){
		if(pxcmStatus.isSuccessful()){
			System.out.println(prefix + " Successfull initialized");
		}else if(pxcmStatus.isWarning()){
			System.out.println("Warning: " + pxcmStatus.toString());
		}else{
			System.out.println("Error: " + pxcmStatus.toString());
		}
	}
	
	@Override
	public void OnRecognition(RecognitionData data) {
		sr.StopRec();
		System.out.println("On recognition");
		
		System.out.println("On recognition" + data.toString() + " Duration: " + data.duration + " Timestamp: " + data.timeStamp  + " Grammar: " + data.grammar + " ");
		NBest[] recognition = data.scores;
		for(NBest n : recognition){
			System.out.println("confidence: " + n.confidence + " Label: " + n.label + " Sentence: " + n.sentence + " Tags: " + n.tags);
		}
	
		System.out.println("On recognition");
	}


	@Override
	public void OnAlert(AlertData data) {
		System.out.println("Alert: "  + data.label + " " + data.timeStamp);
	}
}

 

0 Kudos
0 Replies
Reply