How to load SVM data from file in OpenCV 3.1?

 ̄綄美尐妖づ 提交于 2019-12-10 15:43:41

问题


I have a problem with load trained SVM from file. I use Python and OpenCv 3.1.0. I create svm object by:

svm = cv2.ml.SVM_create()

Next, I train svm and save to file by:

svm.save('data.xml')

Now i want to load this file in other Python script. In docs i can't find any methods to do it.

Is there a trick to load svm from file? Thanks for any responses.


回答1:


I think it's a litte bit confusing that there is no svm.load(filepath) method as a counterpart of svm.save(filepath), but when I read the module help it makes sense to me that SVM_load is a child of cv2.ml (sibling of SVM_create).

Be sure that your opencv master branch is up-to-date (currently version 3.1.0-dev)

>>> import cv2
>>> cv2.__version__
'3.1.0-dev'
>>> help(cv2.ml)

returns

SVM_create(...)
    SVM_create() -> retval

SVM_load(...)
    SVM_load(filepath) -> retval

so you can simply use something like:

if not os.path.isfile('svm.dat'):
    svm = cv2.ml.SVM_create()
    ...
    svm.save('svm.dat')
else:
    svm = cv2.ml.SVM_load('svm.dat')


来源:https://stackoverflow.com/questions/38182132/how-to-load-svm-data-from-file-in-opencv-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!