Update main.py
Browse files
main.py
CHANGED
@@ -2,10 +2,9 @@ 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
|
9 |
from io import BytesIO
|
10 |
|
11 |
app = FastAPI()
|
@@ -19,8 +18,6 @@ def load_model():
|
|
19 |
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
20 |
model.load_model('cartoon4')
|
21 |
|
22 |
-
import logging
|
23 |
-
|
24 |
# Configure logging
|
25 |
logging.basicConfig(level=logging.INFO)
|
26 |
|
@@ -35,11 +32,16 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
35 |
|
36 |
# Convert the uploaded image to numpy array
|
37 |
nparr = np.frombuffer(contents, np.uint8)
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Process the uploaded image
|
42 |
-
aligned_face, instyle, message = model.detect_and_align_image(
|
43 |
if aligned_face is None or instyle is None:
|
44 |
logging.error("Failed to process the image: No face detected or alignment failed.")
|
45 |
return {"error": message}
|
@@ -49,7 +51,7 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
49 |
logging.error("Failed to toonify the image.")
|
50 |
return {"error": message}
|
51 |
|
52 |
-
# Convert BGR to RGB
|
53 |
processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
|
54 |
|
55 |
# Convert processed image to bytes
|
@@ -57,11 +59,12 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
57 |
|
58 |
# Return the processed image as a streaming response
|
59 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
|
|
60 |
# Mount static files directory
|
61 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
62 |
|
63 |
# Define index route
|
64 |
@app.get("/")
|
65 |
def index():
|
66 |
-
|
67 |
-
|
|
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
import torch
|
|
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
+
import logging
|
8 |
from io import BytesIO
|
9 |
|
10 |
app = FastAPI()
|
|
|
18 |
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
19 |
model.load_model('cartoon4')
|
20 |
|
|
|
|
|
21 |
# Configure logging
|
22 |
logging.basicConfig(level=logging.INFO)
|
23 |
|
|
|
32 |
|
33 |
# Convert the uploaded image to numpy array
|
34 |
nparr = np.frombuffer(contents, np.uint8)
|
35 |
+
frame_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
36 |
+
|
37 |
+
if frame_bgr is None:
|
38 |
+
logging.error("Failed to decode the image.")
|
39 |
+
return {"error": "Failed to decode the image. Please ensure the file is a valid image format."}
|
40 |
+
|
41 |
+
logging.info(f"Uploaded image shape: {frame_bgr.shape}")
|
42 |
|
43 |
# Process the uploaded image
|
44 |
+
aligned_face, instyle, message = model.detect_and_align_image(frame_bgr, top, bottom, left, right)
|
45 |
if aligned_face is None or instyle is None:
|
46 |
logging.error("Failed to process the image: No face detected or alignment failed.")
|
47 |
return {"error": message}
|
|
|
51 |
logging.error("Failed to toonify the image.")
|
52 |
return {"error": message}
|
53 |
|
54 |
+
# Convert BGR to RGB for display
|
55 |
processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
|
56 |
|
57 |
# Convert processed image to bytes
|
|
|
59 |
|
60 |
# Return the processed image as a streaming response
|
61 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
62 |
+
|
63 |
# Mount static files directory
|
64 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
65 |
|
66 |
# Define index route
|
67 |
@app.get("/")
|
68 |
def index():
|
69 |
+
from fastapi.responses import FileResponse
|
70 |
+
return FileResponse(path="/app/AB/index.html", media_type="text/html")
|