How to make text disappear when blob intersects words using Kinect V1?

edited December 2016 in Kinect

Hi guys

I'm using version 1 kinect to make a blob interact with sound-reactive raining text. I was wondering how to make the text disappear once the blob intersects with one of the words? Please help! Thank you!

//Make text individual words disappear (change position off page) if blob position is equal word position.

import org.openkinect.freenect.*;
import org.openkinect.processing.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioInput in;
FFT fft;
KinectTracker tracker;
Kinect kinect;

String [] data = {"327-64-4367", "448-01-4857","553-15-8880","016-10-8387","222-07-1302","574-68-5578","104-42-0570","237-40-7110","212-21-5143","559-30-1997", "574-76-8818", "007-64-7860", "145-42-9696", "574-24-6501", "477-49-7241", "517-62-2683", "315-52-7169", "750-12-8784", "678-20-3681","5895 13th Street, Webster, NY 14580","7776 Grove Street, Romulus, MI 48174","2294 Brook Lane, Paramus, NJ 07652","2424 Route 64, Maineville, OH 45039","9645 Valley View Drive, Tewksbury, MA 01876","153 Cross Street, The Villages, FL 32162","920 Route 30, Loxahatchee, FL 33470","238 Lincoln Street, Jamestown, NY 14701","729 Meadow Street, Dubuque, IA 52001","133 Franklin Court, South Portland, ME 04106","994 Elm Avenue, Sun Prairie, WI 53590","392 Woodland Drive, Liverpool, NY 13090","901 Willow Lane, Bridgeport, CT 06606","92236","07731","45420","10701","08094","07030","19047","41017","17543","80302","80021","60134","32082","(494) 133-5459","(249) 186-5356","(818) 402-5177","(424) 926-1653","(201) 672-5146","(245) 199-2255","(147) 520-8998","(349) 179-5381","(758) 621-4845","(988) 984-0554","(786) 693-1798","(291) 731-8164","(345) 209-6039","(537) 175-2683","(918) 990-4772","(820) 906-9228","(916) 565-9637","(633) 293-3501","(881) 991-5264","(193) 382-1818","(329) 603-4359","(160) 347-3446","(870) 293-5829","5165 1130 7400 9060","6011 5436 3995 4943","3455 419447 60971","3486 888573 38316","6011 5910 4628 7894","6011 7865 3449 2309","4929 2788 4706 8154","5427 8194 1616 5774","5446 9823 4141 6972","6011 2940 9543 3242","4532 8600 5212 9117","6011 9832 4310 8231","3784 431486 81659","5520 6129 5998 3075","4485 5114 2769 9519","4716 7962 0326 5780","3418 877718 33322","4539 1378 7971 4063","3730 394465 12151","4539 4187 1183 6953","5295 2968 4751 7338","3755 105752 68078","157.95.65.166","107.165.28.12","75.62.96.132","249.88.95.147","187.244.83.79","134.131.159.96","49.206.109.182","95.33.230.69","211.162.26.78","186.58.2.250","PcCX5mQhf","s58@ubFUM","n1IPf4zJo","ShSUN0pPc","d-2eug@Qj","zy*@fYOaG","npujcoBVE","Kenzie Flores","Shamar Fox","Nadia Hatfield","Brodie Hood","Nathanial James","Leanna Woodard","Wilson Murillo","Lillie Sullivan","Lillianna Mcintyre","Edward Chan","Jay Chambers","Shirley Vaughn","Jaylah Daniel","Eden Gordon","Joanna Freeman","Jazmyn Hamilton","Molly Costa","Julie Snow","Kole Wu","Raul Santos","Jadiel Mercado","Anna Silva","Mckinley Wyatt","Lance Odom","Nylah Whitney","Melanie Berger","Colin Strong","Irvin Kent","Donna Levine","Tess Wilkinson","Micheal Shields","Helen Rocha","Destinee Bowers","Keyon Graves","Bradley Knight","Kaitlynn Santos","Natalya Hernandez","Humberto Knapp","Camryn Farrell","Pablo Aguilar"};  //40

String note;
color c;
int n;
int noteNumber;
int sampleRate= 44100;

float [] max= new float [sampleRate/2];
float maximum;
float frequency;
float hertz;
float midi;
float deg;
float brightnessThresh;

boolean ir = false;
boolean colorDepth = false;
boolean mirror = false;

Drop[] drops = new Drop[150];

void setup() {
  size(1280, 520); //size of kinect screen
  textSize(8);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  kinect.initDepth();
  kinect.initVideo();
  //kinect.enableIR(ir);
  kinect.enableColorDepth(colorDepth);

  brightnessThresh = 0;

  //mirror Image
  mirror = !mirror;
  kinect.enableMirror(mirror);

  deg = kinect.getTilt();
  // kinect.tilt(deg);

  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.MONO, 4096, sampleRate);
  fft = new FFT(in.left.size(), sampleRate);

  textAlign(CENTER);
  for (int i = 0; i < drops.length; i++) {
    drops[i] = new Drop();
  }
}

void draw() {
  background(0);
  //smooth();
  findNote();
  image(kinect.getVideoImage(), 0, 0);
  image(kinect.getDepthImage(), 640, 0); //

  // Run the tracking analysis
  tracker.track();
  // Show the image
  tracker.display();

  int t = tracker.getThreshold();

  for (int i = 0; i < drops.length; i++) {
    drops[i].fall();
    drops[i].show();
  }

  fill(255);
  /*text(
    "Press 'i' to enable/disable between video image and IR image,  " +
    "Press 'c' to enable/disable between color depth and gray scale depth,  " +
    "Press 'm' to enable/diable mirror mode, "+
    "UP and DOWN to tilt camera   " +
    "Framerate: " + int(frameRate), 10, 515);
  text("threshold: " + t + "    " +  "framerate: " + int(frameRate) + "    " + 
    "UP increase threshold, DOWN decrease threshold", 10, 500); */

  }

void keyPressed() {
  int t = tracker.getThreshold();
  if (key == 'i') {
    //ir = !ir;
    //kinect.enableIR(ir);
  } else if (key == 'c') {
    colorDepth = !colorDepth;
    kinect.enableColorDepth(colorDepth);
  } else if (key == CODED) {
    if (keyCode == UP) {
      deg++;
    } else if (keyCode == DOWN) {
      deg--;
    } else if (key == CODED) {
      if (keyCode == RIGHT) {
        t+=5;
      tracker.setThreshold(t);
      }
    } else if (key == CODED) {
      if (keyCode == LEFT) {
        t-=5;
      tracker.setThreshold(t);
      }
    }
    deg = constrain(deg, 0, 30);
    kinect.setTilt(deg);
  }
}

class KinectTracker {
  int threshold = 1000; //depth threshold
  int[] depth;
  PImage display;

  KinectTracker() {
    kinect.initDepth();
    kinect.enableMirror(true);
    // Make a blank image
    display = createImage(kinect.width, kinect.height, RGB); 

  }

  void track() {
    depth = kinect.getRawDepth(); //Get raw depth as array of integers

    if (depth == null) return;

    float sumX = 0;
    float sumY = 0;
    float count = 0;

    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {

        int offset =  x + y*kinect.width;
        // Grabbing the raw depth
        int rawDepth = depth[offset];

        // Testing against threshold
        if (rawDepth < threshold) {
          sumX += x;
          sumY += y;
          count++;
        }
      }
    }
  }

  void display() {
    PImage img = kinect.getDepthImage();

    if (depth == null || img == null) return;

    // Going to rewrite the depth image to show which pixels are in threshold
    display.loadPixels();
    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {

        int offset = x + y * kinect.width;
        // Raw depth
        int rawDepth = depth[offset];
        int pix = x + y * display.width;
        if (rawDepth < threshold) {
          display.pixels[pix] = color(c, 150); //cream color 252,251,227
        } else {
          display.pixels[pix] = color(0);
        }
      }
    }
    display.updatePixels();


    // Draw blob image
    image(display, 0, 0);


  }

  int getThreshold() {
    return threshold;
  }
  void setThreshold(int t) {
    threshold =  t;
  }
}


//NOTES With Sounds
void findNote() {
  fft.forward(in.left);
  for (int f=0;f<sampleRate/2;f++) { //analyses the amplitude of each frequency analysed, between 0 and 22050 hertz
    max[f]=fft.getFreq(float(f)); //each index is correspondent to a frequency and contains the amplitude value 
  }
  maximum=max(max);//get the maximum value of the max array in order to find the peak of volume

  for (int i=0; i<max.length; i++) {
    if (max[i] == maximum) {
      frequency= i;
    }
  }

  midi= 69+12*(log((frequency-6)/440));// formula that transform frequency to midi numbers
  n= int (midi);

//the octave has 12 tones and semitones. 
if (n%12==9)
  {
    note = ("a");
    c = color (255, 99, 0);
  }

  if (n%12==10)
  {
    note = ("a#");
    c = color (255, 236, 0);
  }

  if (n%12==11)
  {
    note = ("b");
    c = color (153, 255, 0);
  }

  if (n%12==0)
  {
    note = ("c");
    c = color (40, 255, 0);
  }

  if (n%12==1)
  {
    note = ("c#");
    c = color (0, 255, 232);
  }

  if (n%12==2)
  {
    note = ("d");
    c = color (0, 124, 255);
  }

  if (n%12==3)
  {
    note = ("d#");
    c = color (5, 0, 255);
  }

  if (n%12==4)
  {
    note = ("e");
    c = color (69, 0, 234);
  }

  if (n%12==5)
  {
    note = ("f");
    c = color (85, 0, 79);
  }

  if (n%12==6)
  {
    note = ("f#");
    c = color (116, 0, 0);
  }

  if (n%12==7)
  {
    note = ("g");
    c = color (179, 0, 0);
  }

  if (n%12==8)
  {
    note = ("g#");
    c = color (238, 0, 0);
  }
}

void stop()
{
  in.close();
  minim.stop();

  super.stop();
}

class Drop {
  float x;
  float y;
  float z;
  float len;
  float yspeed;
  String textHolder = "text";
  float word;

  Drop() {
    x  = random(width); //rain drops at random width on x-axis
    y  = random(0, 700); //sections of rain
    z  = random(0, 1); //general speed
    len = map(z, 0, 20, 10, 20);
    yspeed  = map(z, 0, 20, 5, 2); //Speeds of raindrops, 4 is variation

    textHolder = data[int(random(data.length))];

  }

  void fall() { //speed of rain
    y = y + yspeed;
    float grav = map(z, 0, 20, 0, 0.2); //(z, 0, 20, 0, 0.2)
    yspeed = yspeed - grav; //+ grav;


    if (y > height) {
      y = random(-500, 40);
      yspeed = map(z, 0, 20, 4, 20);  //(z, 0, 20, 4, 1000);
    }
  }

  void show() {
    float thick = map(z, 0, 20, 1, 3);

    fill(c);
    text(textHolder, x, y, y+len); 
  }
}

Answers

Sign In or Register to comment.