使用 Java 检索 python 脚本的输出



我想使用 Java 运行一个基于语音识别的 python 脚本,并使用 Java 检索脚本输出。

我毫不费力地调用了脚本并运行了它。它工作得很好。但是我不明白为什么我不能恢复printjava的输出。

这是脚本python:

import aiml
import os
import time, sys
import pyttsx
import warnings
# Initialisation of the different mode
# If no specification, Jarvis will run as a text Personnal
mode = "text"
if len(sys.argv) > 1:
if sys.argv[1] == "--voice" or sys.argv[1] == "voice":
import speech_recognition as sr
mode = "voice"
# Jarvis speaking part
def offline_speak(jarvis_speech):
engine = pyttsx.init()
engine.say(jarvis_speech)
engine.runAndWait()
# Jarvis listenning part
def listen():
# Jarvis listen the environnemnt to capture the voice
r = sr.Recognizer()
with sr.Microphone() as source:
print("Talk to JARVIS: ")
# Jarvis is listenning
audio = r.listen(source)
try:
# Print and return what Jarvis heard
print ("test")
print r.recognize_google(audio, language='fr-FR')
return r.recognize_google(audio, language='fr-FR')
except sr.UnknownValueError:
# If Jarvis doesn't know the sentence or the word you said
offline_speak("Je n'ai pas compris ce que vous avez dit, pouvez vous repeter s'il vous plait ?")
print ("test")
# Return what he heard
return(listen())
except sr.RequestError as e:
# Jarvis didn't understand what you said
print("Could not request results from Speech Recognition service; {0}".format(e))

# Jarvis running part
while True:
if mode == "voice":
response = listen()
print ("test")
else:
response = raw_input("Talk to JARVIS : ")
offline_speak(response)
print ("test")

这是我的java 类

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PythonCaller {
private final String pythonPrjPath;
private final String scriptName;
private final String args;

public PythonCaller(String scriptName, String args) {
this.scriptName = scriptName;
this.pythonPrjPath = argTreatment();
this.args = args;
}

public void call() throws Exception {
try {
List<String> commands = new LinkedList<>();
commands.add("python");
commands.add(pythonPrjPath);
commands.add(args);
ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
throw e;
}
}

private String argTreatment() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(scriptName).getFile());
StringBuilder resPpythonPrjPath = new StringBuilder(file.getAbsolutePath());
StringBuilder sb = new StringBuilder(resPpythonPrjPath.subSequence(0,90));
return sb.toString();
}

public static void main(String[] args) {
String tabArgs = "voice";
PythonCaller pc = new PythonCaller("listener.py", tabArgs);
try {
pc.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}

感谢您的帮助

我无法解决我的问题,所以我决定避免它。我不是读取print,而是将要检索的数据写入文本文件中,然后从java读取文本文件

最新更新