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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()