Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 8 |
-
src = np.array(source_img.convert("RGB"))
|
| 9 |
-
tgt = np.array(target_img.convert("RGB"))
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
src_face = src[top_s:bottom_s, left_s:right_s]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
face_height = bottom_t - top_t
|
| 24 |
-
face_width = right_t - left_t
|
| 25 |
|
| 26 |
-
|
| 27 |
-
src_face_resized = cv2.resize(src_face, (
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
tgt[
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
inputs=[
|
| 38 |
-
gr.Image(type="pil", label="
|
| 39 |
-
gr.Image(type="pil", label="
|
| 40 |
],
|
| 41 |
-
outputs=gr.Image(type="pil", label="
|
| 42 |
-
title="
|
| 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 |
+
|