krantimaina86 commited on
Commit
b01a078
·
verified ·
1 Parent(s): fe331e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -1,37 +1,44 @@
1
  import gradio as gr
2
  import cv2
3
- from PIL import Image
4
  import numpy as np
 
 
5
 
6
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
7
-
8
- def swap_faces(src_img, tgt_img):
9
- src = np.array(src_img.convert("RGB"))
10
- tgt = np.array(tgt_img.convert("RGB"))
11
-
12
- src_gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
13
- tgt_gray = cv2.cvtColor(tgt, cv2.COLOR_RGB2GRAY)
14
 
15
- src_faces = face_cascade.detectMultiScale(src_gray, 1.3, 5)
16
- tgt_faces = face_cascade.detectMultiScale(tgt_gray, 1.3, 5)
17
 
18
  if len(src_faces) == 0 or len(tgt_faces) == 0:
19
- return "❌ चेहरे नहीं मिले।"
 
 
 
 
20
 
21
- (x, y, w, h) = src_faces[0]
22
- src_face = src[y:y+h, x:x+w]
 
 
23
 
24
- (x2, y2, w2, h2) = tgt_faces[0]
25
- src_face_resized = cv2.resize(src_face, (w2, h2))
26
 
27
- tgt[y2:y2+h2, x2:x2+w2] = src_face_resized
 
 
28
 
29
  return Image.fromarray(tgt)
30
 
31
  gr.Interface(
32
- fn=swap_faces,
33
- inputs=[gr.Image(type="pil", label="Source Face"), gr.Image(type="pil", label="Target Image")],
34
- outputs=gr.Image(type="pil", label="Swapped Output"),
35
- title="🤖 RefaceMania Lite",
36
- description="Face Swapping tool (Fast Build, No Errors)"
 
 
 
37
  ).launch()
 
1
  import gradio as gr
2
  import cv2
 
3
  import numpy as np
4
+ from PIL import Image
5
+ import face_recognition
6
 
7
+ def face_swap(source_img, target_img):
8
+ src = np.array(source_img.convert("RGB"))
9
+ tgt = np.array(target_img.convert("RGB"))
 
 
 
 
 
10
 
11
+ src_faces = face_recognition.face_locations(src, model="hog")
12
+ tgt_faces = face_recognition.face_locations(tgt, model="hog")
13
 
14
  if len(src_faces) == 0 or len(tgt_faces) == 0:
15
+ return "❌ चेहरा detect नहीं हुआ।"
16
+
17
+ # Source face crop
18
+ top_s, right_s, bottom_s, left_s = src_faces[0]
19
+ src_face = src[top_s:bottom_s, left_s:right_s]
20
 
21
+ # Target face replacement
22
+ top_t, right_t, bottom_t, left_t = tgt_faces[0]
23
+ face_height = bottom_t - top_t
24
+ face_width = right_t - left_t
25
 
26
+ # Resize source face to match target size
27
+ src_face_resized = cv2.resize(src_face, (face_width, face_height))
28
 
29
+ # Blend with target face area
30
+ blended = 0.5 * tgt[top_t:bottom_t, left_t:right_t] + 0.5 * src_face_resized
31
+ tgt[top_t:bottom_t, left_t:right_t] = blended.astype(np.uint8)
32
 
33
  return Image.fromarray(tgt)
34
 
35
  gr.Interface(
36
+ fn=face_swap,
37
+ inputs=[
38
+ gr.Image(type="pil", label="👤 Source Face"),
39
+ gr.Image(type="pil", label="🎯 Target Face")
40
+ ],
41
+ outputs=gr.Image(type="pil", label="✨ Swapped Output"),
42
+ title="🔁 RefaceMania (Simple AI Blending)",
43
+ description="अपना चेहरा किसी भी फोटो में लगाएं – फेस ब्लेंडिंग के साथ।"
44
  ).launch()