I have created a program in Python which opens the webcam and recognizes the faces found in the camera frame in real time. I feel it looks unpolished to run the python code from my IDE. I want to execute the python code when the user clicks a button in my Java GUI form.
Thanks in advance!, Ashwin
-
1The answer here may help :stackoverflow.com/questions/8898765/calling-python-in-javaBenkerroum Mohamed– Benkerroum Mohamed2019年07月25日 09:01:40 +00:00Commented Jul 25, 2019 at 9:01
-
See stackoverflow.com/questions/792024/… for example. Note: be sure to not do any lengthy calls on the event dispatcher thread, so read about SwingUtilities.invokeLater() for example. And note: I would rather look into staying pure java or pure python. And yes, you could use jython to have a jvm that runs python inside.GhostCat– GhostCat2019年07月25日 09:01:51 +00:00Commented Jul 25, 2019 at 9:01
3 Answers 3
A dirty hacky way of doing this is to call Runtime.exec("python command here") and attach a listener to the process created by this. This article explains the methods associated with this technique: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html . A rough example would look like:
button.setOnAction(event -> {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("python command");
process.getOutputStream() // add handling code here
});
However, consider whether this is something you actually want to do. Why not create the user interface in Python. The popular GTK GUI library has Python bindings (docs at https://python-gtk-3-tutorial.readthedocs.io/en/latest/).
Or consider writing the face recognition component in Java. If you have written it purely from scratch this may be difficult, but if using a library like OpenCV, there are probably Java bindings available.
In general, without very special care, communicating cross-language is difficult and highly error-prone, so think very carefully about whether you need this exact setup.
3 Comments
I think you can using
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(path + "XXX.py");
ref: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
and Waitting for py to finish output JSON format, last using java rading JSON data process you want to do.
4 Comments
Honestly to I guess the Answer given above is correct. Just use another thread inside the button event so your Java programs main thread wont have to wait till the thing finishes and could prevent the UI from freezing.
Create a Thread
public class MyRunnable implements Runnable {
private String commandParameters = "";
// Just Creating a Constructor
public MyRunnable(String cmd)
{
this.commandParameters = cmd;
}
public void run()
{
try
{
Runtime runtime = Runtime.getRuntime();
// Custom command parameters can be passed through the constructor.
Process process = runtime.exec("python " + commandParameters);
process.getOutputStream();
}
catch(Exception e)
{
// Some exception to be caught..
}
}
}
And in Your Button Event do this
yourBtn.setOnAction(event -> {
try{
Thread thread = new Thread(new MyRunnable("command parameter string"));
thread.start();
}
catch(Exception e)
{
// Some Expection..
}
});
Now your main thread you not freeze or wait for the command execution to complete. Hope this solves the issue. if you want to pass some variable values to the "python command" just make you of a constructor while creating MyRunnable Class and pass the it as parameters to the constructor of the MyRunnable Class.
Now this will run a new thread when you click the button. That would not mess with your main UI thread.