forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detection_impl_mac_unittest.mm
201 lines (174 loc) · 7.08 KB
/
face_detection_impl_mac_unittest.mm
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/shape_detection/face_detection_impl_mac.h"
#include <dlfcn.h>
#include <memory>
#include <utility>
#include "base/base64.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "services/shape_detection/face_detection_impl_mac_vision.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gl/gl_switches.h"
using base::test::RunOnceClosure;
using ::testing::TestWithParam;
using ::testing::ValuesIn;
namespace shape_detection {
namespace {
std::unique_ptr<mojom::FaceDetection> CreateFaceDetectorImplMac(
shape_detection::mojom::FaceDetectorOptionsPtr options) {
return std::make_unique<FaceDetectionImplMac>(
mojom::FaceDetectorOptions::New());
}
std::unique_ptr<mojom::FaceDetection> CreateFaceDetectorImplMacVision(
shape_detection::mojom::FaceDetectorOptionsPtr options) {
if (@available(macOS 10.13, *)) {
return std::make_unique<FaceDetectionImplMacVision>();
} else {
return nullptr;
}
}
using FaceDetectorFactory =
base::RepeatingCallback<std::unique_ptr<mojom::FaceDetection>(
shape_detection::mojom::FaceDetectorOptionsPtr)>;
struct TestParams {
bool fast_mode;
int image_width;
int image_height;
const char* image_path;
size_t num_faces;
size_t num_landmarks;
size_t num_mouth_points;
FaceDetectorFactory factory;
};
std::vector<TestParams> GetTestParams() {
if (@available(macOS 10.14, *)) {
return {
{false, 120, 120, "services/test/data/mona_lisa.jpg", 1, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{true, 120, 120, "services/test/data/mona_lisa.jpg", 1, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{false, 120, 120, "services/test/data/mona_lisa.jpg", 1, 4, 10,
base::BindRepeating(&CreateFaceDetectorImplMacVision)},
{false, 240, 240, "services/test/data/the_beatles.jpg", 4, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{true, 240, 240, "services/test/data/the_beatles.jpg", 4, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{false, 240, 240, "services/test/data/the_beatles.jpg", 4, 4, 10,
base::BindRepeating(&CreateFaceDetectorImplMacVision)},
};
} else {
return {
{false, 120, 120, "services/test/data/mona_lisa.jpg", 1, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{true, 120, 120, "services/test/data/mona_lisa.jpg", 1, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{false, 120, 120, "services/test/data/mona_lisa.jpg", 1, 4, 10,
base::BindRepeating(&CreateFaceDetectorImplMacVision)},
{false, 240, 240, "services/test/data/the_beatles.jpg", 3, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{true, 240, 240, "services/test/data/the_beatles.jpg", 3, 3, 1,
base::BindRepeating(&CreateFaceDetectorImplMac)},
{false, 240, 240, "services/test/data/the_beatles.jpg", 4, 4, 10,
base::BindRepeating(&CreateFaceDetectorImplMacVision)},
};
}
}
} // anonymous namespace
class FaceDetectionImplMacTest : public TestWithParam<struct TestParams> {
public:
~FaceDetectionImplMacTest() override {}
void SetUp() override {
if (@available(macOS 10.13, *)) {
vision_framework_ = dlopen(
"/System/Library/Frameworks/Vision.framework/Vision", RTLD_LAZY);
}
}
void TearDown() override {
if (@available(macOS 10.13, *)) {
if (vision_framework_)
dlclose(vision_framework_);
}
}
void DetectCallback(size_t num_faces,
size_t num_landmarks,
size_t num_mouth_points,
std::vector<mojom::FaceDetectionResultPtr> results) {
EXPECT_EQ(num_faces, results.size());
for (const auto& face : results) {
EXPECT_EQ(num_landmarks, face->landmarks.size());
EXPECT_EQ(mojom::LandmarkType::EYE, face->landmarks[0]->type);
EXPECT_EQ(mojom::LandmarkType::EYE, face->landmarks[1]->type);
EXPECT_EQ(mojom::LandmarkType::MOUTH, face->landmarks[2]->type);
EXPECT_EQ(num_mouth_points, face->landmarks[2]->locations.size());
}
Detection();
}
MOCK_METHOD0(Detection, void(void));
std::unique_ptr<mojom::FaceDetection> impl_;
base::test::SingleThreadTaskEnvironment task_environment_;
void* vision_framework_;
};
TEST_P(FaceDetectionImplMacTest, CreateAndDestroy) {
impl_ = GetParam().factory.Run(mojom::FaceDetectorOptions::New());
if (!impl_ && base::mac::IsAtMostOS10_12()) {
LOG(WARNING) << "FaceDetectionImplMacVision is not available before Mac "
"OSX 10.13. Skipping test.";
return;
}
}
TEST_P(FaceDetectionImplMacTest, ScanOneFace) {
// Face detection test needs a GPU.
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseGpuInTests)) {
return;
}
auto options = shape_detection::mojom::FaceDetectorOptions::New();
options->fast_mode = GetParam().fast_mode;
impl_ = GetParam().factory.Run(std::move(options));
if (!impl_ && base::mac::IsAtMostOS10_12()) {
LOG(WARNING) << "FaceDetectionImplMacVision is not available before Mac "
"OSX 10.13. Skipping test.";
return;
}
// Load image data from test directory.
base::FilePath image_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &image_path));
image_path = image_path.AppendASCII(GetParam().image_path);
ASSERT_TRUE(base::PathExists(image_path));
std::string image_data;
ASSERT_TRUE(base::ReadFileToString(image_path, &image_data));
std::unique_ptr<SkBitmap> image = gfx::JPEGCodec::Decode(
reinterpret_cast<const uint8_t*>(image_data.data()), image_data.size());
ASSERT_TRUE(image);
ASSERT_EQ(GetParam().image_width, image->width());
ASSERT_EQ(GetParam().image_height, image->height());
const gfx::Size size(image->width(), image->height());
const size_t num_bytes = size.GetArea() * 4 /* bytes per pixel */;
// This assert assumes there is no padding in the bitmap's rowbytes
ASSERT_EQ(num_bytes, image->computeByteSize());
base::RunLoop run_loop;
// Send the image to Detect() and expect the response in callback.
EXPECT_CALL(*this, Detection())
.WillOnce(RunOnceClosure(run_loop.QuitClosure()));
impl_->Detect(
*image,
base::BindOnce(&FaceDetectionImplMacTest::DetectCallback,
base::Unretained(this), GetParam().num_faces,
GetParam().num_landmarks, GetParam().num_mouth_points));
run_loop.Run();
}
INSTANTIATE_TEST_SUITE_P(, FaceDetectionImplMacTest, ValuesIn(GetTestParams()));
} // shape_detection namespace