[Python] How to return an image in fastAPI

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página