# How can I make my code work for multiple figure detection (poseNet)?

**URL:** <https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [April 29, 2019, 6:50pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762 "2019-04-29T18:50:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sguzmanolmos](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sguzmanolmos](https://discourse.processing.org/u/sguzmanolmos)\
**Post date:** [April 29, 2019, 6:50pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/1 "2019-04-29T18:50:34Z")

</div>

Hoi!

I’ve just started learning how to code and in my school, there’s not much guidance about the subject. Currently, I’m doing a project with p5.js and poseNet. But I’m stuck trying to modify each keypoint independently, **it’s only working with one figure at a time**. How can I command the keypoints of multiple subjects at the same time? Many thanks!

```auto
let video;
let poseNet;
let poses = [];
//let maxPoseDetections = 5;

let noseX = 0;
let noseY = 0;
let leftShoulderX = 0;
let leftShoulderY = 0;
let rightShoulderX = 0;
let rightShoulderY = 0;
let leftElbowX = 0;
let leftElbowY = 0;
let rightElbowX = 0;
let rightElbowY = 0;
let leftWristX = 0;
let leftWristY = 0;
let rightWristX = 0;
let rightWristY = 0;

 
function setup() {
  createCanvas(640, 480);
  video = createCapture(VIDEO);
  video.size(width, height);
  
  poseNet = ml5.poseNet(video, modelReady);
  poseNet.on('pose', gotPoses);
  poseNet.on('pose', function (results) {
  poses = results;
  });
  video.hide();
}

function modelReady() {
  console.log('model ready');
}

function draw() {
  image(video, 0, 0, width, height);
  drawKeypoints();
  //drawSkeleton();
}

function gotPoses(poses) {
  //console.log(poses);
  if (poses.length > 0) {
    let nX = poses[0].pose.keypoints[0].position.x;
    let nY = poses[0].pose.keypoints[0].position.y;
    let lsX = poses[0].pose.keypoints[5].position.x;
    let lsY = poses[0].pose.keypoints[5].position.y;
    let rsX = poses[0].pose.keypoints[6].position.x;
    let rsY = poses[0].pose.keypoints[6].position.y;
    let leX = poses[0].pose.keypoints[7].position.x;
    let leY = poses[0].pose.keypoints[7].position.y;
    let reX = poses[0].pose.keypoints[8].position.x;
    let reY = poses[0].pose.keypoints[8].position.y;
    let lwX = poses[0].pose.keypoints[9].position.x;
    let lwY = poses[0].pose.keypoints[9].position.y;
    let rwX = poses[0].pose.keypoints[10].position.x;
    let rwY = poses[0].pose.keypoints[10].position.y;

    noseX = lerp(noseX, nX, 0.4);
    noseY = lerp(noseY, nY, 0.4);
    leftShoulderX = lerp(leftShoulderX, lsX, 0.3);
    leftShoulderY = lerp(leftShoulderY, lsY, 0.3);
    rightShoulderX = lerp(rightShoulderX, rsX, 0.3);
    rightShoulderY = lerp(rightShoulderY, rsY, 0.3);
    leftElbowX = lerp(leftElbowX, leX, 0.5);
    leftElbowY = lerp(leftElbowY, leY, 0.5);
    rightElbowX = lerp(rightElbowX, reX, 0.5);
    rightElbowY = lerp(rightElbowY, reY, 0.5);
    leftWristX = lerp(leftWristX, lwX, 0.5);
    leftWristY = lerp(leftWristY, lwY, 0.5);
    rightWristX = lerp(rightWristX, rwX, 0.5);
    rightWristY = lerp(rightWristY, rwY, 0.5);
  }
}

function drawKeypoints() {
  for (let i = 0; i < poses.length; i++) {
  let pose = poses[i].pose;
    for (let j = 0; j < pose.keypoints.length; j++) {
  let keypoint = pose.keypoints[j];
  if (noseX, noseY, leftShoulderX, leftShoulderY, rightShoulderX, rightShoulderY, leftElbowX, rightElbowX, leftWristX, leftWristY > 0.4) {
  //image(video, 0, 0, width, height)
  //filter(INVERT)
  //let d = dist(noseX, noseY, leftShoulderY, leftShoulderX);
  
  noStroke();
  // blue
  fill(0, 0, 255);
  ellipse(noseX, noseY, 10);
  // red
  fill(255,0,0);
  ellipse(leftShoulderX, leftShoulderY, 10);
  fill(255,0,0);
  ellipse(rightShoulderX, rightShoulderY, 10);
  // red
  fill(255,0,0);
  ellipse(leftElbowX, leftElbowY, 10);
  fill(255,0,0);
  ellipse(rightElbowX, rightElbowY, 10);
  // green
  fill(0,255,0);
  ellipse(leftWristX, leftWristY, 10);
  fill(0,255,0);
  ellipse(rightWristX, rightWristY, 10);
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 29, 2019, 8:10pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/2 "2019-04-29T20:10:17Z")

</div>

If you post your question to multiple sites, please link between those posts so we know what help you’ve already received.

This question has also been posted here:

> <https://stackoverflow.com/questions/55908018/how-can-i-make-my-p5-js-code-work-for-multiple-figure-detection-posenet-at-the>

---

<div class="post-metadata">

**Author:** ![sguzmanolmos](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sguzmanolmos](https://discourse.processing.org/u/sguzmanolmos)\
**Post date:** [April 29, 2019, 8:20pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/3 "2019-04-29T20:20:50Z")

</div>

Ok, thanks for letting me know 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 1, 2019, 9:53pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/4 "2019-05-01T21:53:23Z")

</div>

> [@sguzmanolmos](#):
>
> it’s only working with one figure at a time

What do you mean by this? Do you mean that you can modify poses[0], but cannot modify poses[1]?

---

<div class="post-metadata">

**Author:** ![sguzmanolmos](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sguzmanolmos](https://discourse.processing.org/u/sguzmanolmos)\
**Post date:** [May 2, 2019, 6:17pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/5 "2019-05-02T18:17:15Z")

</div>

The keypoints only track one person, when someone else appears on the video feed the code won’t generate a second set of keypoints. So it ends up shuffling between them.

I hope that was clear enough Jeremy 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 2, 2019, 7:13pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/6 "2019-05-02T19:13:13Z")

</div>

Right – my question was, is posenet returning multiple poses at all? Does a second pose, poses[1], exist? If it does, then you can change your code to work with it – but that is the first question.

Next – when you say “work” for multiple figures – what do you want to do with each of them?

---

<div class="post-metadata">

**Author:** ![sguzmanolmos](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sguzmanolmos](https://discourse.processing.org/u/sguzmanolmos)\
**Post date:** [May 2, 2019, 11:15pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/7 "2019-05-02T23:15:46Z")

</div>

Not entirely sure how can I check if poseNet is returning multiple poses. I know there’s a version of the algorithm that works with single detection and one with multiple, but that’s as far as my knowledge goes, and the code 😕

The “let nX = poses[0].pose.keypoints[0].position.x;” was taken from [this](https://youtu.be/EA3-k9mnLHs) tutorial.

I’m trying to assign a different color to each key point of every figure detected, for a design project. Later I want to make an Arduino send those points to a led panel. Really thanks for your time!!

---

<div class="post-metadata">

**Author:** ![babyP5](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/babyp5/32/9257_2.png) [@babyP5](https://discourse.processing.org/u/babyP5)\
**Post date:** [June 9, 2020, 1:38pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/8 "2020-06-09T13:38:39Z")

</div>

> [@sguzmanolmos](#):
>
> rom

having the same problem hit me up when u find smth

---

<div class="post-metadata">

**Author:** ![Achille009911](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Achille009911](https://discourse.processing.org/u/Achille009911)\
**Post date:** [August 12, 2021, 3:06pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/9 "2021-08-12T15:06:29Z")

</div>

I confirm it’s possible to detect multiple people in the same pic/image.  
Probably the option is max\_pose\_detections=10,

This example for me works

> <https://github.com/rwightman/posenet-python/blob/master/image_demo.py>

I am using the python version.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 18, 2021, 4:58pm UTC](https://discourse.processing.org/t/how-can-i-make-my-code-work-for-multiple-figure-detection-posenet/10762/10 "2021-08-18T16:58:55Z")

</div>

Thank you for sharing this working example, @Achille009911, and pointing out the `max_pose_detections` argument.

Note that the original posenet stackoverflow thread linked above is since deleted and not in wayback machine – I’m just linking here to a releated search of all posenet questions for those still seeking answers for JavaScript:

- [Stackoverflow - posenet](https://stackoverflow.com/search?q=posenet)
