Update main.py
Browse files
main.py
CHANGED
|
@@ -7,56 +7,41 @@ import numpy as np
|
|
| 7 |
import cv2
|
| 8 |
import torch
|
| 9 |
from vtoonify_model import Model
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
-
|
| 13 |
-
# Load the model
|
| 14 |
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
-
exstyle, message = model.load_model("cartoon1")
|
| 16 |
-
|
| 17 |
-
class ImageRequest(BaseModel):
|
| 18 |
-
image_file: UploadFile = File(...)
|
| 19 |
-
|
| 20 |
-
from fastapi import FastAPI, File, UploadFile
|
| 21 |
-
from fastapi.responses import JSONResponse
|
| 22 |
-
import numpy as np
|
| 23 |
-
import cv2
|
| 24 |
-
import torch
|
| 25 |
-
from vtoonify_model import Model
|
| 26 |
|
| 27 |
-
app
|
| 28 |
-
|
| 29 |
-
# Load the model
|
| 30 |
-
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
| 31 |
-
exstyle, message = model.load_model("cartoon1")
|
| 32 |
-
|
| 33 |
-
@app.post("/upload/")
|
| 34 |
-
async def toonify_image(image_file: UploadFile = File(...)):
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Determine the file extension from the content type
|
| 39 |
-
ext = image_file.content_type.split("/")[-1]
|
| 40 |
-
# Construct a unique filename with the extension
|
| 41 |
-
filename = f"uploaded_image.{ext}"
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
# Decode the image using OpenCV
|
| 46 |
-
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
# Return the
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 57 |
|
| 58 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
| 59 |
|
| 60 |
@app.get("/")
|
| 61 |
def index() -> FileResponse:
|
| 62 |
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|
|
|
|
|
|
| 7 |
import cv2
|
| 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 upload_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
with open("temp.jpg", "wb") as buffer:
|
| 20 |
+
shutil.copyfileobj(file.file, buffer)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Perform image processing using the model
|
| 23 |
+
result_image, _, _ = model.detect_and_align_image("temp.jpg", top, bottom, left, right)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Save the result image temporarily
|
| 26 |
+
result_path = "result.jpg"
|
| 27 |
+
cv2.imwrite(result_path, result_image)
|
| 28 |
|
| 29 |
+
# Return the result image
|
| 30 |
+
with open(result_path, "rb") as result_buffer:
|
| 31 |
+
result_image_bytes = result_buffer.read()
|
| 32 |
+
|
| 33 |
+
# Remove temporary files
|
| 34 |
+
os.remove("temp.jpg")
|
| 35 |
+
os.remove(result_path)
|
| 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 |
+
|