1#!/usr/bin/env python 2 3from __future__ import print_function 4 5import os 6import sys 7import unittest 8import hashlib 9import random 10import argparse 11 12import numpy as np 13import cv2 as cv 14 15# Python 3 moved urlopen to urllib.requests 16try: 17 from urllib.request import urlopen 18except ImportError: 19 from urllib import urlopen 20 21class NewOpenCVTests(unittest.TestCase): 22 23 # path to local repository folder containing 'samples' folder 24 repoPath = None 25 extraTestDataPath = None 26 # github repository url 27 repoUrl = 'https://raw.github.com/opencv/opencv/master' 28 29 def find_file(self, filename, searchPaths=[], required=True): 30 searchPaths = searchPaths if searchPaths else [self.repoPath, self.extraTestDataPath] 31 for path in searchPaths: 32 if path is not None: 33 candidate = path + '/' + filename 34 if os.path.isfile(candidate): 35 return candidate 36 if required: 37 self.fail('File ' + filename + ' not found') 38 return None 39 40 41 def get_sample(self, filename, iscolor = None): 42 if iscolor is None: 43 iscolor = cv.IMREAD_COLOR 44 if not filename in self.image_cache: 45 filepath = self.find_file(filename) 46 with open(filepath, 'rb') as f: 47 filedata = f.read() 48 self.image_cache[filename] = cv.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor) 49 return self.image_cache[filename] 50 51 def setUp(self): 52 cv.setRNGSeed(10) 53 self.image_cache = {} 54 55 def hashimg(self, im): 56 """ Compute a hash for an image, useful for image comparisons """ 57 return hashlib.md5(im.tostring()).hexdigest() 58 59 if sys.version_info[:2] == (2, 6): 60 def assertLess(self, a, b, msg=None): 61 if not a < b: 62 self.fail('%s not less than %s' % (repr(a), repr(b))) 63 64 def assertLessEqual(self, a, b, msg=None): 65 if not a <= b: 66 self.fail('%s not less than or equal to %s' % (repr(a), repr(b))) 67 68 def assertGreater(self, a, b, msg=None): 69 if not a > b: 70 self.fail('%s not greater than %s' % (repr(a), repr(b))) 71 72 @staticmethod 73 def bootstrap(): 74 parser = argparse.ArgumentParser(description='run OpenCV python tests') 75 parser.add_argument('--repo', help='use sample image files from local git repository (path to folder), ' 76 'if not set, samples will be downloaded from github.com') 77 parser.add_argument('--data', help='<not used> use data files from local folder (path to folder), ' 78 'if not set, data files will be downloaded from docs.opencv.org') 79 args, other = parser.parse_known_args() 80 print("Testing OpenCV", cv.__version__) 81 print("Local repo path:", args.repo) 82 NewOpenCVTests.repoPath = args.repo 83 try: 84 NewOpenCVTests.extraTestDataPath = os.environ['OPENCV_TEST_DATA_PATH'] 85 except KeyError: 86 print('Missing opencv extra repository. Some of tests may fail.') 87 random.seed(0) 88 unit_argv = [sys.argv[0]] + other 89 unittest.main(argv=unit_argv) 90 91 92def intersectionRate(s1, s2): 93 94 x1, y1, x2, y2 = s1 95 s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]]) 96 97 x1, y1, x2, y2 = s2 98 s2 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]]) 99 100 area, _intersection = cv.intersectConvexConvex(s1, s2) 101 return 2 * area / (cv.contourArea(s1) + cv.contourArea(s2)) 102 103def isPointInRect(p, rect): 104 if rect[0] <= p[0] and rect[1] <=p[1] and p[0] <= rect[2] and p[1] <= rect[3]: 105 return True 106 else: 107 return False 108