Skip to content

Commit 5598824

Browse files
authored
[pose-detection] Movenet multipose demo (#775)
* Add MoveNet multi-pose option to live demo * Address reviewer's comments
1 parent ae5507c commit 5598824

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

pose-detection/demos/live_video/src/index.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ async function createDetector() {
5353
return posedetection.createDetector(STATE.model, {
5454
runtime,
5555
modelType: STATE.modelConfig.type,
56-
solutionPath: 'https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]'
56+
solutionPath:
57+
'https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]'
5758
});
5859
} else if (runtime === 'tfjs') {
5960
return posedetection.createDetector(
6061
STATE.model, {runtime, modelType: STATE.modelConfig.type});
6162
}
6263
case posedetection.SupportedModels.MoveNet:
63-
const modelType = STATE.modelConfig.type == 'lightning' ?
64-
posedetection.movenet.modelType.SINGLEPOSE_LIGHTNING :
65-
posedetection.movenet.modelType.SINGLEPOSE_THUNDER;
64+
let modelType;
65+
if (STATE.modelConfig.type == 'lightning') {
66+
modelType = posedetection.movenet.modelType.SINGLEPOSE_LIGHTNING;
67+
} else if (STATE.modelConfig.type == 'thunder') {
68+
modelType = posedetection.movenet.modelType.SINGLEPOSE_THUNDER;
69+
} else if (STATE.modelConfig.type == 'multipose') {
70+
modelType = posedetection.movenet.modelType.MULTIPOSE;
71+
}
6672
if (STATE.modelConfig.customModel !== '') {
67-
return posedetection.createDetector(STATE.model, {modelType,
68-
modelUrl: STATE.modelConfig.customModel});
73+
return posedetection.createDetector(
74+
STATE.model, {modelType, modelUrl: STATE.modelConfig.customModel});
6975
}
7076
return posedetection.createDetector(STATE.model, {modelType});
7177
}
@@ -83,13 +89,21 @@ async function checkGuiUpdate() {
8389

8490
window.cancelAnimationFrame(rafId);
8591

86-
detector.dispose();
92+
if (detector != null) {
93+
detector.dispose();
94+
}
8795

8896
if (STATE.isFlagChanged || STATE.isBackendChanged) {
8997
await setBackendAndEnvFlags(STATE.flags, STATE.backend);
9098
}
9199

92-
detector = await createDetector(STATE.model);
100+
try {
101+
detector = await createDetector(STATE.model);
102+
} catch (error) {
103+
detector = null;
104+
alert(error);
105+
}
106+
93107
STATE.isFlagChanged = false;
94108
STATE.isBackendChanged = false;
95109
STATE.isModelChanged = false;
@@ -125,21 +139,35 @@ async function renderResult() {
125139
});
126140
}
127141

128-
// FPS only counts the time it takes to finish estimatePoses.
129-
beginEstimatePosesStats();
130-
131-
const poses = await detector.estimatePoses(
132-
camera.video,
133-
{maxPoses: STATE.modelConfig.maxPoses, flipHorizontal: false});
142+
let poses = null;
143+
144+
// Detector can be null if initialization failed (for example when loading
145+
// from a URL that does not exist).
146+
if (detector != null) {
147+
// FPS only counts the time it takes to finish estimatePoses.
148+
beginEstimatePosesStats();
149+
150+
// Detectors can throw errors, for example when using custom URLs that
151+
// contain a model that doesn't provide the expected output.
152+
try {
153+
poses = await detector.estimatePoses(
154+
camera.video,
155+
{maxPoses: STATE.modelConfig.maxPoses, flipHorizontal: false});
156+
} catch (error) {
157+
detector.dispose();
158+
detector = null;
159+
alert(error);
160+
}
134161

135-
endEstimatePosesStats();
162+
endEstimatePosesStats();
163+
}
136164

137165
camera.drawCtx();
138166

139167
// The null check makes sure the UI is not in the middle of changing to a
140168
// different model. If during model change, the result is from an old model,
141169
// which shouldn't be rendered.
142-
if (poses.length > 0 && !STATE.isModelChanged) {
170+
if (poses && poses.length > 0 && !STATE.isModelChanged) {
143171
camera.drawResults(poses);
144172
}
145173
}

pose-detection/demos/live_video/src/option_panel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function setupDatGui(urlParams) {
5656
break;
5757
case 'movenet':
5858
params.STATE.model = posedetection.SupportedModels.MoveNet;
59-
if (type !== 'lightning' && type !== 'thunder') {
59+
if (type !== 'lightning' && type !== 'thunder' && type !== 'multipose') {
6060
// Nulify invalid value.
6161
type = null;
6262
}
@@ -158,14 +158,15 @@ function addMoveNetControllers(modelConfigFolder, type) {
158158
params.STATE.modelConfig.type = type != null ? type : 'lightning';
159159

160160
const typeController = modelConfigFolder.add(
161-
params.STATE.modelConfig, 'type', ['lightning', 'thunder']);
161+
params.STATE.modelConfig, 'type', ['lightning', 'thunder', 'multipose']);
162162
typeController.onChange(_ => {
163163
// Set isModelChanged to true, so that we don't render any result during
164164
// changing models.
165165
params.STATE.isModelChanged = true;
166166
});
167167

168-
const customModelController = modelConfigFolder.add(params.STATE.modelConfig, 'customModel');
168+
const customModelController =
169+
modelConfigFolder.add(params.STATE.modelConfig, 'customModel');
169170
customModelController.onFinishChange(_ => {
170171
params.STATE.isModelChanged = true;
171172
})

0 commit comments

Comments
 (0)