如何在FastAPI中返回NumPy数组


@app.post("/predict/")
async def predict(file:UploadFile = File(...)):
bytes_str = io.BytesIO(await file.read())
img = Image.open(bytes_str)
img_array = np.array(img.convert('RGB'))
img_array = cv2.resize(img_array, (1024,1024))
img_final = np.expand_dims(img_array, axis = 0)
pred = model.predict(img_final)

这段代码返回一个NumPy数组,我需要在端点的FastAPI上返回它,但策略是什么?

尝试一个简单的pred.tolist((来返回fastapi能够理解的普通列表。

最新更新