使用J检索python脚本的输出

2024-05-15 02:02:24 发布

您现在位置: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();
        }
    }

}

谢谢你的帮助


Tags: theimport脚本newstringsysargsjava

热门问题