Merge pull request #19668 from asmorkalov:as/filesystem_py
authorAlexander Smorkalov <2536374+asmorkalov@users.noreply.github.com>
Thu, 4 Mar 2021 16:17:57 +0000 (19:17 +0300)
committerGitHub <noreply@github.com>
Thu, 4 Mar 2021 16:17:57 +0000 (16:17 +0000)
* Add Python Bindings for getCacheDirectory function

* Added getCacheDirectory interop test with image codecs.

Co-authored-by: Sergey Slashchinin <sergei.slashchinin@xperience.ai>
modules/core/include/opencv2/core/bindings_utils.hpp
modules/core/src/bindings_utils.cpp
modules/python/test/test_fs_cache_dir.py [new file with mode: 0644]

index 179d60323ff3e1320db44602c1a0da4a0c1692d5..96d5c771696a2fb734068d909b4fb4ee9b3da00f 100644 (file)
@@ -144,7 +144,10 @@ AsyncArray testAsyncException()
     return p.getArrayResult();
 }
 
+namespace fs {
+    CV_EXPORTS_W cv::String getCacheDirectoryForDownloads();
+} // namespace fs
 //! @}
-}} // namespace
+}} // namespaces cv /  utils
 
 #endif // OPENCV_CORE_BINDINGS_UTILS_HPP
index 050b7247f87fbc889d238da73ae3846c9d2218e9..78716c21f6cb9b1c7c7c3aa5aa0b2e924e94af6b 100644 (file)
@@ -5,6 +5,8 @@
 #include "precomp.hpp"
 #include "opencv2/core/bindings_utils.hpp"
 #include <sstream>
+#include <opencv2/core/utils/filesystem.hpp>
+#include <opencv2/core/utils/filesystem.private.hpp>
 
 namespace cv { namespace utils {
 
@@ -208,4 +210,15 @@ CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argume
     return ss.str();
 }
 
+namespace fs {
+cv::String getCacheDirectoryForDownloads()
+{
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
+    return cv::utils::fs::getCacheDirectory("downloads", "OPENCV_DOWNLOADS_CACHE_DIR");
+#else
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif
+}
+} // namespace fs
+
 }} // namespace
diff --git a/modules/python/test/test_fs_cache_dir.py b/modules/python/test/test_fs_cache_dir.py
new file mode 100644 (file)
index 0000000..6cb40b2
--- /dev/null
@@ -0,0 +1,41 @@
+# Python 2/3 compatibility
+from __future__ import print_function
+
+import numpy as np
+import cv2 as cv
+import os
+import datetime
+
+from tests_common import NewOpenCVTests
+
+class get_cache_dir_test(NewOpenCVTests):
+    def test_get_cache_dir(self):
+        #New binding
+        path = cv.utils.fs.getCacheDirectoryForDownloads()
+        self.assertTrue(os.path.exists(path))
+        self.assertTrue(os.path.isdir(path))
+
+    def get_cache_dir_imread_interop(self, ext):
+        path = cv.utils.fs.getCacheDirectoryForDownloads()
+        gold_image = np.ones((16, 16, 3), np.uint8)
+        read_from_file = np.zeros((16, 16, 3), np.uint8)
+        test_file_name = os.path.join(path, "test." + ext)
+        try:
+            cv.imwrite(test_file_name, gold_image)
+            read_from_file = cv.imread(test_file_name)
+        finally:
+            os.remove(test_file_name)
+
+        self.assertEqual(cv.norm(gold_image, read_from_file), 0)
+
+    def test_get_cache_dir_imread_interop_png(self):
+        self.get_cache_dir_imread_interop("png")
+
+    def test_get_cache_dir_imread_interop_jpeg(self):
+        self.get_cache_dir_imread_interop("jpg")
+
+    def test_get_cache_dir_imread_interop_tiff(self):
+        self.get_cache_dir_imread_interop("tif")
+
+if __name__ == '__main__':
+    NewOpenCVTests.bootstrap()