-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
207 lines (178 loc) · 6.9 KB
/
Copy pathindex.html
File metadata and controls
207 lines (178 loc) · 6.9 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
<!DOCTYPE html>
<html>
<head>
<title>Face Filter</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/p5@1.0.0/lib/p5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clmtrackr@1.1.2/build/clmtrackr.min.js"></script>
<script>
let outputWidth;
let outputHeight;
let faceTracker; // Face Tracking
let videoInput;
let imgSpidermanMask; // Spiderman Mask Filter
let imgIronManColor;
let imgIronManBlack;
let imgTonyStark;
let imgDoctorStrangeRing;
let imgDogEarRight, imgDogEarLeft, imgDogNose; // Dog Face Filter
let selected = -1; // Default no filter
/*
* **p5.js** library automatically executes the `preload()` function. Basically, it is used to load external files. In our case, we'll use it to load the images for our filters and assign them to separate variables for later use.
*/
function preload()
{
// Spiderman Mask Filter asset
imgSpidermanMask = loadImage("https://i.ibb.co/9HB2sSv/spiderman-mask-1.png");
// Dog Face Filter assets
imgDogEarRight = loadImage("https://i.ibb.co/bFJf33z/dog-ear-right.png");
imgDogEarLeft = loadImage("https://i.ibb.co/dggwZ1q/dog-ear-left.png");
imgDogNose = loadImage("https://i.ibb.co/PWYGkw1/dog-nose.png");
imgIronManColor = loadImage("https://i.ibb.co/94nmYcH/iron-man-color.png");
imgIronManBlack = loadImage("https://i.ibb.co/C9d7s03/iron-man-black.png");
imgTonyStark = loadImage("https://i.ibb.co/BLzQ6Ty/tony-stark.png");
imgDoctorStrangeRing = loadImage("https://i.ibb.co/23YKPZ9/doctor-strange-ring.png");
}
/**
* In p5.js, `setup()` function is executed at the beginning of our program, but after the `preload()` function.
*/
function setup()
{
const maxWidth = Math.min(windowWidth, windowHeight);
pixelDensity(1);
outputWidth = maxWidth;
outputHeight = maxWidth * 0.75; // 4:3
createCanvas(outputWidth, outputHeight);
// webcam capture
videoInput = createCapture(VIDEO);
videoInput.size(outputWidth, outputHeight);
videoInput.hide();
// select filter
const sel = createSelect();
const selectList = ['Spiderman Mask', 'Dog Filter', 'Iron Man Red' , 'Iron Man Black' ,'Tony Stark' , 'Doctor Strange Ring']; // list of filters
sel.option('Select Filter', -1); // Default no filter
for (let i = 0; i < selectList.length; i++)
{
sel.option(selectList[i], i);
}
sel.changed(applyFilter);
// tracker
faceTracker = new clm.tracker();
faceTracker.init();
faceTracker.start(videoInput.elt);
}
// callback function
function applyFilter()
{
selected = this.selected(); // change filter type
}
/*
* In p5.js, draw() function is executed after setup(). This function runs inside a loop until the program is stopped.
*/
function draw()
{
image(videoInput, 0, 0, outputWidth, outputHeight); // render video from webcam
// apply filter based on choice
switch(selected)
{
case '-1': break;
case '0': drawSpidermanMask(); break;
case '1': drawDogFace(); break;
case '2': drawIronMan(true); break;
case '3': drawIronMan(false); break;
case '4': drawTonyStark(); break;
case '5': drawDoctorStrangeRing();break;
}
}
// Spiderman Mask Filter
function drawSpidermanMask()
{
const positions = faceTracker.getCurrentPosition();
if (positions !== false)
{
push();
const wx = Math.abs(positions[13][0] - positions[1][0]) * 1.2; // The width is given by the face width, based on the geometry
const wy = Math.abs(positions[7][1] - Math.min(positions[16][1], positions[20][1])) * 1.2; // The height is given by the distance from nose to chin, times 2
translate(-wx/2, -wy/2);
image(imgSpidermanMask, positions[62][0], positions[62][1], wx, wy); // Show the mask at the center of the face
pop();
}
}
// Spiderman Mask Filter
function drawIronMan(val)
{
const positions = faceTracker.getCurrentPosition();
if (positions !== false)
{
push();
const wx = Math.abs(positions[13][0] - positions[1][0]) * 1.2; // The width is given by the face width, based on the geometry
const wy = Math.abs(positions[7][1] - Math.min(positions[16][1], positions[20][1])) * 1.2; // The height is given by the distance from nose to chin, times 2
translate(-wx/2, -wy/2);
image((val)?imgIronManColor:imgIronManBlack, positions[62][0], positions[62][1], wx, wy); // Show the mask at the center of the face
pop();
}
}
// Spiderman Mask Filter
function drawTonyStark()
{
const positions = faceTracker.getCurrentPosition();
if (positions !== false)
{
push();
const wx = Math.abs(positions[13][0] - positions[1][0]) * 1.2; // The width is given by the face width, based on the geometry
const wy = Math.abs(positions[7][1] - Math.min(positions[16][1], positions[20][1])) * 1.2; // The height is given by the distance from nose to chin, times 2
translate(-wx/2, -wy/2);
image(imgTonyStark, positions[62][0], positions[62][1], wx, wy); // Show the mask at the center of the face
pop();
}
}
function drawDoctorStrangeRing(){
const positions = faceTracker.getCurrentPosition();
if (positions !== false)
{
push();
const wx = Math.abs(positions[13][0] - positions[1][0]) * 1.2; // The width is given by the face width, based on the geometry
const wy = Math.abs(positions[7][1] - Math.min(positions[16][1], positions[20][1])) * 1.2; // The height is given by the distance from nose to chin, times 2
translate(-wx/2, -wy/2);
image(imgDoctorStrangeRing, positions[62][0], positions[62][1], wx, wy); // Show the mask at the center of the face
pop();
}
}
// Dog Face Filter
function drawDogFace()
{
const positions = faceTracker.getCurrentPosition();
if (positions !== false)
{
if (positions.length >= 20) {
push();
translate(-100, -150); // offset adjustment
image(imgDogEarRight, positions[20][0], positions[20][1]);
pop();
}
if (positions.length >= 16) {
push();
translate(-20, -150); // offset adjustment
image(imgDogEarLeft, positions[16][0], positions[16][1]);
pop();
}
if (positions.length >= 62) {
push();
translate(-57, -20); // offset adjustment
image(imgDogNose, positions[62][0], positions[62][1]);
pop();
}
}
}
function windowResized()
{
const maxWidth = Math.min(windowWidth, windowHeight);
pixelDensity(1);
outputWidth = maxWidth;
outputHeight = maxWidth * 0.75; // 4:3
resizeCanvas(outputWidth, outputHeight);
}
</script>
</body>
</html>