WEKA Segmentation issue with Python

Hi everyone!

I’m fairly new to the trainable WEKA segmentation plugin, and currently in the process of creating a Python (Jython) script in order to apply a classifier to all images in a folder based off of the existing Beanshell script here. However, whenever I run this Python script within the FIJI Script Editor, I end up just getting a folder of all the input images used to apply my classifier model to. I don’t believe it’s an issue with the classifier model itself because that works when applied within the Beanshell script.

Would anybody help point me in the right direction and help me see if I’m doing something incorrectly in the script? I figured that I shouldn’t need to re-train the classifier model as I’m only looking to apply an already existing one to a set of images. Thanks!

image

import os
from trainableSegmentation import WekaSegmentation, Weka_Segmentation, utils
from ij import IJ, ImagePlus

weka = WekaSegmentation()
weka.loadClassifier(r'C:\Users\angu312\Scripts\VectorClassifier.model')
 
def run():
	srcDir = r'C:\Users\angu312\Test\Pre-Processed'
	dstDir = r'C:\Users\angu312\Test\Post-Processed'
	for root, directories, filenames in os.walk(srcDir):
		filenames.sort();
		for filename in filenames:
      		# Check for file extension
			if not filename.endswith('.jpg'):
        			continue
			process(srcDir, dstDir, root, filename)
 
def process(srcDir, dstDir, currentDir, fileName):
	print "Processing:"

	# Opening the image
	print "Open image file", fileName
	image = IJ.openImage(os.path.join(currentDir, fileName))

	# apply classifier and get results
	result = weka.applyClassifier(image, 0, False)
	# assign same LUT as in GUI
	result.setLut(utils.Utils.getGoldenAngleLUT())

	# Saving the image as a .tif file
	saveDir = dstDir
	if not os.path.exists(saveDir):
		os.makedirs(saveDir)
	print "Saving to", saveDir
	IJ.saveAs(image, "Tif", os.path.join(saveDir, fileName))
	image.close()
 
run()

For anyone browsing these forums in the future, I figured out the issue. In the last few lines of the “process” function, I was saving the original image into the destination folder instead of the segmented image into that folder - a small mistake I overlooked! I also manually set the LUT that was used in the GUI. See full script below:

import os
from trainableSegmentation import WekaSegmentation, Weka_Segmentation
from ij import IJ, ImagePlus
from ij.plugin import LutLoader
 
def run():
	srcDir = r'C:\Users\angu312\Test\2. Pre Processed'
	dstDir = r'C:\Users\angu312\Test\3. WEKA Processed'
	for root, directories, filenames in os.walk(srcDir):
		filenames.sort();
		for filename in filenames:
      		# Check for file extension
			if not filename.endswith('.jpg'):
        			continue
			process(srcDir, dstDir, root, filename)
 
def process(srcDir, dstDir, currentDir, fileName):
	print "Processing:"

	# Opening the image
	print "Open image file", fileName
	image = IJ.openImage(os.path.join(srcDir, fileName))

	weka = WekaSegmentation()
	weka.setTrainingImage(image)

	# loads manually trained classifier
	weka.loadClassifier(r'C:\Users\angu312\Documents\VectorClassifier.model')
	# apply classifier and get results
	segmented_image = weka.applyClassifier(image, 0, False)
	# assign same LUT as in GUI. Within WEKA GUI, right-click on classified image and use Command Finder to save the "LUT" within Fiji.app\luts
	lut = LutLoader.openLut(r'C:\Users\angu312\Documents\Fiji.app\luts\Classification result.lut')
	segmented_image.getProcessor().setLut(lut)

	# Saving the image as a .tif file
	saveDir = dstDir
	if not os.path.exists(saveDir):
		os.makedirs(saveDir)
	print "Saving to", saveDir
	IJ.saveAs(segmented_image, "Tif", os.path.join(saveDir, fileName))
	image.close()

run()