//Includes video library to capture images from the webcam (in computer) import hypermedia.video.*; //Includes shape classes that keep track of face coordinates import java.awt.*; //Serial library to communicate with Arduino import processing.serial.*; import processing.video.*; //Open CV object OpenCV opencv; //Screen Size int width = 400; int height = 400; int threshold = 80; int firsttime = 0; int totalPixels; int cellSize = 25; int[] previousFrame; int[] differenceFrame; int cols; int rows; int movementSum = 0; //list of Face objects ArrayList faceList; //how many faces are found over time int faceCount = 0; boolean find = true; Serial myPort; void setup() { size(width, height); background(0); //initializes the OpenCV object opencv = new OpenCV(this); //opens video capture stream opencv.capture(width, height); totalPixels = opencv.width*opencv.height; previousFrame = new int[totalPixels]; differenceFrame = new int[totalPixels]; cols = width/cellSize; rows = height/cellSize; //load face detection (frontal faces) > "haarcascade_frontalface_alt.xml" opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT2); faceList = new ArrayList(); println(Serial.list()); //myPort = new Serial(this, Serial.list()[4], 9600); println("Press space bar to record background image"); println(cols); println(rows); } public void stop() { opencv.stop(); super.stop(); } void draw() { float corx, cory; //grabs frame from camera opencv.read(); // converts the difference image to grayscale opencv.convert( GRAY); image(opencv.image(OpenCV.GRAY), 0, 0); //proceed detection; canny pruning edge detector to reject some image regions that contain too much or too few edges //detect(scale, min_neighbors, flags, min_width, min_height); Rectangle [] faces = opencv.detect(1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 25, 25); //Scenario 1: faceList is empty if (faceList.isEmpty()) { // make a face object for every face rectangle for (int i = 0; i < faces.length; i++) { faceList.add(new Face(faces[i].x, faces[i].y, faces[i].width, faces[i].height)); } //Scenario 2: fewer Face objects than face Rectangles found from OpenCV } else if (faceList.size() <= faces.length) { boolean[] used = new boolean[faces.length]; // match existing face objects with rectangle for (Face f : faceList) { // find faces(index) closest to face f, and set used to true so it can't be used twice float record = 50000; int index = -1; for (int i = 0; i < faces.length; i++){ float d = dist(faces[i].x, faces[i].y, f.r.x, f.r.y); if (d < record && !used[i]) { record = d; index = i; } } // Update face object location used [index] = true; f.update(faces[index]); } //Scenario 3: more Face objects than face Rectangles found } else { // all face objects start out as available for (Face f : faceList) { f.available = true; } //match rectangle with face object for (int i = 0; i < faces.length; i++) { //find face object closest to faces [i] rectangle and set available to false float record = 50000; int index = -1; for (int j = 0; j < faceList.size(); j++) { Face f = faceList.get(j); float d = dist(faces[i].x, faces[i].y, f.r.x, f.r.y); if (d < record && f.available) { record = d; index = j; } } //Upate face object location Face f = faceList.get(index); f.available = false; f.update(faces[i]); } //kill any leftover Face objects for (Face f: faceList) { if (f.available) { f.countDown(); if (f.dead()) { f.delete = true; } } } //delete any faces that should be deleted for (int i = faceList.size() - 1; i >= 0; i--) { Face f = faceList.get(i); if (f.delete) { faceList.remove(i); } } //draw all the faces for (int i = 0; i < faces.length; i++) { noFill(); stroke(255, 0, 0); rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height); } //divideScreen(); } } void divideScreen() { opencv.image().loadPixels(); for (int g=0;g