如何从FastAPI端点返回字典+图像? [英] How do I return a dict + an image from a FastAPI endpoint?

查看:334
本文介绍了如何从FastAPI端点返回字典+图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FastAPI允许我的(泊坞窗化的)服务器响应返回的API调用

I am trying to use FastAPI to allow my (dockerized) server to respond to API calls returning

  • 图片 image
  • 字典 additional_dict

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

(for a machine learning example, this could be classification labels from a classifier and a saliency map).

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

In my case, I think it makes sense to make use the same endpoint to get the two objects, because these get generated with the same computations.

我可以使用类似 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上的swagger UI调用服务时,这甚至可以看到图像响应.

This allows me even to see the image response when I invoke the service through the swagger UI on localhost:8000/docs.

但是,我不知道如何将图像二进制和字典一起获取.

However, I don't know how to get the image binary and the dictionary together.

我尝试将return语句替换为:

I tried to replace my return statement with:

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

但这实际上并没有用(当尝试在localhost:8000/docs上大摇大摆时,我只是得到下面的json回复,并创建了临时文件的路径)

but this doesn't really work (when trying on swagger at localhost:8000/docs, I just get the json reply below, with a path to the temp file created)

[{
"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值?

It is possible to get in the response the binary image and the additional dict, from the same endpoint? If so, what's the best way to do it?
Is it possible to have the image rendered in the Swagger UI /docs and read the dict values there?

推荐答案

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

You can encode binary data (it would be in image in your case) with base64 and send the encoded string via dictionary.

   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编码的图像和任何其他数据字段.

So this dictionary will contain the base64 encoded image and any other data fields.

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

On the front-end you may decode an image from base64 string back to bytes and present it to the user.

这篇关于如何从FastAPI端点返回字典+图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆