Skip to content

Instantly share code, notes, and snippets.

@bilalsavas
Last active August 29, 2015 14:02
Show Gist options
  • Save bilalsavas/6306622cc8092ea53235 to your computer and use it in GitHub Desktop.
Save bilalsavas/6306622cc8092ea53235 to your computer and use it in GitHub Desktop.
import processing.video.*;
import processing.serial.*;
Serial myPort;
Capture video;
boolean[][] laserArray = new boolean[640][360];
void setup() {
colorMode(HSB);
frameRate(30);
size(640, 360);
myPort = new Serial(this, Serial.list()[2], 9600);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
video = new Capture(this, cameras[1]);
video.start();
}
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
laserArray[x][y]=false;
}
}
}
long pabsX=0, pabsY=0;
boolean currentPixel;
int dontrun=0;
void draw() {
colorMode(HSB, 360, 100, 100);
if (video.available() == true) {
video.read();
}
set(0, 0, video);
int num=0;
long absX=0, absY=0;
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
color c = get(x, y);
float curhue = abs(128-((hue(c) + 128) % 255));
float curbri = brightness(c);
currentPixel=(curhue < 30 && curbri > 95) ? true : false;
laserArray[x][y]=currentPixel;
if (currentPixel)
{
num++;
absX+=x;
absY+=y;
colorMode(RGB);
set(x, y, color(0, 255, 0));
}
}
}
// laserarray check
if (num!=0)
{
absX=absX/num;
absY=absY/num;
}
//println(absX + " " +absY);
// 30 fps = 1 sn
if (dontrun>15)
{
if ((abs(pabsX-absX) + abs(pabsY-absY)) > 10)
{
println("KEDİ DETECTED!");
myPort.write("A");
dontrun=0;
}
}
dontrun++;
pabsX=absX;
pabsY=absY;
fill(255, 0, 0);
ellipse(absX, absY, 10, 10);
}
void checkSerial()
{
while (myPort.available() > 0) {
String inBuffer = myPort.readString();
if (inBuffer != null) {
println(inBuffer);
}
}
}
void mousePressed()
{
color c = get(mouseX, mouseY);
println(hue(c)+"-"+saturation(c)+"-"+brightness(c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment