现有一个python源码XXXX.py 存放在桌面上
现在有一个python代码片段
def detect(imgfile)
返回参数为:
return sorted_label, origimg
其中需要往其中传递一个String类型的url
求大神给出java调用此接口的代码 谢谢大神们
/**
* Jython环境,生存python解释器
* @author webim
*
*/
public final class JythonEnvironment
{
private static JythonEnvironment INSTANCE = new JythonEnvironment();
/**
* 私有构造方法
*/
private JythonEnvironment()
{
}
/**
* 获取单例
* @return JythonEnvironment
*/
public static JythonEnvironment getInstance()
{
return INSTANCE;
}
/**
* 获取python系统状态,可根据需要指定classloader/sys.stdin/sys.stdout等
* @return PySystemState
*/
private PySystemState getPySystemState()
{
PySystemState.initialize();
final PySystemState py = new PySystemState();
py.setClassLoader(getClass().getClassLoader());
return py;
}
/**
* 获取python解释器
* @return PythonInterpreter
*/
public PythonInterpreter getPythonInterpreter(){
PythonInterpreter inter = new PythonInterpreter(null, getPySystemState());
// PythonInterpreter inter = new PythonInterpreter();
return inter;
}
}
添加 JPython 依赖
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
上面这个依赖有问题,可用下面依赖避免各种问题。
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
/*enum的这个用法,可以作为变种的安全单例,值得借鉴哦 ^_^ */
@Service
@Component
public class ExecPython {
public static final Logger logger = LoggerFactory.getLogger(Exception.class);
//定义 python 解释器
private static PythonInterpreter inter;
public ExecPython() {
this.inter = JythonEnvironment.getInstance().getPythonInterpreter();
this.inter.execfile("C:\\test.py");
}
//设置 python 脚本的路径
public void setPythonPath (String pythonPath){
this.inter.execfile(pythonPath);
}
public void execute(String scriptFile, Map<String,String> properties)
{
logger.info("获取解释器");
try
{
PyFunction getNetInfo = (PyFunction) inter.get("getNetInfo", PyFunction.class);
PyObject netInfo = getNetInfo.__call__();
System.out.println("anwser = " + netInfo.toString());
}
catch (Exception e)
{
e.printStackTrace();
logger.info("Python 脚本文件执行失败");
}
}
//获取 Python 字符串
public String getString(){
//获取到python 脚本中的接口
PyFunction func = (PyFunction) inter.get("adder", PyFunction.class);
PyObject pyobj = func.__call__();
System.out.println("anwser = " + pyobj.toString());
return pyobj.toString();
}
// 获取当前数组
public String getArr() {
PyFunction getArr = (PyFunction) inter.get("getArr", PyFunction.class);
PyObject pyobjTwo = getArr.__call__();
pyobjTwo.__len__();
System.out.println("anwser = " + pyobjTwo.toString()+" len:"+pyobjTwo.__len__());
//将 PyObject 对象转换成 java 对象
//Object object = pyobjTwo.__tojava__(List.class);
//List<String> list = (List<String>) object;
//将查询到数据转换成一个 JSON 字符串
String result = pyobjTwo.toString();
String JsonStr = "{" + result + "}";
logger.info(JsonStr);
logger.info("将查询的结果转换成 JSON 字符串:",JsonStr);
return pyobjTwo.toString();
}
}
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Java_Python_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\add.py");
// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
int a = 5, b = 10;
//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”
PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("the anwser is: " + pyobj);
}
}
https://blog.csdn.net/qq_26591517/article/details/80441540
简单的能用,对于复杂,引用读个三方库文件有问题的