Spaces:
Runtime error
Runtime error
Aadhithya
commited on
Commit
·
e515b84
1
Parent(s):
6390e4a
Update app.py
Browse files
app.py
CHANGED
@@ -1,215 +1,69 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import os
|
4 |
-
|
5 |
-
from utils.utils import norm_crop, estimate_norm, inverse_estimate_norm, transform_landmark_points, get_lm
|
6 |
-
from networks.layers import AdaIN, AdaptiveAttention
|
7 |
-
from tensorflow_addons.layers import InstanceNormalization
|
8 |
import numpy as np
|
9 |
-
import
|
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
# read frame
|
77 |
-
im = target
|
78 |
-
im_h, im_w, _ = im.shape
|
79 |
-
im_shape = (im_w, im_h)
|
80 |
-
|
81 |
-
detection_scale = im_w // 640 if im_w > 640 else 1
|
82 |
-
|
83 |
-
faces = RetinaFace(np.expand_dims(cv2.resize(im,
|
84 |
-
(im_w // detection_scale,
|
85 |
-
im_h // detection_scale)), axis=0)).numpy()
|
86 |
-
|
87 |
-
total_img = im / 255.0
|
88 |
-
for annotation in faces:
|
89 |
-
lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
|
90 |
-
[annotation[6] * im_w, annotation[7] * im_h],
|
91 |
-
[annotation[8] * im_w, annotation[9] * im_h],
|
92 |
-
[annotation[10] * im_w, annotation[11] * im_h],
|
93 |
-
[annotation[12] * im_w, annotation[13] * im_h]],
|
94 |
-
dtype=np.float32)
|
95 |
-
|
96 |
-
# align the detected face
|
97 |
-
M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
|
98 |
-
im_aligned = (cv2.warpAffine(im, M, (256, 256), borderValue=0.0) - 127.5) / 127.5
|
99 |
-
|
100 |
-
if "adversarial defense" in settings:
|
101 |
-
eps = adv_slider / 200
|
102 |
-
X = tf.convert_to_tensor(np.expand_dims(im_aligned, axis=0))
|
103 |
-
with tf.GradientTape() as tape:
|
104 |
-
tape.watch(X)
|
105 |
-
|
106 |
-
X_z = ArcFaceE(tf.image.resize(X * 0.5 + 0.5, [112, 112]))
|
107 |
-
output = R([X, X_z])
|
108 |
-
|
109 |
-
loss = tf.reduce_mean(tf.abs(0 - output))
|
110 |
-
|
111 |
-
gradient = tf.sign(tape.gradient(loss, X))
|
112 |
-
|
113 |
-
adv_x = X + eps * gradient
|
114 |
-
im_aligned = tf.clip_by_value(adv_x, -1, 1)[0]
|
115 |
-
|
116 |
-
if "anonymize" in settings and "reconstruction attack" not in settings:
|
117 |
-
"""source_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
118 |
-
anon_ratio = int(512 * (slider / 100))
|
119 |
-
anon_vector = np.ones(shape=(1, 512))
|
120 |
-
anon_vector[:, :anon_ratio] = -1
|
121 |
-
np.random.shuffle(anon_vector)
|
122 |
-
source_z *= anon_vector"""
|
123 |
-
|
124 |
-
slider_weight = slider / 100
|
125 |
-
|
126 |
-
target_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) * 0.5 + 0.5, axis=0))
|
127 |
-
# source_z = IDP.predict(target_z)
|
128 |
-
|
129 |
-
source_z = slider_weight * source_z + (1 - slider_weight) * target_z
|
130 |
-
|
131 |
-
if "reconstruction attack" in settings:
|
132 |
-
source_z = ArcFaceE.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) * 0.5 + 0.5, axis=0))
|
133 |
-
|
134 |
-
# face swap
|
135 |
-
if "reconstruction attack" not in settings:
|
136 |
-
changed_face_cage = G.predict([np.expand_dims(im_aligned, axis=0),
|
137 |
-
source_z])
|
138 |
-
changed_face = changed_face_cage[0] * 0.5 + 0.5
|
139 |
-
|
140 |
-
# get inverse transformation landmarks
|
141 |
-
transformed_lmk = transform_landmark_points(M, lm_align)
|
142 |
-
|
143 |
-
# warp image back
|
144 |
-
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
145 |
-
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
146 |
-
|
147 |
-
# blend swapped face with target image
|
148 |
-
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
149 |
-
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
150 |
-
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
151 |
-
else:
|
152 |
-
changed_face_cage = R.predict([np.expand_dims(im_aligned, axis=0),
|
153 |
-
source_z])
|
154 |
-
changed_face = changed_face_cage[0] * 0.5 + 0.5
|
155 |
-
|
156 |
-
# get inverse transformation landmarks
|
157 |
-
transformed_lmk = transform_landmark_points(M, lm_align)
|
158 |
-
|
159 |
-
# warp image back
|
160 |
-
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
161 |
-
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
162 |
-
|
163 |
-
# blend swapped face with target image
|
164 |
-
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
165 |
-
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
166 |
-
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
167 |
-
|
168 |
-
if "compare" in settings:
|
169 |
-
total_img = np.concatenate((im / 255.0, total_img), axis=1)
|
170 |
-
|
171 |
-
total_img = np.clip(total_img, 0, 1)
|
172 |
-
total_img *= 255.0
|
173 |
-
total_img = total_img.astype('uint8')
|
174 |
-
|
175 |
-
return total_img
|
176 |
-
except Exception as e:
|
177 |
-
print(e)
|
178 |
-
return None
|
179 |
-
|
180 |
-
|
181 |
-
description = "Performs subject agnostic identity transfer from a source face to all target faces. \n\n" \
|
182 |
-
"Implementation and demo of FaceDancer, accepted to WACV 2023. \n\n" \
|
183 |
-
"Pre-print: https://arxiv.org/abs/2210.10473 \n\n" \
|
184 |
-
"Code: https://github.com/felixrosberg/FaceDancer \n\n" \
|
185 |
-
"\n\n" \
|
186 |
-
"Options:\n\n" \
|
187 |
-
"-Compare returns the target image concatenated with the results.\n\n" \
|
188 |
-
"-Anonymize will ignore the source image and perform an identity permutation of target faces.\n\n" \
|
189 |
-
"-Reconstruction attack will attempt to invert the face swap or the anonymization.\n\n" \
|
190 |
-
"-Adversarial defense will add a permutation noise that disrupts the reconstruction attack.\n\n" \
|
191 |
-
"NOTE: There is no guarantees with the anonymization process currently.\n\n" \
|
192 |
-
"NOTE: source image with too high resolution may not work properly!"
|
193 |
-
examples = [["assets/rick.jpg", "assets/musk.jpg", 100, 10, ["compare"]],
|
194 |
-
["assets/musk.jpg", "assets/musk.jpg", 100, 10, ["anonymize"]]]
|
195 |
-
article = """
|
196 |
-
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
197 |
-
"""
|
198 |
-
|
199 |
-
iface = gradio.Interface(run_inference,
|
200 |
-
[gradio.Image(shape=None, type="pil", label='Target'),
|
201 |
-
gradio.Image(shape=None, type="pil", label='Source'),
|
202 |
-
gradio.Slider(0, 100, default=100, label="Anonymization ratio (%)"),
|
203 |
-
gradio.Slider(0, 100, default=100, label="Adversarial defense ratio (%)"),
|
204 |
-
gradio.CheckboxGroup(["compare",
|
205 |
-
"anonymize",
|
206 |
-
"reconstruction attack",
|
207 |
-
"adversarial defense"],
|
208 |
-
label='Options')],
|
209 |
-
"image",
|
210 |
-
title="Face Swap",
|
211 |
-
description=description,
|
212 |
-
examples=examples,
|
213 |
-
article=article,
|
214 |
-
layout="vertical")
|
215 |
-
iface.launch()
|
|
|
1 |
+
# -* coding:UTF-8 -*
|
2 |
+
# !/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
import roop.globals
|
6 |
+
from roop.core import (
|
7 |
+
start,
|
8 |
+
decode_execution_providers,
|
9 |
+
suggest_max_memory,
|
10 |
+
suggest_execution_threads,
|
11 |
+
)
|
12 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
13 |
+
from roop.utilities import normalize_output_path
|
14 |
+
import os
|
15 |
+
from PIL import Image
|
16 |
+
|
17 |
+
|
18 |
+
def swap_face(source_file, target_file):
|
19 |
+
|
20 |
+
source_path = "input.jpg"
|
21 |
+
target_path = "target.jpg"
|
22 |
+
|
23 |
+
source_image = Image.fromarray(source_file)
|
24 |
+
source_image.save(source_path)
|
25 |
+
target_image = Image.fromarray(target_file)
|
26 |
+
target_image.save(target_path)
|
27 |
+
|
28 |
+
print("source_path: ", source_path)
|
29 |
+
print("target_path: ", target_path)
|
30 |
+
|
31 |
+
roop.globals.source_path = source_path
|
32 |
+
roop.globals.target_path = target_path
|
33 |
+
output_path = "output.jpg"
|
34 |
+
roop.globals.output_path = normalize_output_path(
|
35 |
+
roop.globals.source_path, roop.globals.target_path, output_path
|
36 |
+
)
|
37 |
+
roop.globals.frame_processors = ["face_swapper"]
|
38 |
+
roop.globals.headless = True
|
39 |
+
roop.globals.keep_fps = True
|
40 |
+
roop.globals.keep_audio = True
|
41 |
+
roop.globals.keep_frames = False
|
42 |
+
roop.globals.many_faces = False
|
43 |
+
roop.globals.video_encoder = "libx264"
|
44 |
+
roop.globals.video_quality = 18
|
45 |
+
roop.globals.max_memory = suggest_max_memory()
|
46 |
+
roop.globals.execution_providers = decode_execution_providers(["cpu"])
|
47 |
+
roop.globals.execution_threads = suggest_execution_threads()
|
48 |
+
|
49 |
+
print(
|
50 |
+
"start process",
|
51 |
+
roop.globals.source_path,
|
52 |
+
roop.globals.target_path,
|
53 |
+
roop.globals.output_path,
|
54 |
+
)
|
55 |
+
|
56 |
+
for frame_processor in get_frame_processors_modules(
|
57 |
+
roop.globals.frame_processors
|
58 |
+
):
|
59 |
+
if not frame_processor.pre_check():
|
60 |
+
return
|
61 |
+
|
62 |
+
start()
|
63 |
+
return output_path
|
64 |
+
|
65 |
+
|
66 |
+
app = gr.Interface(
|
67 |
+
fn=swap_face, inputs=[gr.Image(), gr.Image()], outputs="image"
|
68 |
+
)
|
69 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|