-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
295 lines (246 loc) · 11 KB
/
tests.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
import cv2
import pytest
import math
import random
from hough import find_circles
import multiprocessing
from PIL import Image
import time
def check_success_execution(*args, **kwargs):
circles = find_circles(*args, **kwargs)
assert len(circles) > 0, "No circles found in the obvious case"
def check_if_circle_found(real_center, found_centers, shape, correct_window=0.02):
window_in_px = round(max(shape) * correct_window) + 2
for center in found_centers:
if (
abs(real_center[0] - center[0]) <= window_in_px and
abs(real_center[1] - center[1]) <= window_in_px
):
return True
return False
def check_center_circle(height, width, time_limit=20):
radius = min(height // 2, width // 2)
image = get_drawn_circles(
shape=(height, width),
centers=[(height // 2, width // 2)],
radius=radius,
thickness=2
)
def worker():
check_success_execution(image, radius)
p = multiprocessing.Process(target=worker)
# Run testing
p.start()
p.join(time_limit)
if p.is_alive():
p.terminate()
raise TimeoutError(f"Terminated. Time limit exceeded (>={time_limit}s). Shape {(height, width)}")
def get_drawn_circles(shape, centers, radius, thickness):
image = np.zeros(shape, dtype=np.uint8)
for center in centers:
center = (center[1], center[0])
cv2.circle(image, center, radius, 255, thickness)
return image
class TestFormat:
#### JPEG format ####
@staticmethod
def test_jpeg_grayscale():
check_success_execution('test_images/circle_template_41.jpg', 41)
check_success_execution('test_images/circle_inversed_template_41.jpg', 41)
@staticmethod
def test_jpeg_color():
check_success_execution('test_images/colored_circle_41.jpg', 41)
#### PNG format ####
@staticmethod
def test_png_grayscale():
check_success_execution('test_images/circle_template_41.png', 41)
check_success_execution('test_images/circle_inversed_template_41.png', 41)
@staticmethod
def test_png_color():
check_success_execution('test_images/colored_circle_41.png', 41)
@staticmethod
def test_png_transparent():
check_success_execution('test_images/transparent_circle_41.png', 41)
#### GIF format ####
@staticmethod
def test_gif_grayscale():
check_success_execution('test_images/circle_template_41.gif', 41)
check_success_execution('test_images/circle_inversed_template_41.gif', 41)
@staticmethod
def test_gif_color():
check_success_execution('test_images/colored_circle_41.gif', 41)
class TestShape:
@staticmethod
def test_tiny():
check_success_execution('test_images/circle_template_5.png', 5, quantile=0.9)
check_success_execution('test_images/transparent_circle_3x3.png', 1, quantile=0.5)
check_success_execution('test_images/one_pixel.png', 1, quantile=0)
@staticmethod
def test_elongated():
check_success_execution('test_images/circle_template_5_shape_10x100.png', 5)
check_success_execution('test_images/circle_template_5_shape_100x10.png', 5)
check_success_execution('test_images/colored_circles_50_shape_100x1000.png', 50)
check_success_execution('test_images/circle_template_1_shape_1x100.png', 1, quantile=0.75)
@staticmethod
@pytest.mark.parametrize(
"height,width",
[(64, 64), (128, 128), (256, 256), (512, 512),
(64, 128), (256, 128), (512, 1024), (2048, 64), (1024, 128)])
def test_2k_size(height, width):
check_center_circle(height, width)
@staticmethod
@pytest.mark.parametrize(
"height,width",
[(64, 64), (128, 128), (256, 256), (512, 512),
(64, 128), (256, 128), (512, 1024), (2048, 64), (1024, 128)])
def test_2k_plus_minus_one(height, width):
for dx in range(-1, 2):
for dy in range(-1, 2):
if dx == 0 and dy == 0:
continue
check_center_circle(height + dx, width + dy)
class TestSingleCircle:
circles = [
(100, 200, (50, 100), 50),
(200, 100, (50, 100), 50),
(100, 200, (30, 100), 20),
(100, 200, (50, 20 ), 40),
(200, 100, (180, 50), 10),
(200, 200, (130, 150), 15),
(300, 300, (150, 140), 130),
(400, 200, (350, 70 ), 70),
(400, 400, (200, 200), 100)
]
@staticmethod
@pytest.mark.parametrize("height,width,center,radius", circles)
def test_find_circle(height, width, center, radius):
'''
Test on finding at least one correct circle.
The circle center must be near to the output for find_circles(...).
The quantile is set to get one the most obvious circle.
'''
image = get_drawn_circles((height, width), [center], radius, 2)
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, "No circles found in the obvious case"
assert check_if_circle_found(center, centers, (height, width)), \
f"Circle {center} not found in the obvious case"
@staticmethod
@pytest.mark.parametrize("height,width,center,radius", circles)
def test_find_only_one_circle(height, width, center, radius):
'''
Test on finding just a one circle.
The circle center must be the only output for find_circles(...).
The quantile is set to get one the most obvious circle.
'''
image = get_drawn_circles((height, width), [center], radius, 2)
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, "No circles found in the obvious case"
assert len(centers) == 1, "Found more than one circle: " + str(centers)
@staticmethod
@pytest.mark.parametrize("noise_lvl", [10, 100, 255])
@pytest.mark.parametrize("height,width,center,radius", circles)
def test_find_noised_circle(noise_lvl, height, width, center, radius):
image = get_drawn_circles((height, width), [center], radius, 2)
#Noising
image = image.astype(int)
image += np.random.randint(-noise_lvl, noise_lvl+1, size=(height, width))
image = np.maximum(image, 0)
image = np.minimum(image, 255)
image = image.astype(np.uint8)
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, "No circles found in the noised case"
assert check_if_circle_found(center, centers, (height, width)), \
f"Circle {center} not found in the noised case"
@staticmethod
@pytest.mark.parametrize("height,width,center,radius", circles)
def test_find_1px_circle(height, width, center, radius):
image = get_drawn_circles((height, width), [center], radius, 1)
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, "No circles found in the obvious case"
assert check_if_circle_found(center, centers, (height, width)), \
f"Circle {center} not found in the obvious case"
@staticmethod
@pytest.mark.parametrize("height,width,center,radius", circles)
def test_find_bold_circle(height, width, center, radius):
image = get_drawn_circles((height, width), [center], radius, 6)
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, "No circles found in the obvious case"
assert check_if_circle_found(center, centers, (height, width)), \
f"Circle {center} not found in the obvious case"
cut_circles = [
(100, 200, (40, 100), 50),
(200, 100, (40, 40 ), 50),
(100, 200, (30, 100), 35),
(100, 200, (50, 20 ), 40),
(200, 100, (195, 95), 7),
(200, 200, (199, 199), 40),
(300, 300, (150, 140), 170),
(400, 200, (350, 170), 170),
(400, 400, (200, 200), 300)
]
@staticmethod
@pytest.mark.parametrize("height,width,center,radius", cut_circles)
def test_find_cut_circle(height, width, center, radius):
image = get_drawn_circles((height, width), [center], radius, 2)
full_circle_pixels = 4 * math.pi * radius
got_circle_pixels = image.sum() // 255
circle_coverage = got_circle_pixels / full_circle_pixels * 100
quantile = 1 - 1.1 / (height * width)
centers = find_circles(image, radius, quantile)
assert len(centers) > 0, f"No circles found, case {circle_coverage}% coverage."
assert check_if_circle_found(center, centers, (height, width)), \
f"Circle {center} not found, case {circle_coverage}% coverage."
##################################
#### Randomly generated tests ####
##################################
class TestRandom:
configurations = [
(100, 200, 30, 5, 10),
(200, 200, 40, 2, 10),
(20, 200, 10, 4, 5),
(200, 500, 30, 10, 5),
(300, 500, 50, 10, 5),
(300, 500, 30, 20, 5)
]
@staticmethod
@pytest.mark.parametrize("height,width,radius,n_circles,n_runs", configurations)
def test_find_all_circles(height, width, radius, n_circles, n_runs):
'''
Test on finding randomly drawn circles.
Each circle center must be in the output of find_circles(...)
But there is no restriction on the number of points in the output
(See not_many_found_test).
'''
for run_id in range(n_runs):
centers = [
(random.randint(0, height), random.randint(0, width)) # (x, y)
for _ in range(n_circles)
]
image = get_drawn_circles((height, width), centers, radius, 2)
found_centers = find_circles(image, radius, 0.97)
for center in centers:
assert check_if_circle_found(center, found_centers, (height, width)), \
f"Circle {center} not found. Shape: {(height, width)}, Radius: {radius}. Run: {run_id}."
@staticmethod
@pytest.mark.parametrize("height,width,radius,n_circles,n_runs", configurations)
def test_not_many_found(height, width, radius, n_circles, n_runs):
'''
Test on finding randomly drawn circles without large amount of false circles.
There is a restriction on a maximum number of found circles.
'''
max_number = 2 * n_circles
for run_id in range(n_runs):
centers = [
(random.randint(0, height), random.randint(0, width)) # (x, y)
for _ in range(n_circles)
]
image = get_drawn_circles((height, width), centers, radius, 2)
found_centers = find_circles(image, radius, 0.97)
assert len(found_centers) <= max_number, \
f"Found {len(found_centers)} circles when there were only {n_circles}. Run: {run_id}."