如何从 OpenCV 3.1 中的文件加载 SVM 数据? [英] How to load SVM data from file in OpenCV 3.1?
问题描述
我在从文件加载训练的 SVM 时遇到问题.我使用 Python 和 OpenCv 3.1.0.我通过以下方式创建 svm 对象:
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()
接下来,我训练 svm 并通过以下方式保存到文件:
Next, I train svm and save to file by:
svm.save('data.xml')
现在我想在其他 Python 脚本中加载这个文件.在文档中,我找不到任何方法来做到这一点.
Now i want to load this file in other Python script. In docs i can't find any methods to do it.
是否有从文件加载 svm 的技巧?感谢您的任何回复.
Is there a trick to load svm from file? Thanks for any responses.
推荐答案
我认为没有 svm.load(filepath) 方法作为 svm.save(filepath) 的对应方法有点令人困惑,但是当我阅读模块帮助我认为 SVM_load 是 cv2.ml 的子项(SVM_create 的兄弟).
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).
确保您的 opencv master 分支是最新的(当前版本为 3.1.0-dev)
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)
返回
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')
这篇关于如何从 OpenCV 3.1 中的文件加载 SVM 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!