-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyPoseNet.js
98 lines (88 loc) · 2.2 KB
/
easyPoseNet.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
// how to use this
// run startPoseNet() in your setup
// define initial values with x and y off the screen
let initialValues = {
x: -10000,
y: -10000,
confidence: 0,
};
// define initial score (currently not added to person)
let score = 0;
// define initial keypoints
let keypoints = [
{
score: 0,
part: "",
position: {
x: -10000,
y: -10000,
},
},
];
// define the default person
let person = {
nose: initialValues,
leftEye: initialValues,
rightEye: initialValues,
leftEar: initialValues,
rightEar: initialValues,
leftShoulder: initialValues,
rightShoulder: initialValues,
leftElbow: initialValues,
rightElbow: initialValues,
leftWrist: initialValues,
rightWrist: initialValues,
leftHip: initialValues,
rightHip: initialValues,
leftKnee: initialValues,
rightKnee: initialValues,
leftAnkle: initialValues,
rightAnkle: initialValues,
};
function startPoseNet(videoOrImage) {
input = videoOrImage;
if (!input) {
poseNetVideo = createCapture(VIDEO);
poseNetVideo.size(width, height);
poseNetVideo.hide();
input = poseNetVideo;
imageInput = false;
} else if (!input.elt || input.elt.nodeName !== "VIDEO") {
imageInput = true;
}
poseNet = ml5.poseNet(input, "single", modelReady);
// every time new poses are detected store them in 'poses'
poseNet.on("pose", function (results) {
poses = results;
if (poses.length > 0) {
addProps(poses[0].pose, "pose");
}
});
}
function addProps(obj, objName) {
let result = "";
for (const i in obj) {
// Object.hasOwn() is used to exclude properties from the object's
// prototype chain and only show "own properties"
if (Object.hasOwn(obj, i)) {
// remove 'score' and 'keypoints' and add them to separate variables
if (i !== "score" && i !== "keypoints") {
person[i] = obj[i];
} else if (i === "score") {
score = obj[i];
} else if (i === "keypoints") {
keypoints = obj[i];
}
}
}
}
function modelReady() {
console.log("Easy PoseNet is ready");
if(imageInput) poseNet.singlePose(input)
}
// define all the variables we need
let poseNet;
let poses = [];
let imageInput = false;
let input;
let poseNetVideo;