Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: services/shape_detection/face_detection_impl_mac.mm

Issue 2528743002: Shape Detection: Implement FaceDetection on Mac as out-of-process service (Closed)
Patch Set: Rebase Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/shape_detection/face_detection_impl_mac.h"
6
7 #include "base/mac/scoped_cftyperef.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/memory/shared_memory.h"
10 #include "media/capture/video/scoped_result_callback.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "mojo/public/cpp/system/platform_handle.h"
13 #include "services/shape_detection/face_detection_provider_impl.h"
14
15 namespace shape_detection {
16
17 namespace {
18
19 // kCIFormatRGBA8 is not exposed to public until Mac 10.11. So we define the
20 // same constant to support RGBA8 format in earlier versions.
21 #if !defined(MAC_OS_X_VERSION_10_11) || \
22 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11
23 const int kCIFormatRGBA8 = 24;
24 #endif
25
26 void RunCallbackWithFaces(
27 const shape_detection::mojom::FaceDetection::DetectCallback& callback,
28 shape_detection::mojom::FaceDetectionResultPtr faces) {
29 callback.Run(std::move(faces));
30 }
31
32 void RunCallbackWithNoFaces(
33 const shape_detection::mojom::FaceDetection::DetectCallback& callback) {
34 callback.Run(shape_detection::mojom::FaceDetectionResult::New());
35 }
36
37 } // anonymous namespace
38
39 void FaceDetectionProviderImpl::CreateFaceDetection(
40 shape_detection::mojom::FaceDetectionRequest request,
41 shape_detection::mojom::FaceDetectorOptionsPtr options) {
42 mojo::MakeStrongBinding(
43 base::MakeUnique<FaceDetectionImplMac>(std::move(options)),
44 std::move(request));
45 }
46
47 FaceDetectionImplMac::FaceDetectionImplMac(
48 shape_detection::mojom::FaceDetectorOptionsPtr options) {
49 context_.reset([[CIContext alloc] init]);
50 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh};
51 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeFace
52 context:context_
53 options:opts] retain]);
54 }
55
56 FaceDetectionImplMac::~FaceDetectionImplMac() {}
57
58 void FaceDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data,
59 uint32_t width,
60 uint32_t height,
61 const DetectCallback& callback) {
62 media::ScopedResultCallback<DetectCallback> scoped_callback(
63 base::Bind(&RunCallbackWithFaces, callback),
64 base::Bind(&RunCallbackWithNoFaces));
65
66 base::CheckedNumeric<uint32_t> num_pixels =
67 base::CheckedNumeric<uint32_t>(width) * height;
68 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4;
69 if (!num_bytes.IsValid()) {
70 DLOG(ERROR) << "Data overflow";
71 return;
72 }
73
74 base::SharedMemoryHandle memory_handle;
75 size_t memory_size = 0;
76 bool read_only_flag = false;
77 const MojoResult result = mojo::UnwrapSharedMemoryHandle(
78 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag);
79 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle";
80 if (!memory_size || memory_size != num_bytes.ValueOrDie()) {
81 DLOG(ERROR) << "Invalid image size";
82 return;
83 }
84
85 auto shared_memory =
86 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */);
87 if (!shared_memory->Map(memory_size)) {
88 DLOG(ERROR) << "Failed to map bytes from shared memory";
89 return;
90 }
91
92 NSData* byte_data = [NSData dataWithBytesNoCopy:shared_memory->memory()
93 length:num_bytes.ValueOrDie()
94 freeWhenDone:NO];
95
96 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace(
97 CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
98
99 // CIImage will return nil when RGBA8 is not supported in a certain version.
100 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc]
101 initWithBitmapData:byte_data
102 bytesPerRow:width * 4
103 size:CGSizeMake(width, height)
104 format:kCIFormatRGBA8
105 colorSpace:colorspace]);
106 if (!ci_image) {
107 DLOG(ERROR) << "Failed to create CIImage";
108 return;
109 }
110
111 NSArray* const features = [detector_ featuresInImage:ci_image];
112
113 shape_detection::mojom::FaceDetectionResultPtr faces =
114 shape_detection::mojom::FaceDetectionResult::New();
115 for (CIFaceFeature* const f in features) {
116 // In the default Core Graphics coordinate space, the origin is located
117 // in the lower-left corner, and thus |ci_image| is flipped vertically.
118 // We need to adjust |y| coordinate of bounding box before sending it.
119 gfx::RectF boundingbox(f.bounds.origin.x,
120 height - f.bounds.origin.y - f.bounds.size.height,
121 f.bounds.size.width, f.bounds.size.height);
122 faces->bounding_boxes.push_back(boundingbox);
123 }
124 scoped_callback.Run(std::move(faces));
125 }
126
127 } // namespace shape_detection
OLDNEW
« no previous file with comments | « services/shape_detection/face_detection_impl_mac.h ('k') | services/shape_detection/face_detection_provider_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698