Skip to content

Commit 38d242a

Browse files
authored
[pose-detection]Add named output util. (#659)
1 parent 5590b41 commit 38d242a

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

pose-detection/src/constants.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const BLAZEPOSE_CONNECTED_KEYPOINTS_PAIRS_UPPER_BODY = [
6565
[12, 24], [13, 15], [15, 17], [16, 18], [16, 20], [15, 17], [15, 19],
6666
[15, 21], [16, 22], [17, 19], [18, 20], [23, 24]
6767
];
68-
export const COCO_KEYPOINTS_NAMED_MAP: {[index: string]: number} = {
68+
export const COCO_KEYPOINTS_BY_NAME: {[index: string]: number} = {
6969
nose: 0,
7070
left_eye: 1,
7171
right_eye: 2,
@@ -84,3 +84,67 @@ export const COCO_KEYPOINTS_NAMED_MAP: {[index: string]: number} = {
8484
left_ankle: 15,
8585
right_ankle: 16
8686
};
87+
export const BLAZEPOSE_KEYPOINTS_BY_NAME_UPPER_BODY:
88+
{[index: string]: number} = {
89+
nose: 0,
90+
left_eye_inner: 1,
91+
left_eye: 2,
92+
left_eye_outer: 3,
93+
right_eye_inner: 4,
94+
right_eye: 5,
95+
right_eye_outer: 6,
96+
left_ear: 7,
97+
right_ear: 8,
98+
mouth_left: 9,
99+
mouth_right: 10,
100+
left_shoulder: 11,
101+
right_shoulder: 12,
102+
left_elbow: 13,
103+
right_elbow: 14,
104+
left_wrist: 15,
105+
right_wrist: 16,
106+
left_pinky: 17,
107+
right_pinky: 18,
108+
left_index: 19,
109+
right_index: 20,
110+
left_thumb: 21,
111+
right_thumb: 22,
112+
left_hip: 23,
113+
right_hip: 24
114+
};
115+
export const BLAZEPOSE_KEYPOINTS_BY_NAME_FULL_BODY:
116+
{[index: string]: number} = {
117+
nose: 0,
118+
left_eye_inner: 1,
119+
left_eye: 2,
120+
left_eye_outer: 3,
121+
right_eye_inner: 4,
122+
right_eye: 5,
123+
right_eye_outer: 6,
124+
left_ear: 7,
125+
right_ear: 8,
126+
mouth_left: 9,
127+
mouth_right: 10,
128+
left_shoulder: 11,
129+
right_shoulder: 12,
130+
left_elbow: 13,
131+
right_elbow: 14,
132+
left_wrist: 15,
133+
right_wrist: 16,
134+
left_pinky: 17,
135+
right_pinky: 18,
136+
left_index: 19,
137+
right_index: 20,
138+
left_thumb: 21,
139+
right_thumb: 22,
140+
left_hip: 23,
141+
right_hip: 24,
142+
left_knee: 25,
143+
right_knee: 26,
144+
left_ankle: 27,
145+
right_ankle: 28,
146+
left_heel: 29,
147+
right_heel: 30,
148+
left_foot_index: 31,
149+
right_foot_index: 32
150+
};

pose-detection/src/movenet/detector.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {BoundingBox} from '../calculators/interfaces/shape_interfaces';
2424
import {isVideo} from '../calculators/is_video';
2525
import {KeypointsOneEuroFilter} from '../calculators/keypoints_one_euro_filter';
2626
import {LowPassFilter} from '../calculators/low_pass_filter';
27-
import {COCO_KEYPOINTS, COCO_KEYPOINTS_NAMED_MAP} from '../constants';
27+
import {COCO_KEYPOINTS, COCO_KEYPOINTS_BY_NAME} from '../constants';
2828
import {BasePoseDetector, PoseDetector} from '../pose_detector';
2929
import {InputResolution, Keypoint, Pose, PoseDetectorInput} from '../types';
3030

@@ -343,13 +343,13 @@ export class MoveNetDetector extends BasePoseDetector {
343343

344344
torsoVisible(keypoints: Keypoint[]): boolean {
345345
return (
346-
keypoints[COCO_KEYPOINTS_NAMED_MAP['left_hip']].score >
346+
keypoints[COCO_KEYPOINTS_BY_NAME['left_hip']].score >
347347
MIN_CROP_KEYPOINT_SCORE &&
348-
keypoints[COCO_KEYPOINTS_NAMED_MAP['right_hip']].score >
348+
keypoints[COCO_KEYPOINTS_BY_NAME['right_hip']].score >
349349
MIN_CROP_KEYPOINT_SCORE &&
350-
keypoints[COCO_KEYPOINTS_NAMED_MAP['left_shoulder']].score >
350+
keypoints[COCO_KEYPOINTS_BY_NAME['left_shoulder']].score >
351351
MIN_CROP_KEYPOINT_SCORE &&
352-
keypoints[COCO_KEYPOINTS_NAMED_MAP['right_shoulder']].score >
352+
keypoints[COCO_KEYPOINTS_BY_NAME['right_shoulder']].score >
353353
MIN_CROP_KEYPOINT_SCORE);
354354
}
355355

@@ -381,7 +381,7 @@ export class MoveNetDetector extends BasePoseDetector {
381381
let maxBodyYrange = 0.0;
382382
let maxBodyXrange = 0.0;
383383
for (const key of Object.keys(targetKeypoints)) {
384-
if (keypoints[COCO_KEYPOINTS_NAMED_MAP[key]].score <
384+
if (keypoints[COCO_KEYPOINTS_BY_NAME[key]].score <
385385
MIN_CROP_KEYPOINT_SCORE) {
386386
continue;
387387
}
@@ -412,10 +412,10 @@ export class MoveNetDetector extends BasePoseDetector {
412412
imageWidth: number): BoundingBox {
413413
const targetKeypoints: {[index: string]: number[]} = {};
414414

415-
for (const key of Object.keys(COCO_KEYPOINTS_NAMED_MAP)) {
415+
for (const key of Object.keys(COCO_KEYPOINTS_BY_NAME)) {
416416
targetKeypoints[key] = [
417-
keypoints[COCO_KEYPOINTS_NAMED_MAP[key]].y * imageHeight,
418-
keypoints[COCO_KEYPOINTS_NAMED_MAP[key]].x * imageWidth
417+
keypoints[COCO_KEYPOINTS_BY_NAME[key]].y * imageHeight,
418+
keypoints[COCO_KEYPOINTS_BY_NAME[key]].x * imageWidth
419419
];
420420
}
421421

pose-detection/src/util.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ export function getAdjacentPairs(model: SupportedModels): number[][] {
4444
throw new Error(`Model ${model} is not supported.`);
4545
}
4646
}
47+
48+
export function getKeypointIndexByName(model: SupportedModels):
49+
{[index: string]: number} {
50+
switch (model) {
51+
case SupportedModels.MediapipeBlazeposeUpperBody:
52+
return constants.BLAZEPOSE_KEYPOINTS_BY_NAME_UPPER_BODY;
53+
case SupportedModels.MediapipeBlazeposeFullBody:
54+
return constants.BLAZEPOSE_KEYPOINTS_BY_NAME_FULL_BODY;
55+
case SupportedModels.PoseNet:
56+
case SupportedModels.MoveNet:
57+
return constants.COCO_KEYPOINTS_BY_NAME;
58+
default:
59+
throw new Error(`Model ${model} is not supported.`);
60+
}
61+
}

0 commit comments

Comments
 (0)