Python 从Java浮点读取numpy数组
我有一个Java浮点数组,我正在发布到服务器上,并从服务器端接收到的unicode数组重建一个numpy数组,如下所示Python 从Java浮点读取numpy数组,python,android,arrays,numpy,Python,Android,Arrays,Numpy,我有一个Java浮点数组,我正在发布到服务器上,并从服务器端接收到的unicode数组重建一个numpy数组,如下所示 # Android, java side JSONObject jsonParams = new JSONObject(); jsonParams.put("test", new Float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}); StringEntity entity = new StringEntity(jsonParams.toS
# Android, java side
JSONObject jsonParams = new JSONObject();
jsonParams.put("test", new Float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
StringEntity entity = new StringEntity(jsonParams.toString());
client.post(this, postEndPoint, entity, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e("PostToServer","Post has failed!");
}
});
# Python flask server side
@app.route("/test", methods=['POST'])
def processdata():
if request.headers['Content-Type'] == 'application/json':
params = request.get_json()
if params.get('test', None) is not None:
test = params.get('test', None)
test_val = np.frombuffer(test)
print(test_val) #!= what I sent
return make_response(jsonify({"":""}),200)
我试图将数据类型设置为
np.float32
等,是否有关于如何在服务器端重建精确数组的指针 完全忽略了我需要字符串化浮点数组的事实
您可以使用gson或类似的工具轻松地转换数组并发布
# android side
Gson gson = new Gson();
jsonParams.put("test", gson.toJson(new Float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}));
# server side
if params.get('test', None) is not None:
test = params.get('test', None)
if type(test) is unicode:
test_val = np.array(test[1:-1].split(','), dtype=float)
# strips '[' and ']' and split the rest using ',' and read them into a numpy array
希望这对别人有帮助 完全忽略了我需要字符串化浮点数组的事实 您可以使用gson或类似的工具轻松地转换数组并发布
# android side
Gson gson = new Gson();
jsonParams.put("test", gson.toJson(new Float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}));
# server side
if params.get('test', None) is not None:
test = params.get('test', None)
if type(test) is unicode:
test_val = np.array(test[1:-1].split(','), dtype=float)
# strips '[' and ']' and split the rest using ',' and read them into a numpy array
希望这对别人有帮助