Path: blob/master/modules/python/test/tests_common.py
16337 views
#!/usr/bin/env python12from __future__ import print_function34import os5import sys6import unittest7import hashlib8import random9import argparse1011import numpy as np12import cv2 as cv1314# Python 3 moved urlopen to urllib.requests15try:16from urllib.request import urlopen17except ImportError:18from urllib import urlopen1920class NewOpenCVTests(unittest.TestCase):2122# path to local repository folder containing 'samples' folder23repoPath = None24extraTestDataPath = None25# github repository url26repoUrl = 'https://raw.github.com/opencv/opencv/master'2728def find_file(self, filename, searchPaths=[], required=True):29searchPaths = searchPaths if searchPaths else [self.repoPath, self.extraTestDataPath]30for path in searchPaths:31if path is not None:32candidate = path + '/' + filename33if os.path.isfile(candidate):34return candidate35if required:36self.fail('File ' + filename + ' not found')37return None383940def get_sample(self, filename, iscolor = None):41if iscolor is None:42iscolor = cv.IMREAD_COLOR43if not filename in self.image_cache:44filepath = self.find_file(filename)45with open(filepath, 'rb') as f:46filedata = f.read()47self.image_cache[filename] = cv.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor)48return self.image_cache[filename]4950def setUp(self):51cv.setRNGSeed(10)52self.image_cache = {}5354def hashimg(self, im):55""" Compute a hash for an image, useful for image comparisons """56return hashlib.md5(im.tostring()).hexdigest()5758if sys.version_info[:2] == (2, 6):59def assertLess(self, a, b, msg=None):60if not a < b:61self.fail('%s not less than %s' % (repr(a), repr(b)))6263def assertLessEqual(self, a, b, msg=None):64if not a <= b:65self.fail('%s not less than or equal to %s' % (repr(a), repr(b)))6667def assertGreater(self, a, b, msg=None):68if not a > b:69self.fail('%s not greater than %s' % (repr(a), repr(b)))7071@staticmethod72def bootstrap():73parser = argparse.ArgumentParser(description='run OpenCV python tests')74parser.add_argument('--repo', help='use sample image files from local git repository (path to folder), '75'if not set, samples will be downloaded from github.com')76parser.add_argument('--data', help='<not used> use data files from local folder (path to folder), '77'if not set, data files will be downloaded from docs.opencv.org')78args, other = parser.parse_known_args()79print("Testing OpenCV", cv.__version__)80print("Local repo path:", args.repo)81NewOpenCVTests.repoPath = args.repo82try:83NewOpenCVTests.extraTestDataPath = os.environ['OPENCV_TEST_DATA_PATH']84except KeyError:85print('Missing opencv extra repository. Some of tests may fail.')86random.seed(0)87unit_argv = [sys.argv[0]] + other88unittest.main(argv=unit_argv)899091def intersectionRate(s1, s2):9293x1, y1, x2, y2 = s194s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])9596x1, y1, x2, y2 = s297s2 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])9899area, _intersection = cv.intersectConvexConvex(s1, s2)100return 2 * area / (cv.contourArea(s1) + cv.contourArea(s2))101102def isPointInRect(p, rect):103if rect[0] <= p[0] and rect[1] <=p[1] and p[0] <= rect[2] and p[1] <= rect[3]:104return True105else:106return False107108109