Android development actual combat, the division SDK with Huawei to develop a passport picture HMS MLKit DIY small program

Primer

On stage to introduce how to use How Huawei HMS MLKit SDK thirty minutes at Andrews on the development of a smiling snapshot artifact please stamp , this time to share with a new hands-on experience.
I do not know if you have such experience, companies need to provide the school or suddenly let an inch or two inches to provide a personal profile picture, to apply for passes, what student card, and the background of the photo is required, there are a lot of people not currently need to take good passport photo studio remake, or has previously been filmed, but the photo does not meet the requirements of background, small series have had a similar experience when school lets do a grounds pass, school and studio closed , and took a cell phone under the rush, then use the sheet as a background to deal with, the result was the teacher he lashed out.
Years later Huawei's HMS MLKit machine learning with image segmentation function, use this SDK to develop a small program DIY passport can be the perfect solution to those that confronted the embarrassment small series.
Ado, in order to create a strong visual impact, Xiao Bian also fight, and then turn out embarrassing photos college days, to show you the power of Huawei at HMS MLKit: the
Here Insert Picture Description
Here Insert Picture Description
how, the effect is OK, just write a small program you can quickly achieve!

Core Tip: This free SDK, Android models full coverage!

DIY develop real passport

1 Development Readiness

1.1 Adding Huawei maven warehouse at the project level in gradle

Open AndroidStudio project level build.gradle file.
Here Insert Picture Description
Incremental maven add the following address:

buildscript {
    repositories {        
        maven {url 'http://developer.huawei.com/repo/'}
    }    
}
allprojects {
    repositories {       
        maven { url 'http://developer.huawei.com/repo/'}
    }
}

1.2 application level dependent build.gradle inside with SDK

The face recognition SDK and SDK introduced foundation

dependencies{ 
  // 引入基础SDK 
  implementation 'com.huawei.hms:ml-computer-vision:1.0.2.300' 
  // 引入人脸检测能力包 
  implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:1.0.2.301'  
 }

1.3 Adding model automatically downloads a file called AndroidManifest.xml inside increment

To make an application after the user can install your applications market applications from Huawei, automatic machine learning models the latest update to the user equipment, add the following statement to the AndroidManifest.xml file for the application:

<manifest    
   <application  
       <meta-data                     
           android:name="com.huawei.hms.ml.DEPENDENCY"          
           android:value= "imgseg "/>        	        
   </application>
</manifest> 

1.4 AndroidManifest.xml file inside the camera application and storage rights

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

2 code key step in the development of

2.1 Dynamic Access Request

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (!allPermissionsGranted()) {
        getRuntimePermissions();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode != PERMISSION_REQUESTS) {
        return;
    }
    boolean isNeedShowDiag = false;
    for (int i = 0; i < permissions.length; i++) {
        if (permissions[i].equals(Manifest.permission.READ_EXTERNAL_STORAGE) && grantResults[i] != PackageManager.PERMISSION_GRANTED) {
            // 如果相机或者存储权限没有授权,则需要弹出授权提示框
            isNeedShowDiag = true;
        }
    }
    if (isNeedShowDiag && !ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage(getString(R.string.camera_permission_rationale))
                .setPositiveButton(getString(R.string.settings), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getPackageName())); // 根据包名打开对应的设置界面
                        startActivityForResult(intent, 200);
                        startActivity(intent);
                    }
                })
                .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).create();
        dialog.show();
    }
}

2.2 Creating image segmentation detector

You may be divided by an image detector configured "MLImageSegmentationSetting" Create image segmentation detector.

MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
                .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
                .setExact(true)
                .create();        
this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);

2.3 by android.graphics.Bitmap create "MLFrame" object is used to detect whether an image analyzer

You may be divided by an image detector configured "MLImageSegmentationSetting" Create image segmentation detector.

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

2.4 calls "asyncAnalyseFrame" method for image segmentation.

// 创建一个task,处理图像分割检测器返回的结果。 
Task<MLImageSegmentation> task = analyzer.asyncAnalyseFrame(frame); 
// 异步处理图像分割检测器返回结果 
Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame);
            task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() {
                @Override
                public void onSuccess(MLImageSegmentation mlImageSegmentationResults) {
                    // Transacting logic for segment success.
                    if (mlImageSegmentationResults != null) {
                        StillCutPhotoActivity.this.foreground = mlImageSegmentationResults.getForeground();
                        StillCutPhotoActivity.this.preview.setImageBitmap(StillCutPhotoActivity.this.foreground);
                        StillCutPhotoActivity.this.processedImage = ((BitmapDrawable) ((ImageView) StillCutPhotoActivity.this.preview).getDrawable()).getBitmap();
                        StillCutPhotoActivity.this.changeBackground();
                    } else {
                        StillCutPhotoActivity.this.displayFailure();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    // Transacting logic for segment failure.
                    StillCutPhotoActivity.this.displayFailure();
                    return;
                }
            });

2.5 Change picture background.

this.backgroundBitmap = BitmapUtils.loadFromPath(StillCutPhotoActivity.this, id, targetedSize.first, targetedSize.second);
BitmapDrawable drawable = new BitmapDrawable(backgroundBitmap);
this.preview.setDrawingCacheEnabled(true);
this.preview.setBackground(drawable);
this.preview.setImageBitmap(this.foreground);
this.processedImage = Bitmap.createBitmap(this.preview.getDrawingCache());
this.preview.setDrawingCacheEnabled(false);

After the speech knot

In this way, a document DIY small program on the production well, for everyone to look Demo demonstration effect:
Here Insert Picture Description
if you are strong hands, it can be added to replace the suit and other operations, github source code has been uploaded, you can also on github this feature is perfect together.
github source address please poke(Project directory: ID-Photo-DIY)

Image segmentation based on the ability to not only be used to make DIY passport program, you can achieve the following related functions:
1, life portrait photo matting, change the background to make some interesting photos, or blur the background to do to get more beautiful, more photo artistic effect.
2. Identify the image of the sky, plants, food, dogs and cats, flowers, water, etc., sand, buildings, mountains and other elements, do the landscaping for these special elements, such as to make the sky bluer, the water more clear
3, identifying video stream object, stream video effects editing, change the background.

Other features please open brain with holes it!

More detailed reference guide developed by Huawei's development league's official website:
Huawei Developer Connection Machine Learning Services Development Guide

Past Links:
The first phase: with Huawei HMS MLKit SDK thirty minutes on the development of a smiling snapshot artifact Andrews

Next Issue

Huawei machine learning based services, will be behind a series of hands-on experience to share, we can continue to focus -

Released two original articles · won praise 6 · views 1550

Guess you like

Origin blog.csdn.net/AI_talking/article/details/104997952