-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbody-parts.html
More file actions
173 lines (138 loc) · 4.56 KB
/
body-parts.html
File metadata and controls
173 lines (138 loc) · 4.56 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
<!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) {
// 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"
];
return pose.keypoints.filter((point) => {
delete point.score; // don't need this
return usedParts.includes(point.part);
});
}
function poseAsLimbs(pose) {
/*
Actual order that we get is:
leftShoulder, rightShoulder, leftElbow, rightElbow, leftWrist, rightWrist
*/
return {
leftBicep: [
pose[0].position,
pose[2].position,
"bodyparts/bicep_l.png"
],
leftForearm: [
pose[2].position,
pose[4].position,
"bodyparts/forearm_l.png"
],
rightBicep: [
pose[1].position,
pose[3].position,
"bodyparts/bicep_r.png"
],
rightForearm: [
pose[3].position,
pose[5].position,
"bodyparts/forearm_r.png"
]
};
}
function drawImage(image, x, y, scale, rotation) {
ctx.save();
ctx.setTransform(scale, 0, 0, scale, x, y); // sets scale and origin
ctx.rotate(rotation); // rads
ctx.drawImage(image, -image.width / 2, -image.height / 2);
ctx.restore();
}
// Imagine how often you'll typo drawLimb as drawLine
function drawLimb(from, to, img) {
// let rotDeg = Math.atan2(to.y - from.y, to.x - from.x) * 180 / Math.PI;
let angleRadians = Math.atan2(to.y - from.y, to.x - from.x);
let image = new Image();
image.src = img;
console.log(image);
drawImage(image, from.x, from.y, 1, angleRadians);
};
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: 100
});
stream.start()
stream.addEventListener('pose', (event) => {
drawPose(tidiedPoseData(event.data))
})
</script>
</body>
</html>