| from fastapi import FastAPI, UploadFile, File
|
| from fastapi.responses import FileResponse
|
| from fastapi.middleware.cors import CORSMiddleware
|
| import os
|
| import subprocess
|
|
|
| app = FastAPI()
|
|
|
|
|
| app.add_middleware(
|
| CORSMiddleware,
|
| allow_origins=["*"],
|
| allow_credentials=True,
|
| allow_methods=["*"],
|
| allow_headers=["*"],
|
| )
|
|
|
| @app.post("/api/enhance")
|
| async def enhance_image(file: UploadFile = File(...)):
|
|
|
| os.makedirs("temp_input", exist_ok=True)
|
| os.makedirs("temp_output", exist_ok=True)
|
|
|
| input_path = f"temp_input/{file.filename}"
|
| output_path = f"temp_output/enhanced_{file.filename}"
|
|
|
|
|
| with open(input_path, "wb") as buffer:
|
| buffer.write(await file.read())
|
|
|
|
|
|
|
| command = f"realesrgan-ncnn-vulkan.exe -i {input_path} -o {output_path} -n realesrgan-x4plus"
|
| subprocess.run(command, shell=True)
|
|
|
|
|
| return FileResponse(output_path)
|
|
|
| |