视频捕捉人脸检测-安卓

Posted

技术标签:

【中文标题】视频捕捉人脸检测-安卓【英文标题】:Video Capturing Face Detection- Android 【发布时间】:2016-02-25 06:38:08 【问题描述】:

为了在视频捕获期间跟踪/检测面部。我已经达到能够捕获和存储视频。现在我在其中添加人脸检测。我现在正在视频捕获期间检测面部。 如何检查面部检测是否正常工作。我可以做出哪些改变

public class androidVideoCaptureExample extends Activity 
private Camera mCamera;
private int vWidth, vHeight;
private CameraPreview mPreview;
private MediaRecorder mediaRecorder;
private Button capture, switchCamera;
private Context myContext;
private LinearLayout cameraPreview;
private boolean cameraFront = false;
private int desiredwidth = 640, desiredheight = 360;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    myContext = this;
    initialize();


private int findFrontFacingCamera() 
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) 
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 
            cameraId = i;
            cameraFront = true;
            break;
        
    
    return cameraId;


public void onResume() 
    super.onResume();
    if (!hasCamera(myContext)) 
        Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
        toast.show();
        finish();
    
    if (mCamera == null) 
        // if the front facing camera does not exist
        if (findFrontFacingCamera() < 0) 
            Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
            switchCamera.setVisibility(View.GONE);
        
        mCamera = Camera.open(findFrontFacingCamera());
        mCamera.setFaceDetectionListener(new FaceDetectionListener());
        mCamera.setDisplayOrientation(90);
        mPreview.refreshCamera(mCamera);
        startFaceDetection();
    


public void initialize() 
    cameraPreview = (LinearLayout) findViewById(R.id.camera_preview);

    mPreview = new CameraPreview(myContext, mCamera);
    cameraPreview.addView(mPreview);

    capture = (Button) findViewById(R.id.button_capture);
    capture.setOnClickListener(captrureListener);





@Override
protected void onPause() 
    super.onPause();
    // when on Pause, release camera in order to be used from other
    // applications
    releaseCamera();


private boolean hasCamera(Context context) 
    // check if the device has camera
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 

        return true;
     else 
        return false;
    


boolean recording = false;
OnClickListener captrureListener = new OnClickListener() @Override
    public void onClick(View v) 
        if (recording) 
            // stop recording and release camera
            mediaRecorder.stop(); // stop the recording
            releaseMediaRecorder(); // release the MediaRecorder object
            Toast.makeText(AndroidVideoCaptureExample.this, "Video captured!", Toast.LENGTH_LONG).show();

            Toast.makeText(AndroidVideoCaptureExample.this, vWidth + "BY" + vHeight, Toast.LENGTH_LONG).show();

            recording = false;
         else 
            if (!prepareMediaRecorder()) 
                Toast.makeText(AndroidVideoCaptureExample.this, "Failed in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
                finish();
            
            // work on UiThread for better performance
            runOnUiThread(new Runnable() 
                public void run() 
                    // If there are stories, add them to the table

                    try 
                        mediaRecorder.start();

                     catch (final Exception ex) 
                        // Log.i("---","Exception in thread");
                    
                
            );

            recording = true;
        
    
;

private void releaseMediaRecorder() 
    if (mediaRecorder != null) 
        mediaRecorder.reset(); // clear recorder configuration
        mediaRecorder.release(); // release the recorder object
        mediaRecorder = null;
        mCamera.lock(); // lock camera for later use
    


private boolean prepareMediaRecorder() 

    List < Camera.Size > videosizes = mCamera.getParameters().getSupportedVideoSizes();
    Camera.Size videosize = videosizes.get(1);
    //Camera.Size optimalPreviewSize = getOptimalPreviewSize(previewsizes, desiredwidth, desiredheight);
    Camera.Size optimalVideoSize = getOptimalPreviewSize(videosize, desiredwidth, desiredheight);
    vWidth = optimalVideoSize.width; //mCamera.getParameters().getPreviewSize().width;
    vHeight = optimalVideoSize.height; //mCamera.getParameters().getPreviewSize().height;

    mediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);

    //startFaceDetection();
    //mPreview.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);
    //mCamera.unlock();
    //mMediaRecorder = new MediaRecorder();
    //mMediaRecorder.setCamera(mCamera);
    //mMediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
    //myCamera.setParameters(p);
    mediaRecorder.setAudiosource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    mediaRecorder.setVideoEncodingBitRate(512 * 1000);
    mediaRecorder.setVideoFrameRate(15);
    mediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
    mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
    mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M

    try 
        mediaRecorder.prepare();
     catch (IllegalStateException e) 
        releaseMediaRecorder();
        return false;
     catch (IOException e) 
        releaseMediaRecorder();
        return false;
    
    return true;



private void releaseCamera() 
    // stop and release camera
    if (mCamera != null) 
        mCamera.release();
        mCamera = null;
    


private Camera.Size getOptimalPreviewSize(Camera.Size sizes, int w, int h) 
    final double ASPECT_TOLERANCE = 0.2;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    Camera.Size size = sizes;

    Log.d("Camera", "Checking size " + size.width + "w " + size.height + "h");
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) <= ASPECT_TOLERANCE) if (Math.abs(size.height - targetHeight) < minDiff) 
        optimalSize = size;
        minDiff = Math.abs(size.height - targetHeight);
    

    // Cannot find the one match the aspect ratio, ignore the
    // requirement
    if (optimalSize == null) 
        minDiff = Double.MAX_VALUE;
        if (Math.abs(size.height - targetHeight) < minDiff) 
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        

    
    return optimalSize;



public void startFaceDetection() 
    // Try starting Face Detection
    Camera.Parameters params = mCamera.getParameters();

    // start face detection only *after* preview has started
    if (params.getMaxNumDetectedFaces() > 0) 
        // camera supports face detection, so can start it:
        mCamera.startFaceDetection();
    

这里是人脸检测监听类:

public class FaceDetectionListener implements Camera.FaceDetectionListener 

@Override
public void onFaceDetection(Camera.Face[] faces, Camera c)

    if (faces.length > 0)
    Log.d("FaceDetection", "face detected: " + faces.length +
            " Face 1 Location X: " + faces[0].rect.centerX() +
            "Y: " + faces[0].rect.centerY());

    

【问题讨论】:

如果我在 Google 上正确搜索,该 API 自 API 级别 21 以来似乎已被弃用。 好的,保持视频捕获代码不变。如何实现人脸检测部分。 FaceDetectionListener 可用于视频捕获(请参阅Camera 类)。并且它由有限的设备集支持,因为它取决于硬件。对于软件人脸检测或本地文件,您必须使用外部库,如 OpenCV 嗨!我也在做同样的事情。我使用了你的代码,但无法捕捉到面部检测到的视频。 logcat 什么的?没有它我不能告诉你原因。 【参考方案1】:

我使用 Mobile Vision API 从实时视频中进行人脸检测,您可以搜索 Mobile Vision API 进行人脸检测,它可以正常工作, 但我没有任何方法可以捕获检测到的面部并将检测到的面部从实时视频中保存到图库中

【讨论】:

很抱歉回复晚了..,请访问此链接了解 Mobile Vision API,arjunu.com/2015/08/face-detection-with-android-vision-api 我觉得你给的网站没用

以上是关于视频捕捉人脸检测-安卓的主要内容,如果未能解决你的问题,请参考以下文章

在检测到人脸时显示圆圈 - Android [重复]

我们公司要做人脸识别的考勤,哪里有免费的活体检测安卓版本SDK?可以识别真人的

人脸检测安卓

数码相机中的人脸检测有啥作用?使用人脸对焦的技巧和方法是啥?

Android之基于Facenet模型比对视频中的人脸

视频人脸检测——Dlib版