Run python script (with numpy dependency) from java

前端 未结 3 1856
逝去的感伤
逝去的感伤 2021-01-16 01:05

In a java application I need to use a specific image processing algorithm that is currently implemented in python. What would be the best approach, knowing that this script

相关标签:
3条回答
  • 2021-01-16 01:38

    For using numpy within a Java process, Jep now supports numpy. You can also use jpy or JyNI.

    With those libraries you could pass the images between Java and Python as byte arrays and ndarrays as you do your calculations.

    0 讨论(0)
  • 2021-01-16 01:41

    While Joe's answer may be simple, you will have performance issue if the image is big (2 complete write and read). I am no Java expert, but according to http://wiki.cacr.caltech.edu/danse/index.php/Communication_between_Java_and_Python#Communication_through_bindings, you can developp c <--> java bindings. You can do the same with python (with ctypes or cython if you don't want to learn the python c api).

    With those, you can build yourself a nice python <--> java bridge in c, and pass around a pointer to the image data (which will be faster than writing and reading it on the hard drive).

    0 讨论(0)
  • 2021-01-16 01:47

    If you're using Numpy you probably have to just use C Python, as it's a compiled extension. I'd recommend saving the image to disk, perhaps as a temporary file, and then calling the Python as a subprocess. If you're dealing with binary data you could even try memory mapping the data in Java and passing in in the path to the subprocess.

    Alternatively, depending on your circumstances, you could set up a simple data processing server in Python which accepts requests and returns processed data.

    0 讨论(0)
提交回复
热议问题