-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex.js
108 lines (99 loc) · 3.32 KB
/
ex.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
// Our input frames will come from here.
const videoElement = document.getElementsByClassName('input_video')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const controlsElement = document.getElementsByClassName('control-panel')[0];
const canvasCtx = canvasElement.getContext('2d');
// We'll add this to our control panel later, but we'll save it here so we can
// call tick() each time the graph runs.
const fpsControl = new FPS();
// Optimization: Turn off animated spinner after its hiding animation is done.
const spinner = document.querySelector('.loading');
spinner.ontransitionend = () => {
spinner.style.display = 'none';
};
function zColor(data) {
return 'white';
}
function onResults(results) {
// Hide the spinner.
document.body.classList.add('loaded');
// Update the frame rate.
fpsControl.tick();
// Draw the overlays.
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(
results.image, 0, 0, canvasElement.width, canvasElement.height);
drawConnectors(
canvasCtx, results.poseLandmarks, POSE_CONNECTIONS, {
visibilityMin: 0.65,
color: 'white'
});
drawLandmarks(
canvasCtx,
Object.values(POSE_LANDMARKS_LEFT)
.map(index => results.poseLandmarks[index]),
{visibilityMin: 0.65, color: zColor, fillColor: 'rgb(255,138,0)'});
drawLandmarks(
canvasCtx,
Object.values(POSE_LANDMARKS_RIGHT)
.map(index => results.poseLandmarks[index]),
{visibilityMin: 0.65, color: zColor, fillColor: 'rgb(0,217,231)'});
drawLandmarks(
canvasCtx,
Object.values(POSE_LANDMARKS_NEUTRAL)
.map(index => results.poseLandmarks[index]),
{visibilityMin: 0.65, color: zColor, fillColor: 'white'});
canvasCtx.restore();
}
const pose = new Pose({locateFile: (file) => {
return `./@mediapipe/pose/${file}`;
}});
pose.onResults(onResults);
/**
* Instantiate a camera. We'll feed each frame we receive into the solution.
*/
const camera = new Camera(videoElement, {
onFrame: async () => {
await pose.send({image: videoElement});
},
width: 1280,
height: 720
});
camera.start();
// Present a control panel through which the user can manipulate the solution
// options.
new ControlPanel(controlsElement, {
selfieMode: true,
modelComplexity: 1,
smoothLandmarks: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
})
.add([
new StaticText({title: 'MediaPipe Pose'}),
fpsControl,
new Toggle({title: 'Selfie Mode', field: 'selfieMode'}),
new Slider({
title: 'Model Complexity',
field: 'modelComplexity',
discrete: ['Lite', 'Full', 'Heavy'],
}),
new Toggle({title: 'Smooth Landmarks', field: 'smoothLandmarks'}),
new Slider({
title: 'Min Detection Confidence',
field: 'minDetectionConfidence',
range: [0, 1],
step: 0.01
}),
new Slider({
title: 'Min Tracking Confidence',
field: 'minTrackingConfidence',
range: [0, 1],
step: 0.01
}),
])
.on(options => {
videoElement.classList.toggle('selfie', options.selfieMode);
pose.setOptions(options);
});