Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

How to remove image background using rembg?

$
0
0

This is the app.py file for my Python app:

from typing import Unionfrom fastapi import FastAPI, File, UploadFilefrom rembg import removeimport requestsfrom io import BytesIOapp = FastAPI()@app.get("/")def read_root():    return {"Hello": "World"}@app.get("/items/{item_id}")def read_item(item_id: int, q: Union[str, None] = None):    return {"item_id": item_id, "q": q}def download_image(url):    response = requests.get(url)    if response.status_code != 200:        raise ValueError(f"Failed to download image from URL: {url}")    return BytesIO(response.content)@app.post("/remove_background")async def remove_background(url: str):    try:        # Download the image from the URL        image_content = download_image(url)        # Remove background        output = remove(image_content.read())        return output    except Exception as e:        return {"error": str(e)} # at last, the bottom of the file/moduleif __name__ == "__main__":    uvicorn.run(app, host="127.0.0.1", port=5049)

The GET requests are working fine. But when I post to this endpoint remove_background, I get this error - {"detail":"Not Found"}. I need this function to download images from URL ,and then remove background from it. What am I doing wrong here?


Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>