added automatic cleaning the temp folder
Browse files
main.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
import subprocess
|
3 |
from fastapi import FastAPI, UploadFile, File
|
4 |
from fastapi.responses import FileResponse
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
@@ -15,9 +18,18 @@ app.add_middleware(
|
|
15 |
allow_headers=["*"], # Allows all headers
|
16 |
)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@app.post("/experiments/remove-background/")
|
19 |
async def remove_background(file: UploadFile = File(...)):
|
20 |
input_path = f"temp/{file.filename}"
|
|
|
21 |
output_path = f"temp/no-bg-{file.filename}"
|
22 |
|
23 |
# Ensure the temp folder exists
|
@@ -27,11 +39,23 @@ async def remove_background(file: UploadFile = File(...)):
|
|
27 |
with open(input_path, "wb") as buffer:
|
28 |
buffer.write(await file.read())
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
|
33 |
-
#
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
import subprocess
|
5 |
from fastapi import FastAPI, UploadFile, File
|
6 |
from fastapi.responses import FileResponse
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from PIL import Image
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
18 |
allow_headers=["*"], # Allows all headers
|
19 |
)
|
20 |
|
21 |
+
async def delete_files_after_delay(delay, path):
|
22 |
+
await asyncio.sleep(delay)
|
23 |
+
# Check if path is a file or directory
|
24 |
+
if os.path.isfile(path):
|
25 |
+
os.remove(path)
|
26 |
+
elif os.path.isdir(path):
|
27 |
+
shutil.rmtree(path)
|
28 |
+
|
29 |
@app.post("/experiments/remove-background/")
|
30 |
async def remove_background(file: UploadFile = File(...)):
|
31 |
input_path = f"temp/{file.filename}"
|
32 |
+
compressed_path = f"temp/compressed-{file.filename}"
|
33 |
output_path = f"temp/no-bg-{file.filename}"
|
34 |
|
35 |
# Ensure the temp folder exists
|
|
|
39 |
with open(input_path, "wb") as buffer:
|
40 |
buffer.write(await file.read())
|
41 |
|
42 |
+
# Load the image
|
43 |
+
original_image = Image.open(input_path)
|
44 |
|
45 |
+
# If the image has an alpha channel, convert it to RGB
|
46 |
+
if original_image.mode == 'RGBA':
|
47 |
+
original_image = original_image.convert('RGB')
|
48 |
+
|
49 |
+
# Save the image with reduced quality (e.g., quality=85)
|
50 |
+
original_image.save(compressed_path, 'JPEG', quality=85)
|
51 |
|
52 |
+
# Run rembg command on the compressed image
|
53 |
+
subprocess.run(["rembg", "i", compressed_path, output_path], check=True)
|
54 |
+
|
55 |
+
# Schedule deletion of the input and output images after 30 seconds
|
56 |
+
asyncio.create_task(delete_files_after_delay(60, input_path))
|
57 |
+
asyncio.create_task(delete_files_after_delay(60, compressed_path))
|
58 |
+
asyncio.create_task(delete_files_after_delay(60, output_path))
|
59 |
+
|
60 |
+
# Return the processed file
|
61 |
+
return FileResponse(output_path)
|