Update main.py
Browse files
main.py
CHANGED
@@ -1,47 +1,39 @@
|
|
1 |
-
from
|
2 |
-
from fastapi.responses import FileResponse, StreamingResponse
|
3 |
-
from fastapi.staticfiles import StaticFiles
|
4 |
from fastapi import FastAPI, File, UploadFile
|
5 |
-
from
|
6 |
-
|
7 |
-
import
|
8 |
import torch
|
|
|
|
|
9 |
from vtoonify_model import Model
|
10 |
-
import shutil
|
11 |
-
import os
|
12 |
|
13 |
app = FastAPI()
|
14 |
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
15 |
|
16 |
@app.post("/upload/")
|
17 |
-
async def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return {"result_image": result_image_bytes}
|
38 |
-
|
39 |
-
except Exception as e:
|
40 |
-
return {"error": str(e)}
|
41 |
|
42 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
43 |
|
44 |
@app.get("/")
|
45 |
def index() -> FileResponse:
|
46 |
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|
47 |
-
|
|
|
1 |
+
from __future__ import annotations
|
|
|
|
|
2 |
from fastapi import FastAPI, File, UploadFile
|
3 |
+
from fastapi.responses import FileResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
import shutil
|
6 |
import torch
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
from vtoonify_model import Model
|
|
|
|
|
10 |
|
11 |
app = FastAPI()
|
12 |
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
13 |
|
14 |
@app.post("/upload/")
|
15 |
+
async def process_image(file: UploadFile = File(...)):
|
16 |
+
# Save the uploaded image locally
|
17 |
+
with open("uploaded_image.jpg", "wb") as buffer:
|
18 |
+
shutil.copyfileobj(file.file, buffer)
|
19 |
+
|
20 |
+
# Load the model (assuming 'cartoon1' is always used)
|
21 |
+
exstyle, load_info = model.load_model('cartoon1')
|
22 |
+
|
23 |
+
# Process the uploaded image
|
24 |
+
top, bottom, left, right = 200, 200, 200, 200
|
25 |
+
aligned_face, _, input_info = model.detect_and_align_image("uploaded_image.jpg", top, bottom, left, right)
|
26 |
+
processed_image, message = model.image_toonify(aligned_face, None, exstyle, style_degree=0.5, style_type='cartoon1')
|
27 |
+
|
28 |
+
# Save the processed image
|
29 |
+
with open("result_image.jpg", "wb") as result_buffer:
|
30 |
+
result_buffer.write(processed_image)
|
31 |
+
|
32 |
+
# Return the processed image
|
33 |
+
return FileResponse("result_image.jpg", media_type="image/jpeg", headers={"Content-Disposition": "attachment; filename=result_image.jpg"})
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
36 |
|
37 |
@app.get("/")
|
38 |
def index() -> FileResponse:
|
39 |
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|
|