InsightWhispersAI commited on
Commit
8344f8b
·
verified ·
1 Parent(s): 8d77bc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -7
app.py CHANGED
@@ -3,59 +3,96 @@ import numpy as np
3
  import cv2
4
  import gradio as gr
5
  from insightface.app import FaceAnalysis
6
- import onnxruntime
7
  import os
8
 
 
 
 
 
 
9
  app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
10
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.3)
11
 
 
12
  inswapper_path = "checkpoints/inswapper_128.onnx"
13
  if not os.path.exists(inswapper_path):
14
  raise FileNotFoundError(f"Model not found at {inswapper_path}")
15
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
16
 
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
  def sharpen_image(img):
24
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
 
25
  sharpened = cv2.filter2D(img, -1, kernel)
26
  return np.clip(sharpened, 0, 255).astype(np.uint8)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
29
  try:
 
30
  src = preprocess_image(src_img)
31
  dst = preprocess_image(dst_img)
32
 
 
33
  src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
34
  dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
35
 
 
36
  src_faces = app.get(src_rgb)
37
  dst_faces = app.get(dst_rgb)
38
 
39
  if not src_faces or not dst_faces:
40
  raise ValueError("No faces detected in one of the images.")
41
 
 
42
  src_face = src_faces[0]
43
  dst_face = dst_faces[0]
44
 
 
45
  swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
46
 
 
47
  if blur_strength > 0:
48
  swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
49
 
 
50
  if sharpen:
51
  swapped_img = sharpen_image(swapped_img)
52
 
 
 
 
 
53
  result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
54
  return result
55
  except Exception as e:
56
  print(f"Error: {str(e)}")
57
- return np.zeros((640, 640, 3), dtype=np.uint8)
58
 
 
59
  title = "🧠 Futuristic Face Swapper with inswapper_128"
60
  description = (
61
  "Upload a source face and a target image. The AI swaps the face using inswapper_128.onnx "
@@ -77,4 +114,4 @@ demo = gr.Interface(
77
  )
78
 
79
  if __name__ == "__main__":
80
- demo.queue().launch()
 
3
  import cv2
4
  import gradio as gr
5
  from insightface.app import FaceAnalysis
 
6
  import os
7
 
8
+ # Suppress NumPy rcond warning (optional)
9
+ import warnings
10
+ warnings.filterwarnings("ignore", category=FutureWarning)
11
+
12
+ # Initialize InsightFace
13
  app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
14
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.3)
15
 
16
+ # Load inswapper model for face swapping
17
  inswapper_path = "checkpoints/inswapper_128.onnx"
18
  if not os.path.exists(inswapper_path):
19
  raise FileNotFoundError(f"Model not found at {inswapper_path}")
20
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
21
 
22
  def preprocess_image(img):
23
+ """Preprocess image to enhance it for better swap results."""
24
+ # Adjust contrast and brightness for better facial features extraction
25
+ alpha = 1.3 # contrast control
26
+ beta = 15 # brightness control
27
  adjusted = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
28
+
29
+ # Optional: histogram equalization to enhance details
30
+ adjusted = cv2.cvtColor(adjusted, cv2.COLOR_BGR2GRAY)
31
+ adjusted = cv2.equalizeHist(adjusted)
32
+ return cv2.cvtColor(adjusted, cv2.COLOR_GRAY2BGR)
33
 
34
  def sharpen_image(img):
35
+ """Enhance the sharpness of the face swapped image."""
36
+ kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
37
  sharpened = cv2.filter2D(img, -1, kernel)
38
  return np.clip(sharpened, 0, 255).astype(np.uint8)
39
 
40
+ def color_correction(src_img, dst_img):
41
+ """Color correction to ensure the source face blends seamlessly with the target face."""
42
+ src_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2RGB)
43
+ dst_img = cv2.cvtColor(dst_img, cv2.COLOR_BGR2RGB)
44
+
45
+ src_mean, src_std = cv2.meanStdDev(src_img)
46
+ dst_mean, dst_std = cv2.meanStdDev(dst_img)
47
+
48
+ for c in range(3):
49
+ src_img[:, :, c] = ((src_img[:, :, c] - src_mean[c]) * (dst_std[c] / (src_std[c] + 1e-5)) + dst_mean[c])
50
+
51
+ return cv2.cvtColor(np.clip(src_img, 0, 255).astype(np.uint8), cv2.COLOR_RGB2BGR)
52
+
53
  def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
54
  try:
55
+ # Preprocess images for better face swapping
56
  src = preprocess_image(src_img)
57
  dst = preprocess_image(dst_img)
58
 
59
+ # Convert images to RGB for InsightFace processing
60
  src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
61
  dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
62
 
63
+ # Detect faces in source and destination images
64
  src_faces = app.get(src_rgb)
65
  dst_faces = app.get(dst_rgb)
66
 
67
  if not src_faces or not dst_faces:
68
  raise ValueError("No faces detected in one of the images.")
69
 
70
+ # Extract the first face from each image
71
  src_face = src_faces[0]
72
  dst_face = dst_faces[0]
73
 
74
+ # Perform face swapping using the inswapper model
75
  swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
76
 
77
+ # Apply Gaussian blur if specified
78
  if blur_strength > 0:
79
  swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
80
 
81
+ # Apply sharpening if enabled
82
  if sharpen:
83
  swapped_img = sharpen_image(swapped_img)
84
 
85
+ # Color correction to match the destination face
86
+ swapped_img = color_correction(swapped_img, dst)
87
+
88
+ # Return the final swapped face image
89
  result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
90
  return result
91
  except Exception as e:
92
  print(f"Error: {str(e)}")
93
+ return np.zeros((640, 640, 3), dtype=np.uint8) # Fallback image
94
 
95
+ # Gradio UI setup
96
  title = "🧠 Futuristic Face Swapper with inswapper_128"
97
  description = (
98
  "Upload a source face and a target image. The AI swaps the face using inswapper_128.onnx "
 
114
  )
115
 
116
  if __name__ == "__main__":
117
+ demo.queue().launch()