简体   繁体   中英

How to run python computer vision script in Java?

I need to create a Java user interface to my machine learning project. I need to call a python file from Java client. This python file contains openCV library, yolo model (for detecting humans), mediapipe library (for detecting human gesture) and a tflite model for human gesture predictions. I also tried with Jython but it doesen't work. How can I run this python file in Java?

You can use the Java ProcessBuilder. This example executes a Python file and prints its output:

public static void main(String[] args) throws IOException, InterruptedException {

    ProcessBuilder processBuilder = new ProcessBuilder("python3", "/path/to/python/app.py");

    Process process = processBuilder.start();

    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    process.waitFor();
    System.out.println("Complete!");

    in.close();
    System.exit(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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