Bypass the read/write for disk
Browse files
app.py
CHANGED
@@ -1,15 +1,72 @@
|
|
1 |
from fawkes.protection import Fawkes
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
# import os
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
fwks = Fawkes("extractor_2", '0', 1, mode=level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
fwks.run_protection([img], format='jpeg')
|
9 |
splt = img.split(".")
|
10 |
# print(os.listdir('/tmp'))
|
11 |
return splt[0] + "_cloaked.jpeg"
|
12 |
|
13 |
-
gr.Interface(fn=predict, inputs=[gr.components.Image(type='
|
14 |
-
gr.components.Radio(["low", "mid", "high"], label="Protection Level")],
|
15 |
outputs=gr.components.Image(type="pil"), allow_flagging="never").launch(show_error=True, quiet=False)
|
|
|
1 |
from fawkes.protection import Fawkes
|
2 |
+
from fawkes.utils import Faces, reverse_process_cloaked
|
3 |
+
from fawkes.differentiator import FawkesMaskGeneration
|
4 |
+
import numpy as np
|
5 |
import gradio as gr
|
6 |
# import os
|
7 |
|
8 |
+
IMG_SIZE = 112
|
9 |
+
PREPROCESS = 'raw'
|
10 |
+
|
11 |
+
def generate_cloak_images(protector, image_X, target_emb=None):
|
12 |
+
cloaked_image_X = protector.compute(image_X, target_emb)
|
13 |
+
return cloaked_image_X
|
14 |
+
|
15 |
+
def predict(img, level, th=0.04, sd=1e7, lr=10, max_step=500, batch_size=1, format='png',
|
16 |
+
separate_target=True, debug=False, no_align=False, exp="", maximize=True,
|
17 |
+
save_last_on_failed=True):
|
18 |
+
|
19 |
+
print(img.ndim)
|
20 |
fwks = Fawkes("extractor_2", '0', 1, mode=level)
|
21 |
+
|
22 |
+
current_param = "-".join([str(x) for x in [fwks.th, sd, fwks.lr, fwks.max_step, batch_size, format,
|
23 |
+
separate_target, debug]])
|
24 |
+
faces = Faces(['./Current Face'], [img], fwks.aligner, verbose=1, no_align=False)
|
25 |
+
original_images = faces.cropped_faces
|
26 |
+
|
27 |
+
if len(original_images) == 0:
|
28 |
+
print("No face detected. ")
|
29 |
+
return 2
|
30 |
+
original_images = np.array(original_images)
|
31 |
+
|
32 |
+
if current_param != fwks.protector_param:
|
33 |
+
fwks.protector_param = current_param
|
34 |
+
if fwks.protector is not None:
|
35 |
+
del fwks.protector
|
36 |
+
if batch_size == -1:
|
37 |
+
batch_size = len(original_images)
|
38 |
+
fwks.protector = FawkesMaskGeneration(fwks.feature_extractors_ls,
|
39 |
+
batch_size=batch_size,
|
40 |
+
mimic_img=True,
|
41 |
+
intensity_range=PREPROCESS,
|
42 |
+
initial_const=sd,
|
43 |
+
learning_rate=fwks.lr,
|
44 |
+
max_iterations=fwks.max_step,
|
45 |
+
l_threshold=fwks.th,
|
46 |
+
verbose=debug,
|
47 |
+
maximize=maximize,
|
48 |
+
keep_final=False,
|
49 |
+
image_shape=(IMG_SIZE, IMG_SIZE, 3),
|
50 |
+
loss_method='features',
|
51 |
+
tanh_process=True,
|
52 |
+
save_last_on_failed=save_last_on_failed,
|
53 |
+
)
|
54 |
+
protected_images = generate_cloak_images(fwks.protector, original_images)
|
55 |
+
faces.cloaked_cropped_faces = protected_images
|
56 |
+
|
57 |
+
final_images, images_without_face = faces.merge_faces(
|
58 |
+
reverse_process_cloaked(protected_images, preprocess=PREPROCESS),
|
59 |
+
reverse_process_cloaked(original_images, preprocess=PREPROCESS))
|
60 |
+
|
61 |
+
return final_images[-1]
|
62 |
+
print("Done!")
|
63 |
+
|
64 |
+
|
65 |
fwks.run_protection([img], format='jpeg')
|
66 |
splt = img.split(".")
|
67 |
# print(os.listdir('/tmp'))
|
68 |
return splt[0] + "_cloaked.jpeg"
|
69 |
|
70 |
+
gr.Interface(fn=predict, inputs=[gr.components.Image(type='numpy'),
|
71 |
+
gr.components.Radio(["low", "mid", "high"], label="``Protection Level")],
|
72 |
outputs=gr.components.Image(type="pil"), allow_flagging="never").launch(show_error=True, quiet=False)
|