InsightWhispersAI commited on
Commit
f8822f4
·
verified ·
1 Parent(s): c7fa73e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -48
app.py CHANGED
@@ -1,50 +1,39 @@
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
 
@@ -53,44 +42,35 @@ def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
53
  dst_faces = app.get(dst_rgb)
54
 
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(
 
1
  import insightface
 
2
  import cv2
3
  import gradio as gr
4
+ import numpy as np
5
  import os
6
 
7
+ # Initialize InsightFace model
8
+ app = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
9
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.3)
10
 
 
11
  inswapper_path = "checkpoints/inswapper_128.onnx"
12
  if not os.path.exists(inswapper_path):
13
  raise FileNotFoundError(f"Model not found at {inswapper_path}")
14
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
15
 
16
+ # Preprocess image to enhance it
17
+ def preprocess_image(img):
18
+ alpha = 1.3
19
+ beta = 15
20
+ adjusted = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
21
+ return adjusted
22
 
23
+ # Sharpen the image to make it look better
 
 
 
 
 
 
 
 
24
  def sharpen_image(img):
25
  kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
26
+ sharpened = cv2.filter2D(img, -1, kernel)
27
+ return np.clip(sharpened, 0, 255).astype(np.uint8)
 
 
 
28
 
29
+ # Perform face swapping
30
  def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
31
  try:
32
+ # Preprocess images to improve lighting and details
33
+ src = preprocess_image(src_img)
34
+ dst = preprocess_image(dst_img)
 
35
 
36
+ # Convert images to RGB
37
  src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
38
  dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
39
 
 
42
  dst_faces = app.get(dst_rgb)
43
 
44
  if not src_faces or not dst_faces:
45
+ raise ValueError("No faces detected in one or both of the images.")
46
 
47
+ # Get the first detected face from each image
48
  src_face = src_faces[0]
49
  dst_face = dst_faces[0]
50
 
51
+ # Perform the face swap
52
  swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
53
 
54
+ # Apply blur if specified
55
  if blur_strength > 0:
56
  swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
57
 
58
+ # Apply sharpening if specified
59
  if sharpen:
60
  swapped_img = sharpen_image(swapped_img)
61
 
62
+ # Convert back to BGR for output
63
+ result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
64
+ return result
 
 
 
 
 
 
 
 
 
65
  except Exception as e:
66
  print(f"Error: {str(e)}")
67
  return np.zeros((640, 640, 3), dtype=np.uint8)
68
 
69
+ # Define Gradio interface
70
+ title = "🧠 Futuristic Face Swapper with inswapper_128"
71
  description = (
72
  "Upload a source face and a target image. The AI swaps the face using inswapper_128.onnx "
73
+ "for clean, smooth results. Adjust blur strength or enable sharpening for enhanced output."
74
  )
75
 
76
  demo = gr.Interface(