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

Side by Side Diff: services/shape_detection/detection_utils_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/detection_utils_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" 8 #include "base/mac/scoped_nsobject.h"
9 #include "base/memory/shared_memory.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" 10 #include "mojo/public/cpp/system/platform_handle.h"
13 #include "services/shape_detection/face_detection_provider_impl.h" 11 #include "services/shape_detection/barcode_detection_impl.h"
14 12
15 namespace shape_detection { 13 namespace shape_detection {
16 14
17 namespace { 15 // These formats are available but not public until Mac 10.11.
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) || \ 16 #if !defined(MAC_OS_X_VERSION_10_11) || \
22 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11 17 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11
23 const int kCIFormatRGBA8 = 24; 18 const int kCIFormatRGBA8 = 24;
19 #else
20 //static_assert(kCIFormatRGBA8 == 24, "RGBA8 format enum index.");
24 #endif 21 #endif
25 22
26 void RunCallbackWithFaces( 23 base::scoped_nsobject<CIImage> CreateCIImageFromSharedMemory(
27 const shape_detection::mojom::FaceDetection::DetectCallback& callback, 24 mojo::ScopedSharedBufferHandle frame_data,
28 shape_detection::mojom::FaceDetectionResultPtr faces) { 25 uint32_t width,
29 callback.Run(std::move(faces)); 26 uint32_t height) {
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 = 27 base::CheckedNumeric<uint32_t> num_pixels =
67 base::CheckedNumeric<uint32_t>(width) * height; 28 base::CheckedNumeric<uint32_t>(width) * height;
68 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; 29 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4;
69 if (!num_bytes.IsValid()) { 30 if (!num_bytes.IsValid()) {
70 DLOG(ERROR) << "Data overflow"; 31 DLOG(ERROR) << "Data overflow";
71 return; 32 return base::scoped_nsobject<CIImage>();
72 } 33 }
73 34
74 base::SharedMemoryHandle memory_handle; 35 base::SharedMemoryHandle memory_handle;
75 size_t memory_size = 0; 36 size_t memory_size = 0;
76 bool read_only_flag = false; 37 bool read_only_flag = false;
77 const MojoResult result = mojo::UnwrapSharedMemoryHandle( 38 const MojoResult result = mojo::UnwrapSharedMemoryHandle(
78 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag); 39 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag);
79 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle"; 40 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle";
80 if (!memory_size || memory_size != num_bytes.ValueOrDie()) { 41 if (!memory_size || memory_size != num_bytes.ValueOrDie()) {
81 DLOG(ERROR) << "Invalid image size"; 42 DLOG(ERROR) << "Invalid image size";
82 return; 43 return base::scoped_nsobject<CIImage>();
83 } 44 }
84 45
85 auto shared_memory = 46 auto shared_memory =
86 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); 47 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */);
87 if (!shared_memory->Map(memory_size)) { 48 if (!shared_memory->Map(memory_size)) {
88 DLOG(ERROR) << "Failed to map bytes from shared memory"; 49 DLOG(ERROR) << "Failed to map bytes from shared memory";
89 return; 50 return base::scoped_nsobject<CIImage>();
90 } 51 }
91 52
92 NSData* byte_data = [NSData dataWithBytesNoCopy:shared_memory->memory() 53 NSData* byte_data = [NSData dataWithBytesNoCopy:shared_memory->memory()
93 length:num_bytes.ValueOrDie() 54 length:num_bytes.ValueOrDie()
94 freeWhenDone:NO]; 55 freeWhenDone:NO];
95 56
96 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace( 57 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace(
97 CGColorSpaceCreateWithName(kCGColorSpaceSRGB)); 58 CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
98 59
99 // CIImage will return nil when RGBA8 is not supported in a certain version. 60 // CIImage will return nil if RGBA8 is not supported in a certain version.
100 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc] 61 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc]
101 initWithBitmapData:byte_data 62 initWithBitmapData:byte_data
102 bytesPerRow:width * 4 63 bytesPerRow:width * 4
103 size:CGSizeMake(width, height) 64 size:CGSizeMake(width, height)
104 format:kCIFormatRGBA8 65 format:kCIFormatRGBA8
105 colorSpace:colorspace]); 66 colorSpace:colorspace]);
106 if (!ci_image) { 67 if (!ci_image) {
107 DLOG(ERROR) << "Failed to create CIImage"; 68 DLOG(ERROR) << "Failed to create CIImage";
108 return; 69 return base::scoped_nsobject<CIImage>();
109 } 70 }
110 71 return ci_image;
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 } 72 }
126 73
127 } // namespace shape_detection 74 } // namespace shape_detection
OLDNEW
« no previous file with comments | « services/shape_detection/detection_utils_mac.h ('k') | services/shape_detection/face_detection_impl_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698