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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -45
app.py CHANGED
@@ -1,80 +1,59 @@
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
- # 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
 
@@ -82,17 +61,14 @@ def swap_faces(src_img, dst_img, blur_strength=5, sharpen=False):
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 "
 
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
 
42
+ # Detect faces in both images
43
  src_faces = app.get(src_rgb)
44
  dst_faces = app.get(dst_rgb)
45
 
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
 
 
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 "