2016-01-28 20:43:08 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-09-03 20:01:25 +08:00
|
|
|
import os
|
2016-01-28 20:43:08 +08:00
|
|
|
import sys
|
2017-09-03 20:01:25 +08:00
|
|
|
import unittest
|
2016-01-28 20:43:08 +08:00
|
|
|
import hashlib
|
2017-09-03 20:01:25 +08:00
|
|
|
import random
|
|
|
|
import argparse
|
|
|
|
|
2016-01-28 20:43:08 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2016-01-28 20:43:08 +08:00
|
|
|
|
|
|
|
# Python 3 moved urlopen to urllib.requests
|
|
|
|
try:
|
|
|
|
from urllib.request import urlopen
|
|
|
|
except ImportError:
|
|
|
|
from urllib import urlopen
|
|
|
|
|
|
|
|
class NewOpenCVTests(unittest.TestCase):
|
|
|
|
|
|
|
|
# path to local repository folder containing 'samples' folder
|
|
|
|
repoPath = None
|
2016-02-05 23:46:43 +08:00
|
|
|
extraTestDataPath = None
|
2016-01-28 20:43:08 +08:00
|
|
|
# github repository url
|
2016-06-14 21:01:36 +08:00
|
|
|
repoUrl = 'https://raw.github.com/opencv/opencv/master'
|
2016-01-28 20:43:08 +08:00
|
|
|
|
2018-09-18 06:13:01 +08:00
|
|
|
def find_file(self, filename, searchPaths=[], required=True):
|
2018-09-13 21:31:31 +08:00
|
|
|
searchPaths = searchPaths if searchPaths else [self.repoPath, self.extraTestDataPath]
|
|
|
|
for path in searchPaths:
|
|
|
|
if path is not None:
|
|
|
|
candidate = path + '/' + filename
|
|
|
|
if os.path.isfile(candidate):
|
|
|
|
return candidate
|
2018-09-18 06:13:01 +08:00
|
|
|
if required:
|
|
|
|
self.fail('File ' + filename + ' not found')
|
2018-09-13 21:31:31 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-07-25 02:31:40 +08:00
|
|
|
def get_sample(self, filename, iscolor = None):
|
|
|
|
if iscolor is None:
|
|
|
|
iscolor = cv.IMREAD_COLOR
|
2016-01-28 20:43:08 +08:00
|
|
|
if not filename in self.image_cache:
|
2018-09-13 21:31:31 +08:00
|
|
|
filepath = self.find_file(filename)
|
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
filedata = f.read()
|
2017-12-11 17:55:03 +08:00
|
|
|
self.image_cache[filename] = cv.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor)
|
2016-01-28 20:43:08 +08:00
|
|
|
return self.image_cache[filename]
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.setRNGSeed(10)
|
2016-01-28 20:43:08 +08:00
|
|
|
self.image_cache = {}
|
|
|
|
|
|
|
|
def hashimg(self, im):
|
|
|
|
""" Compute a hash for an image, useful for image comparisons """
|
2016-01-29 18:07:08 +08:00
|
|
|
return hashlib.md5(im.tostring()).hexdigest()
|
2016-01-28 20:43:08 +08:00
|
|
|
|
|
|
|
if sys.version_info[:2] == (2, 6):
|
|
|
|
def assertLess(self, a, b, msg=None):
|
|
|
|
if not a < b:
|
|
|
|
self.fail('%s not less than %s' % (repr(a), repr(b)))
|
|
|
|
|
|
|
|
def assertLessEqual(self, a, b, msg=None):
|
|
|
|
if not a <= b:
|
|
|
|
self.fail('%s not less than or equal to %s' % (repr(a), repr(b)))
|
|
|
|
|
|
|
|
def assertGreater(self, a, b, msg=None):
|
|
|
|
if not a > b:
|
2016-02-05 22:46:52 +08:00
|
|
|
self.fail('%s not greater than %s' % (repr(a), repr(b)))
|
|
|
|
|
2017-09-03 20:01:25 +08:00
|
|
|
@staticmethod
|
|
|
|
def bootstrap():
|
|
|
|
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='<not used> 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()
|
2017-12-11 17:55:03 +08:00
|
|
|
print("Testing OpenCV", cv.__version__)
|
2017-09-03 20:01:25 +08:00
|
|
|
print("Local repo path:", args.repo)
|
|
|
|
NewOpenCVTests.repoPath = args.repo
|
|
|
|
try:
|
|
|
|
NewOpenCVTests.extraTestDataPath = os.environ['OPENCV_TEST_DATA_PATH']
|
|
|
|
except KeyError:
|
|
|
|
print('Missing opencv extra repository. Some of tests may fail.')
|
|
|
|
random.seed(0)
|
|
|
|
unit_argv = [sys.argv[0]] + other
|
|
|
|
unittest.main(argv=unit_argv)
|
|
|
|
|
|
|
|
|
2016-02-05 22:46:52 +08:00
|
|
|
def intersectionRate(s1, s2):
|
|
|
|
|
|
|
|
x1, y1, x2, y2 = s1
|
2016-02-12 20:55:06 +08:00
|
|
|
s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])
|
2016-02-05 22:46:52 +08:00
|
|
|
|
|
|
|
x1, y1, x2, y2 = s2
|
2016-02-12 20:55:06 +08:00
|
|
|
s2 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
area, _intersection = cv.intersectConvexConvex(s1, s2)
|
|
|
|
return 2 * area / (cv.contourArea(s1) + cv.contourArea(s2))
|
2016-02-12 20:55:06 +08:00
|
|
|
|
|
|
|
def isPointInRect(p, rect):
|
|
|
|
if rect[0] <= p[0] and rect[1] <=p[1] and p[0] <= rect[2] and p[1] <= rect[3]:
|
|
|
|
return True
|
|
|
|
else:
|
2018-09-13 21:31:31 +08:00
|
|
|
return False
|