K00B404 commited on
Commit
dd1e37a
·
verified ·
1 Parent(s): e3d9cfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -58
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
- # Load the input images
12
- img1 = cv2.imread(image1.name)
13
- img2 = cv2.imread(image2.name)
14
-
15
- # Detect faces in the images
16
- faces = self.face_cascade.detectMultiScale(img1)
17
- faces1 = self.face_cascade.detectMultiScale(img2)
18
-
19
- if len(faces) > 0:
20
- for (x, y, w, h) in faces:
21
- # Crop the face region from the first image
22
- cropped_face = img1[y:y+h, x:x+w]
23
-
24
- # Calculate dimensions for resizing the face from the second image
25
- desired_width = faces1[0][0] + faces1[0][2]
26
- desired_height = faces1[0][1] + faces1[0][3]
27
-
28
- # Resize the cropped face to match dimensions
29
- resized_cropped_face = cv2.resize(cropped_face, (desired_width, desired_height))
30
-
31
- # Blend the faces using the alpha value
32
- blended_face = cv2.addWeighted(resized_cropped_face, alpha, img2[y:y+h, x:x+w], 1-alpha, 0)
33
-
34
- # Position the blended face on the first image
35
- x_position = x
36
- y_position = y
37
- img1[y_position:y_position+desired_height, x_position:x_position+desired_width] = blended_face
38
-
39
- return img1
40
- else:
 
 
 
 
 
41
  return None
42
 
43
  def generate_morph(self, image1, image2, num_frames, alpha):
44
- # Load the input images
45
- img1 = cv2.imread(image1.name)
46
- img2 = cv2.imread(image2.name)
47
-
48
- # Detect faces in the images
49
- faces = self.face_cascade.detectMultiScale(img1)
50
- faces1 = self.face_cascade.detectMultiScale(img2)
51
-
52
- if len(faces) > 0:
53
- frames = []
54
- for i in range(num_frames):
55
- alpha_i = alpha + (1-alpha) * i / (num_frames-1)
56
- img = self.swap_faces(image1, image2, alpha_i)
57
- frames.append(img)
58
-
59
- # Convert the frames to a video
60
- fps = 30
61
- height, width, _ = frames[0].shape
62
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
63
- video = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))
64
-
65
- for frame in frames:
66
- video.write(frame)
67
-
68
- video.release()
69
- return 'output.mp4'
70
- else:
 
 
 
 
 
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(