Spaces:
Runtime error
Runtime error
Commit
·
1649ca8
1
Parent(s):
7206806
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,228 +12,7 @@ token = os.environ['model_fetch']
|
|
| 12 |
|
| 13 |
anonymizer_repo = Repository(local_dir="anonymizer", clone_from="felixrosberg/anonymizer", use_auth_token=token)
|
| 14 |
|
| 15 |
-
from anonymizer.
|
| 16 |
-
|
| 17 |
-
description = "Interactive demo of facial anonymization for images. " \
|
| 18 |
-
"You can also perform " \
|
| 19 |
-
"zero-shot face swapping using the anonymizer. **Note** that this model " \
|
| 20 |
-
"display " \
|
| 21 |
-
"an interesting behaviour, where it in most cases preserves ethnicity and gender even if source and " \
|
| 22 |
-
"target differ in these two attributes. You can also play around with *reconstruction attacks*, an adversarial " \
|
| 23 |
-
"model that can revert the anonymization. Furthermore, you can choose to include adversarial defense " \
|
| 24 |
-
"noise against the *reconstruction attack* model.\n\n"
|
| 25 |
-
|
| 26 |
-
article = "## Options \n " \
|
| 27 |
-
"- **Zero-shot face swap**: Performs face swap using the *name pending* anonymizer. \n" \
|
| 28 |
-
"- **Reconstruction attack**: After anonymization or face swap, performs a reconstruction attack, " \
|
| 29 |
-
"retrieving the original identity. \n " \
|
| 30 |
-
"- **Adverarial Defense**: Applies a uniform noise after anonymization or face swap, before a " \
|
| 31 |
-
"reconstruction attack, disrupting the reconstruction attack. \n " \
|
| 32 |
-
"- **Use anchor**: With this option, we generate anchor identities from precalculated identities. If " \
|
| 33 |
-
"this is not used, the original identity could be easily figured out be searching for a match with " \
|
| 34 |
-
"-z, where z is an identity embedding in a data base. \n" \
|
| 35 |
-
"- **3DMM**: Change to model regularized by a 3D Morphable Model. Better pose performance, slightly worse " \
|
| 36 |
-
"identity performance. As of now, reconstruction attack does not work on this model. \n" \
|
| 37 |
-
"## Sliders \n " \
|
| 38 |
-
"- **Defense Strength**: Controls how much adversarial defense noise to add. \n " \
|
| 39 |
-
"- **Margin**: Controls the cosine distance margin allowed when sampling anonymization identities. \n " \
|
| 40 |
-
"- **SLERP Factor**: Controls how the anchors are established. Expect variation in the anonymized face " \
|
| 41 |
-
"when changing this together with the margin. \n" \
|
| 42 |
-
"- **ID Interpolation**: Controls linear interpolation between target and source identity. \n " \
|
| 43 |
-
"- **Detection Scale**: Resizes the images with given value before passing the the detector. Useful " \
|
| 44 |
-
"for image with smaller faces that the detector struggle to detect. Increases inference time if > 1 " \
|
| 45 |
-
"and vice versa. \n" \
|
| 46 |
-
"## Outputs \n " \
|
| 47 |
-
"- **Output**: Manipulated face(s), anonymization, face swap, reconstruction attack etc. \n " \
|
| 48 |
-
"- **Mask**: The predicted mask from the model or the reconstruction attack model. \n" \
|
| 49 |
-
"- **Cosine Distances**: The mean cosine distance between anonymized face, target, inverse target, " \
|
| 50 |
-
"source and reconstruction. Visualizes the identity difference. If there are several faces present, " \
|
| 51 |
-
"this will demonstrate the mean. \n\n" \
|
| 52 |
-
"To consider the anonymization a success, we generally want the cosine distance between anonymized and " \
|
| 53 |
-
"target, anonymized and inverse target, and target and reconstruction to be > 0.63 (the " \
|
| 54 |
-
"threshold for ArcFace for a false acceptance rate of 0.001). \n \n" \
|
| 55 |
-
"## Disclaimer: DeepFake Capabilities \n " \
|
| 56 |
-
"This model is able to do identity swaps that is able to be exceptionally convincing for state-of-the-art facial " \
|
| 57 |
-
"recognition models. However, when changing the identity, the results are perceptually limited. Meaning that in " \
|
| 58 |
-
"most cases you are not really to notice the imposed identity. Thus, it is NOT practical for deep faking people in " \
|
| 59 |
-
"a malicious manner. To summaries, DeepFake capabilities are poor, anonymization capabilities are strong!"
|
| 60 |
-
|
| 61 |
-
theme = gr.themes.Monochrome(
|
| 62 |
-
secondary_hue="emerald",
|
| 63 |
-
neutral_hue="teal",
|
| 64 |
-
).set(
|
| 65 |
-
body_background_fill='*primary_950',
|
| 66 |
-
body_background_fill_dark='*secondary_950',
|
| 67 |
-
body_text_color='*primary_50',
|
| 68 |
-
body_text_color_dark='*secondary_100',
|
| 69 |
-
body_text_color_subdued='*primary_300',
|
| 70 |
-
body_text_color_subdued_dark='*primary_300',
|
| 71 |
-
background_fill_primary='*primary_600',
|
| 72 |
-
background_fill_primary_dark='*primary_400',
|
| 73 |
-
background_fill_secondary='*primary_950',
|
| 74 |
-
background_fill_secondary_dark='*primary_950',
|
| 75 |
-
border_color_accent='*secondary_600',
|
| 76 |
-
border_color_primary='*secondary_50',
|
| 77 |
-
border_color_primary_dark='*secondary_50',
|
| 78 |
-
color_accent='*secondary_50',
|
| 79 |
-
color_accent_soft='*primary_500',
|
| 80 |
-
color_accent_soft_dark='*primary_500',
|
| 81 |
-
link_text_color='*secondary_950',
|
| 82 |
-
link_text_color_dark='*primary_50',
|
| 83 |
-
link_text_color_active='*primary_50',
|
| 84 |
-
link_text_color_active_dark='*primary_50',
|
| 85 |
-
link_text_color_hover='*primary_50',
|
| 86 |
-
link_text_color_hover_dark='*primary_50',
|
| 87 |
-
link_text_color_visited='*primary_50',
|
| 88 |
-
block_background_fill='*primary_950',
|
| 89 |
-
block_background_fill_dark='*primary_950',
|
| 90 |
-
block_border_color='*secondary_500',
|
| 91 |
-
block_border_color_dark='*secondary_500',
|
| 92 |
-
block_info_text_color='*primary_50',
|
| 93 |
-
block_info_text_color_dark='*primary_50',
|
| 94 |
-
block_label_background_fill='*primary_950',
|
| 95 |
-
block_label_background_fill_dark='*secondary_950',
|
| 96 |
-
block_label_border_color='*secondary_500',
|
| 97 |
-
block_label_border_color_dark='*secondary_500',
|
| 98 |
-
block_label_text_color='*secondary_500',
|
| 99 |
-
block_label_text_color_dark='*secondary_500',
|
| 100 |
-
block_title_background_fill='*primary_950',
|
| 101 |
-
panel_background_fill='*primary_950',
|
| 102 |
-
panel_border_color='*primary_950',
|
| 103 |
-
checkbox_background_color='*primary_950',
|
| 104 |
-
checkbox_background_color_dark='*primary_950',
|
| 105 |
-
checkbox_background_color_focus='*primary_950',
|
| 106 |
-
checkbox_border_color='*secondary_500',
|
| 107 |
-
input_background_fill='*primary_800',
|
| 108 |
-
input_background_fill_focus='*primary_950',
|
| 109 |
-
input_background_fill_hover='*secondary_950',
|
| 110 |
-
input_placeholder_color='*secondary_950',
|
| 111 |
-
slider_color='*primary_950',
|
| 112 |
-
slider_color_dark='*primary_950',
|
| 113 |
-
table_even_background_fill='*primary_800',
|
| 114 |
-
table_odd_background_fill='*primary_600',
|
| 115 |
-
button_primary_background_fill='*primary_800',
|
| 116 |
-
button_primary_background_fill_dark='*primary_800'
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
random_ch = ['_', '/', '|', '*', '.', '#', '¤', '£', '?', '!', '%', '~', '^', '-', '=', '@', 'o', ']', '[', '§', 'o']
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
def button_change(inputs):
|
| 123 |
-
if "Reconstruction Attack" in inputs and "Zero-shot Face Swap" in inputs:
|
| 124 |
-
s_s = ""
|
| 125 |
-
for s in "Face Swap and Reconstruct":
|
| 126 |
-
ch_0 = random_ch[np.random.randint(len(random_ch))]
|
| 127 |
-
ch_1 = random_ch[np.random.randint(len(random_ch))]
|
| 128 |
-
s_s += s
|
| 129 |
-
time.sleep(0.01)
|
| 130 |
-
yield gr.Button(ch_0 + s_s + ch_1), gr.CheckboxGroup(value=inputs, interactive=False)
|
| 131 |
-
time.sleep(0.01)
|
| 132 |
-
yield gr.Button(s_s), gr.CheckboxGroup(value=inputs, interactive=True)
|
| 133 |
-
elif "Reconstruction Attack" in inputs and "Zero-shot Face Swap" not in inputs:
|
| 134 |
-
s_s = ""
|
| 135 |
-
for s in "Anonymize and Reconstruct":
|
| 136 |
-
ch_0 = random_ch[np.random.randint(len(random_ch))]
|
| 137 |
-
ch_1 = random_ch[np.random.randint(len(random_ch))]
|
| 138 |
-
s_s += s
|
| 139 |
-
time.sleep(0.01)
|
| 140 |
-
yield gr.Button(ch_0 + s_s + ch_1), gr.CheckboxGroup(value=inputs, interactive=False)
|
| 141 |
-
time.sleep(0.01)
|
| 142 |
-
yield gr.Button(s_s), gr.CheckboxGroup(value=inputs, interactive=True)
|
| 143 |
-
elif "Zero-shot Face Swap" in inputs:
|
| 144 |
-
s_s = ""
|
| 145 |
-
for s in "Face Swap":
|
| 146 |
-
ch_0 = random_ch[np.random.randint(len(random_ch))]
|
| 147 |
-
ch_1 = random_ch[np.random.randint(len(random_ch))]
|
| 148 |
-
s_s += s
|
| 149 |
-
time.sleep(0.03)
|
| 150 |
-
yield gr.Button(ch_0 + s_s + ch_1), gr.CheckboxGroup(value=inputs, interactive=False)
|
| 151 |
-
time.sleep(0.03)
|
| 152 |
-
yield gr.Button(s_s), gr.CheckboxGroup(value=inputs, interactive=True)
|
| 153 |
-
else:
|
| 154 |
-
s_s = ""
|
| 155 |
-
for s in "Anonymize":
|
| 156 |
-
ch_0 = random_ch[np.random.randint(len(random_ch))]
|
| 157 |
-
ch_1 = random_ch[np.random.randint(len(random_ch))]
|
| 158 |
-
s_s += s
|
| 159 |
-
time.sleep(0.03)
|
| 160 |
-
yield gr.Button(ch_0 + s_s + ch_1), gr.CheckboxGroup(value=inputs, interactive=False)
|
| 161 |
-
time.sleep(0.03)
|
| 162 |
-
yield gr.Button(s_s), gr.CheckboxGroup(value=inputs, interactive=True)
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
with gr.Blocks(theme=theme) as blk_demo:
|
| 166 |
-
gr.Markdown(value="# Face Anonymizer")
|
| 167 |
-
with gr.Row():
|
| 168 |
-
with gr.Column():
|
| 169 |
-
with gr.Box():
|
| 170 |
-
trg_in = gr.Image(type="pil", label='Target', height=300)
|
| 171 |
-
src_in = gr.Image(type="pil", label='Source', height=300)
|
| 172 |
-
with gr.Row():
|
| 173 |
-
b1 = gr.Button("Anonymize")
|
| 174 |
-
with gr.Row():
|
| 175 |
-
with gr.Accordion("Options", open=False):
|
| 176 |
-
chk_in = gr.CheckboxGroup(["Zero-shot Face Swap",
|
| 177 |
-
"Reconstruction Attack",
|
| 178 |
-
"Adversarial Defense",
|
| 179 |
-
"Use Anchor",
|
| 180 |
-
"3DMM"],
|
| 181 |
-
value=["Use Anchor"],
|
| 182 |
-
label="Mode",
|
| 183 |
-
info="Perform zero-shot face swap? "
|
| 184 |
-
"Apply reconstruction attack? "
|
| 185 |
-
"Apply defense against reconstruction attack? "
|
| 186 |
-
"Use anchor identities for fake sampling?")
|
| 187 |
-
def_in = gr.Slider(0.0, 0.2, value=0.1,
|
| 188 |
-
label='Defense Strength',
|
| 189 |
-
info="Strength of the defense noise against reconstruction attacks.")
|
| 190 |
-
mrg_in = gr.Slider(0.0, 1.0, value=0.3,
|
| 191 |
-
label='Margin',
|
| 192 |
-
info="Margin for sampling fake identities.")
|
| 193 |
-
slp_in = gr.Slider(0.0, 1.0, value=0.5,
|
| 194 |
-
label='SLERP Factor',
|
| 195 |
-
info="SLERP disance when establishing anchor identities.")
|
| 196 |
-
idi_in = gr.Slider(0.0, 1.0, value=0.0,
|
| 197 |
-
label='ID Interpolation',
|
| 198 |
-
info="Interpolation between target and source id vectors.")
|
| 199 |
-
det_in = gr.Slider(0.1, 2.0, value=1.0,
|
| 200 |
-
label='Detection Scale',
|
| 201 |
-
info="Resizes the images before passing to the detector.")
|
| 202 |
-
|
| 203 |
-
gr.Examples(examples=[['anonymizer/assets/0.jpg'], ['anonymizer/assets/1.jpg'], ['anonymizer/assets/2.jpg'], ['anonymizer/assets/3.jpg'], ['anonymizer/assets/4.jpg']],
|
| 204 |
-
inputs=trg_in)
|
| 205 |
-
with gr.Column():
|
| 206 |
-
with gr.Box():
|
| 207 |
-
ano_out = gr.Image(type="pil", label='Output', height=300, min_width=0)
|
| 208 |
-
msk_out = gr.Image(type="pil", label='Mask', height=300, min_width=0)
|
| 209 |
-
with gr.Row():
|
| 210 |
-
with gr.Accordion("Cosine Distances", open=False):
|
| 211 |
-
plt_out_0 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 212 |
-
info="Anonymized and target")
|
| 213 |
-
plt_out_1 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 214 |
-
info="Anonymized and source")
|
| 215 |
-
plt_out_2 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 216 |
-
info="Anonymized and -target")
|
| 217 |
-
plt_out_3 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 218 |
-
info="Target and source")
|
| 219 |
-
plt_out_4 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 220 |
-
info="Anonymized and reconstructed")
|
| 221 |
-
plt_out_5 = gr.Slider(0.0, 2.0, value=0.0, label='',
|
| 222 |
-
info="Target and reconstructed")
|
| 223 |
-
cos_info_box = gr.Markdown("Cosine distance between embeddings of target, anonymized, "
|
| 224 |
-
"reconstruction and source. Note that source is the fake identity "
|
| 225 |
-
"when using the default anonymization (which will be similar or the same "
|
| 226 |
-
"depending on if anchor identities are used). Threshold for False Acceptance "
|
| 227 |
-
"Rate (FAR) of 0.001 is 0.63. If you anonymize using no anchors, you can expect facial "
|
| 228 |
-
"recognition will find the true identity by searching for -target.")
|
| 229 |
-
with gr.Row():
|
| 230 |
-
with gr.Accordion("Information", open=False):
|
| 231 |
-
with gr.Box():
|
| 232 |
-
info_box = gr.Markdown(description + article)
|
| 233 |
-
|
| 234 |
-
b1.click(inference, inputs=[trg_in, src_in, chk_in, def_in, mrg_in, slp_in, idi_in, det_in],
|
| 235 |
-
outputs=[ano_out, msk_out, plt_out_0, plt_out_1, plt_out_2, plt_out_3, plt_out_4, plt_out_5])
|
| 236 |
-
chk_in.change(button_change, inputs=[chk_in], outputs=[b1, chk_in])
|
| 237 |
-
|
| 238 |
|
|
|
|
| 239 |
blk_demo.launch()
|
|
|
|
| 12 |
|
| 13 |
anonymizer_repo = Repository(local_dir="anonymizer", clone_from="felixrosberg/anonymizer", use_auth_token=token)
|
| 14 |
|
| 15 |
+
from anonymizer.face_anonymizer import fetch_demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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