-
Notifications
You must be signed in to change notification settings - Fork 187
/
test_image_face_blur_mapper.py
111 lines (95 loc) · 3.38 KB
/
test_image_face_blur_mapper.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
import os
import shutil
import unittest
from data_juicer.core.data import NestedDataset as Dataset
from data_juicer.ops.mapper.image_face_blur_mapper import ImageFaceBlurMapper
from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase
class ImageFaceBlurMapperTest(DataJuicerTestCaseBase):
maxDiff = None
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..',
'data')
img1_path = os.path.join(data_path, 'cat.jpg') # no face
img2_path = os.path.join(data_path, 'lena.jpg') # [[228, 228, 377, 377]]
img3_path = os.path.join(data_path,
'lena-face.jpg') # [[29, 29, 178, 178]]
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.chk_path = os.path.join(cls.data_path, cls.__name__)
shutil.rmtree(cls.chk_path, ignore_errors=True)
os.makedirs(cls.chk_path)
def _run_helper(self, op, source_list, np=1):
dataset = Dataset.from_list(source_list)
dataset = dataset.map(op.process, num_proc=np)
res_list = dataset.to_list()
for source, res in zip(source_list, res_list):
self.assertEqual(len(source[op.image_key]), len(res[op.image_key]))
# for manual check
for path in res[op.image_key]:
basename = os.path.basename(path)
dst = f'{self.chk_path}/{op.blur_type}:{op.radius}_np:{np}_{basename}'
shutil.copy(path, dst)
def test_gaussian(self):
ds_list = [{
'images': [self.img1_path]
}, {
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}]
op = ImageFaceBlurMapper(blur_type='gaussian')
self._run_helper(op, ds_list)
def test_gaussian_radius(self):
ds_list = [{
'images': [self.img1_path]
}, {
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}]
op = ImageFaceBlurMapper(blur_type='gaussian', radius=10)
self._run_helper(op, ds_list)
def test_box(self):
ds_list = [{
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}, {
'images': [self.img1_path]
}]
op = ImageFaceBlurMapper(blur_type='box')
self._run_helper(op, ds_list)
def test_box_radius(self):
ds_list = [{
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}, {
'images': [self.img1_path]
}]
op = ImageFaceBlurMapper(blur_type='box', radius=10)
self._run_helper(op, ds_list)
def test_mean(self):
ds_list = [{
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}, {
'images': [self.img1_path]
}]
op = ImageFaceBlurMapper(blur_type='mean')
self._run_helper(op, ds_list)
def test_gaussian_radius_parallel(self):
import multiprocess as mp
mp.set_start_method('forkserver', force=True)
ds_list = [{
'images': [self.img1_path]
}, {
'images': [self.img2_path]
}, {
'images': [self.img3_path]
}]
op = ImageFaceBlurMapper(blur_type='gaussian', radius=10)
self._run_helper(op, ds_list, np=3)
if __name__ == '__main__':
unittest.main()