File manager - Edit - /home/kridsana/webapp.cm.in.th/673190902/u67319090017/remove-bg-web/app.py
Back
from __future__ import annotations import os from pathlib import Path from typing import Optional from fastapi import FastAPI, File, Form, UploadFile, Request from fastapi.responses import HTMLResponse, Response from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from image_processing import Params, decode_image, remove_background, encode_png BASE_DIR = Path(__file__).resolve().parent UPLOAD_DIR = BASE_DIR / "uploads" OUTPUT_DIR = BASE_DIR / "outputs" UPLOAD_DIR.mkdir(exist_ok=True) OUTPUT_DIR.mkdir(exist_ok=True) app = FastAPI(title="Remove Background Web") app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static") templates = Jinja2Templates(directory=str(BASE_DIR / "templates")) @app.get("/", response_class=HTMLResponse) def index(request: Request): return templates.TemplateResponse("index.html", {"request": request}) @app.post("/api/remove-bg") async def api_remove_bg( image: UploadFile = File(...), mode: str = Form("threshold"), blur_ksize: int = Form(5), threshold: int = Form(140), canny_low: int = Form(60), canny_high: int = Form(160), feather: int = Form(0), ): content = await image.read() img_bgr = decode_image(content) params = Params( mode=mode if mode in ("threshold", "canny") else "threshold", blur_ksize=blur_ksize, threshold=threshold, canny_low=canny_low, canny_high=canny_high, feather=feather, ) out_bgra, _mask = remove_background(img_bgr, params) png_bytes = encode_png(out_bgra) return Response(content=png_bytes, media_type="image/png") @app.post("/api/mask") async def api_mask( image: UploadFile = File(...), mode: str = Form("threshold"), blur_ksize: int = Form(5), threshold: int = Form(140), canny_low: int = Form(60), canny_high: int = Form(160), ): content = await image.read() img_bgr = decode_image(content) params = Params( mode=mode if mode in ("threshold", "canny") else "threshold", blur_ksize=blur_ksize, threshold=threshold, canny_low=canny_low, canny_high=canny_high, ) _out_bgra, mask = remove_background(img_bgr, params) ok, buf = __import__("cv2").imencode(".png", mask) if not ok: return Response(content=b"", status_code=500) return Response(content=buf.tobytes(), media_type="image/png")
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.38 |
proxy
|
phpinfo
|
Settings