繁体   English   中英

我正在使用 Jython 2.7.2 在 Java 中执行 .py 文件,并收到错误为 org.python.core.PyException: ImportError: No module named speech_recognition

[英]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

我正在使用 Jython 2.7.2 在 Java 中执行 .py 文件,并收到错误为 org.python.core.PyException: ImportError: No module named speech_recognition。 任何人都可以请帮助解决问题。

附上我的 java 和 .py 文件的代码片段和错误消息。

        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 代码:(文件名: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")

错误信息

    org.python.core.PyException: ImportError: No module named speech_recognition
        at appium.test.pythonReader.record_audio_while_doing_voice_over(pythonReader.java:18)

如果要使用 SpeechRecognition,则需要安装 Python package。

您可以通过在终端中运行以下命令来安装它:

jython -m pip install SpeechRecognition

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM