Update app.py
Browse files
app.py
CHANGED
@@ -47,8 +47,7 @@ def process_video(
|
|
47 |
tmp_vid = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
48 |
out_vid = cv2.VideoWriter(tmp_vid, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w_o, h_o))
|
49 |
|
50 |
-
|
51 |
-
|
52 |
# --- Frame-by-frame processing ---
|
53 |
for i in range(total):
|
54 |
ret, frame = cap.read()
|
@@ -66,11 +65,7 @@ def process_video(
|
|
66 |
alpha = float(np.clip((trans+1)/2, 0, 1))
|
67 |
mor = ff.morph_faces(crop_resized, ref_morph, alpha, res, step)
|
68 |
|
69 |
-
|
70 |
-
first_crop = crop_resized.copy()
|
71 |
-
first_ref = ref_morph.copy()
|
72 |
-
first_mask = cv2.resize(mask_roi, (res, res), interpolation=cv2.INTER_LINEAR)
|
73 |
-
first_morphed = mor.copy()
|
74 |
|
75 |
mor_back = cv2.resize(mor, (w, h))
|
76 |
mask_n = (mask_roi.astype(np.float32)[..., None] / 255.0)
|
@@ -82,49 +77,35 @@ def process_video(
|
|
82 |
cap.release()
|
83 |
out_vid.release()
|
84 |
|
85 |
-
|
86 |
-
if first_morphed is not None and first_mask is not None:
|
87 |
-
mask_n0 = first_mask.astype(np.float32)[..., None] / 255.0
|
88 |
-
first_morphed = (first_morphed.astype(np.float32) * mask_n0).astype(np.uint8)
|
89 |
-
else:
|
90 |
-
zero = np.zeros((res, res, 3), dtype=np.uint8)
|
91 |
-
first_crop = first_crop or zero
|
92 |
-
first_ref = first_ref or ref_morph
|
93 |
-
first_morphed = zero
|
94 |
|
95 |
progress(1.0, desc="Done")
|
96 |
-
return tmp_vid
|
97 |
-
|
98 |
-
cv2.cvtColor(first_ref, cv2.COLOR_BGR2RGB), \
|
99 |
-
cv2.cvtColor(first_morphed, cv2.COLOR_BGR2RGB)
|
100 |
-
|
101 |
# --- Gradio App ---
|
102 |
css = """video, img { object-fit: contain !important; }"""
|
103 |
with gr.Blocks(css=css) as iface:
|
104 |
-
gr.Markdown("#
|
105 |
with gr.Row():
|
106 |
vid = gr.Video(label='Input Video')
|
107 |
ref = gr.Image(type='numpy', label='Reference Image')
|
108 |
with gr.Row():
|
109 |
-
res = gr.Dropdown([256,384,512,768], value=512, label='Resolution')
|
110 |
-
step = gr.Slider(1,4,value=
|
111 |
-
feather = gr.Slider(0.
|
112 |
-
|
113 |
-
trans = gr.Slider(-1.0,1.0,value=-0.35,step=0.05,label='
|
114 |
btn = gr.Button('Generate Morph 🚀')
|
115 |
out_vid = gr.Video(label='Morphed Video')
|
116 |
-
|
117 |
-
out_ref = gr.Image(label='Masked Reference')
|
118 |
-
out_morph = gr.Image(label='Masked Morphed First Frame')
|
119 |
|
120 |
btn.click(
|
121 |
fn=process_video,
|
122 |
-
inputs=[vid, ref, trans, res, step, feather,
|
123 |
-
outputs=[out_vid
|
124 |
show_progress=True
|
125 |
)
|
126 |
|
127 |
-
gr.Markdown("---\n*Default values set and feather/padding are now relative percentages.*")
|
128 |
|
129 |
# Enable queueing so progress updates render
|
130 |
iface.queue().launch(debug=True)
|
|
|
47 |
tmp_vid = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
48 |
out_vid = cv2.VideoWriter(tmp_vid, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w_o, h_o))
|
49 |
|
50 |
+
|
|
|
51 |
# --- Frame-by-frame processing ---
|
52 |
for i in range(total):
|
53 |
ret, frame = cap.read()
|
|
|
65 |
alpha = float(np.clip((trans+1)/2, 0, 1))
|
66 |
mor = ff.morph_faces(crop_resized, ref_morph, alpha, res, step)
|
67 |
|
68 |
+
|
|
|
|
|
|
|
|
|
69 |
|
70 |
mor_back = cv2.resize(mor, (w, h))
|
71 |
mask_n = (mask_roi.astype(np.float32)[..., None] / 255.0)
|
|
|
77 |
cap.release()
|
78 |
out_vid.release()
|
79 |
|
80 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
progress(1.0, desc="Done")
|
83 |
+
return tmp_vid
|
84 |
+
|
|
|
|
|
|
|
85 |
# --- Gradio App ---
|
86 |
css = """video, img { object-fit: contain !important; }"""
|
87 |
with gr.Blocks(css=css) as iface:
|
88 |
+
gr.Markdown("# FaceFlux: Super Fast face swap")
|
89 |
with gr.Row():
|
90 |
vid = gr.Video(label='Input Video')
|
91 |
ref = gr.Image(type='numpy', label='Reference Image')
|
92 |
with gr.Row():
|
93 |
+
res = gr.Dropdown([256,384,512,768], value=512, label='Face Resolution')
|
94 |
+
step = gr.Slider(1,4,value=1,step=1,label='Quality')
|
95 |
+
feather = gr.Slider(0.1,0.24,value=0.1,step=0.01,label='Feather (%)')
|
96 |
+
|
97 |
+
trans = gr.Slider(-1.0,1.0,value=-0.35,step=0.05,label='Strength')
|
98 |
btn = gr.Button('Generate Morph 🚀')
|
99 |
out_vid = gr.Video(label='Morphed Video')
|
100 |
+
|
|
|
|
|
101 |
|
102 |
btn.click(
|
103 |
fn=process_video,
|
104 |
+
inputs=[vid, ref, trans, res, (5 - step), feather, 0.24],
|
105 |
+
outputs=[out_vid],
|
106 |
show_progress=True
|
107 |
)
|
108 |
|
|
|
109 |
|
110 |
# Enable queueing so progress updates render
|
111 |
iface.queue().launch(debug=True)
|