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

Side by Side Diff: services/shape_detection/android/java/src/org/chromium/shape_detection/FaceDetectionImpl.java

Issue 2886943008: Revert of Shape Detection: Face detection junit tests (Closed)
Patch Set: Created 3 years, 7 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 package org.chromium.shape_detection; 5 package org.chromium.shape_detection;
6 6
7 import android.graphics.Bitmap; 7 import android.graphics.Bitmap;
8 import android.graphics.PointF; 8 import android.graphics.PointF;
9 import android.media.FaceDetector; 9 import android.media.FaceDetector;
10 import android.media.FaceDetector.Face; 10 import android.media.FaceDetector.Face;
11 import android.os.AsyncTask; 11 import android.os.AsyncTask;
12 12
13 import org.chromium.base.Log; 13 import org.chromium.base.Log;
14 import org.chromium.base.VisibleForTesting;
15 import org.chromium.gfx.mojom.RectF; 14 import org.chromium.gfx.mojom.RectF;
16 import org.chromium.mojo.system.MojoException; 15 import org.chromium.mojo.system.MojoException;
17 import org.chromium.mojo.system.SharedBufferHandle; 16 import org.chromium.mojo.system.SharedBufferHandle;
18 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; 17 import org.chromium.mojo.system.SharedBufferHandle.MapFlags;
19 import org.chromium.shape_detection.mojom.FaceDetection; 18 import org.chromium.shape_detection.mojom.FaceDetection;
20 import org.chromium.shape_detection.mojom.FaceDetectionResult; 19 import org.chromium.shape_detection.mojom.FaceDetectionResult;
21 import org.chromium.shape_detection.mojom.FaceDetectorOptions; 20 import org.chromium.shape_detection.mojom.FaceDetectorOptions;
22 import org.chromium.shape_detection.mojom.Landmark; 21 import org.chromium.shape_detection.mojom.Landmark;
23 22
24 import java.nio.ByteBuffer; 23 import java.nio.ByteBuffer;
25 24
26 /** 25 /**
27 * Android implementation of the FaceDetection service defined in 26 * Android implementation of the FaceDetection service defined in
28 * services/shape_detection/public/interfaces/facedetection.mojom 27 * services/shape_detection/public/interfaces/facedetection.mojom
29 */ 28 */
30 public class FaceDetectionImpl implements FaceDetection { 29 public class FaceDetectionImpl implements FaceDetection {
31 private static final String TAG = "FaceDetectionImpl"; 30 private static final String TAG = "FaceDetectionImpl";
32 private static final int MAX_FACES = 32; 31 private static final int MAX_FACES = 32;
33 private static FaceDetector sFaceDetector;
34 private final boolean mFastMode; 32 private final boolean mFastMode;
35 private final int mMaxFaces; 33 private final int mMaxFaces;
36 34
37 FaceDetectionImpl(FaceDetectorOptions options) { 35 FaceDetectionImpl(FaceDetectorOptions options) {
38 mFastMode = options.fastMode; 36 mFastMode = options.fastMode;
39 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); 37 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES);
40 } 38 }
41 39
42 @VisibleForTesting
43 public static void setFaceDetector(FaceDetector faceDetector) {
44 sFaceDetector = faceDetector;
45 }
46
47 @Override 40 @Override
48 public void detect(SharedBufferHandle frameData, final int width, final int height, 41 public void detect(SharedBufferHandle frameData, final int width, final int height,
49 final DetectResponse callback) { 42 final DetectResponse callback) {
50 final long numPixels = (long) width * height; 43 final long numPixels = (long) width * height;
51 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . 44 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking .
52 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { 45 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) {
53 Log.d(TAG, "Invalid argument(s)."); 46 Log.d(TAG, "Invalid argument(s).");
54 callback.call(new FaceDetectionResult[0]); 47 callback.call(new FaceDetectionResult[0]);
55 return; 48 return;
56 } 49 }
57 50
58 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); 51 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() );
59 if (imageBuffer.capacity() <= 0) { 52 if (imageBuffer.capacity() <= 0) {
60 Log.d(TAG, "Failed to map from SharedBufferHandle."); 53 Log.d(TAG, "Failed to map from SharedBufferHandle.");
61 callback.call(new FaceDetectionResult[0]); 54 callback.call(new FaceDetectionResult[0]);
62 return; 55 return;
63 } 56 }
64 57
65 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); 58 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88);
66 59
67 // An int array is needed to construct a Bitmap. However the Bytebuffer 60 // An int array is needed to construct a Bitmap. However the Bytebuffer
68 // we get from |sharedBufferHandle| is directly allocated and does not 61 // we get from |sharedBufferHandle| is directly allocated and does not
69 // have a supporting array. Therefore we need to copy from |imageBuffer| 62 // have a supporting array. Therefore we need to copy from |imageBuffer|
70 // to create this intermediate Bitmap. 63 // to create this intermediate Bitmap.
71 // TODO(xianglu): Consider worker pool as appropriate threads. 64 // TODO(xianglu): Consider worker pool as appropriate threads.
72 // http://crbug.com/655814 65 // http://crbug.com/655814
73 try { 66 bitmap.copyPixelsFromBuffer(imageBuffer);
74 bitmap.copyPixelsFromBuffer(imageBuffer);
75 } catch (RuntimeException e) {
76 callback.call(new FaceDetectionResult[0]);
77 return;
78 }
79 67
80 // A Bitmap must be in 565 format for findFaces() to work. See 68 // A Bitmap must be in 565 format for findFaces() to work. See
81 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 69 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124
82 // 70 //
83 // It turns out that FaceDetector is not able to detect correctly if 71 // It turns out that FaceDetector is not able to detect correctly if
84 // simply using pixmap.setConfig(). The reason might be that findFaces() 72 // simply using pixmap.setConfig(). The reason might be that findFaces()
85 // needs non-premultiplied ARGB arrangement, while the alpha type in the 73 // needs non-premultiplied ARGB arrangement, while the alpha type in the
86 // original image is premultiplied. We can use getPixels() which does 74 // original image is premultiplied. We can use getPixels() which does
87 // the unmultiplication while copying to a new array. See 75 // the unmultiplication while copying to a new array. See
88 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538 76 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538
89 int[] pixels = new int[width * height]; 77 int[] pixels = new int[width * height];
90 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 78 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
91 final Bitmap unPremultipliedBitmap = 79 final Bitmap unPremultipliedBitmap =
92 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 ); 80 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 );
93 81
94 // FaceDetector creation and findFaces() might take a long time and trig ger a 82 // FaceDetector creation and findFaces() might take a long time and trig ger a
95 // "StrictMode policy violation": they should happen in a background thr ead. 83 // "StrictMode policy violation": they should happen in a background thr ead.
96 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { 84 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
97 @Override 85 @Override
98 public void run() { 86 public void run() {
99 final FaceDetector detector = (sFaceDetector != null) 87 final FaceDetector detector = new FaceDetector(width, height, mM axFaces);
100 ? sFaceDetector
101 : new FaceDetector(width, height, mMaxFaces);
102 Face[] detectedFaces = new Face[mMaxFaces]; 88 Face[] detectedFaces = new Face[mMaxFaces];
103 // findFaces() will stop at |mMaxFaces|. 89 // findFaces() will stop at |mMaxFaces|.
104 final int numberOfFaces = detector.findFaces(unPremultipliedBitm ap, detectedFaces); 90 final int numberOfFaces = detector.findFaces(unPremultipliedBitm ap, detectedFaces);
105 91
106 FaceDetectionResult[] faceArray = new FaceDetectionResult[number OfFaces]; 92 FaceDetectionResult[] faceArray = new FaceDetectionResult[number OfFaces];
107 93
108 for (int i = 0; i < numberOfFaces; i++) { 94 for (int i = 0; i < numberOfFaces; i++) {
109 faceArray[i] = new FaceDetectionResult(); 95 faceArray[i] = new FaceDetectionResult();
110 96
111 final Face face = detectedFaces[i]; 97 final Face face = detectedFaces[i];
(...skipping 17 matching lines...) Expand all
129 } 115 }
130 116
131 @Override 117 @Override
132 public void close() {} 118 public void close() {}
133 119
134 @Override 120 @Override
135 public void onConnectionError(MojoException e) { 121 public void onConnectionError(MojoException e) {
136 close(); 122 close();
137 } 123 }
138 } 124 }
OLDNEW
« no previous file with comments | « services/BUILD.gn ('k') | services/shape_detection/android/junit/src/org/chromium/shape_detection/FaceDetectionImplTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698