Retouching artifact-ultra-simple realization of Huawei HMS ML Kit image super-resolution

Preface

I don't know if you have encountered such a situation, the sharpness of the picture after the compression is received and downloaded, the picture quality is blurred, let alone the enlarged view. I recently encountered it. I received a compressed travel photo package from a friend. After opening it, OMG, the dim scene, the black portrait, and the blurry picture, how can I post it to the circle of friends to show off. I had no choice but to go online to ask for help. It really made me discover a super easy-to-use and easy-to-operate Huawei HMS ML Kit image super resolution. The point is that this SDK is completely free and applicable to various Android models.

Background introduction

The image super-resolution of Huawei HMS ML Kit is based on a deep neural network and provides 1x and 3x super-resolution capabilities applicable to mobile terminals. 1x super division removes compression noise, 3x super division provides 3 times amplification capability while suppressing compression noise. To put it simply and plainly, Huawei’s image super-resolution provides 1x and 3x services. 1x super-resolution does not change the size of the picture, but it improves the clarity of the picture and provides a more realistic and natural visual experience. 3x super-resolution is the edge of the image. The long magnification is 3 times, the pixel magnification is 9 times, the resolution is higher, and the detailed texture is clearer.

Related scenes

Image super-resolution is widely used in various scenes in real life (pictures of green plants, food, portraits, landscapes, etc.), not just to optimize human faces and text scenes. For example, shopping apps integrate this service. When users zoom in on product images, they can get clearer product details through the 3x function of ML Kit images. News reading apps integrate this service, and users can get clearer pictures through the 1x super-resolution function without changing the picture resolution. The camera APP integrates this service. When users take pictures, they can obtain more realistic and natural pictures through the image super-division function.

Development combat

1. Development Preparation

Before starting the API development work, you need to complete the necessary development preparations. At the same time, please make sure that the Maven warehouse address of the HMS Core SDK has been configured in your project, and the SDK integration of this service has been completed.

For related steps, please refer to Huawei Developer Alliance :

https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4

1.1 Configure Maven warehouse address in project-level gradle

buildscript {
    repositories {
             ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
}
 dependencies {
                 ...
        classpath 'com.huawei.agconnect:agcp:1.3.1.300'
    }
allprojects {
    repositories {
             ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

1.2 Configure SDK dependencies in application-level gradle

dependencies{       
        // 引入集合包。
        implementation 'com.huawei.hms:ml-computer-vision-imageSuperResolution:2.0.2.300'
        implementation 'com.huawei.hms:ml-computer-vision-imageSuperResolution-model:2.0.2.300'     
 }

note:

To use the image super-resolution service, you need to set the targetSdkVersion to be less than 29 in the build.gradle configuration file of the application.

1.3 Add configuration to the file header

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

1.4 Add the following statement to the AndroidManifest.xml file to automatically update the machine learning model to the device

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

1.5 Read local file permissions

<!--读权限-->
<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" />

2. Development steps

2.1 Create an image super-resolution analyzer.

The analyzer can be created by custom parameter class MLImageSuperResolutionAnalyzerSetting.

// 方式1:使用默认的参数配置即1x超分。
MLImageSuperResolutionAnalyzer analyzer =  MLImageSuperResolutionAnalyzerFactory.getInstance() .getImageSuperResolutionAnalyzer();
// 方式2:使用自定义的参数配置,当前仅支持1x超分,后续可扩展。
MLImageSuperResolutionAnalyzerSetting settings = new  MLImageSuperResolutionAnalyzerSetting  .Factory()
// 设置图像超分辨率倍数1x
setScale(MLImageSuperResolutionAnalyzerSetting.ISR_SCALE_1X) .create();
MLImageSuperResolutionAnalyzer analyzer =  MLImageSuperResolutionAnalyzerFactory.getInstance()  .getImageSuperResolutionAnalyzer(setting)

2.2 Construct MLFrame through android.graphics.Bitmap (note that the bitmap type here must be ARGB8888, please pay attention to the necessary conversion).

// 通过bitmap创建MLFrame,bitmap为输入的图片数据。
MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();

2.3 Super-resolution processing of pictures for
error code information can be found in: Machine Learning Service Error Code

Task<MLImageSuperResolutionResult> task = analyzer.asyncAnalyseFrame(frame); 
task.addOnSuccessListener(new OnSuccessListener<MLImageSuperResolutionResult>() {
        public void onSuccess(MLImageSuperResolutionResult result) {
                // 识别成功的处理逻辑。
        }
 }).addOnFailureListener(new OnFailureListener() {
        public void onFailure(Exception e) {
                // 识别失败的处理逻辑。
                // failure.
                if(e instanceof MLException) {
                         MLException mlException = (MLException)e;
                        // 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
                        int errorCode = mlException.getErrCode();
                        // 获取报错信息,开发者可以结合错误码,快速定位问题。
                        String errorMessage = mlException.getMessage();
                } else {
                        // 其他异常。
                }
        }
});

2.4 After the recognition is complete, stop the analyzer and release the detection resources.

if (analyzer != null) {
        analyzer.stop();
}

Demo

The editor for image super-division introduces so much content, the following is the effect comparison chart to show the power of this function.

Original image
Insert picture description here
1x renderings
Insert picture description here
Original image
Insert picture description here
3x renderings
Insert picture description here

Related Links

Huawei HMS ML Kit machine learning integrates functions and services such as text recognition, card recognition, text translation, face recognition, speech recognition, speech synthesis, picture classification, image segmentation, and photo shopping.

If you are interested, you can click the link below to learn:

https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-introduction-4

Github source code

For more detailed development guidelines, please refer to the official website of Huawei Developer Alliance


Original link: https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202348901052600500&fid=18 Author: leave leaves

Guess you like

Origin blog.51cto.com/14772288/2539216