Update app.py
Browse files
app.py
CHANGED
|
@@ -2,72 +2,84 @@ import cv2
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
from moviepy.editor import *
|
| 5 |
-
|
|
|
|
| 6 |
class FaceSwapper:
|
| 7 |
def __init__(self):
|
|
|
|
| 8 |
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 9 |
|
| 10 |
def swap_faces(self, image1, image2, alpha):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return None
|
| 42 |
|
| 43 |
def generate_morph(self, image1, image2, num_frames, alpha):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return None
|
| 72 |
|
| 73 |
iface = gr.Interface(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
from moviepy.editor import *
|
| 5 |
+
import logger
|
| 6 |
+
logging.basicConfig(filename='logs/error.log', level=logging.ERROR)
|
| 7 |
class FaceSwapper:
|
| 8 |
def __init__(self):
|
| 9 |
+
self.logger = logging.getLogger()
|
| 10 |
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 11 |
|
| 12 |
def swap_faces(self, image1, image2, alpha):
|
| 13 |
+
try:
|
| 14 |
+
# Load the input images
|
| 15 |
+
img1 = cv2.imread(image1.name)
|
| 16 |
+
img2 = cv2.imread(image2.name)
|
| 17 |
+
|
| 18 |
+
# Detect faces in the images
|
| 19 |
+
faces = self.face_cascade.detectMultiScale(img1)
|
| 20 |
+
faces1 = self.face_cascade.detectMultiScale(img2)
|
| 21 |
+
|
| 22 |
+
if len(faces) > 0:
|
| 23 |
+
for (x, y, w, h) in faces:
|
| 24 |
+
# Crop the face region from the first image
|
| 25 |
+
cropped_face = img1[y:y+h, x:x+w]
|
| 26 |
+
|
| 27 |
+
# Calculate dimensions for resizing the face from the second image
|
| 28 |
+
desired_width = faces1[0][0] + faces1[0][2]
|
| 29 |
+
desired_height = faces1[0][1] + faces1[0][3]
|
| 30 |
+
|
| 31 |
+
# Resize the cropped face to match dimensions
|
| 32 |
+
resized_cropped_face = cv2.resize(cropped_face, (desired_width, desired_height))
|
| 33 |
+
|
| 34 |
+
# Blend the faces using the alpha value
|
| 35 |
+
blended_face = cv2.addWeighted(resized_cropped_face, alpha, img2[y:y+h, x:x+w], 1-alpha, 0)
|
| 36 |
+
|
| 37 |
+
# Position the blended face on the first image
|
| 38 |
+
x_position = x
|
| 39 |
+
y_position = y
|
| 40 |
+
img1[y_position:y_position+desired_height, x_position:x_position+desired_width] = blended_face
|
| 41 |
+
|
| 42 |
+
return img1
|
| 43 |
+
else:
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.error(f"An error occurred during face swapping: {e}")
|
| 48 |
return None
|
| 49 |
|
| 50 |
def generate_morph(self, image1, image2, num_frames, alpha):
|
| 51 |
+
try:
|
| 52 |
+
# Load the input images
|
| 53 |
+
img1 = cv2.imread(image1.name)
|
| 54 |
+
img2 = cv2.imread(image2.name)
|
| 55 |
+
|
| 56 |
+
# Detect faces in the images
|
| 57 |
+
faces = self.face_cascade.detectMultiScale(img1)
|
| 58 |
+
faces1 = self.face_cascade.detectMultiScale(img2)
|
| 59 |
+
|
| 60 |
+
if len(faces) > 0:
|
| 61 |
+
frames = []
|
| 62 |
+
for i in range(num_frames):
|
| 63 |
+
alpha_i = alpha + (1-alpha) * i / (num_frames-1)
|
| 64 |
+
img = self.swap_faces(image1, image2, alpha_i)
|
| 65 |
+
frames.append(img)
|
| 66 |
+
|
| 67 |
+
# Convert the frames to a video
|
| 68 |
+
fps = 30
|
| 69 |
+
height, width, _ = frames[0].shape
|
| 70 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 71 |
+
video = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))
|
| 72 |
+
|
| 73 |
+
for frame in frames:
|
| 74 |
+
video.write(frame)
|
| 75 |
+
|
| 76 |
+
video.release()
|
| 77 |
+
return 'output.mp4'
|
| 78 |
+
else:
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger.error(f"An error occurred during morphing: {e}")
|
| 83 |
return None
|
| 84 |
|
| 85 |
iface = gr.Interface(
|