Reputation: 1215
Copying the code directly from google: link
gives me a 7 second wait time for the face to be detected, using a 2MB image. If I compress the bitmap by a factor of 10, it gives me a 0.05 second face detection time. However nowhere else can I find people suggesting you should compress the bitmap before running facedetection, in fact the docs suggest it should only take 10ms to detect a face, and it does it in real time when i use the camera as the image source.
Related unanswered question: mobile vision API takes too long to detect face
Just wondering if this is normal, or I am doing something wrong:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private int FACE_DETECTION_RATIO_MINIMUM = 10;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: 1");
ImageView myImageView = (ImageView) findViewById(R.id.imageView);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap myBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.test2,
options);
Log.d(TAG, "onClick: 2");
Log.d(TAG, "onClick: width: " + myBitmap.getWidth() + " height: " + myBitmap.getHeight());
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
Log.d(TAG, "onClick: 3");
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, myBitmap.getWidth()/10, myBitmap.getHeight() /10, false);
Log.d(TAG, "scaled width: " + scaledBitmap.getWidth() + " height: " + scaledBitmap.getHeight());
//tempBitmap.compress(Bitmap.CompressFormat.JPEG, 40, tempBitmap)
Canvas tempCanvas = new Canvas(tempBitmap);
Canvas tempCanvasScaled = new Canvas(scaledBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
Log.d(TAG, "onClick: 4");
FaceDetector faceDetector = new
FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).setMode(FAST_MODE).setLandmarkType(NO_LANDMARKS).setClassificationType(NO_CLASSIFICATIONS)
.build();
if(!faceDetector.isOperational()){
new AlertDialog.Builder(v.getContext()).setMessage("Could not set up the face detector!").show();
return;
}
Log.d(TAG, "onClick: 5");
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
Frame frameScaled = new Frame.Builder().setBitmap(scaledBitmap).build();
Log.d(TAG, "onClick: 5.1");
SparseArray<Face> faces = faceDetector.detect(frameScaled);
-->> SparseArray<Face> faces2 = faceDetector.detect(frame); <-- Takes 7 seconds
Log.d(TAG, "onClick: 6");
for(int i=0; i<faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " + x2 +", " + y2);
}
for(int i=0; i<faces2.size(); i++) {
Face thisFace = faces2.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
Log.d(TAG, "onClick: x,y,x,y: " + x1 +", " + y1 + ", " + x2 +", " + y2);
}
Log.d(TAG, "onClick: 5");
myImageView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
faceDetector.release();
//cropImage(x1,y1,x2,y2);
}
});
}
Upvotes: 0
Views: 339
Reputation: 149
I had same issue with vision 17.0.2/18.0.0 versions and I overcame it with downgrading vision library version.
You can use these setup
for top-level build.gradle
dependencies {
//Google analytics
classpath 'com.google.gms:google-services:4.3.2'
}
for app-level build.gradle
dependencies {
implementation 'com.google.android.gms:play-services-vision:16.2.0'
}
if you are using analytics or firebase, these version is compatibility with vision 16.2.0
dependencies {
//Google analytics
implementation 'com.google.android.gms:play-services-analytics:16.0.6'
//firebase
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
Upvotes: 1