Skip to content

Commit 2e42926

Browse files
Merge pull request opencv#19668 from asmorkalov:as/filesystem_py
* Add Python Bindings for getCacheDirectory function * Added getCacheDirectory interop test with image codecs. Co-authored-by: Sergey Slashchinin <sergei.slashchinin@xperience.ai>
1 parent e2ca50f commit 2e42926

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

modules/core/include/opencv2/core/bindings_utils.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ AsyncArray testAsyncException()
144144
return p.getArrayResult();
145145
}
146146

147+
namespace fs {
148+
CV_EXPORTS_W cv::String getCacheDirectoryForDownloads();
149+
} // namespace fs
147150
//! @}
148-
}} // namespace
151+
}} // namespaces cv / utils
149152

150153
#endif // OPENCV_CORE_BINDINGS_UTILS_HPP

modules/core/src/bindings_utils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "precomp.hpp"
66
#include "opencv2/core/bindings_utils.hpp"
77
#include <sstream>
8+
#include <opencv2/core/utils/filesystem.hpp>
9+
#include <opencv2/core/utils/filesystem.private.hpp>
810

911
namespace cv { namespace utils {
1012

@@ -208,4 +210,15 @@ CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argume
208210
return ss.str();
209211
}
210212

213+
namespace fs {
214+
cv::String getCacheDirectoryForDownloads()
215+
{
216+
#if OPENCV_HAVE_FILESYSTEM_SUPPORT
217+
return cv::utils::fs::getCacheDirectory("downloads", "OPENCV_DOWNLOADS_CACHE_DIR");
218+
#else
219+
CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
220+
#endif
221+
}
222+
} // namespace fs
223+
211224
}} // namespace
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Python 2/3 compatibility
2+
from __future__ import print_function
3+
4+
import numpy as np
5+
import cv2 as cv
6+
import os
7+
import datetime
8+
9+
from tests_common import NewOpenCVTests
10+
11+
class get_cache_dir_test(NewOpenCVTests):
12+
def test_get_cache_dir(self):
13+
#New binding
14+
path = cv.utils.fs.getCacheDirectoryForDownloads()
15+
self.assertTrue(os.path.exists(path))
16+
self.assertTrue(os.path.isdir(path))
17+
18+
def get_cache_dir_imread_interop(self, ext):
19+
path = cv.utils.fs.getCacheDirectoryForDownloads()
20+
gold_image = np.ones((16, 16, 3), np.uint8)
21+
read_from_file = np.zeros((16, 16, 3), np.uint8)
22+
test_file_name = os.path.join(path, "test." + ext)
23+
try:
24+
cv.imwrite(test_file_name, gold_image)
25+
read_from_file = cv.imread(test_file_name)
26+
finally:
27+
os.remove(test_file_name)
28+
29+
self.assertEqual(cv.norm(gold_image, read_from_file), 0)
30+
31+
def test_get_cache_dir_imread_interop_png(self):
32+
self.get_cache_dir_imread_interop("png")
33+
34+
def test_get_cache_dir_imread_interop_jpeg(self):
35+
self.get_cache_dir_imread_interop("jpg")
36+
37+
def test_get_cache_dir_imread_interop_tiff(self):
38+
self.get_cache_dir_imread_interop("tif")
39+
40+
if __name__ == '__main__':
41+
NewOpenCVTests.bootstrap()

0 commit comments

Comments
 (0)