Deshwal
Deshwal

Reputation: 4162

FastAPI: 422 Unprocessable Entity with Image upload while same code is working perfectly for 3.7 but giving error in 3.9

Error is duce to the version. python 3.7 is working fine and 3.9 is not

Here is my code:

from io import BytesIO
import uvicorn
from fastapi import FastAPI, HTTPException, UploadFile
from fastapi.responses import Response
import traceback
from rembg.rembg import remove as remove_bg

app = FastAPI(title='BG Remove')

@app.post('/segment')
async def segment_image(image: UploadFile):
    """
    Remove Background from an image
    """
    try:
        print('hello')
        image = await image.read()
        buffer = BytesIO(image)

    except Exception as e:
        e = traceback.format_exc()
        raise HTTPException(status_code=420, detail=f"Image loading error :: {e}")

    try:
        data = remove_bg(buffer)

        return Response(content=data, media_type="image/png")


    except Exception as e:
        e = traceback.format_exc()
        raise HTTPException(status_code=420, detail=f"Segmentation Error:: {e}")

if __name__ == "__main__":
    uvicorn.run("fast_app:app", reload=True, debug = True, host = '0.0.0.0')

Every time I send my image in Postman, it gives the error as: enter image description here

Butt the main part is that when I run the same code on python 3.9 it runs so smoothly

Upvotes: 0

Views: 1054

Answers (1)

Ashkan Goleh Pour
Ashkan Goleh Pour

Reputation: 522

from fastapi import File, UploadFile, FastAPI

try to use it UploadFile = File(...)

Upvotes: 1

Related Questions