我如何 return 使用 FastAPI 的 NumPy 数组?
How can I return a NumPy array using FastAPI?
我有一个 h5 文件形式的 TensorFlow Keras deep learning 模型。
如何在 FastAPI 中上传图片和 return NumPy 数组?
import numpy as np
import cv2
from fastapi import FastAPI, File, UploadFile
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf
model=load_model("complete_model.h5")
app = FastAPI()
def prepare(image):
IMG_SIZE = 224
new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)
@app.post("/")
async def root(file: UploadFile = File(...)):
global model
content = await file.read()
nparr = np.fromstring(content, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.float32)
prediction = model.predict(prepare(img))
return prediction
使用 Swagger UI 上传图片时,出现以下错误:
line 137, in jsonable_encoder
data = dict(obj)
TypeError: 'numpy.float32' object is not iterable
没有 FastAPI 的工作代码:
import numpy as np
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf
import cv2
model=load_model("complete_model.h5")
def prepare(image):
IMG_SIZE = 224
new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)
img = cv2.imread("./test.jpeg").astype(np.float32)
prediction = model.predict(prepare(img))
print(prediction)
终端中的结果:
[[0.25442022 0.74557984]]
如何在使用 FastAPI 时获得相同的结果?
当从您的端点 returning response
(即在您的情况下为 prediction
)时抛出错误。看起来 FastAPI 正在尝试使用 jsonable_encoder
, as shown in the error you provided (have a look at the discussion here, as well as the documentation) 将 NumPy 数组转换为 dict
。因此,您可以做的是将 NumPy 数组转换为 Python list
,然后将其序列化为 JSON
对象:
return json.dumps(prediction.tolist())
在 OpenAPI (Swagger UI) 上,您仍然可以看到预期的结果。但是,如果您需要将其转换回 NumPy 数组,您可以解析 JSON 字符串,如下所示。
arr = np.asarray(json.loads(resp.json())) # resp.json() if using Python requests
如果您想 return NumPy 数组作为原始字节并在浏览器中显示图像或下载它,请查看 。
我有一个 h5 文件形式的 TensorFlow Keras deep learning 模型。
如何在 FastAPI 中上传图片和 return NumPy 数组?
import numpy as np
import cv2
from fastapi import FastAPI, File, UploadFile
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf
model=load_model("complete_model.h5")
app = FastAPI()
def prepare(image):
IMG_SIZE = 224
new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)
@app.post("/")
async def root(file: UploadFile = File(...)):
global model
content = await file.read()
nparr = np.fromstring(content, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.float32)
prediction = model.predict(prepare(img))
return prediction
使用 Swagger UI 上传图片时,出现以下错误:
line 137, in jsonable_encoder
data = dict(obj)
TypeError: 'numpy.float32' object is not iterable
没有 FastAPI 的工作代码:
import numpy as np
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow as tf
import cv2
model=load_model("complete_model.h5")
def prepare(image):
IMG_SIZE = 224
new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3)
img = cv2.imread("./test.jpeg").astype(np.float32)
prediction = model.predict(prepare(img))
print(prediction)
终端中的结果:
[[0.25442022 0.74557984]]
如何在使用 FastAPI 时获得相同的结果?
当从您的端点 returning response
(即在您的情况下为 prediction
)时抛出错误。看起来 FastAPI 正在尝试使用 jsonable_encoder
, as shown in the error you provided dict
。因此,您可以做的是将 NumPy 数组转换为 Python list
,然后将其序列化为 JSON
对象:
return json.dumps(prediction.tolist())
在 OpenAPI (Swagger UI) 上,您仍然可以看到预期的结果。但是,如果您需要将其转换回 NumPy 数组,您可以解析 JSON 字符串,如下所示。
arr = np.asarray(json.loads(resp.json())) # resp.json() if using Python requests
如果您想 return NumPy 数组作为原始字节并在浏览器中显示图像或下载它,请查看