habibahmad commited on
Commit
3601b2f
·
verified ·
1 Parent(s): 40e57d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -5,45 +5,62 @@ from PIL import Image
5
  import insightface
6
  from insightface.app import FaceAnalysis
7
 
8
- # Load face detector and swapper
9
  app = FaceAnalysis(name='buffalo_l')
10
  app.prepare(ctx_id=0, det_size=(640, 640))
 
11
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx')
12
 
13
  def to_numpy(img):
 
14
  if isinstance(img, Image.Image):
15
  return np.array(img.convert("RGB"))
16
  return img
17
 
18
- def swap_faces(source_img, dest_img, source_index=1, dest_index=1):
 
 
 
 
 
 
 
 
 
 
 
19
  source_np = to_numpy(source_img)
20
- dest_np = to_numpy(dest_img)
21
 
22
  # Detect faces
23
  source_faces = sorted(app.get(source_np), key=lambda x: x.bbox[0])
24
- dest_faces = sorted(app.get(dest_np), key=lambda x: x.bbox[0])
25
 
26
- if len(source_faces) < source_index or len(dest_faces) < dest_index:
27
  raise gr.Error("Face index out of range!")
28
 
29
  source_face = source_faces[source_index - 1]
30
- dest_face = dest_faces[dest_index - 1]
 
 
 
 
 
 
 
31
 
32
- # Swap
33
- result = swapper.get(dest_np, dest_face, source_face, paste_back=True)
34
- result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
35
- return Image.fromarray(result_rgb)
36
 
37
- # Gradio app
38
  gr.Interface(
39
  fn=swap_faces,
40
  inputs=[
41
  gr.Image(label="Source Image"),
42
  gr.Image(label="Target Image"),
43
  gr.Number(value=1, label="Source Face Index"),
44
- gr.Number(value=1, label="Target Face Index"),
45
  ],
46
  outputs=gr.Image(label="Swapped Output"),
47
- title="Face Swap with InsightFace",
48
- description="Swap faces using InsightFace and ONNX model (inswapper_128.onnx)."
49
  ).launch()
 
5
  import insightface
6
  from insightface.app import FaceAnalysis
7
 
8
+ # Initialize face analysis and swapper model
9
  app = FaceAnalysis(name='buffalo_l')
10
  app.prepare(ctx_id=0, det_size=(640, 640))
11
+
12
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx')
13
 
14
  def to_numpy(img):
15
+ """Convert PIL image to NumPy array in RGB."""
16
  if isinstance(img, Image.Image):
17
  return np.array(img.convert("RGB"))
18
  return img
19
 
20
+ def enhance_image_colors(image_np):
21
+ """Apply slight contrast and brightness enhancements."""
22
+ lab = cv2.cvtColor(image_np, cv2.COLOR_RGB2LAB)
23
+ l, a, b = cv2.split(lab)
24
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
25
+ l_clahe = clahe.apply(l)
26
+ lab_enhanced = cv2.merge((l_clahe, a, b))
27
+ enhanced = cv2.cvtColor(lab_enhanced, cv2.COLOR_LAB2RGB)
28
+ return enhanced
29
+
30
+ def swap_faces(source_img, target_img, source_index=1, target_index=1):
31
+ # Convert images to NumPy
32
  source_np = to_numpy(source_img)
33
+ target_np = to_numpy(target_img)
34
 
35
  # Detect faces
36
  source_faces = sorted(app.get(source_np), key=lambda x: x.bbox[0])
37
+ target_faces = sorted(app.get(target_np), key=lambda x: x.bbox[0])
38
 
39
+ if len(source_faces) < source_index or len(target_faces) < target_index:
40
  raise gr.Error("Face index out of range!")
41
 
42
  source_face = source_faces[source_index - 1]
43
+ target_face = target_faces[target_index - 1]
44
+
45
+ # Perform face swap
46
+ result_bgr = swapper.get(target_np, target_face, source_face, paste_back=True)
47
+ result_rgb = cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB)
48
+
49
+ # Enhance image for better visual quality
50
+ enhanced_result = enhance_image_colors(result_rgb)
51
 
52
+ return Image.fromarray(enhanced_result)
 
 
 
53
 
54
+ # Gradio Interface
55
  gr.Interface(
56
  fn=swap_faces,
57
  inputs=[
58
  gr.Image(label="Source Image"),
59
  gr.Image(label="Target Image"),
60
  gr.Number(value=1, label="Source Face Index"),
61
+ gr.Number(value=1, label="Target Face Index")
62
  ],
63
  outputs=gr.Image(label="Swapped Output"),
64
+ title="✨ Face Swap with InsightFace",
65
+ description="Upload two images and swap faces using InsightFace (inswapper_128.onnx). Adjust face indices if needed."
66
  ).launch()