forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detection_impl_mac.mm
69 lines (58 loc) · 2.73 KB
/
face_detection_impl_mac.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
// 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 "base/mac/scoped_cftyperef.h"
#include "services/shape_detection/detection_utils_mac.h"
namespace shape_detection {
FaceDetectionImplMac::FaceDetectionImplMac(
shape_detection::mojom::FaceDetectorOptionsPtr options) {
NSString* const accuracy =
options->fast_mode ? CIDetectorAccuracyLow : CIDetectorAccuracyHigh;
// The CIDetectorMaxFeatureCount option introduced in Mac OS SDK 10.12 can
// only be used with Rectangle Detectors.
NSDictionary* const detector_options = @{CIDetectorAccuracy : accuracy};
detector_.reset([[CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:detector_options] retain]);
}
FaceDetectionImplMac::~FaceDetectionImplMac() {}
void FaceDetectionImplMac::Detect(const SkBitmap& bitmap,
DetectCallback callback) {
base::scoped_nsobject<CIImage> ci_image = CreateCIImageFromSkBitmap(bitmap);
if (!ci_image) {
std::move(callback).Run({});
return;
}
NSArray* const features = [detector_ featuresInImage:ci_image];
const int height = bitmap.height();
std::vector<mojom::FaceDetectionResultPtr> results;
for (CIFaceFeature* const f in features) {
auto face = shape_detection::mojom::FaceDetectionResult::New();
face->bounding_box = ConvertCGToGfxCoordinates(f.bounds, height);
if (f.hasLeftEyePosition) {
auto landmark = shape_detection::mojom::Landmark::New();
landmark->type = shape_detection::mojom::LandmarkType::EYE;
landmark->locations.emplace_back(f.leftEyePosition.x,
height - f.leftEyePosition.y);
face->landmarks.push_back(std::move(landmark));
}
if (f.hasRightEyePosition) {
auto landmark = shape_detection::mojom::Landmark::New();
landmark->type = shape_detection::mojom::LandmarkType::EYE;
landmark->locations.emplace_back(f.rightEyePosition.x,
height - f.rightEyePosition.y);
face->landmarks.push_back(std::move(landmark));
}
if (f.hasMouthPosition) {
auto landmark = shape_detection::mojom::Landmark::New();
landmark->type = shape_detection::mojom::LandmarkType::MOUTH;
landmark->locations.emplace_back(f.mouthPosition.x,
height - f.mouthPosition.y);
face->landmarks.push_back(std::move(landmark));
}
results.push_back(std::move(face));
}
std::move(callback).Run(std::move(results));
}
} // namespace shape_detection