0

I am executing .py file in Java using Jython 2.7.2 and getting error as org.python.core.PyException: ImportError: No module named speech_recognition. Can anyone please help resolving the issue.

Attached the code snippet of my java and .py files and error message.

        import java.io.IOException;
        
        import javax.script.ScriptException;
        import org.apache.commons.exec.ExecuteException;
        import org.python.util.PythonInterpreter;
        import org.testng.annotations.Test;
        
        public class pythonReader {
        
            @Test
            public void record_audio_while_doing_voice_over()
                    throws InterruptedException, ScriptException, ExecuteException, IOException {
        
                PythonInterpreter python = new PythonInterpreter();
                python.execfile("C:\\SpeechtoText\\audio_transcriber.py");
            }
        
        }

Python Code : (File Name :audio_transcriber.py)

        import speech_recognition as sr
        
        filename = "C:\\SpeechToText\\16-122828-0002.wav"
        
        # initialize the recognizer
        r = sr.Recognizer()
        with sr.AudioFile(filename) as source:
                # listen for the data (load audio to memory)
                audio_data = r.record(source)
        try:
                    # recognize (convert from speech to text)
                    text = r.recognize_google(audio_data)
                    print(text)
        except sr.UnknownValueError:
                print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
                print("Google Speech Recognition error; {0}".format(e))
        
        print("Transaction complete")

Error Message

    org.python.core.PyException: ImportError: No module named speech_recognition
        at appium.test.pythonReader.record_audio_while_doing_voice_over(pythonReader.java:18)
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Raja
  • 1

1 Answers1

0

You need to install the Python package for SpeechRecognition if you want to use it.

You can install it by running the following command in the terminal:

jython -m pip install SpeechRecognition
Mehrdad Moradi
  • 521
  • 1
  • 6
  • 21
  • SpeechRecognition module is installed and Python script works fine when executing through Pycharm IDE but fails when executing the python file through Java – Raja Jul 27 '22 at 23:16
  • is SpeechRecognition installed globally or is it installed in a virtual environment? – Mehrdad Moradi Jul 28 '22 at 00:24