手势识别
手势识别,对设备的要求较高,推荐设备为小米8、小米9。
开发准备
(1).添加类库和开源代码
手势识别,采用开源的方式提供,将类库handtrackinggpu_deploy.jar和libmediapipe_jni.so、libopencv_java4.so等so文件加载到工程中。将开源代码下载解压后加入工程。类库和开源代码下载地址为:https://pan.baidu.com/s/1PUk4K3k2YC9iFl2-saDLmQ,提取码:ztdn。
图:“手势识别“添加类库示意 |
图:“手势识别”添加开源代码示意 |
(2).添加模型文件
将模型文件,拷贝到工程“..\app\src\main\assets”目录下。模型下载地址:https://pan.baidu.com/s/1PUk4K3k2YC9iFl2-saDLmQ,提取码:ztdn。
图:“手势识别”加载训练模型示意 |
编写代码
//设置预览显示视图
private void setupPreviewDisplayView() {
previewDisplayView.setVisibility(View.GONE);
ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
viewGroup.addView(previewDisplayView);
previewDisplayView.getHolder().addCallback(
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Size viewSize = new Size(width, height);
Size displaySize = cameraHelper.computeDisplaySizeFromViewSize(viewSize);
converter.setSurfaceTextureAndAttachToGLContext(
previewFrameTexture, displaySize.getWidth(), displaySize.getHeight());
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(null);
}
});
}
// 初始化
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);
PermissionHelper.checkAndRequestCameraPermissions(this);
//打开摄像头
cameraHelper = new CameraXPreviewHelper();
cameraHelper.setOnCameraStartedListener(new CameraHelper.OnCameraStartedListener() {
@Override
public void onCameraStarted(SurfaceTexture surfaceTexture) {
previewFrameTexture = surfaceTexture;
previewDisplayView.setVisibility(View.VISIBLE);
}
});
cameraHelper.startCamera(this, CAMERA_FACING, null);//CAMERA_FACING:前置摄像头
图:手势识别 |
以上示例代码下载地址:https://pan.baidu.com/s/1PUk4K3k2YC9iFl2-saDLmQ,提取码:ztdn。