|
from fastapi import FastAPI, File, UploadFile, HTTPException |
|
from fastapi.responses import FileResponse |
|
import cv2 |
|
import shutil |
|
from model import Model |
|
|
|
app = FastAPI() |
|
model = Model(device='cpu') |
|
|
|
@app.post("/upload/") |
|
async def process_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)): |
|
|
|
with open("uploaded_image.jpg", "wb") as buffer: |
|
shutil.copyfileobj(file.file, buffer) |
|
|
|
|
|
exstyle, load_info = model.load_model('cartoon1') |
|
|
|
|
|
|
|
aligned_face, instyle, message = model.detect_and_align_image("uploaded_image.jpg", top, bottom, left, right) |
|
|
|
|
|
|
|
style_degree = 0.5 |
|
style_type = 'cartoon1' |
|
result_image, message = model.image_toonify(aligned_face, instyle, exstyle, style_degree, style_type) |
|
|
|
|
|
result_image_path = "result_image.jpg" |
|
cv2.imwrite(result_image_path, result_image) |
|
|
|
|
|
return FileResponse(result_image_path, media_type="image/jpeg", headers={"Content-Disposition": "attachment; filename=result_image.jpg"}) |
|
|