krantimaina86 commited on
Commit
6e381e6
·
verified ·
1 Parent(s): 9419608

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -2,43 +2,40 @@ 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()
 
 
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
 
 
7
 
8
+ def simple_face_swap(src_img, tgt_img):
9
+ src = cv2.cvtColor(np.array(src_img), cv2.COLOR_RGB2BGR)
10
+ tgt = cv2.cvtColor(np.array(tgt_img), cv2.COLOR_RGB2BGR)
11
 
12
+ src_faces = face_cascade.detectMultiScale(src, 1.1, 4)
13
+ tgt_faces = face_cascade.detectMultiScale(tgt, 1.1, 4)
14
 
15
+ if len(src_faces) == 0 or len(tgt_faces) == 0:
16
+ return "❌ किसी एक फोटो में चेहरा नहीं मिला।"
 
17
 
18
+ (x1, y1, w1, h1) = src_faces[0]
19
+ (x2, y2, w2, h2) = tgt_faces[0]
 
 
20
 
21
+ src_face = src[y1:y1+h1, x1:x1+w1]
22
+ src_face_resized = cv2.resize(src_face, (w2, h2))
23
 
24
+ # ब्लेंडिंग
25
+ mask = 0.5 * tgt[y2:y2+h2, x2:x2+w2] + 0.5 * src_face_resized
26
+ tgt[y2:y2+h2, x2:x2+w2] = mask.astype(np.uint8)
27
 
28
+ output = cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB)
29
+ return Image.fromarray(output)
30
 
31
  gr.Interface(
32
+ fn=simple_face_swap,
33
  inputs=[
34
+ gr.Image(type="pil", label="Source Face"),
35
+ gr.Image(type="pil", label="Target Image")
36
  ],
37
+ outputs=gr.Image(type="pil", label="Output"),
38
+ title="RefaceMania Lite",
39
+ description="चेहरे की आसान अदला-बदली, बिना किसी error के।"
40
  ).launch()
41
+