mediapipe中的人脸检测(java)代码初探

1.android代码,从创建activity说起.

facedetectioncpu(我们暂时讲cpu版本) 下的MainActivity.java文件.

a. 毫无疑问,我们来看看.

protected void onCreate(Bundle savedInstanceState);

 

在创建activity的时候,做了些什么工作呢?

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previewDisplayView = new SurfaceView(this);
    setupPreviewDisplayView();

    // Initialize asset manager so that MediaPipe native libraries can access the app assets, e.g.,
    // binary graphs.
    AndroidAssetUtil.initializeNativeAssetManager(this);

    eglManager = new EglManager(null);
    processor =
        new FrameProcessor(
            this,
            eglManager.getNativeContext(),
            BINARY_GRAPH_NAME,
            INPUT_VIDEO_STREAM_NAME,
            OUTPUT_VIDEO_STREAM_NAME);
    processor.getVideoSurfaceOutput().setFlipY(FLIP_FRAMES_VERTICALLY);

以上是 部分代码.

b.加载了 layout文件夹下的 activity_main.xml 文件.

c.创建了 SurfaceView ..

然后设置一下 预览的视图 参数. 下面是代码:

    previewDisplayView.setVisibility(View.GONE);
    ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
    viewGroup.addView(previewDisplayView);

只是 部分代码.

只是刚开始,控件是不可见的,

然后去xml 文件 去找这个控件,添加之.

Surface的 三种状态 回调代码就 不贴出了,  A.创建,B. 改变 C. 销毁 (这三个)

 

2.接着我们继续看

AndroidAssetUtil.initializeNativeAssetManager(this);

从 字面本意上看,是初始化本地assert 文件管理.

跟踪进去看,原来这个是静态函数,实际上调用的是 native 写的.

  public static boolean initializeNativeAssetManager(Context androidContext) {
    return nativeInitializeAssetManager(
        androidContext, androidContext.getCacheDir().getAbsolutePath());
  }

至于这个c/c++代码,实现了啥,这里暂时不跟下去.

 

3.接着我们继续往下看.

    eglManager = new EglManager(null);

哦,这个原来是 OpenGL 的 初始化地方.

那它做了神嘛呢?

点进去看了下.

原来 EglManager(null),只是它其中的一个构造函数.调用了下面这个

  public EglManager(@Nullable Object parentContext, @Nullable int[] additionalConfigAttributes) {
    singleIntArray = new int[1];
    egl = (EGL10) EGLContext.getEGL();
    eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
      throw new RuntimeException("eglGetDisplay failed");
    }
    int[] version = new int[2];
    if (!egl.eglInitialize(eglDisplay, version)) {
      throw new RuntimeException("eglInitialize failed");
    }
....
}

这里,我们不过多介绍了,我们理解它 可以正确完成初始化就好.

 

4.继续往下看...

    processor =
        new FrameProcessor(
            this,
            eglManager.getNativeContext(),
            BINARY_GRAPH_NAME,
            INPUT_VIDEO_STREAM_NAME,
            OUTPUT_VIDEO_STREAM_NAME);

其中的 BINRAY_GRAPH_NAME 变量是:     facedetectioncpu.binarypb

从代码上看 是要加载这个文件.(估计这个是核心部分)

 

事实上,在我们创建app 时,bazel build的时候,做了一些预处理.

来看我么的BUILD 文件.

genrule(
    name = "binary_graph",
    srcs = ["//mediapipe/graphs/face_detection:mobile_cpu_binary_graph"],
    outs = ["facedetectioncpu.binarypb"],
    cmd = "cp $< $@",
)
android_library(
    name = "mediapipe_lib",
    srcs = glob(["*.java"]),
    assets = [
        ":binary_graph",
        "//mediapipe/models:face_detection_front.tflite",
        "//mediapipe/models:face_detection_front_labelmap.txt",
    ],
...

)

 

在 graphs下的 face_detection下的BUILD 文件里

mediapipe_binary_graph(
    name = "mobile_cpu_binary_graph",
    graph = "face_detection_mobile_cpu.pbtxt",
    output_name = "mobile_cpu.binarypb",
    deps = [":mobile_calculators"],
)

恩?

那么face_detection_mobile_cpu.pbtxt 这个文件是干嘛的呢?

难道就是上面 两三段代码,将定义的 xx.pbtxt 与 xx_front.tflite 和 xx_front_labelmap.txt  相关联的?

 

5.进去看了看(face_detection_mobile_cpu.pbtxt)

定义了一些Node,还有算子,(包括配置算子参数等)

难道这里面 就是真正实现了 检测人脸算法的过程?(一系列,甚至包括,人脸检测出来信息,框体位置转化,甚至绘制在表面都在这里完成? )

 

然后 就初始化了,跑起来了?

(是这样的,看完代码之后才知道,原来检测速度流畅是有原因的)

后面没什么代码了 (就是打开摄像头.(如果权限有的话,就打开摄像头...))

代码是:

  @Override
  protected void onResume() {
    super.onResume();
    converter = new ExternalTextureConverter(eglManager.getContext());
    converter.setFlipY(FLIP_FRAMES_VERTICALLY);
    converter.setConsumer(processor);
    if (PermissionHelper.cameraPermissionsGranted(this)) {
      startCamera();
    }
  }

 

作者对加载过程还是不太明白,如有正在研究这框架的伙伴,欢迎 各位大佬 指点迷津:

留言即可.谢谢啦

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teleger

你的支持是我前进的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值