Android開発者の顔認識統計の顔数


本論文の例では、Android開発者の顔認識統計の具体的なコードを共有しています。

最近のプロジェクトの需要は現在のカメラの中の人の顔の個数を統計するので、Androidは既製のAppを提供して、最後に効果を実現して上の図のようです。
思考とステップの分析:
主に使用されるクラス:SurfaceView、Camera Manager、Camera Device.StteCallback、Camera Capture Session.StatiCallback。
1.SurfaceViewでプレビューエリアを表示します。
2.SurfaceViewの作成に成功した後、現在の設備で使用可能なカメラをCamera Managerで取得します。
3.カメラの権限を検出する。
4.カメラをCamera Managerで開けます。
5.オープン成功のコールバックで、CameraDeviceを通じてセッションを作成します。
6.セッションの作成に成功したコールバック方法でプレビュー要求を作成します。
7.プレビュー要求のコールバック方法one Capture Completted()メソッドでプレビュー画面から返された顔の数を取得します。
以下は実現ステップです。
1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">

 <SurfaceView
  android:id="@+id/sv"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" />
</LinearLayout>
java

cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  surfaceHolder = sv.getHolder();
  surface = surfaceHolder.getSurface();

//         
     String[] cameraIdList = cameraManager.getCameraIdList();

if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
      // TODO: Consider calling
      // Activity#requestPermissions
      // here to request the missing permissions, and then overriding
      // public void onRequestPermissionsResult(int requestCode, String[] permissions,
      //           int[] grantResults)
      // to handle the case where the user grants the permission. See the documentation
      // for Activity#requestPermissions for more details.
      return;
     }

//           
     CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraIdList[1]);
     //             ,         
     faceDetectModes = characteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);//         
     cameraManager.openCamera(cameraIdList[1], mStateCallback, null);

camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
     @Override
     public void onConfigured(@NonNull CameraCaptureSession session) {
      
     }

     @Override
     public void onConfigureFailed(@NonNull CameraCaptureSession session) {

     }
    }, new Handler());

CaptureRequest.Builder captureRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
       captureRequest.addTarget(surface);
       captureRequest.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
       captureRequest.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, getFaceDetectMode());
       session.setRepeatingRequest(captureRequest.build(),captureCallback,new Handler());

 /**
  *     
  */
 private CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
  @Override
  public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
   super.onCaptureStarted(session, request, timestamp, frameNumber);
  }

  @Override
  public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
   super.onCaptureProgressed(session, request, partialResult);
  }

  @Override
  public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
   super.onCaptureCompleted(session, request, result);
   Face faces[] = result.get(CaptureResult.STATISTICS_FACES);
   if (faces.length != face) {
    Log.e("lee", faces.length + "  ");
   }
  }

  @Override
  public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
   super.onCaptureFailed(session, request, failure);
  }

  @Override
  public void onCaptureSequenceCompleted(@NonNull CameraCaptureSession session, int sequenceId, long frameNumber) {
   super.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
  }

  @Override
  public void onCaptureSequenceAborted(@NonNull CameraCaptureSession session, int sequenceId) {
   super.onCaptureSequenceAborted(session, sequenceId);
  }

  @Override
  public void onCaptureBufferLost(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull Surface target, long frameNumber) {
   super.onCaptureBufferLost(session, request, target, frameNumber);
  }
 };
以下はすべてのコードです。
MainActivity.java

package com.zg.lcddemo;

import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.Face;
import android.hardware.camera2.params.SessionConfiguration;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.FaceDetector;
import android.media.ImageReader;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Semaphore;

public class MainActivity extends AppCompatActivity {
 private SurfaceView sv;
 private SurfaceHolder surfaceHolder;
 private CameraManager cameraManager;
 private Surface surface;
 int[] faceDetectModes;
 int face;
 List<Bitmap> bitmaps = new ArrayList<>();

 /**
  *              
  *
  * @return
  */
 private int getFaceDetectMode() {
  if (faceDetectModes == null) {
   return CaptureRequest.STATISTICS_FACE_DETECT_MODE_FULL;
  } else {
   return faceDetectModes[faceDetectModes.length - 1];
  }
 }

 @RequiresApi(api = Build.VERSION_CODES.M)
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  sv = findViewById(R.id.sv);
  cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  surfaceHolder = sv.getHolder();
  surface = surfaceHolder.getSurface();
  surfaceHolder.addCallback(new SurfaceHolder.Callback() {
   @RequiresApi(api = Build.VERSION_CODES.M)
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    try {
     //         
     String[] cameraIdList = cameraManager.getCameraIdList();
     if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
      // TODO: Consider calling
      // Activity#requestPermissions
      // here to request the missing permissions, and then overriding
      // public void onRequestPermissionsResult(int requestCode, String[] permissions,
      //           int[] grantResults)
      // to handle the case where the user grants the permission. See the documentation
      // for Activity#requestPermissions for more details.
      return;
     }
     //           
     CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraIdList[1]);
     //             ,         
     faceDetectModes = characteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);//         
     cameraManager.openCamera(cameraIdList[1], mStateCallback, null);
    } catch (CameraAccessException e) {
     e.printStackTrace();
    }
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {

   }
  });

 }

 /**
  *       
  */
 private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
  @Override
  public void onOpened(@NonNull final CameraDevice camera) {
   //    ,    CameraDevice  
   try {
    camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
     @Override
     public void onConfigured(@NonNull CameraCaptureSession session) {
      try {
       CaptureRequest.Builder captureRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
       captureRequest.addTarget(surface);
       captureRequest.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
       captureRequest.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, getFaceDetectMode());
       session.setRepeatingRequest(captureRequest.build(),captureCallback,new Handler());
      } catch (CameraAccessException e) {
       e.printStackTrace();
      }
     }

     @Override
     public void onConfigureFailed(@NonNull CameraCaptureSession session) {

     }
    }, new Handler());
   } catch (CameraAccessException e) {
    e.printStackTrace();
   }

  }

  @Override
  public void onDisconnected(@NonNull CameraDevice camera) {
   //    
   Toast.makeText(MainActivity.this, "onDisconnected", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onError(@NonNull CameraDevice camera, final int error) {
   //    
   Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
  }
 };


 /**
  *     
  */
 private CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
  @Override
  public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
   super.onCaptureStarted(session, request, timestamp, frameNumber);
  }

  @Override
  public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
   super.onCaptureProgressed(session, request, partialResult);
  }

  @Override
  public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
   super.onCaptureCompleted(session, request, result);
   Face faces[] = result.get(CaptureResult.STATISTICS_FACES);
   if (faces.length != face) {
    Log.e("lee", faces.length + "  ");
    face = faces.length;  
   }
  }

  @Override
  public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
   super.onCaptureFailed(session, request, failure);
  }

  @Override
  public void onCaptureSequenceCompleted(@NonNull CameraCaptureSession session, int sequenceId, long frameNumber) {
   super.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
  }

  @Override
  public void onCaptureSequenceAborted(@NonNull CameraCaptureSession session, int sequenceId) {
   super.onCaptureSequenceAborted(session, sequenceId);
  }

  @Override
  public void onCaptureBufferLost(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull Surface target, long frameNumber) {
   super.onCaptureBufferLost(session, request, target, frameNumber);
  }
 };
}
xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">

 <SurfaceView
  android:id="@+id/sv"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" />
</LinearLayout>
以上で、現在のカメラの下にどれぐらいの人の顔があるかを統計することができます。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。