From 84379140ba9de88fcbd081936c2289351bead13d Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Wed, 3 Dec 2014 18:12:01 +0300 Subject: [PATCH] Python test: added options to provide local data path --- modules/python/test/test.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/modules/python/test/test.py b/modules/python/test/test.py index cb923ce68b..a0f0daa56c 100644 --- a/modules/python/test/test.py +++ b/modules/python/test/test.py @@ -15,6 +15,7 @@ import operator import functools import numpy as np import cv2 +import argparse # Python 3 moved urlopen to urllib.requests try: @@ -24,9 +25,21 @@ except ImportError: class NewOpenCVTests(unittest.TestCase): + # path to local repository folder containing 'samples' folder + repoPath = None + # github repository url + repoUrl = 'https://raw.github.com/Itseez/opencv/master' + def get_sample(self, filename, iscolor = cv2.IMREAD_COLOR): if not filename in self.image_cache: - filedata = urlopen("https://raw.github.com/Itseez/opencv/master/" + filename).read() + filedata = None + if NewOpenCVTests.repoPath is not None: + candidate = NewOpenCVTests.repoPath + '/' + filename + if os.path.isfile(candidate): + with open(candidate, 'rb') as f: + filedata = f.read() + if filedata is None: + filedata = urlopen(NewOpenCVTests.repoUrl + '/' + filename).read() self.image_cache[filename] = cv2.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor) return self.image_cache[filename] @@ -133,6 +146,15 @@ class Hackathon244Tests(NewOpenCVTests): self.assertLessEqual(abs(mr - mr0), 5) if __name__ == '__main__': + parser = argparse.ArgumentParser(description='run OpenCV python tests') + parser.add_argument('--repo', help='use sample image files from local git repository (path to folder), ' + 'if not set, samples will be downloaded from github.com') + parser.add_argument('--data', help=' use data files from local folder (path to folder), ' + 'if not set, data files will be downloaded from docs.opencv.org') + args, other = parser.parse_known_args() print("Testing OpenCV", cv2.__version__) + print("Local repo path:", args.repo) + NewOpenCVTests.repoPath = args.repo random.seed(0) - unittest.main() + unit_argv = [sys.argv[0]] + other; + unittest.main(argv=unit_argv)