How to use Weka in a Python script?

Hello!
Could you please help me with Weka segmentation? I am writing a Python script with Weka and I am trying to use Weka API.
If I write …

CLpath = "...path to the .model file with the classifier"
imp = IJ.getImage()
weka = WekaSegmentation(imp)
weka.loadClassifier(CLpath)
weka.applyClassifier(0, False)
result = weka.getClassifiedImage()

it works normally. But I am trying to use an empty constructor, as shown in numerous examples:

CLpath = "...path to the .model file with the classifier"
imp = IJ.getImage()
weka = WekaSegmentation()
weka.loadClassifier(CLpath)
weka.applyClassifier(imp, 0, False)
result = weka.getClassifiedImage()

in this case I get an error:

*File "D:\OneDrive\Images\ScriptingWeka.py", line 19, in <module>*
*result.show()*
*AttributeError: 'NoneType' object has no attribute 'show'*

meaning that result is a NoneType.
What am I doing wrong?

Hello @DBagr and sorry for the delay on answering you!

I’ve never played with the TWS scripting from Python, but your code seems OK. Did the calls to loadClassifier and applyClassifier return any output?

Hello!
These both commands (loadClassifier and applyClassifier) invoked a series of messages in the Log window.
It seems that the first variant of the code leads to a normal result, and the second variant loads the classifier, applies it to the image and - for whatever reason - returns Null, NoneType or some other empty output.

Anyway, I managed to solve my problem using the first variant of the code, so I am OK :slight_smile:

1 Like