Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
-
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import torch
|
| 5 |
-
import shutil
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
-
import io
|
| 9 |
from io import BytesIO
|
| 10 |
|
| 11 |
app = FastAPI()
|
|
@@ -20,7 +18,7 @@ def load_model():
|
|
| 20 |
model.load_model('cartoon4')
|
| 21 |
|
| 22 |
@app.post("/upload/")
|
| 23 |
-
async def process_image(file: UploadFile = File(...)
|
| 24 |
global model
|
| 25 |
if model is None:
|
| 26 |
load_model()
|
|
@@ -32,8 +30,13 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
| 32 |
nparr = np.frombuffer(contents, np.uint8)
|
| 33 |
frame_rgb = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Process the uploaded image
|
| 36 |
-
aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, top, bottom, left, right)
|
| 37 |
processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon4')
|
| 38 |
|
| 39 |
# Convert BGR to RGB
|
|
@@ -45,12 +48,10 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
| 45 |
# Return the processed image as a streaming response
|
| 46 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
| 47 |
|
| 48 |
-
|
| 49 |
# Mount static files directory
|
| 50 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
| 51 |
|
| 52 |
# Define index route
|
| 53 |
@app.get("/")
|
| 54 |
def index():
|
| 55 |
-
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|
| 56 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import torch
|
|
|
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 18 |
model.load_model('cartoon4')
|
| 19 |
|
| 20 |
@app.post("/upload/")
|
| 21 |
+
async def process_image(file: UploadFile = File(...)):
|
| 22 |
global model
|
| 23 |
if model is None:
|
| 24 |
load_model()
|
|
|
|
| 30 |
nparr = np.frombuffer(contents, np.uint8)
|
| 31 |
frame_rgb = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 32 |
|
| 33 |
+
# Automatically detect and align the face
|
| 34 |
+
aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, 0, 0, 0, 0)
|
| 35 |
+
|
| 36 |
+
if instyle is None:
|
| 37 |
+
return {"error": "No face detected. Please try a different image."}
|
| 38 |
+
|
| 39 |
# Process the uploaded image
|
|
|
|
| 40 |
processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon4')
|
| 41 |
|
| 42 |
# Convert BGR to RGB
|
|
|
|
| 48 |
# Return the processed image as a streaming response
|
| 49 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
| 50 |
|
|
|
|
| 51 |
# Mount static files directory
|
| 52 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
| 53 |
|
| 54 |
# Define index route
|
| 55 |
@app.get("/")
|
| 56 |
def index():
|
| 57 |
+
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|
|
|