-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceprivacy.js
265 lines (234 loc) · 8.51 KB
/
faceprivacy.js
1
2
3
4
5
6
7
8
9
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// TODO: delete comments and unimportant logs on product version, --minify-- the plugin on product version
var perturbImageFile = null;
function isImageFile(file) {
var ext = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase();
return ext == "png" || ext == "jpeg" || ext == "jpg";
}
async function getModifiedFileList(node) {
var list = new DataTransfer();
for (var file of node.files) {
if (!isImageFile(file)) {
list.items.add(file);
continue;
}
// saveAs(file);
// exit
var perturbedImg = null;
try {
perturbedImg = await perturbImageFile(file);
} catch (e) {
console.error(e);
perturbedImg = file;
}
list.items.add(perturbedImg);
}
// console.log("Modified list is complete!");
return list.files;
}
function clip(v) {
return v > 255 ? 255 : v < 0 ? 0 : v;
}
// function applyPerturbation(image, perturbation, box) {
// var box_x = Math.floor(box.x);
// var box_y = Math.floor(box.y);
// var box_width = Math.ceil(box.width);
// var box_height = Math.ceil(box.height);
// return new Promise((resolve, reject) => {
// image.scan(box_x, box_y, box_width, box_height, function (x, y, idx) {
// var r = idx;
// var g = idx + 1;
// var b = idx + 2;
// var a = idx + 3;
//
// var rel_x = x - box_x;
// var rel_y = y - box_y;
//
// // rgba values run from 0 - 255
// this.bitmap.data[r] = clip(this.bitmap.data[r] + perturbation.getDelta(rel_y, rel_x, 0));
// this.bitmap.data[g] = clip(this.bitmap.data[g] + perturbation.getDelta(rel_y, rel_x, 1));
// this.bitmap.data[b] = clip(this.bitmap.data[b] + perturbation.getDelta(rel_y, rel_x, 2));
//
// if (rel_x == box_width - 1 && rel_y == box_height - 1) {
// // image scan finished
// resolve();
// }
// });
// });
// }
//
// async function detectFaces(dataUrl) {
// var img_obj = new Image();
// img_obj.crossOrigin = "anonymous";
// img_obj.src = dataUrl;
// return await faceapi.detectAllFaces(img_obj, new faceapi.SsdMobilenetv1Options());
// }
// async function noPerturbation(file) {
// var dataUrl = await fileToDataUrl(file);
//
// var image = await Jimp.read(dataUrl);
// var mime = image.getMIME();
//
// var dataArray = await image.getBufferAsync(mime);
// return new File([dataArray], file.name, {
// type: mime,
// });
// }
async function runReportGenerator(file) {
var dataUrl = await fileToDataUrl(file);
var image = await Jimp.read(dataUrl);
// let bitmap3ChannelArray = undefined;
// if (fixOrientation) {
// let fixedOrientationImageObject = await getCorrectedImageOrientation(file,
// await getImageOrientation(file));
// fixedOrientationImageObject.crossOrigin = "anonymous";
//
// bitmap3ChannelArray = await attackAutoTargeting(fixedOrientationImageObject);
// image.bitmap.width = fixedOrientationImageObject.width;
// image.bitmap.height = fixedOrientationImageObject.height;
// }
// else {
// let dataClampedArray = new Uint8ClampedArray(image.bitmap.data);
// let imageData = new ImageData(dataClampedArray, width, height);
// let imageObject = imageDataToImageObject(imageData); // it's working on chrome but not firefox
let name = file.name.split('.').slice(0, -1).join('.');
await performTest(image, file, name);
return file;
}
async function impersonatorPerturbation(file) {
var dataUrl = await fileToDataUrl(file);
var image = await Jimp.read(dataUrl);
let imageObject = await imageObjectExtractorOrienFixing(file);
image.bitmap.width = imageObject.width;
image.bitmap.height = imageObject.height; // fixing orientation may affect on dimensions
let eps = 14; // TODO: should be taken from options
let targetOption;
if(!autoTargeting && (!targetSource || !targetName)){
autoTargeting = true;
}
if(autoTargeting) {
targetOption = targetMode;
}
else {
targetOption = [targetSource, targetName];
}
let bitmap3ChannelArray = await attackOnTarget(imageObject, eps, autoTargeting, targetOption);
for (let i = 0; i < bitmap3ChannelArray.length / 3; i++) { // i: index of each pixel
image.bitmap.data[4 * i] = bitmap3ChannelArray[3 * i];
image.bitmap.data[4 * i + 1] = bitmap3ChannelArray[3 * i + 1];
image.bitmap.data[4 * i + 2] = bitmap3ChannelArray[3 * i + 2];
// and we don't edit the alpha (image.bitmap.data[4*i + 3])
}
var mime = image.getMIME();
let dataArray = await image.getBufferAsync(mime);
return new File([dataArray], file.name, {type: mime,});
}
// async function dummyPerturbation(file) {
// var dataUrl = await fileToDataUrl(file);
// var image = await Jimp.read(dataUrl);
//
// for (let i = 0; i < image.bitmap.data.length; i++) {
// image.bitmap.data[i] = Math.abs(Math.random() * 40)
// }
//
// var mime = image.getMIME();
// let dataArray = await image.getBufferAsync(mime);
// return new File([dataArray], file.name, {type: mime,});
// }
// async function fixedNoisePerturbation(file) {
// var dataUrl = await fileToDataUrl(file);
//
// var faces = await detectFaces(dataUrl);
// var image = await Jimp.read(dataUrl);
// var mime = image.getMIME();
//
// // fetch perturbations for each face box
// var perturbations = faces.map((face) => {
// return new Promise(async (resolve, reject) => {
// var p = await fetchPerturbation(Math.ceil(face.box.height), Math.ceil(face.box.width));
// resolve([face.box, p]);
// });
// });
//
// // await all perturbations, then apply sequentially
// await Promise.all(perturbations).then(async (completed) => {
// var seq = Promise.resolve();
// for (const [box, p] of completed) {
// seq = seq.then(async () => {
// await applyPerturbation(image, p, box)
// });
// }
// await seq;
// });
//
// var dataArray = await image.getBufferAsync(mime);
// return new File([dataArray], file.name, {
// type: mime,
// });
// }
// function onError(error) {
// console.log(`Error: ${error}`);
// }
function setPerturbationAlgorithm() {
// perturbImageFile = impersonatorPerturbation;
perturbImageFile = runReportGenerator; // to generate the latex report
//
// let algo = "impersonator";
// if (item && typeof item === 'string' && item.length > 0) {
// algo = item;
// }
// switch (algo) {
// case "none":
// perturbImageFile = noPerturbation;
// break;
// case "impersonator":
// perturbImageFile = impersonatorPerturbation;
// break;
// }
console.log("Face privacy plugin loaded");
}
setPerturbationAlgorithm();
getStorageValue('custom_victims', victimsDict => {
setStorageValue('custom_victims', victimsDict || {});
});
let autoTargeting, targetMode, targetSource, targetName;
getStorageValue('auto_targeting', _autoTargeting => { // TODO: should program wait for the storage stuffs?
if(typeof(_autoTargeting) === "undefined"){
autoTargeting = true;
}
else {
autoTargeting = _autoTargeting;
}
setStorageValue('auto_targeting', autoTargeting);
});
getStorageValue('target_mode', _targetMode => {
setStorageValue('target_mode', _targetMode || "both");
targetMode = _targetMode || "both";
});
getStorageValue('target_source', _targetSource => {
targetSource = _targetSource;
});
getStorageValue('target_name', _targetName => {
targetName = _targetName;
});
$.initialize('._3jk', async function () {
var isDone = false;
this.addEventListener("change", async function (event) {
if (isDone === true) {
isDone = false;
return;
}
event.stopImmediatePropagation();
await detectorInitializer();
for (let i in this.childNodes) {
let node = this.childNodes[i];
if (node.nodeType === Node.ELEMENT_NODE) {
event.target.files = await getModifiedFileList(node);
// Retrigger event
isDone = true;
node.dispatchEvent(event);
return;
}
}
}, true);
}
);