Counting people's numbers

Hi, I’m a beginner in processing.

I count the number of people with a Kinect program.
It recognizes the current number of people and the previous number of people in real time. But there’s a problem.

From zero to one, the previous count will be zero, the new count will be one, and the previous count will be one, too, in the next frame.The frame is then counted with the previous count 1 and the new count 1.
Similarly, if there are one to two, the previous count is one and the new count is two. In the next frame, the previous count is two and the new count is two.

The problem here is,
From zero to one, the previous count 1 new count is one in the next frame.
And even in the frame after two to one, the previous count 1 and the new count are 1.

To sum up, I want to make a difference in the image of the result when I recognize 0 to 1 person counting from 2 to 1 person counting.

-MAIN-

import KinectPV2.*;
KinectPV2 kinect;
import KinectPV2.KJoint;

boolean foundUsers = false;
Circle circle;
Motion motion;
Motion2 motion2;

int sec;

int stateIndex = 0;

void setup() {
  size(500, 500); 
  background(255);

  circle = new Circle();
  motion = new Motion();
  motion2 = new Motion2();
  
  kinect = new KinectPV2(this);

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

  kinect.init();
}
int c = 0;
void draw() {
  //circle.display();
  stateIndex = circle.getState();
  circle.display(stateIndex);
  //print(stateIndex);
}

-COUNTING CLASS-

//int newCount=0;
int oldCount=0;

class Circle {

  float c = 1;
  float t = 1;
  float r = random(500);
  float cc = 255; // circle color
  float co = 15; // circle opacity
  float x;
  float y;

  int sec;

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

  int getState() {

    int currentCount = kinect.getNumOfUsers();
    int state = 0;
    int check = 0;

    if (currentCount == 1 && oldCount == 0) {
      check = 1;
      state = 1;
    } else if (currentCount == 1 && oldCount == 1) {
      state = 11;
    } else if (currentCount == 1 && oldCount >= 2) {
      check = 2;
      state = 5;
    } else if (currentCount == 1 && oldCount == 1) {
      state = 22;
    } else if (currentCount >= 2 && oldCount == 1) {
      state = 2; 
    } else if (currentCount == 2 && oldCount == 2) {
      //plz dont change please .. :(
      state = 5;
    } else if (currentCount >= 3 && oldCount >= 2) {
      state = 5;
    } else if (currentCount >= 3 && oldCount >= 3) {
      //plz dont change please .. 
      state = 5;
    } else if (foundUsers == false) {
      state = 0;
    } 
    oldCount = currentCount;
    return state;
  }

  void display(int index) {
    switch(index) {

    case 0:
      // 0 myeong
      //pintln("0 : defalut");
      background(0);
      fill(255);
      text("COME ON ~~~~ EVERYBODY ~~", width/2, height/2);
       println("0");
      break;

    case 1:
      // 0->1
      background(255);
      println("1 : 0->1");
      fill(0);
      text("1 : 0->1 ", width/2, height/2);
      break;

    case 2:
      //1->2
      background(255);
      println("2 : 1->2");
      fill(0);
      text("2 : 1->2 ", width/2, height/2);
      break;

    case 5:
      // 2&2
      background(255);
      println("2&2");
      break;

    case 11:
      // 0->1..1&1 /
      // background(255);
      println("0->1 // 1&1");
      break;

    case 22:
      // 2->1..1&1 / same as 2&2
      // background(255);
      println("2->1 // 1&1");
      break;
    }
  }
}

int getNumOfUsers(int newCount) {
  return newCount;
}
1 Like

For how long? Clearly, for more than 1 frame. For 30 frames? for 5 seconds?

It doesn’t matter how long.

For example, I would like to print ‘a’ from 1 (old count) & 1 (new count) after changing from 0 to 1. I’d like to print ‘b’ from 2 to 1 and then 1 to 1 (new count).

But I can’t tell if 1 & 1 is the count after 0 to 1 or the count after 2 to 1. I want to distinguish this.

Forget about the Kinect for a second. You are interested in tracking state.

Here is a sketch that is detecting some number of people.

int peopleCount = 0;

void draw() {
  updatePeople();
}

void updatePeople() {
  // randomly change people
  if (random(1)>0.98) {
    if (random(1)>0.5) {
      peopleCount += 1; // enter
    } else {
      peopleCount -= 1; // leave
    }
    peopleCount = constrain(peopleCount, 0, 2);
    println(peopleCount, millis());
  }
}

In order to know what changed, there needs to be a previous value.

int peopleCount = 0;
int peopleCountPrev = 0;

void draw() {
  updatePeople();
}

void updatePeople() {
  // randomly change people
  if (random(1)>0.98) {
    if (random(1)>0.5) {
      peopleCount += 1; // enter
    } else {
      peopleCount -= 1; // leave
    }
    peopleCount = constrain(peopleCount, 0, 2);
    println(peopleCountPrev, peopleCount, millis());
  }
  peopleCountPrev = peopleCount;
}

Now that we have those, we can do something – for example, saving a color based on the last change, then using it for every frame thereafter until it is replaced.

int peopleCount = 0;
int peopleCountPrev = 0;
int lastChange = color(0);

void draw() {
  background(lastChange);
  updatePeople();
}

void updatePeople() {
  // randomly change people
  if (random(1)>0.98) {
    if (random(1)>0.5) {
      peopleCount += 1; // enter
    } else {
      peopleCount -= 1; // leave
    }
    peopleCount = constrain(peopleCount, 0, 2);
    println(peopleCountPrev, peopleCount, millis());
    saveChange(peopleCountPrev, peopleCount);
  }
  peopleCountPrev = peopleCount;
}

void saveChange(int a, int b){
  if(a==0 && b==1) lastChange = color(0,255,0);
  if(a==1 && b==2) lastChange = color(0,0,255);
  if(a==2 && b==1) lastChange = color(255,0,0);
  if(a==1 && b==0) lastChange = color(0);
}

The saveChange logic can be whatever you like. Notice we don’t do anything on a==1 b==1 – that isn’t a change, so we ignore it.

Now, let’s make that a simple class:

class Counter {
  int val;
  int old;
  Counter(int val){
    this.val = val;
    this.old = val;
  }
  int set(int val){
    this.old = this.val;
    this.val = val;
    return val;
  }
  int add(int num){
    return this.set(this.val + num);
  }
  boolean check(int old, int val){
    return (this.old==old && this.val==val);
  }
}

…which we can use like this:

Counter count = new Counter(0);
color bgColor = color(0);

void draw() {
  background(bgColor);
  updatePeople();
}

void updatePeople() {
  if (random(1)>0.98) {
    if (random(1)>0.5) {
      count.add(1); // enter
    } else {
      count.add(-1); // leave
    }
    count.val = constrain(count.val, 0, 2);
    println(count.old, count.val, millis());
    detectChange();
  }
}

void detectChange() {
  if (count.check(0, 1)) bgColor = color(0, 255, 0);
  if (count.check(1, 2)) bgColor = color(0, 0, 255);
  if (count.check(2, 1)) bgColor = color(255, 0, 0);
  if (count.check(1, 0)) bgColor = color(0);
}
1 Like

Thank you very much.
On second thought, I realized that what (how long?) you said was important.
I’ll try again, step by step, as you told me.
Thank you again.