0

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

asked Jul 25, 2019 at 8:56
2

3 Answers 3

1

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.

answered Jul 25, 2019 at 9:02
Sign up to request clarification or add additional context in comments.

3 Comments

That is a total hack. It runs a lengthy action on the event dispatcher thread. This freezes the java UI immediately. Not at all a good starting point.
Yes, it is a "dirty hacky way of doing this". If seriously considering implementing this, a JavaFX Service class is likely the best way of doing this. SwingUtilites.invokeLater() will also work if using Swing
I prefer Java because I like the easy drag and drop Interface of Netbeans.
0

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.

answered Jul 25, 2019 at 9:07

4 Comments

java.io.IOException: Cannot run program "D:\Ashwin\Python_Class\Open_CV\Video-tracker.py": CreateProcess error=193, %1 is not a valid Win32 application
my fault I forgot in exec("python path+XXX.py")
I did use a string command ... Process pr = rt.exec("D:\\Ashwin\\Python_Class\\Open_CV\\Video-tracker.py");
You didn't write python
0

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.

answered Jul 25, 2019 at 9:19

2 Comments

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError Got this error...
is it a run time or compile time exception. please provide more details to help you out.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.