使用Java检索python脚本的输出

时间:2018-05-08 13:34:26

标签: java python

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

我毫不费力地调用脚本并运行它。它完美地运作。但是我不明白为什么我无法用java恢复输出print

这是脚本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();
        }
    }

}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我无法解决我的问题所以我决定避免它。我没有阅读print,而是在文本文件中编写要检索的数据,并从 java

中读取文本文件