forked from Snap-Engineering-Academy-2021/ml5-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_crown.js
111 lines (93 loc) · 2.73 KB
/
script_crown.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
let video;
let latestPrediction = null;
let modelIsLoading = true;
let crownImage;
let mySound;
// const FOREHEAD_POINT = 151;
const FOREHEAD_POINT = 11;
const LEFT_FORHEAD = 104;
const RIGHT_FORHEAD = 333;
// p5 function
function preload() {
crownImage = loadImage("assets/falcon-helmet1.png");
soundFormats('mp3', 'ogg');
mySound = loadSound("assets/falcon-taunt.mp3");
}
// p5 function
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
// ml5 function
let facemesh = ml5.facemesh(video, () => {
console.log("Model is ready!");
modelIsLoading = false;
});
// ml5 function
facemesh.on("predict", (results) => {
// results is an Array
// we care about the first object only
// results[0]
// console.log(results[0]);
latestPrediction = results[0];
});
video.hide();
}
// // p5 function
// function draw() {
// // if (modelIsLoading)
// // show a loading screen
// // draw webcam video
// image(video, 0, 0, width, height);
// //-----------------------------------
// if (latestPrediction == null) return; // don't draw anything else
// // get forhead location
// let foreheadLocation = latestPrediction.scaledMesh[FOREHEAD_POINT];
// console.log(foreheadLocation);
// image(
// crownImage,
// foreheadLocation[0 /* x */] - 50,
// foreheadLocation[1 /* y */] - 50,
// 100,
// 100
// );
// let leftForheadLocation = latestPrediction.scaledMesh[LEFT_FORHEAD];;
// let rightForheadLocation = latestPrediction.scaledMesh[RIGHT_FORHEAD];
// line(
// leftForheadLocation[0],
// leftForheadLocation[1],
// rightForheadLocation[0],
// rightForheadLocation[1]
// );
// }
// p5 function
function draw() {
// if (modelIsLoading)
// show a loading screen
// draw webcam video
imageMode(CORNER);
image(video, 0, 0, width, height);
//-----------------------------------
if (latestPrediction == null) return; // don't draw anything else
// get forhead locations
let foreheadLocation = latestPrediction.scaledMesh[FOREHEAD_POINT];
let leftForeheadLocation = latestPrediction.scaledMesh[LEFT_FORHEAD];
let rightForeheadLocation = latestPrediction.scaledMesh[RIGHT_FORHEAD];
let foreheadWidth = dist(
leftForeheadLocation[0 /* x */],
leftForeheadLocation[1 /* y */],
rightForeheadLocation[0 /* x */],
rightForeheadLocation[1 /* y */]
);
// console.log(foreheadWidth);
let crownWidth = foreheadWidth * 2.5;
let crownHeight = (crownImage.height / crownImage.width) * crownWidth;
imageMode(CENTER);
image(
crownImage,
foreheadLocation[0 /* x */],
foreheadLocation[1 /* y */] - crownHeight / 2,
crownWidth /* width */,
crownHeight /* height */
);
}