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