Skip to content

Commit 31db008

Browse files
authored
Merge pull request #1 from mayaman/development
feat(src/PoseNet/index.js): Ability to access parts of pose by name.
2 parents 4814764 + b878fa4 commit 31db008

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/PoseNet/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,22 @@ class PoseNet extends EventEmitter {
6161
return posenet.getAdjacentKeyPoints(keypoints, confidence);
6262
}
6363

64+
// eslint-disable-next-line class-methods-use-this
65+
mapParts(pose) {
66+
const newPose = JSON.parse(JSON.stringify(pose));
67+
newPose.keypoints.forEach((keypoint) => {
68+
newPose[keypoint.part] = {
69+
x: keypoint.position.x,
70+
y: keypoint.position.y,
71+
confidence: keypoint.score,
72+
};
73+
});
74+
return newPose;
75+
}
76+
6477
/* eslint max-len: ["error", { "code": 180 }] */
6578
async singlePose(inputOr) {
6679
let input;
67-
6880
if (inputOr instanceof HTMLImageElement || inputOr instanceof HTMLVideoElement) {
6981
input = inputOr;
7082
} else if (typeof inputOr === 'object' && (inputOr.elt instanceof HTMLImageElement || inputOr.elt instanceof HTMLVideoElement)) {
@@ -74,7 +86,8 @@ class PoseNet extends EventEmitter {
7486
}
7587

7688
const pose = await this.net.estimateSinglePose(input, this.imageScaleFactor, this.flipHorizontal, this.outputStride);
77-
const result = [{ pose, skeleton: this.skeleton(pose.keypoints) }];
89+
const poseWithParts = this.mapParts(pose);
90+
const result = [{ poseWithParts, skeleton: this.skeleton(pose.keypoints) }];
7891
this.emit('pose', result);
7992
if (this.video) {
8093
return tf.nextFrame().then(() => this.singlePose());
@@ -94,7 +107,8 @@ class PoseNet extends EventEmitter {
94107
}
95108

96109
const poses = await this.net.estimateMultiplePoses(input, this.imageScaleFactor, this.flipHorizontal, this.outputStride);
97-
const result = poses.map(pose => ({ pose, skeleton: this.skeleton(pose.keypoints) }));
110+
const posesWithParts = poses.map(pose => (this.mapParts(pose)));
111+
const result = posesWithParts.map(pose => ({ pose, skeleton: this.skeleton(pose.keypoints) }));
98112
this.emit('pose', result);
99113
if (this.video) {
100114
return tf.nextFrame().then(() => this.multiPose());
@@ -129,5 +143,4 @@ const poseNet = (videoOrOptionsOrCallback, optionsOrCallback, cb) => {
129143

130144
return new PoseNet(video, options, detectionType, callback);
131145
};
132-
133-
export default poseNet;
146+
export default poseNet;

0 commit comments

Comments
 (0)