openKinect question:
in
Contributed Library Questions
•
2 years ago
It will have 3 zones, an Enter space (< 800), a Middle space (> 800 && < 960), and a Back space (> 960).
What's wrong with this code?
(It's an adaptation from Daniel Shiffman's average point tracking example that comes with the library.)
(It's an adaptation from Daniel Shiffman's average point tracking example that comes with the library.)
- import org.openkinect.*;
- import org.openkinect.processing.*;
- // Showing how we can farm all the kinect stuff out to a separate class
- KinectTracker tracker;
- // Kinect Library object
- Kinect kinect;
- int kw = 640;
- int kh = 480;
- PImage depthImg;
- int minDepth = 800;
- int maxDepth = 990;
- int minimumPresence = 100;
- int presenceFront = 0;
- int presenceMiddle = 0;
- int presenceBack = 0;
- final int NOTHING = 0;
- final int ENTER = 1;
- final int RECORDING = 2;
- final int SOUND_PLAYBACK = 3;
- int currentPos = -1;
- int whereIsViewer = NOTHING;
- int by = 0;
- //frame differencing stuff
- int numPixels;
- int[] averagePixels;
- int[] toScreenPixels;
- color white = color(255);
- color black = color(0);
- boolean firstTime = true;
- boolean showBackgroundSubtraction = false;
- boolean showThresholded = false;
- void setup() {
- size(640, 520);
- numPixels = kw * kh;
- averagePixels = new int[numPixels];
- toScreenPixels = new int[numPixels];
- // Make the pixels[] array available for direct manipulation
- loadPixels();
- kinect = new Kinect(this);
- tracker = new KinectTracker();
- //frameRate(1);
- }
- void draw() {
- background(255);
- float a = map(kw, 0, width, 0, 1);//I'm not sure what to put instead of mouseX or Y
- float thresh = map(kh, 0, height, 0, 255);
- kinect.getVideoImage(); // Read a new video frame
- //kinect.getVideoPix(); //I'm not sure where to go with this portion
- for (int i = 0; i < numPixels; i++) {
- int currColor = loadPixels(i);// it tells me that the method loadPixels() in the type PApplet is not applicable for the arguments (int)
- int bkgdColor = averagePixels[i];
- int currR = (currColor >> 16) & 0xFF;
- int currG = (currColor >> 8) & 0xFF;
- int currB = currColor & 0xFF;
- int bkgdR = (bkgdColor >> 16) & 0xFF;
- int bkgdG = (bkgdColor >> 8) & 0xFF;
- int bkgdB = bkgdColor & 0xFF;
- int redAvg = int(a * currR + (1 - a) * bkgdR);
- int greenAvg = int(a * currG + (1 - a) * bkgdG);
- int blueAvg = int(a * currB + (1 - a) * bkgdB);
- // equiv to color(redAvg, greenAvg, blueAvg); , but perhaps slightly faster
- averagePixels[i] = 0xFF000000 | (redAvg << 16) | (greenAvg << 8) | blueAvg; // equiv to color(redAvg, greenAvg, blueAvg);
- if (showBackgroundSubtraction) {
- int bgSubR = abs(bkgdR - currR);
- int bgSubG = abs(bkgdG - currG);
- int bgSubB = abs(bkgdB - currB);
- toScreenPixels[i] = 0xFF000000 | (bgSubR << 16) | (bgSubG << 8) | bgSubB;
- }
- else {
- toScreenPixels[i] = averagePixels[i];
- }
- if (showThresholded) {
- toScreenPixels[i] = brightness(toScreenPixels[i]) > thresh ? 1 : 0;
- }
- }
- fill(255);
- // Run the tracking analysis
- tracker.track();
- // Show the image
- // Let's draw the raw location
- PVector v1 = tracker.getPos();
- // Let's draw the "lerped" location
- PVector v2 = tracker.getLerpedPos();
- // Display some info
- int t = tracker.getThreshold();
- }
- void stop() {
- tracker.quit();
- super.stop();
- }
and
- class KinectTracker {
- // Size of kinect image
- int kw = 640;
- int kh = 480;
- int threshold = 900;
- // Raw location
- PVector loc;
- // Interpolated location
- PVector lerpedLoc;
- // Depth data
- int[] depth;
- int[] depthBg = new int[kw*kh];
- PImage display;
- PImage display2;
- KinectTracker() {
- kinect.start();
- kinect.enableDepth(true);
- // We could skip processing the grayscale image for efficiency
- // but this example is just demonstrating everything
- kinect.processDepthImage(true);
- display = createImage(kw, kh, PConstants.RGB);
- display2 = createImage(kw, kh, PConstants.RGB);
- loc = new PVector(0, 0);
- lerpedLoc = new PVector(0, 0);
- }
- void track() {
- // Get the raw depth as array of integers
- depth = kinect.getRawDepth();
- // Being overly cautious here
- if (depth == null) return;
- float sumX = 0;
- float sumY = 0;
- float count = 0;
- presenceFront = 0;
- presenceMiddle = 0;
- presenceBack = 0;
- display2.loadPixels();
- for (int x = 0; x < kw; x++) {
- for (int y = 0; y < kh; y++) {
- // Mirroring the image
- int offset = kw-x-1+y*kw;
- int bgDepth = depthBg[offset];
- // Grabbing the raw depth
- int rawDepth = depth[offset];
- int correctedBg = rawDepth - bgDepth;
- int bgAvg = int(a * rawDepth + (1 - a) * bgDepth);
- depthBg[offset] = bgAvg;
- display2.pixels[offset] = (color(rawDepth - depthBg[offset]));
- // Testing against threshold
- if (correctedBg < threshold) {
- sumX += x;
- sumY += y;
- count++;
- }
- if (correctedBg < minDepth) {
- presenceFront++;
- }
- else if (correctedBg >= minDepth && correctedBg < maxDepth) {
- presenceMiddle++;
- }
- else {
- presenceBack++;
- }
- }
- }
- display2.updatePixels();
- int currentPos = -1;
- int [] list = {
- presenceFront, presenceMiddle, presenceBack
- };
- println(list);
- int maxPresence = max(list);
- // do we have a minimal amount of presence to proceed?
- if (maxPresence > minimumPresence) { // 1000 is the minimum
- if (maxPresence == presenceFront) {
- currentPos = ENTER;
- }
- else if (maxPresence == presenceMiddle) {
- currentPos = RECORDING;
- }
- else if (maxPresence == presenceBack) {
- currentPos = SOUND_PLAYBACK;
- }
- }
- else {
- currentPos = NOTHING;
- }
- if (currentPos != whereIsViewer) {
- // then do checks cause something changed
- if (whereIsViewer == NOTHING && currentPos == ENTER) {
- whereIsViewer = ENTER;
- }
- if (whereIsViewer == ENTER && currentPos == RECORDING) {
- whereIsViewer = RECORDING;
- }
- else if (whereIsViewer == RECORDING && currentPos == SOUND_PLAYBACK) {
- whereIsViewer = SOUND_PLAYBACK;
- }
- if (whereIsViewer == SOUND_PLAYBACK && currentPos == RECORDING) {
- whereIsViewer = NOTHING;
- reset();
- }
- if (whereIsViewer == RECORDING && currentPos == ENTER) {
- whereIsViewer = NOTHING;
- reset();
- }
- if (whereIsViewer == ENTER && currentPos == NOTHING) {
- whereIsViewer = NOTHING;
- reset();
- }
- }
- else {
- // nothing changed, so nothing to do
- }
- println(whereIsViewer);
- //println(currentPos);
- // As long as we found something
- if (count != 0) {
- loc = new PVector(sumX/count, sumY/count);
- }
- // Interpolating the location, doing it arbitrarily for now
- lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
- lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
- }
- //PVector getLerpedPos() {
- //return lerpedLoc;
- //}
- //PVector getPos() {
- //return loc;
- //}
- }
- void display() {
- PImage img = kinect.getDepthImage();
- // Being overly cautious here
- if (depth == null || img == null) return;
- // Going to rewrite the depth image to show which pixels are in threshold
- // A lot of this is redundant, but this is just for demonstration purposes
- display.loadPixels();
- for (int x = 0; x < kw; x++) {
- for (int y = 0; y < kh; y++) {
- // mirroring image
- int offset = kw-x-1+y*kw;
- // Raw depth
- int rawDepth = depth[offset];
- int pix = x+y*display.width;
- if (rawDepth < threshold) {
- // A red color instead
- display.pixels[pix] = color(150, 50, 50);
- }
- else {
- display.pixels[pix] = img.pixels[offset];
- }
- }
- }
- display.updatePixels();
- // the image
- /// image(display, 0, 0);
- image(display2, 0, 0);
- }
- void reset() {
- currentPos = -1;
- }
- void quit() {
- kinect.quit();
- }
- int getThreshold() {
- return threshold;
- }
- void setThreshold(int t) {
- threshold = t;
- }
1