-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
312 lines (309 loc) · 12.7 KB
/
main.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var imgUpload = new Vue({
el: ".editArea",
data: {
uploadedImage: null,
imageLoadedFlag: false,
faceDetectedFlag: false,
faces: undefined,
fileName: undefined,
drawedSize: undefined,
loadingFlag: true,
loadMessage: "顔検出モデルを読み込んでいます...",
pienFlag: false,
pienMode: "apple",
drawMode: undefined
},
methods: {
// ファイルアップロード時に実行されるメソッド
onFileChange: function(e) {
let files = e.target.files || e.dataTransfer.files;
this.fileName = files[0].name;
this.loadMessage = "画像を読み込み中です...";
this.loadingFlag = true;
this.createImage(files[0]);
},
// アップロードした画像を表示
createImage: function(file) {
let reader = new FileReader();
reader.onload = (e) => {
this.uploadedImage = e.target.result;
this.drawOnCanvas(this.uploadedImage);
// 不要になった要素を消す
this.imageLoadedFlag = true;
};
reader.readAsDataURL(file);
},
// canvas上に表示するための操作
drawOnCanvas: function(result) {
// drawImageにはimgタグを入れる必要がある
var img = new Image();
// canvas = document.getElementById("canvas");
canvas = this.$refs.canvas;
img.src = result;
img.onload = async (e) => {
// 画像全体をcanvas中央に配置
var ow = img.naturalWidth;
var oh = img.naturalHeight;
var nw, nh, dx, dy;
if (ow > oh) {
nw = canvas.clientWidth;
nh = oh * (nw / ow);
dx = 0;
dy = canvas.clientHeight / 2 - nh / 2;
} else {
nh = canvas.clientHeight;
nw = ow * (nh / oh);
dx = canvas.clientWidth / 2 - nw / 2;
dy = 0;
}
this.drawedSize = {
dx: dx,
dy: dy,
width: nw,
height: nh
}
ctx = this.setupCanvas(canvas);
ctx.drawImage(img, 0, 0, ow, oh, dx, dy, nw, nh);
await this.detectFace(canvas);
if (!this.faceDetectedFlag) {
UIkit.notification("顔の検出に失敗しました。別の画像をお試しください。", {
timeout: 2000
});
this.eraseCanvas([
this.$refs.canvas,
this.$refs.overlay
]);
this.imageLoadedFlag = false;
} else {
}
}
},
// 高解像度なcanvasのためのメソッド
setupCanvas: function (canvas) {
// Get the device pixel ratio, falling back to 1.
var dpr = window.devicePixelRatio || 1;
// Get the size of the canvas in CSS pixels.
var rect = canvas.getBoundingClientRect();
// Give the canvas pixel dimensions of their CSS
// size * the device pixel ratio.
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
var ctx = canvas.getContext('2d');
// Scale all drawing operations by the dpr, so you
// don't have to worry about the difference.
ctx.scale(dpr, dpr);
return ctx;
},
detectFace: async function (canvas) {
this.loadMessage = "顔検出を実行中です...";
this.loadingFlag = true;
const displaySize = {width: canvas.width, height: canvas.height};
const overlay = this.$refs.overlay;
faceapi.matchDimensions(overlay, displaySize);
const detections = await faceapi.detectAllFaces(canvas).withFaceLandmarks();
this.faceDetectedFlag = detections.length != 0;
const resizedDetections = faceapi.resizeResults(detections, displaySize);
this.faces = resizedDetections;
this.loadingFlag = false;
// console.log(detections);
// faceapi.draw.drawDetections(overlay, resizedDetections);
// faceapi.draw.drawFaceLandmarks(overlay, resizedDetections);
},
eraseCanvas: function (canvases) {
this.pienFlag = false;
if (!Array.isArray(canvases)) {
canvases = [canvases];
}
for (canvas of canvases) {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
},
drawMask: function() {
const faces = this.faces;
const overlay = this.$refs.overlay;
const ctx = overlay.getContext('2d');
// 画像読み込み
const img = new Image();
img.src = `./img/${this.pienMode}.png`;
img.onload = (e) => {
// 描画
for (const face of faces) {
const info = this.calcFaceInfo(face);
const angle = info.angle;
const centerX = info.center.x;
const centerY = info.center.y;
this.drawRotate(ctx, img, info.size, centerX, centerY, angle);
}
}
},
drawEyes: async function() {
const faces = this.faces;
const overlay = this.$refs.overlay;
const ctx = overlay.getContext('2d');
const [left, right] = await this.loadEyes();
for (const face of faces) {
const info = this.calcFaceInfo(face);
this.drawRotate(ctx, left, info.eyes.left.size, info.eyes.left.x, info.eyes.left.y, info.angle);
this.drawRotate(ctx, right, info.eyes.right.size, info.eyes.right.x, info.eyes.right.y, info.angle);
}
},
pienMask: function() {
this.eraseCanvas(this.$refs.overlay);
this.pienFlag = true;
this.drawMode = "mask";
this.pienFlag = true;
this.drawMask();
},
pienEyes: function() {
this.eraseCanvas(this.$refs.overlay);
this.pienFlag = true;
this.drawMode = "eyes";
this.pienFlag = true;
this.drawEyes();
},
changePien: function(mode) {
this.eraseCanvas(this.$refs.overlay);
this.pienFlag = true;
this.pienMode = mode;
if (this.drawMode == "mask") {
this.drawMask();
} else if (this.drawMode == "eyes") {
this.drawEyes();
}
},
calcFaceInfo: function(face) {
// 鼻のむき
// const startX = face.landmarks.positions[27].x;
// const startY = face.landmarks.positions[27].y;
// const endX = face.landmarks.positions[30].x;
// const endY = face.landmarks.positions[30].y;
// const vecX = endX - startX;
// const vecY = endY - startY;
// return Math.atan2(vecX, vecY);
const faceLeft = face.landmarks.positions[0];
const faceRight = face.landmarks.positions[16];
const faceBottom = face.landmarks.positions[8];
const leftEyeWidth = Math.sqrt(
Math.pow(face.landmarks.positions[36].x - face.landmarks.positions[39].x, 2)
+ Math.pow(face.landmarks.positions[36].y - face.landmarks.positions[39].y, 2)
) * 2;
const rightEyeWidth = Math.sqrt(
Math.pow(face.landmarks.positions[42].x - face.landmarks.positions[45].x, 2)
+ Math.pow(face.landmarks.positions[42].y - face.landmarks.positions[45].y, 2)
) * 2;
const leftEye = {
x: (face.landmarks.positions[36].x + face.landmarks.positions[39].x) / 2,
y: (face.landmarks.positions[36].y + face.landmarks.positions[39].y) / 2,
size: {
width: leftEyeWidth,
height: leftEyeWidth
}
};
const rightEye = {
x: (face.landmarks.positions[42].x + face.landmarks.positions[45].x) / 2,
y: (face.landmarks.positions[42].y + face.landmarks.positions[45].y) / 2,
size: {
width: rightEyeWidth,
height: rightEyeWidth
}
};
const center = {
x: (faceLeft.x + faceRight.x) / 2,
y: (faceLeft.y + faceRight.y) / 2
};
const faceHorizontalVec = {
x: faceLeft.x - faceRight.x,
y: faceLeft.y - faceRight.y
};
const faceVerticalVec = {
x: faceHorizontalVec.y,
y: - faceHorizontalVec.x
};
const size = {
width: Math.sqrt(Math.pow(faceHorizontalVec.x, 2) + Math.pow(faceHorizontalVec.y, 2)),
height: Math.sqrt(Math.pow(center.x - faceBottom.x, 2) + Math.pow(center.y - faceBottom.y, 2)) * 2
};
const angle = -Math.atan2(faceVerticalVec.x, faceVerticalVec.y);
return {
angle: angle,
center: center,
size: size,
eyes: {
left: leftEye,
right: rightEye
}
};
},
drawRotate: function(ctx, img, size, x, y, angle) {
// x,y: 描画する画像の中心
// size: 描画サイズ
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle);
ctx.drawImage(
img, 0, 0, img.naturalWidth, img.naturalHeight,
-(size.width / 2), -(size.height / 2),
size.width, size.height
);
ctx.restore();
},
changeFile: function() {
this.pienFlag = false;
const upload = document.getElementById("fileUpload");
upload.click();
},
concatCanvas: async function() {
const canvas = document.createElement("canvas");
var dpr = window.devicePixelRatio || 1;
canvas.width = this.drawedSize.width * dpr;
canvas.height = this.drawedSize.height * dpr;
const ctx = canvas.getContext("2d");
for (const cvs of [this.$refs.canvas, this.$refs.overlay]) {
const img = await this.getImageFromCanvas(cvs);
ctx.drawImage(img,
this.drawedSize.dx * dpr, this.drawedSize.dy * dpr,
this.drawedSize.width * dpr, this.drawedSize.height * dpr,
0, 0,
canvas.width, canvas.height
);
}
return canvas;
},
loadEyes: async function() {
return new Promise((resolve, reject) => {
const left = new Image();
left.onload = () => {
const right = new Image();
right.onload = () => resolve([left, right]);
right.onerror = () => reject(e);
right.src = `img/${this.pienMode}_right.png`;
}
left.onerror = (e) => reject(e);
left.src = `img/${this.pienMode}_left.png`;
});
},
getImageFromCanvas: function(canvas) {
return new Promise((resolve, reject) => {
const image = new Image();
const ctx = canvas.getContext("2d");
image.onload = () => resolve(image);
image.onerror = (e) => reject(e);
image.src = ctx.canvas.toDataURL();
});
},
downloadCanvas: async function() {
const canvas = await this.concatCanvas();
let link = document.createElement("a");
link.href = canvas.toDataURL("image/png");
link.download = `pienized_${this.fileName}.png`;
link.click();
}
},
mounted: async function() {
this.loadMessage = "顔検出モデルを読み込んでいます...";
await faceapi.nets.ssdMobilenetv1.loadFromUri('models/');
await faceapi.nets.faceLandmark68Net.loadFromUri('models/');
this.loadingFlag = false;
}
});