InsightWhispersAI commited on
Commit
f6ca598
·
verified ·
1 Parent(s): 3ee7bc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -1,41 +1,50 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
  import insightface
5
- from insightface.app import FaceAnalysis
 
6
  import gradio as gr
 
 
7
 
8
- # Initialize InsightFace for face detection
9
  app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
10
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.3)
11
 
12
- # Load the InSwapper model (lightweight face swapper)
13
  inswapper_path = "checkpoints/inswapper_128.onnx"
14
  if not os.path.exists(inswapper_path):
15
  raise FileNotFoundError(f"Model not found at {inswapper_path}")
16
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
17
 
18
- # Preprocessing function to adjust contrast and brightness
19
- def preprocess_image(img):
20
- alpha = 1.3 # Contrast control
21
- beta = 15 # Brightness control
22
- adjusted = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
23
- return adjusted
 
 
 
 
 
24
 
25
- # Function to sharpen the image to enhance details
26
  def sharpen_image(img):
27
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) # Sharpening kernel
28
- sharpened = cv2.filter2D(img, -1, kernel)
29
- return np.clip(sharpened, 0, 255).astype(np.uint8)
 
 
 
30
 
31
- # Function to perform face swapping
32
  def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
33
  try:
34
- # Preprocess both images
35
- src = preprocess_image(src_img)
36
- dst = preprocess_image(dst_img)
 
37
 
38
- # Convert images to RGB
39
  src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
40
  dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
41
 
@@ -46,33 +55,42 @@ def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
46
  if not src_faces or not dst_faces:
47
  raise ValueError("No faces detected in one of the images.")
48
 
49
- # Use the first detected faces
50
  src_face = src_faces[0]
51
  dst_face = dst_faces[0]
52
 
53
- # Perform face swapping using the inswapper model
54
  swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
55
 
56
- # Apply Gaussian blur if required
57
  if blur_strength > 0:
58
  swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
59
 
60
- # Apply sharpening if enabled
61
  if sharpen:
62
  swapped_img = sharpen_image(swapped_img)
63
 
64
- # Convert the final image back to BGR
65
- result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
66
- return result
 
 
 
 
 
 
 
 
 
67
  except Exception as e:
68
  print(f"Error: {str(e)}")
69
- return np.zeros((640, 640, 3), dtype=np.uint8) # Return a blank image on error
70
 
71
  # Gradio interface setup
72
- title = "🧠 Futuristic Face Swapper with inswapper_128"
73
  description = (
74
  "Upload a source face and a target image. The AI swaps the face using inswapper_128.onnx "
75
- "for clean, smooth results. Adjust blur strength or enable sharpening for enhanced output."
76
  )
77
 
78
  demo = gr.Interface(
 
 
 
 
1
  import insightface
2
+ import numpy as np
3
+ import cv2
4
  import gradio as gr
5
+ from insightface.app import FaceAnalysis
6
+ import os
7
 
8
+ # Initialize InsightFace
9
  app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
10
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.3)
11
 
12
+ # Load InSwapper model
13
  inswapper_path = "checkpoints/inswapper_128.onnx"
14
  if not os.path.exists(inswapper_path):
15
  raise FileNotFoundError(f"Model not found at {inswapper_path}")
16
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
17
 
18
+ # Denoising to remove noise from the images
19
+ def denoise_image(img):
20
+ return cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
21
+
22
+ # Color transfer to match the source image to the target's color tone
23
+ def color_transfer(src, dst):
24
+ src_mean, src_std = cv2.meanStdDev(src)
25
+ dst_mean, dst_std = cv2.meanStdDev(dst)
26
+ for c in range(3):
27
+ src[:, :, c] = ((src[:, :, c] - src_mean[c]) * (dst_std[c] / (src_std[c] + 1e-5)) + dst_mean[c])
28
+ return np.clip(src, 0, 255).astype(np.uint8)
29
 
30
+ # Sharpening to enhance the details on the swapped face
31
  def sharpen_image(img):
32
+ kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
33
+ return cv2.filter2D(img, -1, kernel)
34
+
35
+ # Seamless Cloning for better blending
36
+ def seamless_cloning(src, dst, mask, center):
37
+ return cv2.seamlessClone(src, dst, mask, center, cv2.NORMAL_CLONE)
38
 
39
+ # Swap faces and improve blending
40
  def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
41
  try:
42
+ # Preprocessing (denoise and color match)
43
+ src = denoise_image(src_img)
44
+ dst = denoise_image(dst_img)
45
+ src = color_transfer(src, dst)
46
 
47
+ # Convert to RGB
48
  src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
49
  dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
50
 
 
55
  if not src_faces or not dst_faces:
56
  raise ValueError("No faces detected in one of the images.")
57
 
58
+ # Select the first detected faces
59
  src_face = src_faces[0]
60
  dst_face = dst_faces[0]
61
 
62
+ # Get the swapped image
63
  swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
64
 
65
+ # Apply blur if necessary
66
  if blur_strength > 0:
67
  swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
68
 
69
+ # Apply sharpening if requested
70
  if sharpen:
71
  swapped_img = sharpen_image(swapped_img)
72
 
73
+ # Convert back to BGR for final output
74
+ swapped_bgr = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
75
+
76
+ # Create mask for seamless cloning
77
+ mask = np.zeros(swapped_bgr.shape, dtype=np.uint8)
78
+ mask[dst_face.ymin:dst_face.ymax, dst_face.xmin:dst_face.xmax] = 255
79
+ center = (dst_face.center[0], dst_face.center[1])
80
+
81
+ # Perform seamless cloning
82
+ final_result = seamless_cloning(swapped_bgr, dst_img, mask, center)
83
+
84
+ return final_result
85
  except Exception as e:
86
  print(f"Error: {str(e)}")
87
+ return np.zeros((640, 640, 3), dtype=np.uint8)
88
 
89
  # Gradio interface setup
90
+ title = "🧠 Futuristic Face Swapper with Enhanced Blending"
91
  description = (
92
  "Upload a source face and a target image. The AI swaps the face using inswapper_128.onnx "
93
+ "with enhancements such as color matching, sharpening, and seamless blending for smoother results."
94
  )
95
 
96
  demo = gr.Interface(