How To Return Image And Json In One Response In Fastapi?
I get an image, change it, then it is classified using a neural network, should return a new image and json with a response. How to do it with one endpoint? image is returned with
Solution 1:
I added json to response headers. change from:
@app.post("/predict")
def predict(file: UploadFile = File(...)):
img = file.read()
new_image = prepare_image(img)
result = predict(new_image)
return StreamingResponse(io.BytesIO(new_image.tobytes()), media_type="image/png")
to
@app.post("/predict/")
def predict(file: UploadFile = File(...)):
file_bytes = file.file.read()
image = Image.open(io.BytesIO(file_bytes))
new_image = prepare_image(image)
result = predict(image)
bytes_image = io.BytesIO()
new_image.save(bytes_image, format='PNG')
return Response(content = bytes_image.getvalue(), headers = result, media_type="image/png")
Post a Comment for "How To Return Image And Json In One Response In Fastapi?"