Ask Your Question

Revision history [back]

What about this:

import numpy
import cv2

points = np.array([[1.0, 2.1], [1, -1], [2, 3], [2, 1]], dtype=np.float32)
labels = np.array([0, 1, 0, 1], dtype=np.float32)

# Train the SVM:
model = cv2.SVM(points, labels)

# Store it by using OpenCV functions:
model.save("/path/to/model.xml")

# Now create a new SVM & load the model:
model2 = cv2.SVM()
model2.load("/path/to/model.xml")

# Predict with model2:
model2.predict(np.array([[1.0, 2.1]], dtype=np.float32))

What about this:

import numpy
numpy as np
import cv2

points = np.array([[1.0, 2.1], [1, -1], [2, 3], [2, 1]], dtype=np.float32)
labels = np.array([0, 1, 0, 1], dtype=np.float32)

# Train the SVM:
model = cv2.SVM(points, labels)

# Store it by using OpenCV functions:
model.save("/path/to/model.xml")

# Now create a new SVM & load the model:
model2 = cv2.SVM()
model2.load("/path/to/model.xml")

# Predict with model2:
model2.predict(np.array([[1.0, 2.1]], dtype=np.float32))