Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.4K
Visualizações
How to return an image in fastAPI

I am trying to return an image in fastAPI after comparing two images using Opencv.

Here's what I have done so far:

from fastapi import FastAPI , File, UploadFile
import numpy as np
from cv2 import *
import os
import base64


app = FastAPI(debug = True)


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...),file1: UploadFile = File(...)):
    content = await file.read()
    nparr = np.fromstring(content, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

    content1 = await file1.read()
    nparr1 = np.fromstring(content1, np.uint8)
    img1 = cv2.imdecode(nparr1, cv2.IMREAD_COLOR)

    akaze = cv2.AKAZE_create()
    kpts1, desc1 = akaze.detectAndCompute(img, None)
    kpts2, desc2 = akaze.detectAndCompute(img1, None)
    matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE_HAMMING)
    matches_1 = matcher.knnMatch(desc1, desc2, 2)
    good_points = []
    for m,n in matches_1:
            if m.distance < 0.7 * n.distance:
                good_points.append(m)
    mat = (round(len(kpts2)/len(good_points),2))

where I am getting an error

    return_img = cv2.processImage(img)
    _, encoded_img = cv2.imencode('.PNG', return_img)
    encoded_img = base64.b64encode(return_img)

    return {"The similarity is": mat,'encoded_img': endcoded_img}

What am I doing wrong?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Yes you can return an image with FastAPI, it's actually so simple.

from fastapi import FastAPI
from fastapi.responses import FileResponse

some_file_path = "some_image.jpeg"
app = FastAPI()


@app.get("/")
async def main():
    return FileResponse(some_file_path)

Make sure to install aiofiles with pip install aiofiles otherwise, you'll be getting an error like this:

AssertionError: 'aiofiles' must be installed to use FileResponse

If you have an image as bytes consider using StreamingResponse

from io import BytesIO

@app.post("/send_image")
async def send():
    image = BytesIO()
    img =                                        # Do something here to create an image
    img.save(image, format='JPEG', quality=85)   # Save image to BytesIO
    image.seek(0)                                # Return cursor to starting point
    return StreamingResponse(image.read(), media_type="image/jpeg")

over 4 years ago · Santiago Trujillo Relatório

0

The solution can be found here. You are converting the Opencv Image before encoding it in the 3rd line.

    return_img = cv2.processImage(img)
    _, encoded_img = cv2.imencode('.PNG', return_img)
    encoded_img = base64.b64encode(return_img)

    return {"The similarity is": mat,'encoded_img': endcoded_img}

Replace return_img with encoded_img and everything should be working as expected.

    return_img = cv2.processImage(img)
    _, encoded_img = cv2.imencode('.PNG', return_img)
    encoded_img = base64.b64encode(encoded_img)

    return {"The similarity is": mat,'encoded_img': endcoded_img}
over 4 years ago · Santiago Trujillo Relatório

0

If you want to return the image only, then return the image itself with the correct encoding in the header see Return a Response Directly

If you need to also return other information with the image in the json, then see this SO question How do you put an image file in a json object? (note: the solutions are in javascript, but it's quite easy to adapt it for python)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda