Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +82 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import mediapipe as mp
|
| 5 |
+
|
| 6 |
+
mp_fd = mp.solutions.face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.6)
|
| 7 |
+
|
| 8 |
+
def read_rgb(image):
|
| 9 |
+
# gradioはnumpy(H,W,3) RGBで来る想定
|
| 10 |
+
if image is None:
|
| 11 |
+
return None
|
| 12 |
+
return image.copy()
|
| 13 |
+
|
| 14 |
+
def detect_face_bbox(rgb):
|
| 15 |
+
h, w, _ = rgb.shape
|
| 16 |
+
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
|
| 17 |
+
res = mp_fd.process(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB))
|
| 18 |
+
if not res.detections:
|
| 19 |
+
return None
|
| 20 |
+
# 最もスコアが高い顔を使う
|
| 21 |
+
det = sorted(res.detections, key=lambda d: d.score[0], reverse=True)[0]
|
| 22 |
+
bb = det.location_data.relative_bounding_box
|
| 23 |
+
x1 = max(int(bb.xmin * w), 0)
|
| 24 |
+
y1 = max(int(bb.ymin * h), 0)
|
| 25 |
+
x2 = min(int((bb.xmin + bb.width) * w), w - 1)
|
| 26 |
+
y2 = min(int((bb.ymin + bb.height) * h), h - 1)
|
| 27 |
+
if x2 <= x1 or y2 <= y1:
|
| 28 |
+
return None
|
| 29 |
+
return (x1, y1, x2, y2)
|
| 30 |
+
|
| 31 |
+
def naive_paste(source_rgb, target_rgb):
|
| 32 |
+
"""
|
| 33 |
+
まずは“動く”こと優先の簡易版:
|
| 34 |
+
targetの顔bboxにsourceの顔bboxをリサイズして貼り付け、簡易フェザーでなじませる。
|
| 35 |
+
(高品質にするには、この後にFaceMesh + 三角ワープ + seamlessCloneへ進む)
|
| 36 |
+
"""
|
| 37 |
+
sb = detect_face_bbox(source_rgb)
|
| 38 |
+
tb = detect_face_bbox(target_rgb)
|
| 39 |
+
if sb is None:
|
| 40 |
+
raise gr.Error("source画像から顔を検出できませんでした。正面顔・明るさを調整して再試行してください。")
|
| 41 |
+
if tb is None:
|
| 42 |
+
raise gr.Error("target画像から顔を検出できませんでした。正面顔・明るさを調整して再試行してください。")
|
| 43 |
+
|
| 44 |
+
sx1, sy1, sx2, sy2 = sb
|
| 45 |
+
tx1, ty1, tx2, ty2 = tb
|
| 46 |
+
|
| 47 |
+
s_face = source_rgb[sy1:sy2, sx1:sx2]
|
| 48 |
+
t_h, t_w = (ty2 - ty1), (tx2 - tx1)
|
| 49 |
+
s_face_rs = cv2.resize(s_face, (t_w, t_h), interpolation=cv2.INTER_LINEAR)
|
| 50 |
+
|
| 51 |
+
out = target_rgb.copy()
|
| 52 |
+
|
| 53 |
+
# マスク(楕円 + ぼかし)で簡易ブレンド
|
| 54 |
+
mask = np.zeros((t_h, t_w), dtype=np.uint8)
|
| 55 |
+
cv2.ellipse(mask, (t_w // 2, t_h // 2), (int(t_w * 0.45), int(t_h * 0.50)), 0, 0, 360, 255, -1)
|
| 56 |
+
mask = cv2.GaussianBlur(mask, (0, 0), sigmaX=max(t_w, t_h) * 0.02)
|
| 57 |
+
|
| 58 |
+
roi = out[ty1:ty2, tx1:tx2]
|
| 59 |
+
# alpha合成
|
| 60 |
+
alpha = (mask.astype(np.float32) / 255.0)[..., None]
|
| 61 |
+
blended = (alpha * s_face_rs + (1 - alpha) * roi).astype(np.uint8)
|
| 62 |
+
out[ty1:ty2, tx1:tx2] = blended
|
| 63 |
+
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
def run(source_img, target_img):
|
| 67 |
+
s = read_rgb(source_img)
|
| 68 |
+
t = read_rgb(target_img)
|
| 69 |
+
if s is None or t is None:
|
| 70 |
+
raise gr.Error("source/target の両方の画像をアップロードしてください。")
|
| 71 |
+
return naive_paste(s, t)
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# Face Swap (HF Spaces / Python)\nアップロードした2枚の画像から顔を検出し、target側にsource顔を当て込みます。")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
src = gr.Image(label="Source(当てたい顔)", type="numpy")
|
| 77 |
+
tgt = gr.Image(label="Target(当てられる側)", type="numpy")
|
| 78 |
+
btn = gr.Button("Swap")
|
| 79 |
+
out = gr.Image(label="Output", type="numpy")
|
| 80 |
+
btn.click(run, inputs=[src, tgt], outputs=out)
|
| 81 |
+
|
| 82 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
mediapipe
|