Hi guys, recently i download library by Greg Borenstein from his website which was PSVM and i tried to look at his example but his code hasn’t finished yet, and i got Syntax Error - Missing operator after return histogram.; can anybody figure it out what is missing? here’s the code
import psvm.*;
SVM model;
// declare Histogram object
Histogram histogram;
// "bin" the histogram into 32 separate
// color groups
int numBins = 32;
PImage testImage;
double testResult;
int[] labels; // 1 = squirrel, 2 = bird
float[][] trainingData;
String[] testFilenames;
void setup() {
size(600, 600);
textSize(32);
// get the names of all of the image files in the "squirrel" folder
java.io.File squirrelFolder = new java.io.File(dataPath("squirrels"));
String[] squirrelFilenames = squirrelFolder.list();
// get the names of all of the image files in the "birds" folder
java.io.File birdFolder = new java.io.File(dataPath("birds"));
String[] birdFilenames = birdFolder.list();
// initialize labels and trainingData arrays
// one label for each training image
labels = new int[squirrelFilenames.length + birdFilenames.length];
// one vector for each training image
// each vector has 3 entries for each bin
// one each for R, G, and B
trainingData = new float[labels.length][numBins*3];
// create the histogram object and tell it how many
// bins we want
histogram = new Histogram();
histogram.setNumBins(numBins);
// build vectors for each squirrel image
// and add the appropriate label
for (int i = 0; i < squirrelFilenames.length; i++) {
println("loading squirrel " + i);
trainingData[i] = buildVector(loadImage("squirrels/" + squirrelFilenames[i]));
labels[i] = 1;
}
// build vectors for each bird image
// and add the appropriate label
for (int i = 0; i < birdFilenames.length; i++) {
println("loading bird " + i);
trainingData[i + squirrelFilenames.length] = buildVector(loadImage("birds/" + birdFilenames[i]));
labels[i+ squirrelFilenames.length] = 2;
}
// setup our SVM model
model = new SVM(this);
SVMProblem problem = new SVMProblem();
// each vector will have three features for each bin
// in our histogram: one each for red, green, and blue
// components
problem.setNumFeatures(numBins*3);
// set the data and train the SVM
problem.setSampleData(labels, trainingData);
model.train(problem);
// load in the test images
java.io.File testFolder = new java.io.File(dataPath("test"));
testFilenames = testFolder.list();
// run the evaluation (see function for more)
evaluateResults();
// loads and tests a new image from the test folder
loadNewTestImage();
}
// This function calculates the histogram for a PImage
// and scales it so that the sum of each RGB entry is 1
// the results are used as the feature vector for SVM
float[] buildVector(PImage img) {
histogram.setImage(img);
histogram.calculateHistogram();
histogram.scale(0, 0.33);
return histogram.;
}
void draw() {
background(0);
pushMatrix();
scale(0.5);
image(testImage, 0, 60);
popMatrix();
// testResult is set in loadNewTestImage()
String message = "";
if((int)testResult == 1){
message = "Squirrel";
}
else if((int)testResult == 2){
message = "Bird";
}
fill(255);
text(message, 10, 25);
text("Percent classified correctly: " + nf(correctPercentage * 100,2,2), 20, height -40);
stroke(255);
histogram.drawRGB(0, height/2, width, height/2);
}
// Loads up a new random image.
// Runs in through the SVM for classification.
// Stores the results in the testResult variable.
void loadNewTestImage(){
int imgNum = (int)random(0, testFilenames.length-1);
testImage = loadImage("test/" + testFilenames[imgNum]);
testResult = model.test(buildVector(testImage));
}
void keyPressed(){
loadNewTestImage();
}
float correctPercentage = 0.0;
void evaluateResults(){
int numCorrect = 0;
PImage img;
for (int i = 0; i < testFilenames.length; i++) {
img = loadImage("test/" + testFilenames[i]);
double r = model.test(buildVector(img));
if(r == 1.0 && split(testFilenames[i], "-")[0].equals("Squirrel")){
numCorrect++;
}
if(r == 2.0 && split(testFilenames[i], "-")[0].equals("Bird")){
numCorrect++;
}
}
correctPercentage = (float)numCorrect/testFilenames.length;
println("Num Bins: " + numBins + " Percent Correct: " + correctPercentage);
}
class Histogram {
int numBins;
int[] pixels;
boolean calculated;
int min, max;
Histogram() {
this.numBins = 256;
this.calculated = false;
this.min = 0;
this.max = 255;
}
void setNumBins(int numBins) {
this.numBins = numBins;
}
void setImage(PImage img) {
img.loadPixels();
this.pixels = img.pixels;
calculated = false;
}
void setPixels(int[] pix) {
this.pixels = pix;
calculated = false;
}
float[] rHist;
float[] bHist;
float[] gHist;
float[] hHist;
float[] sHist;
float[] vHist;
int binIndexForValue(int value) {
return value/binSize();
}
void calculateHistogram() {
rHist = new float[numBins];
gHist = new float[numBins];
bHist = new float[numBins];
hHist = new float[numBins];
sHist = new float[numBins];
vHist = new float[numBins];
for (int i = 0; i < this.pixels.length; i++) {
int rBright = int(red(this.pixels[i]));
int gBright = int(green(this.pixels[i]));
int bBright = int(blue(this.pixels[i]));
int hBright = int(hue(this.pixels[i]));
int sBright = int(saturation(this.pixels[i]));
int vBright = int(brightness(this.pixels[i]));
rHist[binIndexForValue(rBright)]++;
gHist[binIndexForValue(gBright)]++;
bHist[binIndexForValue(bBright)]++;
hHist[binIndexForValue(hBright)]++;
sHist[binIndexForValue(sBright)]++;
vHist[binIndexForValue(vBright)]++;
}
}
void scale(float min, float max) {
for (int i = 0; i < numBins; i++) {
rHist[i] = map(rHist[i], 0, this.pixels.length, min, max);
gHist[i] = map(gHist[i], 0, this.pixels.length, min, max);
bHist[i] = map(bHist[i], 0, this.pixels.length, min, max);
hHist[i] = map(hHist[i], 0, this.pixels.length, min, max);
sHist[i] = map(sHist[i], 0, this.pixels.length, min, max);
vHist[i] = map(vHist[i], 0, this.pixels.length, min, max);
}
}
void setRange(int min, int max) {
this.min = min;
this.max = max;
}
int binSize() {
return ceil((float)(max - min) / numBins);
}
float[] getHSV() {
float[] result = new float[numBins * 3];
for (int i = 0; i < numBins; i++) {
result[i] = hHist[i];
result[i+numBins] = sHist[i];
result[i+(2*numBins)] = vHist[i];
}
return result;
}
float[] getRGB() {
float[] result = new float[numBins * 3];
for (int i = 0; i < numBins; i++) {
result[i] = rHist[i];
result[i+numBins] = gHist[i];
result[i+(2*numBins)] = bHist[i];
}
return result;
}
void drawRGB(int x, int y, int w, int h) {
// int histMax = max(histData);
fill(255, 0, 0);
drawChannel(rHist, x, y, w, h/3);
fill(0, 255, 0);
drawChannel(gHist, x, y+h/3, w, h/3);
fill(0, 0, 255);
drawChannel(bHist, x, y+(2*h)/3, w, h/3);
}
void drawChannel(float[] data, int x, int y, int w, int h) {
int binWidth = w/numBins;
for (int i = 0; i < numBins; i++) {
float mappedH = map(data[i], 0, 0.33, 0, h);
rect((i*binWidth), y, w / numBins, -mappedH);
}
}
}