Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you probably did not try hard enough, it's possible with opencv >= 3 ;)

given a pretrained xml (5 features, 5 data points):

>>> xml = """
<?xml version="1.0"?>
<opencv_storage>
<opencv_ml_svm>
  <format>3</format>
  <svmType>C_SVC</svmType>
  <kernel>
    <type>RBF</type>
    <gamma>1.</gamma></kernel>
  <C>1.</C>
  <term_criteria><epsilon>1.1920928955078125e-07</epsilon>
    <iterations>1000</iterations></term_criteria>
  <var_count>5</var_count>
  <class_count>5</class_count>
  <class_labels type_id="opencv-matrix">
    <rows>5</rows>
    <cols>1</cols>
    <dt>i</dt>
    <data>
      1 2 3 4 5</data></class_labels>
  <sv_total>5</sv_total>
  <support_vectors>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_></support_vectors>
  <decision_functions>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 1</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 2</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 2</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        2 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        2 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        3 4</index></_></decision_functions></opencv_ml_svm>
</opencv_storage>
"""
>>> fs = cv2.FileStorage(f, cv2.FileStorage_READ | cv2.FileStorage_MEMORY)
>>> n = fs.getFirstTopLevelNode()
>>> n.name()
'opencv_ml_svm'
>>> svm2 = cv2.ml.SVM_create()
>>> svm2.read(n)
>>> svm2.isTrained()
True

you probably did not try hard enough, it's possible with opencv >= 3 ;)

given a pretrained xml (5 features, 5 data points):

>>> xml = """
<?xml version="1.0"?>
<opencv_storage>
<opencv_ml_svm>
  <format>3</format>
  <svmType>C_SVC</svmType>
  <kernel>
    <type>RBF</type>
    <gamma>1.</gamma></kernel>
  <C>1.</C>
  <term_criteria><epsilon>1.1920928955078125e-07</epsilon>
    <iterations>1000</iterations></term_criteria>
  <var_count>5</var_count>
  <class_count>5</class_count>
  <class_labels type_id="opencv-matrix">
    <rows>5</rows>
    <cols>1</cols>
    <dt>i</dt>
    <data>
      1 2 3 4 5</data></class_labels>
  <sv_total>5</sv_total>
  <support_vectors>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_>
    <_>
      1. 1. 1. 1. 1.</_></support_vectors>
  <decision_functions>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 1</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 2</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        0 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 2</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        1 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        2 3</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        2 4</index></_>
    <_>
      <sv_count>2</sv_count>
      <rho>0.</rho>
      <alpha>
        1. -1.</alpha>
      <index>
        3 4</index></_></decision_functions></opencv_ml_svm>
</opencv_storage>
"""
>>> fs = cv2.FileStorage(f, cv2.FileStorage(xml, cv2.FileStorage_READ | cv2.FileStorage_MEMORY)
>>> n = fs.getFirstTopLevelNode()
>>> n.name()
'opencv_ml_svm'
>>> svm2 = cv2.ml.SVM_create()
>>> svm2.read(n)
>>> svm2.isTrained()
True