SHREYSH commited on
Commit
e4f543b
·
verified ·
1 Parent(s): 09b5be9

Upload fawkes_wrapper_gradio_v_1_0.py

Browse files
Files changed (1) hide show
  1. fawkes_wrapper_gradio_v_1_0.py +84 -0
fawkes_wrapper_gradio_v_1_0.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """fawkes_wrapper_gradio_V_1.0
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1q9cDeedmQrsMwYqbulhSWs8Wk5ligIs-
8
+ """
9
+
10
+ # fawkes_protection.py
11
+
12
+ from fawkes.protection import Fawkes
13
+ from fawkes.utils import Faces, reverse_process_cloaked
14
+ from fawkes.differentiator import FawkesMaskGeneration
15
+ import tensorflow as tf
16
+ import numpy as np
17
+
18
+ IMG_SIZE = 112
19
+ PREPROCESS = 'raw'
20
+
21
+ class FawkesProtection:
22
+ def __init__(self):
23
+ # Initialize Fawkes instances for different protection levels
24
+ self.fwks_l = Fawkes("extractor_2", '0', 1, mode='low')
25
+ self.fwks_m = Fawkes("extractor_2", '0', 1, mode='mid')
26
+ self.fwks_h = Fawkes("extractor_2", '0', 1, mode='high')
27
+
28
+ def generate_cloak_images(self, protector, image_X, target_emb=None):
29
+ cloaked_image_X = protector.compute(image_X, target_emb)
30
+ return cloaked_image_X
31
+
32
+ def predict(self, img, level, th=0.04, sd=1e7, lr=10, max_step=500, batch_size=1, format='png',
33
+ separate_target=True, debug=False, no_align=False, exp="", maximize=True,
34
+ save_last_on_failed=True):
35
+
36
+ img = img.convert('RGB')
37
+ img = tf.keras.utils.img_to_array(img)
38
+
39
+ if level == 'low':
40
+ fwks = self.fwks_l
41
+ elif level == 'mid':
42
+ fwks = self.fwks_m
43
+ elif level == 'high':
44
+ fwks = self.fwks_h
45
+
46
+ current_param = "-".join([str(x) for x in [fwks.th, sd, fwks.lr, fwks.max_step, batch_size, format,
47
+ separate_target, debug]])
48
+ faces = Faces(['./Current Face'], [img], fwks.aligner, verbose=0, no_align=False)
49
+ original_images = faces.cropped_faces
50
+
51
+ if len(original_images) == 0:
52
+ raise Exception("No face detected. ")
53
+ original_images = np.array(original_images)
54
+
55
+ if current_param != fwks.protector_param:
56
+ fwks.protector_param = current_param
57
+ if fwks.protector is not None:
58
+ del fwks.protector
59
+ if batch_size == -1:
60
+ batch_size = len(original_images)
61
+ fwks.protector = FawkesMaskGeneration(fwks.feature_extractors_ls,
62
+ batch_size=batch_size,
63
+ mimic_img=True,
64
+ intensity_range=PREPROCESS,
65
+ initial_const=sd,
66
+ learning_rate=fwks.lr,
67
+ max_iterations=fwks.max_step,
68
+ l_threshold=fwks.th,
69
+ verbose=0,
70
+ maximize=maximize,
71
+ keep_final=False,
72
+ image_shape=(IMG_SIZE, IMG_SIZE, 3),
73
+ loss_method='features',
74
+ tanh_process=True,
75
+ save_last_on_failed=save_last_on_failed,
76
+ )
77
+ protected_images = self.generate_cloak_images(fwks.protector, original_images)
78
+ faces.cloaked_cropped_faces = protected_images
79
+
80
+ final_images, _ = faces.merge_faces(
81
+ reverse_process_cloaked(protected_images, preprocess=PREPROCESS),
82
+ reverse_process_cloaked(original_images, preprocess=PREPROCESS))
83
+
84
+ return final_images[-1].astype(np.uint8)