0

I'm trying to build a new SimpleCV FeatureExtractor for openCV's Hough Circle Transform but I'm running into an error during my machine learning script's training phase.

I've provided the error below. It is raised by the Orange machine learning library when creating the self.mDataSetOrange variable within SimpleCV's TreeClassifier.py. The size of the dataset does not match Orange's expectation for some reason. I looked into Orange's source code and the found that error is thrown here:

orange/source/orange/cls_example.cpp

int const nvars = dom->variables->size() + dom->classVars->size();
if (Py_ssize_t(nvars) != PyList_Size(lst)) {
    PyErr_Format(PyExc_IndexError, "invalid list size (got %i, expected %i items)",
        PyList_Size(lst), nvars);
    return false;
}

Obviously, my feature extractor is not extracting the things as required by Orange but I can't pinpoint what the problem could be. I'm pretty new to SimpleCV and Orange so I'd be grateful if someone could point out any mistakes I'm making.

The error:

Traceback (most recent call last):
  File "MyClassifier.py", line 113, in <module>
    MyClassifier.run(MyClassifier.TRAIN_RUN_TYPE, trainingPaths)
  File "MyClassifier.py", line 39, in run
    self.decisionTree.train(imgPaths, MyClassifier.CLASSES, verbose=True)
  File "/usr/local/lib/python2.7/dist-packages/SimpleCV-1.3-py2.7.egg/SimpleCV/MachineLearning/TreeClassifier.py", line 282, in train
    self.mDataSetOrange = orange.ExampleTable(self.mOrangeDomain,self.mDataSetRaw)
IndexError: invalid list size (got 266, expected 263 items) (at example 2)

HoughTransformFeatureExtractor.py

class HoughTransformFeatureExtractor(FeatureExtractorBase):

    def extract(self, img):
        bitmap = img.getBitmap()
        cvMat = cv.GetMat(bitmap)
        cvImage = numpy.asarray(cvMat)

        height, width = cvImage.shape[:2]
        gray = cv2.cvtColor(cvImage, cv2.COLOR_BGR2GRAY)

        circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 2.0, width / 2)
        self.featuresLen = 0

        if circles is not None:
            circleFeatures = circles.ravel().tolist()
            self.featuresLen = len(circleFeatures)

            return circleFeatures
        else:
            return None

    def getFieldNames(self):
        retVal = []
        for i in range(self.featuresLen):
            name = "Hough"+str(i)
            retVal.append(name)
        return retVal

    def getNumFields(self):
        return self.featuresLen
Sid
  • 1,144
  • 10
  • 21

1 Answers1

0

So, I figured out my issue. Basically, the problem was with the size of list returned by the extract method. The size of the list varied for each processed image, which is what led to this error. So, here are some examples of the type of lists returned by the extract method:

3 -> [74.0, 46.0, 14.866068840026855]
3 -> [118.0, 20.0, 7.071067810058594]
6 -> [68.0, 8.0, 8.5440034866333, 116.0, 76.0, 13.03840446472168]
3 -> [72.0, 44.0, 8.602325439453125]
9 -> [106.0, 48.0, 15.81138801574707, 20.0, 52.0, 23.409399032592773, 90.0, 122.0, 18.0]

Once I made sure that the size of the list was consistent, no matter the image, the error went away. Hopefully, this will help anyone having similar issues in the future.

Sid
  • 1,144
  • 10
  • 21