-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdots-and-lines.html
More file actions
155 lines (120 loc) · 3.93 KB
/
dots-and-lines.html
File metadata and controls
155 lines (120 loc) · 3.93 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
}
output {
display: block;
white-space: pre;
}
#canvasholder {
/* background-image: url(ben.jpg); */
background-size: cover;
width: 640px;
height: 480px;
position: relative;
}
#canvasholder canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #ccc
}
video {
position: absolute;
top: 0;
left: 0;
opacity: .4;
}
</style>
</head>
<body>
<div id="canvasholder"><canvas></canvas></div>
<output></output>
<script src="https://unpkg.com/@tensorflow/tfjs"></script>
<script src="https://unpkg.com/@tensorflow-models/posenet">
</script>
<script type="module">
import { getDemoPose, getPoseFromImage, PoseStream } from './poses.js';
const canvas = document.querySelector("#canvasholder canvas");
canvas.width = 640;
canvas.height = 480;
const ctx = canvas.getContext('2d');
window.lastKnownGoodPose = null;
function tidiedPoseData(pose) {
// console.log(pose.score);
// If it's a good enough pose, save it
if (pose.score > 0.51) {
window.lastKnownGoodPose = pose;
}
// if we've got one saved then use it
if (window.lastKnownGoodPose) {
pose = window.lastKnownGoodPose;
}
const usedParts = [
"rightShoulder", "rightElbow", "rightWrist",
"leftShoulder", "leftElbow", "leftWrist"
];
const filteredParts = pose.keypoints.filter((point) => {
delete point.score; // don't need this
return usedParts.includes(point.part);
})
return filteredParts;
}
function poseAsLimbs(pose) {
/*
Actual order that we get is:
leftShoulder, rightShoulder, leftElbow, rightElbow, leftWrist, rightWrist
*/
const limbs = {
leftBicep: [pose[0].position, pose[2].position, "#f00"],
leftForearm: [pose[2].position, pose[4].position, "#ff0"],
rightBicep: [pose[1].position, pose[3].position, "#0ff"],
rightForearm: [pose[3].position, pose[5].position, "#00f"]
};
return limbs;
}
// Imagine how often you'll typo drawLimb as drawLine
function drawLimb(from, to, color) {
if (color !== null) {
ctx.strokeStyle = color;
}
ctx.beginPath();
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.lineWidth = 2;
ctx.stroke();
}
function drawPose(pose) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// do some points
pose.forEach((part) => {
const pos = part.position;
ctx.fillStyle = "#000";
ctx.beginPath();
ctx.arc(pos.x, pos.y, 3, 0, Math.PI * 2, true);
ctx.fill();
});
Object.values(poseAsLimbs(pose)).forEach((limb) => {
drawLimb(limb[0], limb[1], limb[2]);
})
}
const stream = new PoseStream({
delay: 10
});
stream.start()
stream.addEventListener('pose', (event) => {
drawPose(tidiedPoseData(event.data))
})
</script>
</body>
</html>