1

从菜鸟到高手, HMS Core图像分割服务教你如何在复杂背景里精细抠图

 2 years ago
source link: https://blog.51cto.com/u_14815220/5069152
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

从菜鸟到高手, HMS Core图像分割服务教你如何在复杂背景里精细抠图

原创

2021年以来,自动驾驶赛道进入爆发期,该行业成为大厂以及初创企业的必争之地。其中众多公司都采用了计算机视觉作为自动驾驶的技术底座,通过图像分割技术,汽车才能够有效理解道路场景,分清楚哪里是路,哪里是人。除了自动驾驶领域,图像分割技术也常出现在其他重要的场景中,比如:

  • 医疗图像分割:帮助医生进行诊断测试

  • 卫星图像分析:适合深入研究大量图像数据

  • 影像娱乐类App:人像抠图、避免视频弹幕遮住人脸

因此,图像分割技术的应用十分重要且广泛。HMS Core机器学习服务图像分割服务采用了具有创新意义的语义分割框架。这种框架将图像中的每个像素点都标签化,即使是发丝细节都可以清晰完整的保留。另外,图像分割服务还提升了对于不同画质、不同尺寸图片的处理能力,针对分割算法常常出现的白边,采用更加结构化学习的训练方式,使边缘更加自然。

那么如此稳定、精细化的分割能力,到底如何实现?

一、开发准备

Maven仓和SDK的配置步骤可以参考开发者网站中的应用开发介绍:
 https://developer.huawei.com/consumer/cn/doc/development/hiai-Guides/image-segmentation-0000001050040109#section1658976113112?ha_source=hms1

1 配置华为Maven仓地址。

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}
 
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

2 添加编译SDK依赖

dependencies {
    // 引入基础SDK
implementation 'com.huawei.hms:ml-computer-vision-segmentation:2.1.0.301'
    // 引入人像分割模型包
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:2.1.0.303'
}

3 在AndroidManifest.xml中添加权限

// 使用存储权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

二、开发步骤

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (!allPermissionsGranted()) {
        getRuntimePermissions();
    }
}
private boolean allPermissionsGranted() {
    for (String permission : getRequiredPermissions()) {
        if (!isPermissionGranted(this, permission)) {
            return false;
        }
    }
    return true;
}
private void getRuntimePermissions() {
    List<String> allNeededPermissions = new ArrayList<>();
    for (String permission : getRequiredPermissions()) {
        if (!isPermissionGranted(this, permission)) {
            allNeededPermissions.add(permission);
        }
    }
    if (!allNeededPermissions.isEmpty()) {
        ActivityCompat.requestPermissions(
                this, allNeededPermissions.toArray(new String[0]), PERMISSION_REQUESTS);
    }
}
private static boolean isPermissionGranted(Context context, String permission) {
    if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) {
        return true; 
    }
    return false; 
}
private String[] getRequiredPermissions() { 
    try {
        PackageInfo info =
                this.getPackageManager()
                        .getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS); 
        String[] ps = info.requestedPermissions;
        if (ps != null && ps.length > 0) { 
            return ps;
        } else { 
            return new String[0];
        } 
    } catch (RuntimeException e) {
        throw e; 
    } catch (Exception e) { 
        return new String[0];
    }
}

2 创建图片分割检测器

MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
// 设置为人像分割
        .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
        .create();
this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);

3 通过android.graphics.Bitmap创建“MLFrame”对象用于分析器检测图片

MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create();

4 调用“asyncAnalyseFrame ”方法进行图像分割

// 创建一个task,处理图像分割检测器返回的结果。
Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame);
// 异步处理图像分割检测器返回的结果。
task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() {
    @Override
    public void onSuccess(MLImageSegmentation mlImageSegmentationResults) {.
        if (mlImageSegmentationResults != null) {
//获得从图片中分割出的人像前景图
            foreground = mlImageSegmentationResults.getForeground();
            preview.setImageBitmap(MainActivity.this.foreground);
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        return;
    }
});

5 更换图片背景

// 从相册中获取图片
backgroundBitmap = Utils.loadFromPath(this, id, targetedSize.first, targetedSize.second);
BitmapDrawable drawable = new BitmapDrawable(backgroundBitmap);
preview.setBackground(drawable);
preview.setImageBitmap(this.foreground);

6 更换图片背景

MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create();

(demo演示视频如下)
从菜鸟到高手, HMS Core图像分割服务教你如何在复杂背景里精细抠图_图像分割

了解更多详情>>

访问 华为开发者联盟官网
获取 开发指导文档
华为移动服务开源仓库地址: GitHub Gitee

关注我们,第一时间了解 HMS Core 最新技术资讯~

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK