File size: 1,399 Bytes
fe331e4
 
 
b01a078
fe331e4
6e381e6
fe331e4
6e381e6
 
 
fe331e4
6e381e6
 
b01a078
6e381e6
 
fe331e4
6e381e6
 
fe331e4
6e381e6
 
fe331e4
6e381e6
 
 
fe331e4
6e381e6
 
fe331e4
 
6e381e6
b01a078
6e381e6
 
b01a078
6e381e6
 
 
fe331e4
6e381e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()