Changes

Jump to: navigation, search

Midterm Project - Tony Lu

7,722 bytes removed, 20:16, 19 May 2010
Documentation
-adapts OpenCV's face tracking code and creates the maze (composed of Cell objects)
import hypermedia.video.*; import ddf.minim.*; OpenCV opencv; // contrast/brightness values int contrast_value = 0;int brightness_value = 0;int cellsize = 100;int cellnum = 42;boolean start = true;int currentcell = 0;int in_cell = 0;boolean move = false;Cell[] cells = new Cell[cellnum];PImage pacman;PImage pacmanAlpha;AudioPlayer player;Minim minim;int playercounter = 0; void setup() {  size( 700, 600 ); minim = new Minim(this); player = minim.loadFile("StartGame.mp3"); player.loop(); pacman = loadImage("pacman.png"); pacmanAlpha = loadImage("pacmanalpha3.png");  opencv = new OpenCV( this ); opencv.capture( width, height ); // open video stream opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection Media: "haarcascade_frontalface_altPhysical_pacman.xml" int k = -cellsize; //int[] temp = { -1, -2, -3, -4, -5 }; for(int i = 0; i < cellnum; i++) { if(i%(width/cellsize) == 0) { k+= cellsize; } cells[ipde] = new Cell(i, (i%(width/cellsize))*cellsize, k, cellsize); } setupAllNeighbors(); // print usage println( "Drag mouse on X-axis inside this sketch window to change contrast" ); println( "Drag mouse on Y-axis inside this sketch window to change brightness" ); }  public void stop() { opencv.stop(); super.stop();}   void draw() { // grab a new frame // and convert to gray opencv.read(); opencv.flip( OpenCV.FLIP_HORIZONTAL ); //opencv.convert( GRAY ); opencv.contrast( contrast_value ); opencv.brightness( brightness_value );  // proceed detection Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );  // display the image image( opencv.image(), 0, 0 ); noStroke(); fill(255); // rect(0, 0, width, height); boolean r; drawMaze(); if(start == false) { cells[currentcell].cellOn(true); } for(int l = 0; l<cellnum; l++) { r = false; if(cells[l].stepped == true) { r = true; } cells[l].circleOn(r); }   // draw face area(s)  for( int i=0; i<faces.length; i++ ) { noFill(); stroke(255,0,0); strokeWeight(5); //rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height ); noStroke(); fill(0, 0, 200, 80); in_cell = findClosestCell(faces[i].x + (faces[i].width)/2, faces[i].y); //ellipse(faces[i].x + (faces[i].width)/2, faces[i].y + (faces[i].height)/2, 100, 100); //ellipse(faces[i].x + (faces[i].width)/2, faces[i].y, 100, 100); //ellipse(cells[in_cell].x, cells[in_cell].y, 100, 100); if(start == true) { cells[in_cell].cellOn(); currentcell = in_cell; start = false; } for(int j = 0; j < 4; j++) { if(cells[currentcell].neighbors[j] == in_cell) { move = true; } } if(move == true) { cells[in_cell].cellOn(); currentcell = in_cell; } move = false; }  }     /** * Changes contrast/brigthness values */void mouseDragged() { contrast_value = (int) map( mouseX, 0, width, -128, 128 ); brightness_value = (int) map( mouseY, 0, width, -128, 128 );} int findClosestCell(int face_x, int face_y) { int cellx; int celly; int xhundred = (face_x / 100) * 100; int xten = face_x % 100; int yhundred = (face_y / 100) * 100; int yten = face_y % 100; if(xten > 85) { if(xhundred < 600) { cellx = xhundred + 100; } else { cellx = xhundred; } } else if(xten < 15) { if(xhundred > 100) { cellx = xhundred - 100; } else { cellx = xhundred; } } else { cellx = xhundred; } if(yten > 85) { if(yhundred < 500) { celly = yhundred + 100; } else { celly = yhundred; } } else if(yten < 15) { if(yhundred > 100) { celly = yhundred - 100; } else { celly = yhundred; } } else { celly = yhundred; } return (cellx/100) + (celly/100)*(width/cellsize);}   void setupNeighbors(int cell, int[] arrayOfNeighbors) { //cell neighbor set up //cells[0].neighbors[0] = 7; cells[cell].neighbors = arrayOfNeighbors; } void setupAllNeighbors() { int[] cell0 = { 7, 1, 0, -1 }; int[] cell1 = { 0, 1, 8, -1 }; int[] cell2 = { 2, 3, -1, -2 }; int[] cell3 = { 2, 3, 10, -1 }; int[] cell4 = { 4, 11, 5, -1 }; int[] cell5 = { 5, 4, 6, -2 }; int[] cell6 = { 5, 6, 13, -2 }; int[] cell7 = { 7, 0, 14, -1 }; int[] cell8 = { 8, 1, 15, -2 }; int[] cell9 = { 9, 16, 10, -2 }; int[] cell10 = { 3, 10, 17, 9 }; int[] cell11 = { 4, 11, 12, -2 }; int[] cell12 = { 12, 11, 19, -2 }; int[] cell13 = { 13, 20, 6, -2 }; int[] cell14 = { 14, 7, 21, -2 }; int[] cell15 = { 15, 16, 8, -2 }; int[] cell16 = { 16, 15, 9, -2 }; int[] cell17 = { 17, 10, 24, -2 }; int[] cell18 = { 18, 19, 25, -2 }; int[] cell19 = { 19, 12, 18, -1 }; int[] cell20 = { 20, 13, -2, -1 }; int[] cell21 = { 21, 22, 14, -2 }; int[] cell22 = { 22, 23, 21, -2 }; int[] cell23 = { 23, 22, 24, 30 }; int[] cell24 = { 24, 31, 17, 23 }; int[] cell25 = { 25, 32, 18, 26 }; int[] cell26 = { 26, 27, 25, -2 }; int[] cell27 = { 27, 26, 34, -2 }; int[] cell28 = { 28, 29, 35, -1 }; int[] cell29 = { 29, 30, 28, -2 }; int[] cell30 = { 30, 23, 29, -1 }; int[] cell31 = { 31, 32, 24, -2 }; int[] cell32 = { 31, 32, 33, 25 }; int[] cell33 = { 33, 32, 40, -1 }; int[] cell34 = { 34, 41, 27, -2 }; int[] cell35 = { 35, 36, 28, -2 }; int[] cell36 = { 36, 35, 37, -2 }; int[] cell37 = { 37, 36, -1, -2 }; int[] cell38 = { 38, 39, -2, -1 }; int[] cell39 = { 39, 40, 38, -2 }; int[] cell40 = { 40, 39, 33, -2 }; int[] cell41 = { 41, 34, -2, -1 }; setupNeighbors(0, cell0); setupNeighbors(1, cell1); setupNeighbors(2, cell2); setupNeighbors(3, cell3); setupNeighbors(4, cell4); setupNeighbors(5, cell5); setupNeighbors(6, cell6); setupNeighbors(7, cell7); setupNeighbors(8, cell8); setupNeighbors(9, cell9); setupNeighbors(10, cell10); setupNeighbors(11, cell11); setupNeighbors(12, cell12); setupNeighbors(13, cell13); setupNeighbors(14, cell14); setupNeighbors(15, cell15); setupNeighbors(16, cell16); setupNeighbors(17, cell17); setupNeighbors(18, cell18); setupNeighbors(19, cell19); setupNeighbors(20, cell20); setupNeighbors(21, cell21); setupNeighbors(22, cell22); setupNeighbors(23, cell23); setupNeighbors(24, cell24); setupNeighbors(25, cell25); setupNeighbors(26, cell26); setupNeighbors(27, cell27); setupNeighbors(28, cell28); setupNeighbors(29, cell29); setupNeighbors(30, cell30); setupNeighbors(31, cell31); setupNeighbors(32, cell32); setupNeighbors(33, cell33); setupNeighbors(34, cell34); setupNeighbors(35, cell35); setupNeighbors(36, cell36); setupNeighbors(37, cell37); setupNeighbors(38, cell38); setupNeighbors(39, cell39); setupNeighbors(40, cell40); setupNeighbors(41, cell41);} void drawMaze() { stroke(0, 0, 255); strokeWeight(4); line(0, 0, 0, height); line(0, 0, width, 0); line(width, 0, width, height); line(0, height, width, height); line(100, 100, 100, 300); line(100, 300, 300, 300); line(300, 300, 300, 200); line(200, 0, 200, 200); line(200, 100, 300, 100); line(0, 400, 200, 400); line(400, 0, 400, 400); line(100, 500, 500, 500); line(300, 400, 300, height); line(500, 400, 600, 400); line(400, 200, 500, 200); line(500, 300, width, 300); line(500, 100, 600, 100); line(600, 100, 600,300); line(600, 400, 600, height);}
52
edits

Navigation menu