【问题标题】:How do I return a dict + an image from a FastAPI endpoint?如何从 FastAPI 端点返回字典 + 图像?
【发布时间】:2020-05-02 18:29:37
【问题描述】:

我正在尝试使用 FastAPI 让我的(dockerized)服务器响应返回的 API 调用

  • 图片image,以及
  • 字典additional_dict

(对于机器学习示例,这可能是来自分类器和显着图的分类标签)。

就我而言,我认为使用相同的端点来获取两个对象是有意义的,因为它们是通过相同的计算生成的。

我可以使用类似于https://stackoverflow.com/a/55905051/4240413 的方法成功地将图像作为二进制返回:

from PIL import Image
from fastapi import FastAPI, File
import tempfile
from starlette.responses import FileResponse

@app.post("/getstuff")
async def get_image_and_data(file: bytes = File(...)):

    image, additional_dict = process_image(image)

    with tempfile.NamedTemporaryFile(mode="w+b", suffix=".png", delete=False) as outfile:
        image.save(outfile)
        return FileResponse(outfile.name, media_type="image/png")

当我通过 localhost:8000/docs 上的招摇 UI 调用服务时,这甚至可以让我看到图像响应。

但是,我不知道如何将图像二进制和字典放在一起。

我尝试将我的退货声明替换为:

return FileResponse(outfile.name, media_type="image/png"), additional_dict

但这并没有真正起作用(在 localhost:8000/docs 上尝试招摇时,我只得到下面的 json 回复,并创建了临时文件的路径)

[{
"path": "/tmp/tmpldwe_79d.png",
"status_code": 200,
"filename": null,
"send_header_only": false,
"media_type": "image/png",
"background": null,
"raw_headers": [
    [
    "content-type",
    "image/png"
    ]
],
"stat_result": null
},
{
"foo": 1,
"bar": 2
}]

是否可以从同一端点获得二进制图像和附加字典的响应?如果是这样,最好的方法是什么?
是否可以在 Swagger UI /docs 中呈现图像并在那里读取 dict 值?

【问题讨论】:

    标签: python fastapi


    【解决方案1】:

    您可以使用 base64 对二进制数据(在您的情况下为图像)进行编码,并通过字典发送编码后的字符串。

       import base64
    
       with open("image.png", "rb") as image_file:
           encoded_image_string = base64.b64encode(image_file.read())
    
       payload = {
           "mime" : "image/png",
           "image": encoded_image_string,
           "some_other_data": None
       }
    

    所以这个字典将包含 base64 编码的图像和任何其他数据字段。

    在前端,您可以将图像从 base64 字符串解码回字节并将其呈现给用户。

    【讨论】:

    • 好的,谢谢!尽管如此,理想情况下,我也希望能够以大摇大摆的方式看到图像,我仍然想知道这是否有可能......
    • @DavideFiocco 您可以将图像作为传统文件发送,并在响应标头中发送附加数据。虽然不太方便,但在这种情况下,您将能够通过 Swagger 看到图像。
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 2022-01-01
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多