使用ML KIT在Android中进行人脸检测

时间:2020-04-30 06:48:57

标签: android firebase-mlkit glsurfaceview

我想从热像仪的流视频中检测人脸。热像仪的供应商向我提供了用于连接到热像仪并从中传输视频的源代码。

每次在摄像机上更改视频数据时,都会执行一个回调函数。 因此,每次回调函数调用时,我都会进行检测:

1-从GLView(扩展了GLSurfaceView)获取当前的位图图像。

2-将位图图像传递给AsyncTask以使用ML kit视觉库对其进行处理。

3-当库检测到人脸时,位图图像将保存到存储中。

以下是我正在使用的源代码:


private void setCallback() {
        LiveAboutImplement.getInstance().setmCallback(new LiveAboutInterface() {

            @Override
            public void getVideoDataCallback(String did, byte[] h264, int type,
                                             int size, int onlinenum, int timesec, AVFrame avframe) {

                if (type == 1) {
                    StartisIFrame = true;
                }
                if (StartisIFrame) {
                    if (recodeHelper.isSavingVideo()) {
                        recodeHelper.saveAvFrame(avframe);
                    }
                }
                // the data decoded to the GL view live_monitorgl
                live_monitorgl.decoderVideoDataOpenGL(DemoLiveActivity.this,did, h264, type, size, onlinenum, timesec, avframe,live_monitorgl);

                // here I check the time
                if (timesec > lastTimesec)
                {
                    try {
                        // get the last playing view as bitmap & pass it to the AsyncTask
                        Bitmap bitmap = live_monitorgl.getLastPlayingView();
                        new ProcessCurrentFrame().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, bitmap);
                    }
                    catch (Exception ex) {// do nothing
                    }
                    lastTimesec = timesec;
                }
            }
        });
    }

AsyncTask代码:

private class ProcessCurrentFrame extends AsyncTask<Bitmap, Void, Void>
    {

        @Override
        protected Void doInBackground(Bitmap... bitmaps)
        {
            if (bitmaps.length>0)
            {
                final Bitmap bitmapImageFromGLView = bitmaps[0];
                if (bitmapImageFromGLView!=null)
                {
                    //Detect the face using ML kit api
                    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmapImageFromGLView);
                    detector.detectInImage(image).addOnSuccessListener(
                        new OnSuccessListener<List<FirebaseVisionFace>>()
                        {
                            @Override
                            public void onSuccess(List<FirebaseVisionFace> faces) {
                                // Task completed successfully
                                // ...
                                Log.d("IN Background","====== PROCESSING ======");
                                if (faces.size()>0)
                                {
                                    Log.d("HEHEHE", "====== Face Found ========");
                                    synchronized (bitmapImageFromGLView) {
                                        SavePhoto savePhoto = new SavePhoto(bitmapImageFromGLView, DemoLiveActivity.this, handler);
                                        String APPLICATION_SAVE_URL = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/cameraApp/";
                                        File var20 = new File(APPLICATION_SAVE_URL);
                                        savePhoto.savePhotoToPath(APPLICATION_SAVE_URL, var20.getAbsoluteFile() + "/image"+count+".jpg");
                                        count++;
                                        System.gc();
                                    }
                                }
                            }
                        });
                }
            }
            return null;
        }
    }

以前的代码检测人脸的速度非常慢,我不确定是否每秒启动一个新的AsynkTask来处理当前的位图是否是一个好习惯。

是否有更好的解决方案?

0 个答案:

没有答案