Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active July 25, 2022 18:29
Show Gist options
  • Select an option

  • Save sunmeat/56689e220178846743cc579ed786fecd to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/56689e220178846743cc579ed786fecd to your computer and use it in GitHub Desktop.
face detector google play vision example android
MainActivity.java:
package com.alex.facedetector;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FaceOverlayView mFaceOverlayView = findViewById(R.id.face_overlay);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.smile);
mFaceOverlayView.setBitmap(b);
}
}
class FaceOverlayView extends View {
private Bitmap mBitmap;
private SparseArray<Face> mFaces;
public FaceOverlayView(Context context) {
this(context, null);
}
public FaceOverlayView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FaceOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setBitmap(Bitmap bitmap) {
mBitmap = bitmap;
// https://developers.google.com/android/reference/com/google/android/gms/vision/face/FaceDetector.Builder
FaceDetector detector = new FaceDetector.Builder(getContext())
.setMinFaceSize(0.1f)
.setMode(FaceDetector.ACCURATE_MODE)
.build();
if (detector.isOperational()) {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
mFaces = detector.detect(frame);
detector.release();
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null && mFaces != null) {
double scale = drawBitmap(canvas);
drawFaceBox(canvas, scale);
}
}
private double drawBitmap(Canvas canvas) {
double viewWidth = canvas.getWidth();
double viewHeight = canvas.getHeight();
double imageWidth = mBitmap.getWidth();
double imageHeight = mBitmap.getHeight();
double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
Rect destBounds = new Rect(0, 0, (int) (imageWidth * scale), (int) (imageHeight * scale));
canvas.drawBitmap(mBitmap, null, destBounds, null);
return scale;
}
private void drawFaceBox(Canvas canvas, double scale) {
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
float left;
float top;
float right;
float bottom;
for (int i = 0; i < mFaces.size(); i++) {
Face face = mFaces.valueAt(i);
left = (float) (face.getPosition().x * scale);
top = (float) (face.getPosition().y * scale);
right = (float) scale * (face.getPosition().x + face.getWidth());
bottom = (float) scale * (face.getPosition().y + face.getHeight());
canvas.drawRect(left, top, right, bottom, paint);
}
}
}
======================================================================================================================
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.alex.facedetector.FaceOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/face_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
======================================================================================================================
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application
...
...>
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
<activity...
======================================================================================================================
build.gradle (Module:app):
dependencies {
...
implementation 'com.google.android.gms:play-services-vision:19.0.0'
}
======================================================================================================================
drawable:
нужна картинка с одним или несколькими лицами, smile.jpg
@malikdawar
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment