Check the number of people

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.

1 Like

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… )

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 ?

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);
  }
}

-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

i not see that from your code.
i assume that

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:

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
}

1 Like

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 :sob: :cold_face:

[MAIN]

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]

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);
  }
}
1 Like

in code, what I hear you saying is:

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.

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.

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.