forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_common.py
More file actions
185 lines (157 loc) · 5.79 KB
/
tests_common.py
File metadata and controls
185 lines (157 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
from __future__ import print_function
import unittest
import sys
import hashlib
import os
import numpy as np
import cv2
import cv2.cv as cv
# Python 3 moved urlopen to urllib.requests
try:
from urllib.request import urlopen
except ImportError:
from urllib import urlopen
class OpenCVTests(unittest.TestCase):
# path to local repository folder containing 'samples' folder
repoPath = None
# github repository url
repoUrl = 'https://raw.github.com/opencv/opencv/2.4'
# path to local folder containing 'camera_calibration.tar.gz'
dataPath = None
# data url
dataUrl = 'http://docs.opencv.org/data'
depths = [ cv.IPL_DEPTH_8U, cv.IPL_DEPTH_8S, cv.IPL_DEPTH_16U, cv.IPL_DEPTH_16S, cv.IPL_DEPTH_32S, cv.IPL_DEPTH_32F, cv.IPL_DEPTH_64F ]
mat_types = [
cv.CV_8UC1,
cv.CV_8UC2,
cv.CV_8UC3,
cv.CV_8UC4,
cv.CV_8SC1,
cv.CV_8SC2,
cv.CV_8SC3,
cv.CV_8SC4,
cv.CV_16UC1,
cv.CV_16UC2,
cv.CV_16UC3,
cv.CV_16UC4,
cv.CV_16SC1,
cv.CV_16SC2,
cv.CV_16SC3,
cv.CV_16SC4,
cv.CV_32SC1,
cv.CV_32SC2,
cv.CV_32SC3,
cv.CV_32SC4,
cv.CV_32FC1,
cv.CV_32FC2,
cv.CV_32FC3,
cv.CV_32FC4,
cv.CV_64FC1,
cv.CV_64FC2,
cv.CV_64FC3,
cv.CV_64FC4,
]
mat_types_single = [
cv.CV_8UC1,
cv.CV_8SC1,
cv.CV_16UC1,
cv.CV_16SC1,
cv.CV_32SC1,
cv.CV_32FC1,
cv.CV_64FC1,
]
def depthsize(self, d):
return { cv.IPL_DEPTH_8U : 1,
cv.IPL_DEPTH_8S : 1,
cv.IPL_DEPTH_16U : 2,
cv.IPL_DEPTH_16S : 2,
cv.IPL_DEPTH_32S : 4,
cv.IPL_DEPTH_32F : 4,
cv.IPL_DEPTH_64F : 8 }[d]
def get_sample(self, filename, iscolor = cv.CV_LOAD_IMAGE_COLOR):
if not filename in self.image_cache:
filedata = None
if OpenCVTests.repoPath is not None:
candidate = OpenCVTests.repoPath + '/' + filename
if os.path.isfile(candidate):
with open(candidate, 'rb') as f:
filedata = f.read()
if filedata is None:
filedata = urllib.urlopen(OpenCVTests.repoUrl + '/' + filename).read()
imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
cv.SetData(imagefiledata, filedata, len(filedata))
self.image_cache[filename] = cv.DecodeImageM(imagefiledata, iscolor)
return self.image_cache[filename]
def get_data(self, filename, urlbase):
if (not os.path.isfile(filename)):
if OpenCVTests.dataPath is not None:
candidate = OpenCVTests.dataPath + '/' + filename
if os.path.isfile(candidate):
return candidate
urllib.urlretrieve(urlbase + '/' + filename, filename)
return filename
def setUp(self):
self.image_cache = {}
def snap(self, img):
self.snapL([img])
def snapL(self, L):
for i,img in enumerate(L):
cv.NamedWindow("snap-%d" % i, 1)
cv.ShowImage("snap-%d" % i, img)
cv.WaitKey()
cv.DestroyAllWindows()
def hashimg(self, im):
""" Compute a hash for an image, useful for image comparisons """
return hashlib.md5(im.tostring()).digest()
class NewOpenCVTests(unittest.TestCase):
# path to local repository folder containing 'samples' folder
repoPath = None
extraTestDataPath = None
# github repository url
repoUrl = 'https://raw.github.com/opencv/opencv/master'
def get_sample(self, filename, iscolor = cv2.IMREAD_COLOR):
if not filename in self.image_cache:
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 NewOpenCVTests.extraTestDataPath is not None:
candidate = NewOpenCVTests.extraTestDataPath + '/' + filename
if os.path.isfile(candidate):
with open(candidate, 'rb') as f:
filedata = f.read()
if filedata is None:
return 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]
def setUp(self):
cv2.setRNGSeed(10)
self.image_cache = {}
def hashimg(self, im):
""" Compute a hash for an image, useful for image comparisons """
return hashlib.md5(im.tostring()).hexdigest()
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:
self.fail('%s not greater than %s' % (repr(a), repr(b)))
def intersectionRate(s1, s2):
x1, y1, x2, y2 = s1
s1 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])
x1, y1, x2, y2 = s2
s2 = np.array([[x1, y1], [x2,y1], [x2, y2], [x1, y2]])
area, intersection = cv2.intersectConvexConvex(s1, s2)
return 2 * area / (cv2.contourArea(s1) + cv2.contourArea(s2))
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:
return False