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

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

Issue 2655303005: Shape detection service: Add QR detection in Mac (Closed)
Patch Set: Don't init explicitly a vector of vectors Created 3 years, 10 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/shape_detection/face_detection_impl_mac.h" 5 #include "services/shape_detection/face_detection_impl_mac.h"
6 6
7 #include "base/mac/scoped_cftyperef.h" 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" 8 #include "media/capture/video/scoped_result_callback.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h" 9 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "mojo/public/cpp/system/platform_handle.h" 10 #include "services/shape_detection/detection_utils_mac.h"
13 #include "services/shape_detection/face_detection_provider_impl.h" 11 #include "services/shape_detection/face_detection_provider_impl.h"
14 12
15 namespace shape_detection { 13 namespace shape_detection {
16 14
17 namespace { 15 namespace {
18 16
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( 17 void RunCallbackWithFaces(
27 const shape_detection::mojom::FaceDetection::DetectCallback& callback, 18 const shape_detection::mojom::FaceDetection::DetectCallback& callback,
28 shape_detection::mojom::FaceDetectionResultPtr faces) { 19 shape_detection::mojom::FaceDetectionResultPtr faces) {
29 callback.Run(std::move(faces)); 20 callback.Run(std::move(faces));
30 } 21 }
31 22
32 void RunCallbackWithNoFaces( 23 void RunCallbackWithNoFaces(
33 const shape_detection::mojom::FaceDetection::DetectCallback& callback) { 24 const shape_detection::mojom::FaceDetection::DetectCallback& callback) {
34 callback.Run(shape_detection::mojom::FaceDetectionResult::New()); 25 callback.Run(shape_detection::mojom::FaceDetectionResult::New());
35 } 26 }
36 27
37 } // anonymous namespace 28 } // anonymous namespace
38 29
39 void FaceDetectionProviderImpl::CreateFaceDetection( 30 void FaceDetectionProviderImpl::CreateFaceDetection(
40 shape_detection::mojom::FaceDetectionRequest request, 31 shape_detection::mojom::FaceDetectionRequest request,
41 shape_detection::mojom::FaceDetectorOptionsPtr options) { 32 shape_detection::mojom::FaceDetectorOptionsPtr options) {
42 mojo::MakeStrongBinding( 33 mojo::MakeStrongBinding(
43 base::MakeUnique<FaceDetectionImplMac>(std::move(options)), 34 base::MakeUnique<FaceDetectionImplMac>(std::move(options)),
44 std::move(request)); 35 std::move(request));
45 } 36 }
46 37
47 FaceDetectionImplMac::FaceDetectionImplMac( 38 FaceDetectionImplMac::FaceDetectionImplMac(
48 shape_detection::mojom::FaceDetectorOptionsPtr options) { 39 shape_detection::mojom::FaceDetectorOptionsPtr options) {
49 context_.reset([[CIContext alloc] init]);
50 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; 40 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh};
51 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeFace 41 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeFace
52 context:context_ 42 context:nil
53 options:opts] retain]); 43 options:opts] retain]);
54 } 44 }
55 45
56 FaceDetectionImplMac::~FaceDetectionImplMac() {} 46 FaceDetectionImplMac::~FaceDetectionImplMac() {}
57 47
58 void FaceDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, 48 void FaceDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data,
59 uint32_t width, 49 uint32_t width,
60 uint32_t height, 50 uint32_t height,
61 const DetectCallback& callback) { 51 const DetectCallback& callback) {
62 media::ScopedResultCallback<DetectCallback> scoped_callback( 52 media::ScopedResultCallback<DetectCallback> scoped_callback(
63 base::Bind(&RunCallbackWithFaces, callback), 53 base::Bind(&RunCallbackWithFaces, callback),
64 base::Bind(&RunCallbackWithNoFaces)); 54 base::Bind(&RunCallbackWithNoFaces));
65 55
66 base::CheckedNumeric<uint32_t> num_pixels = 56 base::scoped_nsobject<CIImage> ci_image =
67 base::CheckedNumeric<uint32_t>(width) * height; 57 CreateCIImageFromSharedMemory(std::move(frame_data), width, height);
68 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; 58 if (!ci_image)
69 if (!num_bytes.IsValid()) {
70 DLOG(ERROR) << "Data overflow";
71 return; 59 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 60
111 NSArray* const features = [detector_ featuresInImage:ci_image]; 61 NSArray* const features = [detector_ featuresInImage:ci_image];
112 62
113 shape_detection::mojom::FaceDetectionResultPtr faces = 63 shape_detection::mojom::FaceDetectionResultPtr faces =
114 shape_detection::mojom::FaceDetectionResult::New(); 64 shape_detection::mojom::FaceDetectionResult::New();
115 for (CIFaceFeature* const f in features) { 65 for (CIFaceFeature* const f in features) {
116 // In the default Core Graphics coordinate space, the origin is located 66 // In the default Core Graphics coordinate space, the origin is located
117 // in the lower-left corner, and thus |ci_image| is flipped vertically. 67 // 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. 68 // We need to adjust |y| coordinate of bounding box before sending it.
119 gfx::RectF boundingbox(f.bounds.origin.x, 69 gfx::RectF boundingbox(f.bounds.origin.x,
120 height - f.bounds.origin.y - f.bounds.size.height, 70 height - f.bounds.origin.y - f.bounds.size.height,
121 f.bounds.size.width, f.bounds.size.height); 71 f.bounds.size.width, f.bounds.size.height);
122 faces->bounding_boxes.push_back(boundingbox); 72 faces->bounding_boxes.push_back(boundingbox);
123 } 73 }
124 scoped_callback.Run(std::move(faces)); 74 scoped_callback.Run(std::move(faces));
125 } 75 }
126 76
127 } // namespace shape_detection 77 } // namespace shape_detection
OLDNEW
« no previous file with comments | « services/shape_detection/face_detection_impl_mac.h ('k') | services/shape_detection/shape_detection_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698