Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 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 =
|
| 16 |
-
tgt_faces =
|
| 17 |
|
| 18 |
if len(src_faces) == 0 or len(tgt_faces) == 0:
|
| 19 |
-
return "❌
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
src_face_resized = cv2.resize(src_face, (
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return Image.fromarray(tgt)
|
| 30 |
|
| 31 |
gr.Interface(
|
| 32 |
-
fn=
|
| 33 |
-
inputs=[
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|