Skip to content

Instantly share code, notes, and snippets.

@GalNissim
Last active August 22, 2016 21:06
Show Gist options
  • Select an option

  • Save GalNissim/164fc35b29208e70da6d to your computer and use it in GitHub Desktop.

Select an option

Save GalNissim/164fc35b29208e70da6d to your computer and use it in GitHub Desktop.
SurveillAnts code
import codeanticode.syphon.*; //<>// //<>// //<>// //<>// //<>// //<>// //<>//
//import java.awt.Color;
class Ant {
// Assign a number to each ant
int m_id;
ArrayList<Point> m_positions;
int[] m_col;
int m_alpha;
int m_strokeWid;
float[] m_col_past;
//constructor
Ant (int id) {
m_id = id;
m_positions = new ArrayList<Point>();
m_col = new int[] {int(random(255)), int(random(255)), int(random(255))};//TO DO: CHANGE
m_alpha = 255;
m_strokeWid = 1;
m_col_past = new float[] {m_col[0], m_col[1], m_col[2]};
}
//add to the positions' list new possition
void addPos(float newPos_x, float newPos_y, long t ) {
Point p = new Point(newPos_x, newPos_y, t);
m_positions.add(p);
}
//returns the dist between the given pos to the Ant's last pos
double lastDist (float[] pos) {
// if there is no last position
if (m_positions.isEmpty()) {
return -1;
}
// the Ant's last position
Point last = m_positions.get(m_positions.size() - 1);
//print("last"+m_id+ ":");
//println(last);
//println(pos);
return dist(last.m_x, last.m_y, pos[0], pos[1]);
}
void drawPath() {
// the for loop draw a line between two adjacent points, therefore the number
//of the loop's iterations will be the number of spaces
pushStyle();
canvas.stroke(m_col[0], m_col[1], m_col[2]);
canvas.strokeWeight(1);
for (int i = 0; i<m_positions.size ()-1; i++) {
canvas.line(m_positions.get(i).m_x, m_positions.get(i).m_y,
m_positions.get(i+1).m_x, m_positions.get(i+1).m_y);
}
// draw the Ant's ID
//canvas.fill(0);
//canvas.text(""+m_id, m_positions.get(m_positions.size ()-1).m_x, m_positions.get(m_positions.size ()-1).m_y);
popStyle();
}
/////copy
void pastStyle(int alpha, int strokeWid, float col) {
m_alpha = alpha;
m_strokeWid = strokeWid;
m_col_past[0] = m_col[0]-col;
m_col_past[1] = m_col[1]+col;
m_col_past[2] = m_col[2]+col/2;
}
void drawPath2(long endTime) {
// the for loop draw a line between two adjacent points, therefore the number
//of the loop's iterations will be the number of spaces
pushStyle();
canvas.stroke(m_col_past[0], m_col_past[1], m_col_past[2], m_alpha);
canvas.strokeWeight(m_strokeWid);
//canvas.strokeWeight(create a mapped variable here);
for (int i = 0; i<m_positions.size ()-1; i++) {
if (m_positions.get(i).m_time > endTime) {
break;
}
if (dist(m_positions.get(i).m_x, m_positions.get(i).m_y,
m_positions.get(i+1).m_x, m_positions.get(i+1).m_y)>70) {
// println(m_id);
break;
}
canvas.line(m_positions.get(i).m_x, m_positions.get(i).m_y,
m_positions.get(i+1).m_x, m_positions.get(i+1).m_y);
}
}
}
class Point {
float m_x;
float m_y;
long m_time;
//constructor
Point(float x, float y, long time) {
m_time = time;
m_x = x;
m_y = y;
}
}
import processing.video.*; //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>//
import codeanticode.syphon.*;
import blobDetection.*;
import java.lang.System;
import java.util.*;
import java.nio.*;
import processing.serial.*;
// This is part of using MadMapper in Processing
PGraphics canvas;
SyphonServer server;
Capture cam;
//This is the blob detection libarary
// - Super Fast Blur v1.1 by Mario Klingemann <http://incubator.quasimondo.com>
// - BlobDetection library
PImage img, def_img;
BlobDetection theBlobDetection;
//defines a size so that we stop detecting shadows and grains...sort of
float min_blobSize = 0.015;
float max_blobSize = 20;
//in order to save between one call to draw to another.
ArrayList<Ant> currAnts, preAnts;
//draw between these points when if the distance is <= offset
int offset = 45;
// captured image size
int wid = 1280;
int hig = 960;
boolean newFrame=false;
// cropping parameters for our source image
int x_crop_left = 105;//300;
int x_crop_right = 410;//550;// 330;
int y_crop_up = 150;//58;
int y_crop_down = 10;//80
//table number
Table table;
int currTableNum = 0;
String nowTable;
long[] today_times, yesterday_times, last_week_times;
int timeY, timeM, timeD, timeH, timeMN, timeS;
//creat a map for past ant ??
HashMap<Integer, Ant> antsPastListToday = new HashMap<Integer, Ant>(),
antsPastListYesterday = new HashMap<Integer, Ant>(), antsPastListLastWeek = new HashMap<Integer, Ant>();
//////////////////////////
//Initialize Serial
Serial myPort;
//int inByte
String myString;
int lf = 10; // ASCII linefeed
int numSensors = 6; //we will be expecting
int sensors[] = new int[numSensors]; // array to read the 3 sensor values
int pSensors[] = new int[numSensors]; // array to store the previous reading and use to compare actual reading with the last one
float turnSpeed;
//boolean activateButton1 = false;
//boolean activateButton2 = false;
//boolean activateButton3 = false;
//May Need to reevaluate this code.
int t_n;
int speed;
boolean todayButton = false;
boolean yesterdayButton = false;
boolean lastWeekButton = false;
int today_time_speed = -5000, yesterday_time_speed = -5000, last_week_time_speed = -5000;
boolean today_pressed = false, yesterday_pressed = false, last_week_pressed = false;
int alpha;
int strokeWid;
float col;
///// Press Enter to toggle back and forth between the projector view and camera view
boolean canvasToggle = false;
/////Syphon Settings
void settings() {
size(640, 480, P3D);
PJOGL.profile = 1;
}
////Processing SetUp
void setup() {
// Size of applet
canvas = createGraphics(640, 480, P3D);
server = new SyphonServer (this, "Processing Syphon");
//choose camera
String[] cameras = Capture.list();
if (cameras.length == 0) {
//println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
printArray(cameras);//print cameras
}
// Capture
cam = new Capture(this, wid, hig, cameras[0], 30); //change "cameras[15]" if you are
//not connected to an external webcam
cam.start();
// BlobDetection
// img which will be sent to detection (a smaller copy of the cam frame)
img = new PImage(wid - x_crop_left-x_crop_right, hig-y_crop_up-y_crop_down);
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(true);
theBlobDetection.setThreshold(0.55f); // will detect bright areas whose luminosity > 0.2f;
//Create a table with the following columns
table = new Table();
table.addColumn("id");
table.addColumn("x");
table.addColumn("y");
table.addColumn("time");
// Name the table correctly by getting the time for the table file name
timeY = year();
timeM = month();
timeD = day();
timeH = hour();
timeMN = minute();
timeS = second();
//Find The Serial Port
printArray(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil(lf);
// end of Setup
// loading maps
today_times = new long[3];
yesterday_times = new long[3];
last_week_times = new long[3];
myLoadTable("00_AntTable_5minDraw.csv", antsPastListToday, today_times);
myLoadTable("01_AntTable_10minDraw.csv", antsPastListYesterday, yesterday_times);
myLoadTable("02_AntTable_23minDraw.csv", antsPastListLastWeek, last_week_times);
}
// ==================================================
// captureEvent()
// ==================================================
void captureEvent(Capture cam)
{
cam.read();
newFrame = true;
}
void draw()
{
canvas.beginDraw();
if (newFrame)
{
newFrame=false;
canvas.background(0);
img.copy(cam, x_crop_left, y_crop_up, img.width, img.height, 0, 0, img.width, img.height);
//Change the threshold to finnesse the brightess/contrast
img.filter(THRESHOLD, 0.35);
fastblur(img, 1);
//img.filter(DILATE);
//To see what the camera sees, uncomment this line.
if (canvasToggle) canvas.image(img, 0, 0, width, height);
theBlobDetection.computeBlobs(img.pixels);
drawBlobsAndEdges(true, true);//finding the ant
}
canvas.endDraw();
image(canvas, 0, 0);
server.sendImage(canvas);
// ============================
/// New SERIAL Sensor Read
// ============================
/// Test if the webcam feed is slow
//text( frameRate, 10, 20);
// End Draw Loop
}
void serialEvent (Serial myPort) {
// read the serial buffer:
myString = myPort.readStringUntil(lf);
// if there are any bytes other than the lf:
if (myString != null) {
myString = trim(myString);
//splits the string at the commas and convert them to integers:
pSensors = sensors;
sensors = int(split(myString, ','));
// print out the values:
//for (int sensorNum = 0; sensorNum<sensors.length; sensorNum++) {
// print("Sensor" + sensorNum + ":" + sensors[sensorNum] + "\t");
//}
////Add a linefeed after all the sensor balues are printed:
//println();
}
}
// ==================================================
// drawBlobsAndEdges()
// ==================================================k
void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges)
{
noFill();
Blob b;
//initiat an array - size 2
currAnts = new ArrayList<Ant>();
int maxId = 0;
double antDistance;
Ant a ;
//constrain the number of ants drawn at once so the code will not crash
int total = theBlobDetection.getBlobNb ();
total = constrain(total, 0, 50);
for (int n=0; n<total; n++) {
float[] cen = new float[2];
a = new Ant(-1);
int antID = -1; // is there a new ant?
// the current Ant's blob
b=theBlobDetection.getBlob(n);
// The Ant's current possition (blobs' centers)
cen[0] = b.x*width;
cen[1] = b.y*height;
float blobSize = 1/b.w *1/b.h/1000;
//println(blobSize);
if (max_blobSize>blobSize) {
//calculate the shortest dist
if (preAnts!=null ) {
double minDistance = width*height;
// iterate over all the Ants from the previos frame and find the min dist
for (int i =0; i<preAnts.size (); i++) {
if (maxId<preAnts.get(i).m_id) {
maxId = preAnts.get(i).m_id;
}
//save the distance between current Ant's position
//and all the previous positions of the Ants from the previous frame
antDistance = preAnts.get(i).lastDist(cen);
//find the min distance and refer the ant's correct id
if (antDistance <minDistance && antDistance< offset) {
minDistance = antDistance;
antID = preAnts.get(i).m_id;
a = preAnts.get(i);
}
}
}
if (antID ==-1) {
maxId++;
a = new Ant(maxId);
}
long t = System.currentTimeMillis();
a.addPos(cen[0], cen[1], t);//add new ant's location
//Ants' track
a.drawPath();
currAnts.add(a);
//save data from current frame
TableRow newRow = table.addRow();
newRow.setInt("id", a.m_id);
newRow.setFloat("x", cen[0]);
newRow.setFloat("y", cen[1]);
newRow.setLong("time", t);
if (frameCount % 60 == 0) {
saveTable(table, "data/"+timeY+"_"+timeM+"_"+timeD+"_"+timeH+"_"+timeMN+"_"+timeS+".csv");
nowTable = "data/"+timeY+"_"+timeM+"_"+timeD+"_"+timeH+"_"+timeMN+"_"+timeS+".csv";
}
//End of Max BlobSize
}
//End For loop of "n" number of ants
}
//println(sensors);
//////////////
// Check if today's button was pressed
if (sensors[3]>0 && !today_pressed) {
today_pressed = true;
today_time_speed *= -1;
todayButton = true;
}
else if(sensors[3]==0){
today_pressed = false;
}
if (sensors[4]>0 && !yesterday_pressed) {
yesterday_pressed = true;
yesterday_time_speed *= -1;
yesterdayButton = true;
}
else if(sensors[4]==0){
yesterday_pressed = false;
}
if (sensors[5]>0 && !last_week_pressed) {
last_week_pressed = true;
last_week_time_speed *= -1;
lastWeekButton = true;
}
else if(sensors[5]==0){
last_week_pressed = false;
}
////////////////
//// Check if yesterday's button was pressed
//if (sensors[4]>0) {
// yesterday_time_speed *= -1;
// yesterdayButton = true;
//}
//// Check if last week's button was pressed
//if (sensors[5]>0) {
// last_week_time_speed *= -1;
// lastWeekButton = true;
//}
// Check if one of the knobs were changed
if (sensors[0] != pSensors[0] || sensors[1] != pSensors[1] || sensors[2] != pSensors[2]) {
println("here");
alpha = floor(map(sensors[0], 0, 1023, 50, 255));
strokeWid = floor(map(sensors[1], 0, 1023, 1, 30));
col = map(sensors[2], 0, 1023, 0, 255);
}
if (todayButton) {
// draw/erase today's history
if(today_times[2] < today_times[1] || today_time_speed < 0){
today_times[2] += today_time_speed;
}
if (today_times[2] <= today_times[0]) {
todayButton = false;
}
drawTime( antsPastListToday, today_times[2]);
}
if (yesterdayButton) {
// draw/erase yesterday's history
if(yesterday_times[2] < yesterday_times[1] || yesterday_time_speed < 0){
yesterday_times[2] += yesterday_time_speed;
}
if (yesterday_times[2] <= yesterday_times[0]) {
yesterdayButton = false;
}
drawTime( antsPastListYesterday, yesterday_times[2]);
}
if (lastWeekButton) {
// draw/erase last week's history
if(last_week_times[2] < last_week_times[1] || last_week_time_speed < 0){
last_week_times[2] += last_week_time_speed;
}
if (last_week_times[2] <= last_week_times[0]) {
lastWeekButton = false;
}
drawTime( antsPastListLastWeek, last_week_times[2]);
}
////draw ants from the past
//Draw Ants from Map1 Knob
//map1 = sensors[3];
//println(map1);
//if (map1) {
// if (activateButton1=false){
// drawTime();
// activateButton1 = true;
//} if (activateButton1=true) {
// antsPastList = new HashMap<Integer, Ant>();
// activateButton1 = false;
// } else if (map1 ==0) {
// antsPastList = new HashMap<Integer, Ant>();
//}
//end_time+= 1000;
//}
////Draw Ants from Map2 Knob
//map2 = sensors[4];
//println(map2);
//if (map2 != 0) {
// drawTime();
//} else if (map2==0) {
// antsPastList = new HashMap<Integer, Ant>();
//}
//end_time+= map2*10;
//}
////Draw Ants from Map3 Knob
//map3 = sensors[5];
//println(map3);
//if (map3 != 0) {
// if (map3<10) {
// drawTime();
// } else if (map3==0) {
// antsPastList = new HashMap<Integer, Ant>();
// }
// end_time+= map3*10;
// }
// draw all the current ants
preAnts = currAnts;
//draw ants from the past
//if (!antsPastList.isEmpty()) {
// Set<Integer> ids = antsPastList.keySet();
// for (Integer i : ids) {
// antsPastList.get(i).drawPath2(end_time);
// }
//}
long t_end = System.currentTimeMillis();
//End void drawBlobsAndEdges()
}
// ==================================================
// Super Fast Blur v1.1
// by Mario Klingemann
// <http://incubator.quasimondo.com>
// ==================================================
void fastblur(PImage img, int radius)
{
if (radius<1) {
return;
}
int w=img.width;
int h=img.height;
int wm=w-1;
int hm=h-1;
int wh=w*h;
int div=radius+radius+1;
int r[]=new int[wh];
int g[]=new int[wh];
int b[]=new int[wh];
int rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw;
int vmin[] = new int[max(w, h)];
int vmax[] = new int[max(w, h)];
int[] pix=img.pixels;
int dv[]=new int[256*div];
for (i=0; i<256*div; i++) {
dv[i]=(i/div);
}
yw=yi=0;
for (y=0; y<h; y++) {
rsum=gsum=bsum=0;
for (i=-radius; i<=radius; i++) {
p=pix[yi+min(wm, max(i, 0))];
rsum+=(p & 0xff0000)>>16;
gsum+=(p & 0x00ff00)>>8;
bsum+= p & 0x0000ff;
}
for (x=0; x<w; x++) {
r[yi]=dv[rsum];
g[yi]=dv[gsum];
b[yi]=dv[bsum];
if (y==0) {
vmin[x]=min(x+radius+1, wm);
vmax[x]=max(x-radius, 0);
}
p1=pix[yw+vmin[x]];
p2=pix[yw+vmax[x]];
rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
yi++;
}
yw+=w;
}
for (x=0; x<w; x++) {
rsum=gsum=bsum=0;
yp=-radius*w;
for (i=-radius; i<=radius; i++) {
yi=max(0, yp)+x;
rsum+=r[yi];
gsum+=g[yi];
bsum+=b[yi];
yp+=w;
}
yi=x;
for (y=0; y<h; y++) {
pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
if (x==0) {
vmin[y]=min(y+radius+1, hm)*w;
vmax[y]=max(y-radius, 0)*w;
}
p1=x+vmin[y];
p2=x+vmax[y];
rsum+=r[p1]-r[p2];
gsum+=g[p1]-g[p2];
bsum+=b[p1]-b[p2];
yi+=w;
}
}
// END OF MARIO's BLUR FUNCTION FOR THE CAMERA
}
//====== LET'S CALL THESE DAMN TABLES! ============
void keyPressed () {
if (keyCode == ENTER) {
canvasToggle = !canvasToggle;
}
}
int prevMode;
int mode = 0;
void drawTime (HashMap<Integer, Ant> antsList, long curr_time) {
Set<Integer> ids = antsList.keySet();
for (Integer i : ids) {
Ant ant = antsList.get(i);
ant.pastStyle(alpha, strokeWid, col);
ant.drawPath2(curr_time);
}
//antsPastList = new HashMap<Integer, Ant>();
////println(mode);
//switch(mode) {
//case 0:
// myLoadTable("new.csv");
// break;
//case 1:
// myLoadTable("00_AntTable_5minDraw.csv");
// break;
//case 2:
// myLoadTable("01_AntTable_10minDraw.csv");
// break;
//}
//if (mode != prevMode) {
// if (mode == 0) {
// drawRealtime();
// } else if (mode == 1) {
// drawSlow(table1);
// } else if (mode == 2) {
// drawFast(table1);
// } else if (mode == 3) {
// drawSlow(table2);
// } else if (mode == 4) {
// drawFast(table2);
// }
//}
//switch( mode ) {
//case 0:
// drawRealtime();
// break;
//case 1:
// drawSlow(table1);
// break;
//case 2:
// drawFast(table1);
// break;
//case 3:
// drawSlow(table2);
// break;
//case 4:
// drawFast(table2);
// break;
//}
//prevMode = mode;
}
void myLoadTable(String tableName, HashMap<Integer, Ant> antList, long[] times) {
Table table_past = loadTable(tableName, "header");//TO DO - PUT CHANGING NAME TO THE TABLE
int id_past_a;
//get the first time value in the table
times[0] = table_past.getLong(0, 3);
times[1] = table_past.getLong(table_past.getRowCount()-1, 3);
times[2] = table_past.getLong(0, 3);
for (int i = 0; i<table_past.getRowCount(); i++) {
id_past_a = table_past.getInt(i, 0);
if (!antList.containsKey(id_past_a)) {
antList.put(id_past_a, new Ant(id_past_a));
}
// remove empty cells
if (table_past.getString(i, 0) != null &&
table_past.getString(i, 1) != null &&
table_past.getString(i, 2) != null &&
table_past.getString(i, 3) != null) {
antList.get(id_past_a).addPos(table_past.getFloat(i, 1),
table_past.getFloat(i, 2), table_past.getLong(i, 3));//column numbers:
//println(antsPastList.get(id_past_a).m_positions.get(i).m_y);
} else {
println("Found Empty Cell");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment