Android动态人脸检测的示例代码(脸数可调)

2020-10-22 10:03:26 浏览数 (1)

人脸检测

这里的人脸检测并非人脸识别,但是却可以识别出是否有人,当有人时候,你可以将帧图进行人脸识别(这里推荐Face 的sdk),当然我写的demo中没有加入人脸识别,有兴趣的朋友可以追加。face

android自带的人脸检测

这里我们用到了人脸检测类为 FaceDetector.这个类提供了强大的人脸检测功能,可以方便我们进行人脸的侦测,因此我们使用他来进行动态的人脸检测,实现原理,其实也挺简单,主要是通过Carmen的回调PreviewCallback 在其中对帧图进行操作,并通过FaceDetector来检测该帧图中是否有人脸。当然如果你想在surfaceview中绘制人脸的范围,可以将画布与其绑定,画完再解绑。

第一步

我们首先来定义一个surfaceview 盖在我们Carmen使用的surfaceview上 进行对人脸范围的绘制

代码语言:javascript复制
public class FindFaceView extends SurfaceView implements SurfaceHolder.Callback {

  private SurfaceHolder holder;
  private int mWidth;
  private int mHeight;
  private float eyesDistance;

  public FindFaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    holder = getHolder();
    holder.addCallback(this);
    holder.setFormat(PixelFormat.TRANSPARENT);
    this.setZOrderOnTop(true);
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
    mWidth = width;
    mHeight = height;
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {

  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {

  }

  public void drawRect(FaceDetector.Face[] faces, int numberOfFaceDetected) {
    Canvas canvas = holder.lockCanvas();
    if (canvas != null) {
      Paint clipPaint = new Paint();
      clipPaint.setAntiAlias(true);
      clipPaint.setStyle(Paint.Style.STROKE);
      clipPaint
          .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
      canvas.drawPaint(clipPaint);
      canvas.drawColor(getResources().getColor(color.transparent));
      Paint paint = new Paint();
      paint.setAntiAlias(true);
      paint.setColor(Color.GREEN);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(5.0f);
      for (int i = 0; i < numberOfFaceDetected; i  ) {
        Face face = faces[i];
        PointF midPoint = new PointF();
        // 获得两眼之间的中间点
        face.getMidPoint(midPoint);
        // 获得两眼之间的距离
        eyesDistance = face.eyesDistance();
        // 换算出预览图片和屏幕显示区域的比例参数
        float scale_x = mWidth / 500;
        float scale_y = mHeight / 600;
        Log.e("eyesDistance=", eyesDistance   "");
        Log.e("midPoint.x=", midPoint.x   "");
        Log.e("midPoint.y=", midPoint.y   "");
        // 因为拍摄的相片跟实际显示的图像是镜像关系,所以在图片上获取的两眼中间点跟手机上显示的是相反方向
        canvas.drawRect((int) (240 - midPoint.x - eyesDistance)
                * scale_x, (int) (midPoint.y * scale_y),
            (int) (240 - midPoint.x   eyesDistance) * scale_x,
            (int) (midPoint.y   3 * eyesDistance) * scale_y, paint);
      }
      holder.unlockCanvasAndPost(canvas);
    }
  }
}

重要的地方

1. holder = getHolder();获取surfaceholder与我们要绘制人脸范围的画布进行绑定Canvas canvas = holder.lockCanvas();这样我们就可以愉快的进行绘制了,当然前提是我们要拿到人脸的坐标位置。

2. 还有重要的一点,就是要让我们用来盖在Carema上的Surfaceview可以同名,并且设置起在视图树的层级为最高。

代码语言:javascript复制
 holder.setFormat(PixelFormat.TRANSPARENT);
 this.setZOrderOnTop(true);

第二步

就是我们对人脸进行检测了,当然前提是我们要获得帧图

代码语言:javascript复制
public class FaceRecognitionDemoActivity extends Activity implements
OnClickListener {
private SurfaceView preview;
private Camera camera;
private Camera.Parameters parameters;
private int orientionOfCamera;// 前置摄像头的安装角度
private int faceNumber;// 识别的人脸数
private FaceDetector.Face[] faces;
private FindFaceView mFindFaceView;
private ImageView iv_photo;
private Button bt_camera;
TextView mTV;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
super.onStart();
iv_photo = (ImageView) findViewById(R.id.iv_photo);
bt_camera = (Button) findViewById(R.id.bt_camera);
mTV = (TextView) findViewById(R.id.show_count);
bt_camera.setOnClickListener(this);
mFindFaceView = (FindFaceView) findViewById(R.id.my_preview);
preview = (SurfaceView) findViewById(R.id.preview);
// 设置缓冲类型(必不可少)
preview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 设置surface的分辨率
preview.getHolder().setFixedSize(176, 144);
// 设置屏幕常亮(必不可少)
preview.getHolder().setKeepScreenOn(true);
preview.getHolder().addCallback(new SurfaceCallback());
}
private final class MyPictureCallback implements PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
Matrix matrix = new Matrix();
matrix.setRotate(-90);
Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap
.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
iv_photo.setImageBitmap(bmp);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private final class SurfaceCallback implements Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (camera != null) {
parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置预览区域的大小
parameters.setPreviewSize(width, height);
// 设置每秒钟预览帧数
parameters.setPreviewFrameRate(20);
// 设置预览图片的大小
parameters.setPictureSize(width, height);
parameters.setJpegQuality(80);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
//设置相机的参数
for (int i = 0; i < cameraCount; i  ) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
camera = Camera.open(i);
camera.setPreviewDisplay(holder);
setCameraDisplayOrientation(i, camera);
//最重要的设置 帧图的回调
camera.setPreviewCallback(new MyPreviewCallback());
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//记得释放,避免OOM和占用
if (camera != null) {
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
}
}
private class MyPreviewCallback implements PreviewCallback {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
//这里需要注意,回调出来的data不是我们直接意义上的RGB图 而是YUV图,因此我们需要
//将YUV转化为bitmap再进行相应的人脸检测,同时注意必须使用RGB_565,才能进行人脸检测,其余无效
Camera.Size size = camera.getParameters().getPreviewSize();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21,
size.width, size.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, size.width, size.height),
80, baos);
byte[] byteArray = baos.toByteArray();
detectionFaces(byteArray);
}
}
/**
* 检测人脸
*
* @param data 预览的图像数据
*/
private void detectionFaces(byte[] data) {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
int width = bitmap1.getWidth();
int height = bitmap1.getHeight();
Matrix matrix = new Matrix();
Bitmap bitmap2 = null;
FaceDetector detector = null;
//设置各个角度的相机,这样我们的检测效果才是最好
switch (orientionOfCamera) {
case 0:
//初始化人脸检测(下同)
detector = new FaceDetector(width, height, 10);
matrix.postRotate(0.0f, width / 2, height / 2);
// 以指定的宽度和高度创建一张可变的bitmap(图片格式必须是RGB_565,不然检测不到人脸)
bitmap2 = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
break;
case 90:
detector = new FaceDetector(height, width, 1);
matrix.postRotate(-270.0f, height / 2, width / 2);
bitmap2 = Bitmap.createBitmap(height, width, Bitmap.Config.RGB_565);
break;
case 180:
detector = new FaceDetector(width, height, 1);
matrix.postRotate(-180.0f, width / 2, height / 2);
bitmap2 = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
break;
case 270:
detector = new FaceDetector(height, width, 1);
matrix.postRotate(-90.0f, height / 2, width / 2);
bitmap2 = Bitmap.createBitmap(height, width, Bitmap.Config.RGB_565);
break;
}
//设置支持的面数(最大支持检测多少人的脸 ,可以根据需要调整,不过需要与findFaces中的参数数值相同,否则会抛出异常)
faces = new FaceDetector.Face[10];
Paint paint = new Paint();
paint.setDither(true);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap2);
canvas.setMatrix(matrix);
// 将bitmap1画到bitmap2上(这里的偏移参数根据实际情况可能要修改)
canvas.drawBitmap(bitmap1, 0, 0, paint);
//这里通过向findFaces中传递帧图转化后的bitmap和最大检测的人脸数face,返回检测后的人脸数
faceNumber = detector.findFaces(bitmap2, faces);
mTV.setText("facnumber----"   faceNumber);
mTV.setTextColor(Color.RED);
//这里就是我们的人脸识别,绘制识别后的人脸区域的类
if (faceNumber != 0) {
mFindFaceView.setVisibility(View.VISIBLE);
mFindFaceView.drawRect(faces, faceNumber);
} else {
mFindFaceView.setVisibility(View.GONE);
}
bitmap2.recycle();
bitmap1.recycle();
}
/**
* 设置相机的显示方向(这里必须这么设置,不然检测不到人脸)
*
* @param cameraId 相机ID(0是后置摄像头,1是前置摄像头)
* @param camera  相机对象
*/
private void setCameraDisplayOrientation(int cameraId, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int degree = 0;
switch (rotation) {
case Surface.ROTATION_0:
degree = 0;
break;
case Surface.ROTATION_90:
degree = 90;
break;
case Surface.ROTATION_180:
degree = 180;
break;
case Surface.ROTATION_270:
degree = 270;
break;
}
orientionOfCamera = info.orientation;
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation   degree) % 360;
result = (360 - result) % 360;
} else {
result = (info.orientation - degree   360) % 360;
}
camera.setDisplayOrientation(result);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_camera:
if (camera != null) {
try {
camera.takePicture(null, null, new MyPictureCallback());
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
}

到这里我们的人脸识别就已经大功告成。demo地址

如果您想了解更多关于人脸识别方面的只是,先去关注并了解OpenCV。

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞