0

Using the Python 2.7 bindings for OpenCV / SimpleCV I have written some code which trains an SVM classifier on three classes.

I am using the HueHistogramFeatureExtractor(), EdgeHistogramFeatureExtractor(), and HaarLikeFeatureExtractor() extraction functions to train.

I put all of those extractors into my SVM classifier as so:

hhfe = HueHistogramFeatureExtractor(10)
ehfe = EdgeHistogramFeatureExtractor(10)
haarfe = HaarLikeFeatureExtractor(fname="haar.txt")  
extractors = [hhfe,ehfe,haarfe]
svm = SVMClassifier(extractors)

Then I train using my datasets and classes.

trainPaths = ['./data/train/bike','./data/train/plane','../data/train/car']
classes = ['bike','plane','car'] 
print svm.train(trainPaths,classes,verbose=False)  

All of this works and is reasonably accurate. But I am having to re-train my classifier everytime I want to re-run the code.

The output I get when I run this code (remember: verbose is turned off.) is this:

[100.0, 0.0, [[17.0, 0.0, 0.0], [0.0, 17.0, 0.0], [0.0, 0.0, 0.0]]]

feature5x1_11 (<17.000, 17.000, 0.000>) 
: <=2194135.000 --> plane (<0.000, 15.000, 0.000>) 

: >2194135.000 
   Angle_feature5x3_8 (<17.000, 2.000, 0.000>) 
   : <=2937533.000 --> bike (<14.000, 0.000, 0.000>) 

   : >2937533.000 --> bike (<3.000, 2.000, 0.000>) 
[94.11764705882352, 5.88235294117647, [[17.0, 0.0, 0.0], [2.0, 15.0, 0.0], [0.0, 0.0, 0.0]]]

How do I save the classifier I have trained so I can use it later?

JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
Blade Nelson
  • 41
  • 1
  • 2
  • 10

1 Answers1

3

I may be wrong here as I know C++ and not Python, but looking at this it should just be along the lines of:

To save:

svm.train(trainPaths,classes,verbose=False)
svm.save("your_svm.xml")

To re-use later:

svm.load("your_svm.xml")
svm.predict(...)
MLMLTL
  • 1,519
  • 5
  • 21
  • 35
  • I will try this. Saving seems to be working but how do I pass .predict() an image? (Even if you can give me a C++ example, I _should_ be able to adapt it.) – Blade Nelson Jun 17 '15 at 19:53
  • You don't pass svm.predict() an _image_, you extract the same N hue, edge and haar features from the image, store them in an array, and pass it to predict. – Nicholas McCarthy Jun 17 '15 at 20:05
  • [C++ SVM Model Code](https://github.com/bkornel/OpenCV_BOW_SVM/blob/master/main.cpp) – MLMLTL Jun 18 '15 at 09:32