java调用python代码并传参和获取结果(亲测可用)
说明:本文通过java调用python的scipy库中的savgol_filter来实现数据的平滑滤波。由于是用到了scipy+numpy等第三方库,所以一开始使用Jython尝试无果。换为进程的方式后,前期参考网上各种博客,但一直没有数据结果的输出,后来才发现是python程序出错了,根据另一篇博客改了之后得到了错误信息。故记录下实现方法。
准备python代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
import sys
import json
def get_savgol_filter_result(data, window, time):
rs = savgol_filter(data, window, time)
return rs
import sys
data = json.loads(sys.argv[1])
window = int(sys.argv[2])
time = int(sys.argv[3])
rs = get_savgol_filter_result(data, window, time)
print(rs)# 通过print输出结果的方式返回结果给java
java代码:
public static void main(String[] args) {
jscienceTest();
}
/**
* java调用python结果,并获得python的返回值
*/
private static void jscienceTest(){
float[] data = new float[] { 55.2f, 57.266666666666666f, 63.1f, 58.61538461538461f, 56.6f, 56.13333333333333f, 54.5f,
55.096774193548384f, 52.666666666666664f, 54.5f, 55.46153846153846f, 55.583333333333336f, 57.857142857142854f,
58.0f, 68.0f, 58.6f, 57.0f, 54.15384615384615f, 58.5f, 55.0f, 54.8421052631579f, 55.111111111111114f, 55.0f, 54.0f,
52.55555555555556f, 52.8f, 51.666666666666664f, 49.285714285714285f, 49.44444444444444f, 52.0f, 49.375f,
52.44444444444444f, 56.142857142857146f, 58.0f, 55.166666666666664f, 56.0f, 57.5f, 56.0f, 53.5f,
54.166666666666664f, 53.875f, 51.2f, 52.5f, 59.25f, 57.0f, 56.1f, 75.11111111111111f, 68.55555555555556f,
73.6923076923077f, 79.66666666666667f, 74.0f, 76.625f, 66.23529411764706f, 71.8f, 73.6f, 65.48275862068965f,
58.0f, 53.0f, 51.166666666666664f, 49.166666666666664f, 52.0f, 50.36363636363637f, 54.77777777777778f, 54.5f,
55.38461538461539f, 54.333333333333336f, 58.5f, 51.62068965517241f, 49.4f, 50.666666666666664f, 57.92f,
56.0f, 54.45454545454545f, 55.0f, 60.55555555555556f, 63.875f, 64.8f, 73.0f, 73.1f, 76.0f, 76.44444444444444f,
71.0f, 63.0188679245283f, 57.142857142857146f, 58.0f, 57.9375f, 52.294117647058826f, 55.45f,
55.285714285714285f, 53.857142857142854f, 53.0f, 53.8235294117647f};
String pythonPath = "E:\\SgFilterScipy.py";
String[] arguments = new String[] {"python",pythonPath,Arrays.toString(data), String.valueOf(15), String.valueOf(2)};//指定命令、路径、传递的参数
StringBuilder sbrs = null;
StringBuilder sberror = null;
try {
ProcessBuilder builder = new ProcessBuilder(arguments);
Process process = builder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));//获取字符输入流对象
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream(), "utf-8"));//获取错误信息的字符输入流对象
String line = null;
sbrs = new StringBuilder();
sberror = new StringBuilder();
//记录输出结果
while ((line = in.readLine()) != null) {
sbrs.append(line);
}
//记录错误信息
while ((line = error.readLine()) != null) {
sberror.append(line);
}
in.close();
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sbrs);
System.out.println(sberror);
}
输出结果:
[60.24340091 58.6189522 57.27032094 56.19750715 55.40051081 54.87933193 54.6339705 54.66442653 54.71267732 55.08327334 56.4198044 57.06964945 57.92019471 58.59009706 58.85697805 58.95832166 58.57160581 58.07364622 57.2685097 56.24311854 55.35030047 54.18602572 54.04383725 53.60025109 52.80061427 51.56894726 51.07092558 50.97681424 51.05917057 51.36541013 52.1531157 53.25139728 54.14215546 55.05951287 55.96800205 56.16002613 55.50007136 55.33548048 54.82914175 53.06391294 53.22514059 53.92628458 54.79145629 57.14153869 59.9925956 63.95482827 66.86395985 69.46659743 72.32164246 74.37852032 75.26138282 75.2456597 73.50382476 69.66405304 66.9671018 62.84911673 59.23956816 56.67586089 54.36810159 52.81805025 51.62838627 51.63949283 52.51362055 52.87071143 53.45595084 53.96657033 54.23601281 53.7654438 53.52019775 53.3605563 53.4318242 54.34737182 55.89010707 58.03562551 61.6808095 65.27542785 68.20414504 69.85492376 71.19938777 71.84923525 70.75538708 68.54201545 66.01535571 63.16618743 59.80987734 58.07672258 56.57361072 55.30054178 54.25751573 53.4445326 52.86159237 52.50869505]
用图像表示滤波前后的数据:
滤波前
滤波后