-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmask.html
More file actions
209 lines (170 loc) · 6.42 KB
/
Copy pathmask.html
File metadata and controls
209 lines (170 loc) · 6.42 KB
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
<!DOCTYPE html>
<html>
<head>
<title>test page</title>
<style>
#imgPreview {
max-width: 800px;
max-height: 1800px;
margin: 10px auto 0px auto;
display: flex;
}
#img3 {
max-width: 800px;
margin: 0 auto;
display: block;
position: relative;
}
#test-canvas {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
max-width: 800px;
pointer-events: none;
z-index: 999;
}
#prompt3 {
width: 100px;
height: 30px;
text-align: center;
margin: 10px auto 0px auto;
position: relative;
}
#imgSpan {
pointer-events: none;
}
input {
position: absolute;
left: 0;
top: 0;
}
.filepath {
width: 100%;
height: 100%;
opacity: 0;
}
</style>
</head>
<body>
<div id="imgPreview" style="position:relative">
<img src="./image/test.jpg" id="img3" />
<canvas id="test-canvas">
</div>
<div id="prompt3">
<span id="imgSpan">
点击上传
</span>
<input type="file" id="file" class="filepath" onchange="changepic(this)"
accept="image/jpg,image/jpeg,image/png,image/PNG">
</div>
</body>
<script type="text/javascript" src="//pic.youzu.com/common/jquery-1.8.3.min.js"></script>
<script src="./js/face.js"></script>
<script>
const prev = async () => {
const MODEL_URL = './weights'
await faceapi.loadSsdMobilenetv1Model(MODEL_URL)
await faceapi.loadFaceLandmarkModel(MODEL_URL)
await faceapi.loadFaceRecognitionModel(MODEL_URL)
await faceapi.loadFaceExpressionModel(MODEL_URL)
await faceapi.loadAgeGenderModel(MODEL_URL)
const input = document.getElementById('img3')
const detectionsWithAgeAndGender = await faceapi.detectAllFaces(input).withFaceLandmarks().withAgeAndGender().withFaceExpressions()
var canvas = document.getElementById('test-canvas');
const displaySize = { width: input.width, height: input.height }
faceapi.matchDimensions(canvas, displaySize)
const resizedDetections = faceapi.resizeResults(detectionsWithAgeAndGender, displaySize)
console.log(resizedDetections)
// faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
const minProbability = 0.05
faceapi.draw.drawFaceExpressions(canvas, resizedDetections, minProbability)
const context = canvas.getContext('2d');
//替換面部器官
for (const face of resizedDetections) {
const features = {
jaw: face.landmarks.positions.slice(0, 17),
eyebrowLeft: face.landmarks.positions.slice(17, 22),
eyebrowRight: face.landmarks.positions.slice(22, 27),
noseBridge: face.landmarks.positions.slice(27, 31),
nose: face.landmarks.positions.slice(31, 36),
eyeLeft: face.landmarks.positions.slice(36, 42),
eyeRight: face.landmarks.positions.slice(42, 48),
lipOuter: face.landmarks.positions.slice(48, 60),
lipInner: face.landmarks.positions.slice(60),
};
// for (const eye of [features.eyeLeft, features.eyeRight]) {
// const eyeBox = getBoxFromPoints(eye);
// const fontSize = 6 * eyeBox.height;
// context.font = `${fontSize}px/${fontSize}px serif`;
// context.textAlign = 'center';
// context.textBaseline = 'bottom';
// context.fillStyle = '#000';
// context.fillText('👄', eyeBox.center.x, eyeBox.center.y + 0.6 * fontSize);
// }
}
resizedDetections.forEach(element => {
let box = element.detection.box
// let age = parseInt(element.age)
// let gender = element.gender
// const drawOptions = {
// label: age + "_" + gender,
// lineWidth: 2
// }
// const drawBox = new faceapi.draw.DrawBox(box, drawOptions)
// drawBox.draw(canvas)
//替換mask
var image = new Image();
image.src = "./image/overlay-skull_06.png";
let eyebrowLeft = element.landmarks.positions.slice(17, 22);
let eyeBox = getBoxFromPoints(eyebrowLeft);
image.onload = function () {
context.drawImage(image, 0, 10, image.width, image.height, box._x, eyeBox.center.y - (box._height / 2.7), box._width, box._height);
}
});
}
prev()
//計算坐標點
function getBoxFromPoints(points) {
const box = {
bottom: -Infinity,
left: Infinity,
right: -Infinity,
top: Infinity,
get center() {
return {
x: this.left + this.width / 2,
y: this.top + this.height / 2,
};
},
get height() {
return this.bottom - this.top;
},
get width() {
return this.right - this.left;
},
};
for (const point of points) {
box.left = Math.min(box.left, point.x);
box.right = Math.max(box.right, point.x);
box.bottom = Math.max(box.bottom, point.y);
box.top = Math.min(box.top, point.y);
}
return box;
}
function changepic() {
// $("#prompt3").css("display", "none");
var reads = new FileReader();
f = document.getElementById('file').files[0];
reads.readAsDataURL(f);
reads.onload = function (e) {
document.getElementById('img3').src = this.result;
$("#img3").css("display", "block");
var canvas = document.getElementById('test-canvas');
const context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
prev()
};
}
</script>
</html>