从Java运行python脚本(具有numpy依赖项)

问题描述:

在Java应用程序中,我需要使用当前在python中实现的特定图像处理算法. 知道此脚本使用了Numpy库,什么是最好的方法?

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 uses the Numpy library ?

我尝试使用jythonc编译器将脚本编译为Java,但似乎它不支持对Numpy之类的本机库的依赖. 我也尝试过使用Jepp,但是在导入Numpy时也会出现ImportError.

I alreayd tried to compile the script to java using the jythonc compiler but it seems that it doesn't support dependencies on native libraries like Numpy. I also tried to use Jepp but I get an ImportError when importing Numpy, too.

有什么建议吗?

如果您使用的是Numpy,则可能只需要使用C Python,因为它是已编译的扩展.我建议将映像保存到磁盘(也许作为临时文件),然后将Python作为子进程调用.如果您要处理二进制数据,甚至可以尝试使用Java将数据映射到内存中,然后将其传递到子进程的路径中.

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.

或者,根据您的情况,您可以在Python中设置一个简单的数据处理服务器,该服务器接受请求并返回处理后的数据.

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