25.2.09

it's all coming together

in a way.

I've used everything i've learnt about processing in the past few months to make this code. It imports text from a document (will be replaced with a RSS feed later), splits it into words and looks for a keyword. It also tracks the reddest pixel from a video feed. Once it finds the keyword (in this case, sunny) it looks for the reddest pixel and plonks a big bright sun on the screen.


yes, i'm watching scrubs.

here's the code:


//mushing lasts projects code into this projects colour recognition code to create a program that
//tells you whether it's sunny or not when it sees red.
//background turns yellow

import processing.video.*;
import processing.candy.*;
import processing.xml.*;

Capture video;
SVG yellowsun;

color trackColor;

String[] tokens; //array
int counter;

void setup() {
size(640,480);
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);

smooth();
yellowsun = new SVG(this, "yellowsun.svg");

String[] lines = loadStrings("feed.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
}

void draw() {

if (video.available()) {
video.read();
}

String s = tokens[counter];
counter = (counter +1) % tokens.length;

video.loadPixels();
image(video,0,0);

float worldRecord = 500;

int closestX = 0;
int closestY = 0;

for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);

float d = dist(r1,g1,b1,r2,g2,b2);

if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}

if (worldRecord < 100) {

fill(trackColor);

if (s.equals("sunny")) {
println("sunny");
yellowsun.draw(closestX-100, closestY-100, 100, 100);

} else {
println("not");
}
}
}

No comments: