OLD | NEW |
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 package org.chromium.shape_detection; | 5 package org.chromium.shape_detection; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.PointF; | 8 import android.graphics.PointF; |
9 import android.util.SparseArray; | 9 import android.util.SparseArray; |
10 | 10 |
11 import com.google.android.gms.vision.Frame; | 11 import com.google.android.gms.vision.Frame; |
12 import com.google.android.gms.vision.face.Face; | 12 import com.google.android.gms.vision.face.Face; |
13 import com.google.android.gms.vision.face.FaceDetector; | 13 import com.google.android.gms.vision.face.FaceDetector; |
14 import com.google.android.gms.vision.face.Landmark; | 14 import com.google.android.gms.vision.face.Landmark; |
15 | 15 |
16 import org.chromium.base.Log; | 16 import org.chromium.base.Log; |
17 import org.chromium.gfx.mojom.RectF; | 17 import org.chromium.gfx.mojom.RectF; |
18 import org.chromium.mojo.system.MojoException; | 18 import org.chromium.mojo.system.MojoException; |
19 import org.chromium.mojo.system.SharedBufferHandle; | |
20 import org.chromium.shape_detection.mojom.FaceDetection; | 19 import org.chromium.shape_detection.mojom.FaceDetection; |
21 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 20 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
22 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 21 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
23 import org.chromium.shape_detection.mojom.LandmarkType; | 22 import org.chromium.shape_detection.mojom.LandmarkType; |
24 | 23 |
25 import java.util.ArrayList; | 24 import java.util.ArrayList; |
26 import java.util.List; | 25 import java.util.List; |
27 | 26 |
28 /** | 27 /** |
29 * Google Play services implementation of the FaceDetection service defined in | 28 * Google Play services implementation of the FaceDetection service defined in |
(...skipping 19 matching lines...) Expand all Loading... |
49 } | 48 } |
50 } catch (IllegalArgumentException e) { | 49 } catch (IllegalArgumentException e) { |
51 Log.e(TAG, "Unexpected exception " + e); | 50 Log.e(TAG, "Unexpected exception " + e); |
52 assert false; | 51 assert false; |
53 } | 52 } |
54 | 53 |
55 mFaceDetector = builder.build(); | 54 mFaceDetector = builder.build(); |
56 } | 55 } |
57 | 56 |
58 @Override | 57 @Override |
59 public void detect( | 58 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { |
60 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { | |
61 // The vision library will be downloaded the first time the API is used | 59 // The vision library will be downloaded the first time the API is used |
62 // on the device; this happens "fast", but it might have not completed, | 60 // on the device; this happens "fast", but it might have not completed, |
63 // bail in this case. | 61 // bail in this case. |
64 if (!mFaceDetector.isOperational()) { | 62 if (!mFaceDetector.isOperational()) { |
65 Log.e(TAG, "FaceDetector is not operational"); | 63 Log.e(TAG, "FaceDetector is not operational"); |
66 | 64 |
67 // Fallback to Android's FaceDetectionImpl. | 65 // Fallback to Android's FaceDetectionImpl. |
68 FaceDetectorOptions options = new FaceDetectorOptions(); | 66 FaceDetectorOptions options = new FaceDetectorOptions(); |
69 options.fastMode = mFastMode; | 67 options.fastMode = mFastMode; |
70 options.maxDetectedFaces = mMaxFaces; | 68 options.maxDetectedFaces = mMaxFaces; |
71 FaceDetectionImpl detector = new FaceDetectionImpl(options); | 69 FaceDetectionImpl detector = new FaceDetectionImpl(options); |
72 detector.detect(frameData, width, height, callback); | 70 detector.detect(bitmapData, callback); |
73 return; | 71 return; |
74 } | 72 } |
75 | 73 |
76 Frame frame = SharedBufferUtils.convertToFrame(frameData, width, height)
; | 74 Frame frame = SharedBufferUtils.convertToFrame(bitmapData); |
77 if (frame == null) { | 75 if (frame == null) { |
78 Log.e(TAG, "Error converting SharedMemory to Frame"); | 76 Log.e(TAG, "Error converting SharedMemory to Frame"); |
79 callback.call(new FaceDetectionResult[0]); | 77 callback.call(new FaceDetectionResult[0]); |
80 return; | 78 return; |
81 } | 79 } |
82 | 80 |
83 final SparseArray<Face> faces = mFaceDetector.detect(frame); | 81 final SparseArray<Face> faces = mFaceDetector.detect(frame); |
84 | 82 |
85 FaceDetectionResult[] faceArray = new FaceDetectionResult[faces.size()]; | 83 FaceDetectionResult[] faceArray = new FaceDetectionResult[faces.size()]; |
86 for (int i = 0; i < faces.size(); i++) { | 84 for (int i = 0; i < faces.size(); i++) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 @Override | 120 @Override |
123 public void close() { | 121 public void close() { |
124 mFaceDetector.release(); | 122 mFaceDetector.release(); |
125 } | 123 } |
126 | 124 |
127 @Override | 125 @Override |
128 public void onConnectionError(MojoException e) { | 126 public void onConnectionError(MojoException e) { |
129 close(); | 127 close(); |
130 } | 128 } |
131 } | 129 } |
OLD | NEW |