mirror of
https://github.com/opencv/opencv.git
synced 2025-06-20 10:00:51 +08:00
Add Ptr KNearest::load and python binding
This commit is contained in:
parent
c9a76ede94
commit
621e3eaed8
@ -505,6 +505,14 @@ public:
|
|||||||
The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
|
The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
|
||||||
*/
|
*/
|
||||||
CV_WRAP static Ptr<KNearest> create();
|
CV_WRAP static Ptr<KNearest> create();
|
||||||
|
/** @brief Loads and creates a serialized knearest from a file
|
||||||
|
*
|
||||||
|
* Use KNearest::save to serialize and store an KNearest to disk.
|
||||||
|
* Load the KNearest from this file again, by calling this function with the path to the file.
|
||||||
|
*
|
||||||
|
* @param filepath path to serialized KNearest
|
||||||
|
*/
|
||||||
|
CV_WRAP static Ptr<KNearest> load(const String& filepath);
|
||||||
};
|
};
|
||||||
|
|
||||||
/****************************************************************************************\
|
/****************************************************************************************\
|
||||||
|
13
modules/ml/misc/python/test/test_knearest.py
Normal file
13
modules/ml/misc/python/test/test_knearest.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import cv2 as cv
|
||||||
|
|
||||||
|
from tests_common import NewOpenCVTests
|
||||||
|
|
||||||
|
class knearest_test(NewOpenCVTests):
|
||||||
|
def test_load(self):
|
||||||
|
k_nearest = cv.ml.KNearest_load(self.find_file("ml/opencv_ml_knn.xml"))
|
||||||
|
self.assertFalse(k_nearest.empty())
|
||||||
|
self.assertTrue(k_nearest.isTrained())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
NewOpenCVTests.bootstrap()
|
@ -515,6 +515,17 @@ Ptr<KNearest> KNearest::create()
|
|||||||
return makePtr<KNearestImpl>();
|
return makePtr<KNearestImpl>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ptr<KNearest> KNearest::load(const String& filepath)
|
||||||
|
{
|
||||||
|
FileStorage fs;
|
||||||
|
fs.open(filepath, FileStorage::READ);
|
||||||
|
|
||||||
|
Ptr<KNearest> knearest = makePtr<KNearestImpl>();
|
||||||
|
|
||||||
|
((KNearestImpl*)knearest.get())->read(fs.getFirstTopLevelNode());
|
||||||
|
return knearest;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user