Skip navigation

chair-classification

Above is an image of my Exercise 4.

==============================================================

Description:

This Processing applet was made to explore how machine learning can be used to classify chairs. The applet classifies images from a database I made of chairs designed by Verner Panton, Charles and Ray Eames, Le Corbusier, Pierre Jeanneret, Charlotte Perriand, Harry Bertoia, and Eero Saarinen. When the applet is ran, it chooses nine chair images and tries to classify them by their designers. The number on the top left of each image represents the actual designer of the chair and the number on the top right of each image represents the applet’s guess for the the designer of the chair. The applet learns from a database of chairs and their designers prior to choosing the nine to guess, and the applet’s guess is displayed in red if it is incorrect, and is displayed in green if it is correct. The designers that the numbers represent are as follows:

1:  Bertoia

2:  Eames

3:  Panton

4:  Saarinen

5:  Le Corbusier, Jeanneret, and Perriand

==============================================================

Code:

import wekaizing.*;
import java.io.File;
import java.lang.Integer;
class digitImage {
int number;
PImage digit;
int[] pixeldata;
public digitImage() {
pixeldata = new int[101];}
}
WekaData digitsTrain;
WekaData digitsTest;
WekaClassifier classifier;
digitImage[] digits;
int[] results;
PFont HNL_font;
void setup() {
background(0);
size(680,680);
HNL_font = loadFont(“HelveticaNeue-Light-100.vlw”);
textFont(HNL_font, 15);
digitsTrain = new WekaData();
digitsTest = new WekaData();
for (int i = 0; i < 100; i++) {
digitsTrain.AddAttribute(Integer.toString(i));
digitsTest.AddAttribute(Integer.toString(i));
}
Object[] digitarray = new Object[] {0,1,2,3,4,5,6,7,8,9};
digitsTrain.AddAttribute(“digit”,digitarray);
digitsTest.AddAttribute(“digit”,digitarray);
loadDigits(“digits”);
digitsTrain.setClassIndex(100);
digitsTest.setClassIndex(100);
classifier = new WekaClassifier(WekaClassifier.LOGISTIC);
classifier.Build(digitsTrain);
print(“Training done”);
results = classifier.Classify(digitsTest);
print(“Classification done”);
drawResults();
}
void loadDigits(String digitfolder) {
File digitfiles = new File(sketchPath, “data/” + digitfolder);
String[] files = digitfiles.list(filter);
digits = new digitImage[files.length];
String numbers[] = loadStrings(digitfolder + “/digits.txt”);
for (int i = 0; i < files.length; i++) {
println(“Loading image ” + files[i]);
digits[i] = new digitImage();
digits[i].digit = loadImage(“data/” + digitfolder + “/” + files[i]);
digits[i].number = Integer.valueOf(numbers[i]);
PImage resizedImg = loadImage(“data/” + digitfolder + “/” + files[i]);
resizedImg.resize(10,10);
resizedImg.loadPixels();
for (int j = 0; j < 100; j++) {
digits[i].pixeldata[j] = resizedImg.pixels[j];
}
digits[i].pixeldata[100] = digits[i].number;
if (i < 40) {
digitsTest.InsertData(digits[i].pixeldata);
} else {
digitsTrain.InsertData(digits[i].pixeldata);
}
}
}
void drawResults() {
float num_correct = 0.0, total = 0.0;
int imgx, imgy;
for (int i = 0; i < 12; i++) {
imgx = (i % 3) * 220 + 20;
imgy = (i / 3) * 220 + 20;
image(digits[i].digit,imgx,imgy);
}
for (int i = 0; i < 9; i++) {
imgx = (i % 3) * 220 + 25;
imgy = (i / 3) * 220 + 35;
fill(0);
text(digits[i].number, imgx, imgy);
if(digits[i].number == results[i]){
fill(0,255,0);
}
else{
fill(255,0,0);
}
text(results[i], imgx + 180, imgy);
total += 1.0;
if(digits[i].number == results[i])
num_correct += 1.0;
}
println(“\n” + “Accuracy = ” + num_correct/total*40 + “%”);
}
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith(“.png”) || name.toLowerCase().endsWith(“.jpg”) || name.toLowerCase().endsWith(“.gif”)) return true;
return false;
}
};

import wekaizing.*;

import java.io.File;

import java.lang.Integer;

class digitImage {

int number;

PImage digit;

int[] pixeldata;

public digitImage() {

pixeldata = new int[101];}

}

WekaData digitsTrain;

WekaData digitsTest;

WekaClassifier classifier;

digitImage[] digits;

int[] results;

PFont HNL_font;

void setup() {

background(0);

size(680,680);

HNL_font = loadFont(“HelveticaNeue-Light-100.vlw”);

textFont(HNL_font, 15);

digitsTrain = new WekaData();

digitsTest = new WekaData();

for (int i = 0; i < 100; i++) {

digitsTrain.AddAttribute(Integer.toString(i));

digitsTest.AddAttribute(Integer.toString(i));

}

Object[] digitarray = new Object[] {0,1,2,3,4,5,6,7,8,9};

digitsTrain.AddAttribute(“digit”,digitarray);

digitsTest.AddAttribute(“digit”,digitarray);

loadDigits(“digits”);

digitsTrain.setClassIndex(100);

digitsTest.setClassIndex(100);

classifier = new WekaClassifier(WekaClassifier.LOGISTIC);

classifier.Build(digitsTrain);

print(“Training done”);

results = classifier.Classify(digitsTest);

print(“Classification done”);

drawResults();

}

void loadDigits(String digitfolder) {

File digitfiles = new File(sketchPath, “data/” + digitfolder);

String[] files = digitfiles.list(filter);

digits = new digitImage[files.length];

String numbers[] = loadStrings(digitfolder + “/digits.txt”);

for (int i = 0; i < files.length; i++) {

println(“Loading image ” + files[i]);

digits[i] = new digitImage();

digits[i].digit = loadImage(“data/” + digitfolder + “/” + files[i]);

digits[i].number = Integer.valueOf(numbers[i]);

PImage resizedImg = loadImage(“data/” + digitfolder + “/” + files[i]);

resizedImg.resize(10,10);

resizedImg.loadPixels();

for (int j = 0; j < 100; j++) {

digits[i].pixeldata[j] = resizedImg.pixels[j];

}

digits[i].pixeldata[100] = digits[i].number;

if (i < 40) {

digitsTest.InsertData(digits[i].pixeldata);

} else {

digitsTrain.InsertData(digits[i].pixeldata);

}

}

}

void drawResults() {

float num_correct = 0.0, total = 0.0;

int imgx, imgy;

for (int i = 0; i < 12; i++) {

imgx = (i % 3) * 220 + 20;

imgy = (i / 3) * 220 + 20;

image(digits[i].digit,imgx,imgy);

}

for (int i = 0; i < 9; i++) {

imgx = (i % 3) * 220 + 25;

imgy = (i / 3) * 220 + 35;

fill(0);

text(digits[i].number, imgx, imgy);

if(digits[i].number == results[i]){

fill(0,255,0);

}

else{

fill(255,0,0);

}

text(results[i], imgx + 180, imgy);

total += 1.0;

if(digits[i].number == results[i])

num_correct += 1.0;

}

println(“\n” + “Accuracy = ” + num_correct/total*40 + “%”);

}

FilenameFilter filter = new FilenameFilter() {

public boolean accept(File dir, String name) {

if (name.toLowerCase().endsWith(“.png”) || name.toLowerCase().endsWith(“.jpg”) || name.toLowerCase().endsWith(“.gif”)) return true;

return false;

}

};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: