Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created October 8, 2012 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/3850251 to your computer and use it in GitHub Desktop.
Save atduskgreg/3850251 to your computer and use it in GitHub Desktop.
Sketch for capturing sample images for machine learning image recognition applications.
/*
This sketch uses the processing video library to capture a series
of still images meant to be used as training images for various
machine learning techniques.
Instructions:
- Change the 'fileprefix' variable to match the object you
are capturing.
(this will make it easy for you to extract the images later with split())
- Start the sketch.
- Hold one of your objects so it is visible inside of the red box.
- Hit the spacebar to capture an image;
- Repeat with the object at different distances and positions.
- Quit the sketch and change the fileprefix variable based on your next object
- Repeat the capturing steps for all of your objects.
*/
import processing.video.*;
Capture video;
int rectW = 300;
int rectH = 300;
PImage croppedImage, currentImage;
int n = 0;
// CHANGEME:
String fileprefix = "myThing";
void setup() {
size(640, 480);
video = new Capture(this, 640, 480);
video.start();
croppedImage = createImage(rectW, rectH, RGB);
currentImage = createImage(width, height, RGB);
}
void captureEvent(Capture c) {
c.read();
}
void draw() {
image(video, 0, 0);
noFill();
stroke(255, 0, 0);
strokeWeight(5);
rect(width - rectW - (width - rectW)/2, height - rectH - (height - rectH)/2, rectW, rectH);
}
void keyPressed() {
currentImage.pixels = video.pixels;
currentImage.updatePixels();
croppedImage.copy(currentImage, width - rectW - (width - rectW)/2 + 3, height - rectH - (height - rectH)/2 + 3, rectW - 6, rectH - 6, 0, 0, rectW, rectH);
croppedImage.updatePixels();
croppedImage.save(fileprefix+"-"+n+".png");
n++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment