我使用Jython 2.7.2在Java中执行.py文件,得到的错误为org.python.core.PyException:ImportError:没有名为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包。
您可以通过在终端中运行以下命令来安装它:
jython -m pip install SpeechRecognition