Skip to content

Instantly share code, notes, and snippets.

@ulrichzwingli
Created January 16, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulrichzwingli/a6862f168d44723732797311e48054d8 to your computer and use it in GitHub Desktop.
Save ulrichzwingli/a6862f168d44723732797311e48054d8 to your computer and use it in GitHub Desktop.
Facer your fear _ Processing code
import org.openkinect.processing.*;
import processing.serial.*;
// Kinect Library object
Kinect kinect;
PImage img;
float minThresh = 900;
float maxThresh = 940;
float mf;
float sumX = 0;
float sumY = 0;
float totalPixels = 0;
// For Arduino
PImage shadow;
PVector a = new PVector(150, 0);
PVector b = new PVector(0, 150);
float angle;
PVector v1;
PVector v2;
float q;
float len;
float root;
float avgX;
float avgY;
Serial myPort;
void setup() {
size(1440, 1080);
kinect = new Kinect(this);
kinect.initDepth();
frameRate(30);
img = createImage(kinect.width, kinect.height, RGB);
//setup for arduino code
shadow = loadImage("shadow.png");
printArray(Serial.list()); // improved function
String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(0);
img.loadPixels();
sumX = 0;
sumY = 0;
totalPixels = 0;
// Get the raw depth as array of integers
int[] depth = kinect.getRawDepth();
for (int x = 0; x < kinect.width; x++) {
for (int y = 0; y < kinect.height; y++) {
int offset = x + y * kinect.width;
int d = depth[offset];
if (d>minThresh && d<maxThresh) {
img.pixels[offset] = color(0);
sumX += x;
sumY += y;
totalPixels ++;
} else {
img.pixels[offset] = color(0);
}
}
img.updatePixels();
avgX = sumX/ totalPixels;
avgY = sumY/ totalPixels;
avgX = 2.25 *avgX;
avgY = 2.25 *avgY;
fill(255);
}
image(img, 0, 0);
// draw loop for arduino
pushMatrix();
fill(255);
translate(width/2, height/2);
ellipse(0,0,190,190);
fill(255);
noStroke();
line(0, 0, a.x, a.y);
line(0, 0, b.x, b.y);
b.set(avgX-width/2, avgY-height/2, 0);
//float calculatedAngle = angle(a, b);
float ang = atan2(b.y, b.x) - atan2(a.y, a.x);
if (ang < 0) ang += TWO_PI;
text(int(degrees(ang)), 0, 0);
q = degrees(ang);
myPort.write(int(q)/4);
popMatrix();
translate(width/2, height/2);
rotate(radians(q+90));
len = sq(avgX-(width/2)) + sq(avgY-(height/2));
root = sqrt(len);
image(shadow, -120, 0, 240, 1.2 * root);
println(frameRate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment