Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 3 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
import torch
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
import logging
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import tempfile
|
| 11 |
+
import os
|
| 12 |
+
from insightface.app import FaceAnalysis
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
# Load model and necessary components
|
| 17 |
+
model = None
|
| 18 |
+
|
| 19 |
+
def load_model():
|
| 20 |
+
global model
|
| 21 |
+
from vtoonify_model import Model
|
| 22 |
+
model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
|
| 23 |
+
model.load_model('cartoon4')
|
| 24 |
+
|
| 25 |
+
# Initialize the InsightFace model for face detection
|
| 26 |
+
face_detector = FaceAnalysis(allowed_modules=['detection'])
|
| 27 |
+
face_detector.prepare(ctx_id=0 if torch.cuda.is_available() else -1, det_size=(640, 640))
|
| 28 |
+
|
| 29 |
+
# Configure logging
|
| 30 |
+
logging.basicConfig(level=logging.INFO)
|
| 31 |
+
|
| 32 |
+
def detect_and_crop_face(image, padding=0.6):
|
| 33 |
+
# Get original dimensions
|
| 34 |
+
orig_h, orig_w = image.shape[:2]
|
| 35 |
+
|
| 36 |
+
# Resize the image for detection
|
| 37 |
+
resized_image = cv2.resize(image, (640, 640))
|
| 38 |
+
|
| 39 |
+
# Detect faces on the resized image
|
| 40 |
+
faces = face_detector.get(resized_image)
|
| 41 |
+
|
| 42 |
+
# If faces are detected, sort by x-coordinate and select the leftmost face
|
| 43 |
+
if faces:
|
| 44 |
+
faces = sorted(faces, key=lambda face: face.bbox[0])
|
| 45 |
+
face = faces[0] # Select the leftmost face
|
| 46 |
+
bbox = face.bbox.astype(int)
|
| 47 |
+
|
| 48 |
+
# Calculate scaling factors
|
| 49 |
+
h_scale = orig_h / 640
|
| 50 |
+
w_scale = orig_w / 640
|
| 51 |
+
|
| 52 |
+
# Map the bounding box to the original image size
|
| 53 |
+
x1, y1, x2, y2 = bbox
|
| 54 |
+
x1 = int(x1 * w_scale)
|
| 55 |
+
y1 = int(y1 * h_scale)
|
| 56 |
+
x2 = int(x2 * w_scale)
|
| 57 |
+
y2 = int(y2 * h_scale)
|
| 58 |
+
|
| 59 |
+
# Calculate padding
|
| 60 |
+
width = x2 - x1
|
| 61 |
+
height = y2 - y1
|
| 62 |
+
x1 = max(0, x1 - int(padding * width))
|
| 63 |
+
y1 = max(0, y1 - int(padding * height))
|
| 64 |
+
x2 = min(orig_w, x2 + int(padding * width))
|
| 65 |
+
y2 = min(orig_h, y2 + int(padding * height))
|
| 66 |
+
|
| 67 |
+
cropped_face = image[y1:y2, x1:x2]
|
| 68 |
+
return cropped_face
|
| 69 |
+
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
@app.post("/upload/")
|
| 73 |
+
async def process_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)):
|
| 74 |
+
global model
|
| 75 |
+
if model is None:
|
| 76 |
+
load_model()
|
| 77 |
+
|
| 78 |
+
# Read the uploaded image file
|
| 79 |
+
contents = await file.read()
|
| 80 |
+
|
| 81 |
+
# Convert the uploaded image to numpy array
|
| 82 |
+
nparr = np.frombuffer(contents, np.uint8)
|
| 83 |
+
frame_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # Read as BGR format by default
|
| 84 |
+
|
| 85 |
+
if frame_bgr is None:
|
| 86 |
+
logging.error("Failed to decode the image.")
|
| 87 |
+
return {"error": "Failed to decode the image. Please ensure the file is a valid image format."}
|
| 88 |
+
logging.info(f"Uploaded image shape: {frame_bgr.shape}")
|
| 89 |
+
|
| 90 |
+
# Detect and crop face
|
| 91 |
+
cropped_face = detect_and_crop_face(frame_bgr)
|
| 92 |
+
if cropped_face is None:
|
| 93 |
+
return {"error": "No face detected or alignment failed."}
|
| 94 |
+
|
| 95 |
+
# Save the cropped face temporarily
|
| 96 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
| 97 |
+
cv2.imwrite(temp_file.name, cropped_face)
|
| 98 |
+
temp_file_path = temp_file.name
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
# Process the cropped face using the file path
|
| 102 |
+
aligned_face, instyle, message = model.detect_and_align_image(temp_file_path, top, bottom, left, right)
|
| 103 |
+
if aligned_face is None or instyle is None:
|
| 104 |
+
logging.error("Failed to process the image: No face detected or alignment failed.")
|
| 105 |
+
return {"error": message}
|
| 106 |
+
|
| 107 |
+
processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon4')
|
| 108 |
+
if processed_image is None:
|
| 109 |
+
logging.error("Failed to toonify the image.")
|
| 110 |
+
return {"error": message}
|
| 111 |
+
|
| 112 |
+
# Convert the processed image to RGB before returning
|
| 113 |
+
processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
|
| 114 |
+
|
| 115 |
+
# Convert processed image to bytes
|
| 116 |
+
_, encoded_image = cv2.imencode('.jpg', processed_image_rgb)
|
| 117 |
+
|
| 118 |
+
# Return the processed image as a streaming response
|
| 119 |
+
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
| 120 |
+
|
| 121 |
+
finally:
|
| 122 |
+
# Clean up the temporary file
|
| 123 |
+
os.remove(temp_file_path)
|
| 124 |
+
|