WEKA Scipting – Batch Process, but only processing one image, instead of entire folder

Hi again,

I’ve been attempting to run a macro that WEKA classifies all images in a folder, and then saves them in another folder.

Currently, it’s classifying the first image in the folder, then stopping. I would like it to process them all.

/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "(/user/jacobi/desktop/folder/)", style = "directory") input
#@ File (label = "(/user/jacobi/desktop/untitled folder/)", style = "directory") output
#@ String (label = "File suffix", value = ".jpg") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder("/user/jacobi/desktop/folder/") {
	list = getFileList("/user/jacobi/desktop/folder/");
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory("/user/jacobi/desktop/folder/") + File.separator + list[i])
			processFolder("/user/jacobi/desktop/folder/") + File.separator + list[i]);
		if(endsWith(list[i], .jpg))
			processFile("/user/jacobi/desktop/folder/"), ("/user/jacobi/desktop/untitled folder/"), list[i]);
	}
}

function processFile("/user/jacobi/desktop/folder/"), ("/user/jacobi/desktop/untitled folder/") {
	run("Trainable Weka Segmentation", "open=/Users/JACOBI/Desktop/folder/");
	wait(3000)
	selectWindow("Trainable Weka Segmentation v3.2.34");
	call("trainableSegmentation.Weka_Segmentation.loadClassifier", "/Users/JACOBI/Desktop/PADRE.model"); 
	call("trainableSegmentation.Weka_Segmentation.getResult");
	selectWindow("Classified image");
	saveAs("Tiff", "/Users/JACOBI/Desktop/untitled folder/Classified image.tif");

This is my code! I’m at your mercy! Thanks so much

Which language is this in? From what I can tell, it looks like you might have some missing closing parenthesis. Could this be the issue?

Andrew, thanks for replying again.

This ProcessFile Script is from ImageJ, so I’m guessing this is the native macro language ImageJ uses. It’s doing exactly what I want, it’s just stopping at the first image within the folder and not doing anything else. Ack!

Hi @CharlesJacobi1,

As @Andrew_Shum said, your problem is missing a closing bracket. So you need to add a

}

at the very bottom of your script.

Hi!

I have added in the bracket, though the script will still only process the first file within the folder. I think I need to do something with the “list”? I’m so close to getting this script to work…

Hi @CharlesJacobi1

So first, just to let you know that from the batch script you don’t need to replace the input with your directory path each time - i.e. in line 12

list = getFileList(input);

Will work fine because the first couple of lines (the script parameter lines that start with #@) are defining a variable called input which is your path. So when “input” is used, ImageJ knows to go to that path. This is done in fact to save you from having to put the path in each time and to make the script generalisable, i.e. next time you would just select a new folder at the start and the script will work on that folder instead.

However, I don’t think changing that will solve the script problems. I have changed it to something that might work but I can’t test it right now so I suspect it might not get you all the way. You could also have a look at the templates on the Scripting the Trainable Weka Segmentation web page.


#@ File (label = "Input Directory", style = "directory") input
#@ File (label = "Output Directory", style = "directory") output
#@ File(label= "Weka model", description="Select the Weka model to apply") modelPath
#@ String (label = "File suffix", value = ".jpg") suffix

processFolder(input);

function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	open(input + File.separator + file);
	title = getTitle();
	run("Trainable Weka Segmentation");
	wait(2000);
	selectWindow("Trainable Weka Segmentation v3.2.33");
	call("trainableSegmentation.Weka_Segmentation.loadClassifier", modelPath);
	call("trainableSegmentation.Weka_Segmentation.applyClassifier", input,  list[i], "showResults=true", "storeResults=false", "probabilityMaps=true", "");
	wait(10000);
	selectWindow("Classification result");
	saveAs("Tiff", output + File.separator + title + "_Classified.tif");
	run("Close All");
}
1 Like

@imurphy

Thanks so much. I wish you could open Weka without selecting an image – than this would be much easier!