gpt4 book ai didi

python - 从 Java Float 中读取 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:27 25 4
gpt4 key购买 nike

我有 Java float 组,我将其发布到服务器并从服务器端接收到的 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.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)

我尝试将 dtype 设置为 np.float32 等,有关如何在服务器端重建精确数组的任何指示?

最佳答案

完全忽略了我需要对 float 组进行字符串化的事实。

您可以使用 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

希望这对某人有帮助!

关于python - 从 Java Float 中读取 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com