# Check the number of people

**URL:** https://discourse.processing.org/t/check-the-number-of-people/13309
**Category:** Libraries
**Created:** [August 11, 2019, 12:35pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309 "2019-08-11T12:35:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jeeees2](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jeeees2](https://discourse.processing.org/u/jeeees2)
#### Post date: [August 11, 2019, 12:35pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/1 "2019-08-11T12:35:55Z")

</div>

Hi, I’m a beginner in processing.

I try to check up to two people using the ‘Kinect divice and program.’ However, I try to print the result images differently from 0 to 1 person and the result images from 2 to 1.

I can print out images by checking the number of people. But I’m trying to make a difference between a decrease in the number of people recognized from two to one and an increase from zero to one.

I don’t know how to distinguish. Please help me.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 11, 2019, 12:53pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/2 "2019-08-11T12:53:24Z")

</div>

possibly you not need to post your full code,  
just the logic part where you evaluate that change of a ?global? variable ( use? old, new )  
( containing the ?counted? number of peoples in a picture/video… )

---

<div class="post-metadata">

### Author: ![jeeees2](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jeeees2](https://discourse.processing.org/u/jeeees2)
#### Post date: [August 11, 2019, 1:11pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/3 "2019-08-11T13:11:24Z")

</div>

i just write only checking people code that is tracking by kinect divice. So I guess the code I wrote was probably useless.  
I think i could write a logic to compare between the variable that checks the number and the variable that contains number that i check … right ?

```auto
class Circle {
  float c = 1;
  float t = 1;
  float r = random(500);
  float cc = 0; // circle color
  float co = 15; // circle opacity
  float x;
  float y;

  int sec;

  Circle() {
    x = random(width);
    y = random(height);
  }

  void display()
  {
    if (frameCount%60==0) {
      sec++;
    }

    int [] rawData = kinect.getRawBodyTrack();

    frameRate(10);
    noStroke();
    fill(random(cc), random(cc), random(cc), 100);
    ellipse(width/2, height/2, c, c);

    foundUsers = false;

    //iterate through 1/5th of the data
    for (int i = 0; i < rawData.length; i+=5) {
      if (rawData[i] != 255) {
        //found something
        foundUsers = true;
        break;
      }
    }

    c = c + 30;
    t = t + 0.3;
    if (c > 1000) {
      c = 1;
      if (t > 2) {
        t = 1;
      }
      // background(225);

      strokeWeight(t);
      stroke(255);
    }

    fill(255);
    textSize(16);
    text(kinect.getNumOfUsers(), 50, 50);

      if (kinect.getNumOfUsers() == 0) {
        text("zero", 110, 90);
      } else if (kinect.getNumOfUsers() == 1) {
        text("a person", 110, 90);
      } else if (kinect.getNumOfUsers() == 2) { 
        text("two", 110, 90);
       }
    text("Found User: "+foundUsers, 50, 70);
  }
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 11, 2019, 1:54pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/4 "2019-08-11T13:54:17Z")

</div>

-a- why you not tell us first where you get the code from ( link…) and if it was working,  
-b- what you changed?

-c- logic

> [@jeeees2](#):
>
> a logic to compare between the variable that checks the number and the variable that contains number

i not see that from your code.  
i assume that

```auto
kinect.getNumOfUsers()

```

gives a ?integer? number what tells you what you want?

now to detect a CHANGE you would need to store that number and  
and ?later?next check? compare the new result with the old ( stored ) one

* * *

now that is something what has nothing to do with kinect library, and that classes in your code…  
so, can we forget all this and start again from basics ( using a new/empty processing sketch).

please play with that:

```auto
int oldUser=0, newUser = 0;

void setup(){println("click with mouse on canvas and press any key");}
void draw(){}
void keyPressed(){
  //newUser = kinect.getNumOfUsers();
  newUser = (int)random(0,5); //0,1,2,3,4
  int diffUser = newUser - oldUser;
  println("oldUser: "+oldUser+" newUser: "+newUser+" CHANGE: "+diffUser);
  oldUser = newUser; // remember
}

```

---

<div class="post-metadata">

### Author: ![jeeees2](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jeeees2](https://discourse.processing.org/u/jeeees2)
#### Post date: [August 11, 2019, 2:44pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/5 "2019-08-11T14:44:25Z")

</div>

First of all, I am sorry that I am not English speaking person so it is hard to make you understand.  
And this code is not from anywhere, it’s from me. So it might be a little insufficient. I want to say is, my code can count people. Right?

And i just thought, if i wanna show a different image between a image from 0 to 1 and 2 to 1, i have to check the number and store. then, when a new people recognizes in the device, it compares new number of people and stored number of people. (You can advise me if I’m wrong.) That’s why I wrote it down like this. i’ll show you the full code. And i’ll try to fix my code as you say.  
Please help me more 😭 🥶

[MAIN]

```auto
import KinectPV2.*;
KinectPV2 kinect;
import KinectPV2.KJoint;

boolean foundUsers = false;

Circle circle;

void setup() {
  size(500, 500); 
  background(255);
  
  circle = new Circle();
  kinect = new KinectPV2(this);

  kinect.enableDepthImg(true);
  kinect.enableBodyTrackImg(true);

  kinect.init();
}

void draw() {
  circle.display();
}

```

[CLASS]

```auto
class Circle {
  float c = 1;
  float t = 1;
  float r = random(500);
  float cc = 0; // circle color
  float co = 15; // circle opacity
  float x;
  float y;

  Circle() {
    x = random(width);
    y = random(height);
  }

  void display()
  {

    int [] rawData = kinect.getRawBodyTrack();

    frameRate(10);
    noStroke();
    fill(random(cc), random(cc), random(cc), 100);
    ellipse(width/2, height/2, c, c);

    foundUsers = false;

    //iterate through 1/5th of the data
    for (int i = 0; i < rawData.length; i+=5) {
      if (rawData[i] != 255) {
        //found something
        foundUsers = true;
        break;
      }
    }

    c = c + 30;
    t = t + 0.3;
    if (c > 1000) {
      c = 1;
      if (t > 2) {
        t = 1;
      }

      strokeWeight(t);
      stroke(255);
    }

    fill(255);
    textSize(16);
    text(kinect.getNumOfUsers(), 50, 50);

    if (kinect.getNumOfUsers() == 0) {
      text("zero", 110, 90);
    } else if (kinect.getNumOfUsers() == 1) {
      text("a person", 110, 90);
    } else if (kinect.getNumOfUsers() == 2) { 
      text("two", 110, 90);
    }
    text("Found User: "+foundUsers, 50, 70);
  }
}

```

---

<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 12, 2019, 11:57pm UTC](https://discourse.processing.org/t/check-the-number-of-people/13309/6 "2019-08-12T23:57:31Z")

</div>

> [@jeeees2](#):
>
> from 0 to 1 and 2 to 1

in code, what I hear you saying is:

```auto
if(oldCount==0 && newCount==1){
  background(0,255,0);
}
else if(oldCount==2 && newCount==1){
  background(0,0,255);
}

```

so imagine the simplest sketch to test that idea–say one where you just type the number of people there currently are with the number keys.

```auto
int newCount=0;
int oldCount=0;

void draw() {
  background(128);
  text(str(oldCount) + " " + str(newCount), width/2, height/2);
}

void keyPressed() {
  if (key>=48 && key<=58) {
    int val=Character.getNumericValue(key);
    if (val!=newCount) {
      oldCount=newCount;
      newCount=val;
    }
  }
}

```

Then drop that code into it and see what happens.

```auto
int newCount=0;
int oldCount=0;

void draw() {
  background(128);
  if (oldCount==0 && newCount==1) {
    background(0, 255, 0);
  } else if (oldCount==2 && newCount==1) {
    background(0, 0, 255);
  }
  text(str(oldCount) + " " + str(newCount), width/2, height/2);
}

void keyPressed() {
  if (key>=48 && key<=58) {
    int val=Character.getNumericValue(key);
    if (val!=newCount) {
      oldCount=newCount;
      newCount=val;
    }
  }
}

```

Now you can work out various rules of interaction–and get feedback and testing help–separately from your Kinect code. Testing with the Kinect is still important, of course–you may need to suppress flickering of the count, for example. But this should start you with the basics.
