Hou*_*bed 2 python api endpoint flask python-requests
我只是创建一个从文件系统返回图像的烧瓶端点。我已经用邮递员做了一些测试,效果很好。这是执行此操作的指令:
return send_file(image_path, mimetype='image/png')
Run Code Online (Sandbox Code Playgroud)
现在我尝试同时发回多个图像,例如在我的例子中,我尝试分别发回给定图像中出现的每张脸。谁能知道如何做到这一点?
Hou*_*bed 10
解决方案是将每张图片编码为字节,将其附加到列表中,然后返回结果(来源:如何从 Python Flask API 返回图像流和文本作为 JSON 响应)。这是代码:
import io
from base64 import encodebytes
from PIL import Image
from flask import jsonify
from Face_extraction import face_extraction_v2
def get_response_image(image_path):
pil_img = Image.open(image_path, mode='r') # reads the PIL image
byte_arr = io.BytesIO()
pil_img.save(byte_arr, format='PNG') # convert the PIL image to byte array
encoded_img = encodebytes(byte_arr.getvalue()).decode('ascii') # encode as base64
return encoded_img
@app.route('/get_images',methods=['GET'])
def get_images():
##reuslt contains list of path images
result = get_images_from_local_storage()
encoded_imges = []
for image_path in result:
encoded_imges.append(get_response_image(image_path))
return jsonify({'result': encoded_imges})
Run Code Online (Sandbox Code Playgroud)
我希望我的解决方案以及@Mooncrater 的解决方案有所帮助。