Home > Enterprise >  sys:1: RuntimeWarning: coroutine "FOO" was never awaited
sys:1: RuntimeWarning: coroutine "FOO" was never awaited

Time:07-19

I have the below code using FastAPI and when I try to provide an image to this function, it gives me this traceback on the API result:

Traceback (most recent call last):  
File \"/home/shady/Desktop/BTS/Segmentation/server/fast_app.py\", line 32, in remove_background"
return Response(content=data, media_type=\"image/png\")
File \"/home/shady/anaconda3/envs/py37/lib/python3.7/site-packages/starlette/responses.py\", line 49, in __init__
self.body = self.render(content)\n
File \"/home/shady/anaconda3/envs/py37/lib/python3.7/site-packages/starlette/responses.py\", line 57, in render
return content.encode(self.charset)\n
AttributeError: 'coroutine' object has no attribute 'encode'"

Below is the code which I'm using:

from io import BytesIO
import uvicorn
from fastapi import FastAPI, HTTPException, UploadFile, Form
from fastapi.responses import Response
import traceback
from remove_bg import remove as remove_background
from PIL import Image

app = FastAPI(title='BG Remove')

@app.post('/remove-bg')
async def remove_background(image: UploadFile, post_process:float = Form(default = False)):
    """
    Remove Background from an image
    """
    try:
        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_background(Image.open(buffer), bool(post_process))
        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', port = 5678)

Code is saved in a file fast_app.py and I run the code as: python fast_app.py

Can someone help me with the issue.

CodePudding user response:

Your endpoint function name remove_background is conflicting with the function imported.

from io import BytesIO
import uvicorn
from fastapi import FastAPI, HTTPException, UploadFile, Form
from fastapi.responses import Response
import traceback
from remove_bg import remove as remove_background
from PIL import Image

app = FastAPI(title='BG Remove')

@app.post('/remove-bg')
async def remove_background_endpoint(image: UploadFile, post_process:float = Form(default = False)):
    """
    Remove Background from an image
    """
    try:
        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_background(Image.open(buffer), bool(post_process))
        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', port = 5678)
  • Related