// Daniel Shiffman // The Nature of Code // http://www.shiffman.net/teaching/nature // Using a Neural Network to recognize pixels of an input image // Neural network code is all in the "code" folder import processing.video.Capture; import nn.*; import java.io.File; /////camera Capture video;// regular processing libary int threshold = 130; int minY; PImage backgroundImage; boolean lookForChange = false; int y; int x; boolean startGame = false; boolean startRecord = false; ////////// // The pixel data from each image file will be our training set ArrayList pixelData; // The size of our image data int w = 0; int h = 0; // Here are the training images // The Neural Network Network nn; // How many training cycles have we done int trainingCount; // We will train 5,000 iterations // We will train 5,000 iterations // A submit button Button submit; Button record; // The points a user has drawn ArrayList points; int maxTrainingIterations = 0; // The input image from the user PImage inputImage; // Some booleans to keep track of what is going on boolean welcome = true; boolean training = false; boolean trainingComplete = false; boolean guessing = false; PFont f; int total = 0; PImage[] trainingImages; // What is the neural network's guess float guess = 0; void setup() { size(640,480); background(255); points = new ArrayList(); w = width/100; h = height/100; File folder = new File(sketchPath("numbers")); File[] filelist = folder.listFiles(); println(filelist.length); total = filelist.length; trainingImages = new PImage[total]; maxTrainingIterations = 15000*total; // Load all the images loadTrainingImages(); // Setup network nn = new Network(w*h,16); f = createFont("Georgia",16,true); // Make a blank input image inputImage = createImage(w,h,RGB); // Submit button submit = new Button(100,440,160,25, "submit"); record = new Button(270, 440, 160, 25, "record"); //////camera video = new Capture(this, 640, 480); backgroundImage = new PImage(video.width, video.height, RGB); /////////// } void draw() { background(255); if (video.available()) { // background(0); video.read(); minY = height; loadPixels(); for (int row = 0; row < video.height; row++) { for (int col = 0; col < video.width; col++) { int offset = row * width + col; int fgColor = video.pixels[offset]; int bgColor = backgroundImage.pixels[offset]; float fgR = red(fgColor); float fgG = green(fgColor); float fgB = blue(fgColor); float bgR = red(bgColor); float bgG = green(bgColor); float bgB = blue(bgColor); float diff = dist(fgR, fgG, fgB, bgR, bgG, bgB); if (diff > threshold) { // pixels[offset] = fgColor; y = offset/width; } if(minY> y){ minY = y; //println(minY); x = offset%width; } } } //if (lookForChange) //grabBackground(); updatePixels(); ellipse(x,minY,10,10); } drawing(); } void grabBackground(){ backgroundImage.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); backgroundImage.updatePixels(); } void drawing(){ // Welcome screen if (welcome) { welcome(); // Training screen } else if (training) { if (!trainingComplete) { train(); } showTraining(); // Guessing screen } else { userDraw(); guess(); } // Draw the guessing image and training images //noFill(); //stroke(0); // strokeWeight(1); // image(inputImage,0,0); // rect(0,0,w,h); //for (int i = 0; i < trainingImages.length; i++) { //image(trainingImages[i],(i+1)*w,0); // rect((i+1)*w,0,w,h); //} }