Spaces:
Running
Running
Upload main (24).py
Browse files- main (24).py +42 -0
main (24).py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
from vtoonify_model import Model
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
12 |
+
|
13 |
+
@app.post("/upload/")
|
14 |
+
async def process_image(file: UploadFile = File(...)):
|
15 |
+
# Save the uploaded image locally
|
16 |
+
with open("uploaded_image.jpg", "wb") as buffer:
|
17 |
+
shutil.copyfileobj(file.file, buffer)
|
18 |
+
|
19 |
+
# Load the model (assuming 'cartoon1' is always used)
|
20 |
+
exstyle, load_info = model.load_model('cartoon1')
|
21 |
+
|
22 |
+
# Process the uploaded image
|
23 |
+
|
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, exstyle, style_type = 'cartoon1' , style_degree=0.5)
|
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")
|
40 |
+
|
41 |
+
|
42 |
+
|