Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
| def simple_face_swap(src_img, tgt_img): | |
| src = cv2.cvtColor(np.array(src_img), cv2.COLOR_RGB2BGR) | |
| tgt = cv2.cvtColor(np.array(tgt_img), cv2.COLOR_RGB2BGR) | |
| src_faces = face_cascade.detectMultiScale(src, 1.1, 4) | |
| tgt_faces = face_cascade.detectMultiScale(tgt, 1.1, 4) | |
| if len(src_faces) == 0 or len(tgt_faces) == 0: | |
| return "❌ किसी एक फोटो में चेहरा नहीं मिला।" | |
| (x1, y1, w1, h1) = src_faces[0] | |
| (x2, y2, w2, h2) = tgt_faces[0] | |
| src_face = src[y1:y1+h1, x1:x1+w1] | |
| src_face_resized = cv2.resize(src_face, (w2, h2)) | |
| # ब्लेंडिंग | |
| mask = 0.5 * tgt[y2:y2+h2, x2:x2+w2] + 0.5 * src_face_resized | |
| tgt[y2:y2+h2, x2:x2+w2] = mask.astype(np.uint8) | |
| output = cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB) | |
| return Image.fromarray(output) | |
| gr.Interface( | |
| fn=simple_face_swap, | |
| inputs=[ | |
| gr.Image(type="pil", label="Source Face"), | |
| gr.Image(type="pil", label="Target Image") | |
| ], | |
| outputs=gr.Image(type="pil", label="Output"), | |
| title="RefaceMania Lite", | |
| description="चेहरे की आसान अदला-बदली, बिना किसी error के।" | |
| ).launch() | |