[Huawei Developers] John Wo:
Have you ever been sent compressed images that have poor definition? Even when you zoom in, the image is still blurry. I recently received a ZIP file of travel photos from a trip I went on with a friend. After opening it, I found to my dismay that each image was either too dark, too dim or too blurry. How am I going to show off with such terrible photos? So, I sought help from the Internet, and luckily, I came across HUAWEI ML Kit's image super-resolution capability. The amazing thing is that this SDK is free of charge, and can be used with all Android phones.

Background

ML Kit's image super-resolution capability is backed by a deep neural network and provides two super-resolution capabilities for mobile apps:

1x super-resolution: Doesn’t change the size but removes compression noise, for vivid, natural images.

3x super-resolution: Provides 3x magnification (with 9x higher pixel resolution) while also removing compression noise, for clear details and textures.

Of course, these capabilities are not easy for non-professionals to develop themselves. But here is the sample code, so you can download and experience it yourself!

Application Scenarios

Image super-resolution is not just about optimizing faces and text; it’s useful in a huge range of situations. When shopping apps integrate 3x image super-resolution, they can give shoppers the option to zoom in on a product, without losing any image quality. For news and reading apps, the 1x image super-resolution capability can give users clearer images without changing the image resolution. Then, for photography apps which integrate image super-resolution, users can capture more vivid and natural images.

Code Development

Before we get started with API development, we need to make necessary development preparations. Also, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.

Note

When using the image super-resolution capability, you need to set the value of targetSdkVersion to less than 29 in the build.gradle file.

1. Create an image super-resolution analyzer.

You can create the analyzer using the MLImageSuperResolutionAnalyzerSetting class.

// Method 1: Use default parameter settings, that is, the 1x super-resolution capability.
MLImageSuperResolutionAnalyzer analyzer = MLImageSuperResolutionAnalyzerFactory.getInstance
().getImageSuperResolutionAnalyzer();
// Method 2: Use custom parameter settings. Currently, only 1x super-resolution is supported. More capabilities will be supported in future.
MLImageSuperResolutionAnalyzerSetting settings = new MLImageSuperResolutionAnalyzerSetting.Factory()
// Set the image super-resolution multiplier to 1x.
setScale(MLImageSuperResolutionAnalyzerSetting.ISR_SCALE_1X)
create();
MLImageSuperResolutionAnalyzer analyzer = MLImageSuperResolutionAnalyzerFactory.getInstance
().getImageSuperResolutionAnalyzer(setting)

2. Create an MLFrame object by using android.graphics.Bitmap. (Note that the bitmap type must be ARGB8888. Convert if necessary.)

// Create an MLFrame object using the bitmap. The parameter bitmap indicates the input image.
MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();

3. Perform super-resolution processing on the image. (For details about the error codes, please refer to ML Kit Error Codes).

Task<MLImageSuperResolutionResult> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLImageSuperResolutionResult>() {
public void onSuccess(MLImageSuperResolutionResult result) {
// Processing logic for detection success.
}})
addOnFailureListener(new OnFailureListener() {
public void onFailure(Exception e) {
// Processing logic for detection failure.
// failure.
if(e instanceof MLException) {
MLException mlException = (MLException)e;
// Obtain the error code. You can process the error code and customize the messages users see.
int errorCode = mlException.getErrCode();
// Obtain the error information. You can quickly locate the fault based on the error code.
String errorMessage = mlException.getMessage();
} else {
// Other errors.
}
}
});

4. Stop the analyzer to release recognition resources after the recognition is complete.

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

Demo

Now, let's compare some before and after images, to see what this capability can do.

Original image

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121536.33824654645112847942339417953832:50511014090351:2800:084E7C595BEAC9CE9845701C577B054E56E3D5F16DD76BBCB5047424F61B9A8F.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121536.33824654645112847942339417953832:50511014090351:2800:084E7C595BEAC9CE9845701C577B054E56E3D5F16DD76BBCB5047424F61B9A8F.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121539.33525747819311503317577722704108:50511014111513:2800:8338120B2D81A49B6A7CF43CD2A267355034452D22C2A9BFAFBE3A6C1745B62E.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121539.33525747819311503317577722704108:50511014111513:2800:8338120B2D81A49B6A7CF43CD2A267355034452D22C2A9BFAFBE3A6C1745B62E.jpg[/IMG]

Image after 1x super-resolution

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121605.68996422074540387694246371339442:50511014090351:2800:AA39F20C68C2A0760BF5D22DFF4DBB1739D141F2AF8285C5C5C6F3C2C1BCB657.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121605.68996422074540387694246371339442:50511014090351:2800:AA39F20C68C2A0760BF5D22DFF4DBB1739D141F2AF8285C5C5C6F3C2C1BCB657.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121608.87795714431750322656668332198540:50511014090351:2800:C7F8076A754D0F6058BC0035AC9C81740D6AF07DD10401EDEF76847D83DD2962.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121608.87795714431750322656668332198540:50511014090351:2800:C7F8076A754D0F6058BC0035AC9C81740D6AF07DD10401EDEF76847D83DD2962.jpg[/IMG]

Original image

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121646.00457677649940752740266288482695:50511014090351:2800:9B66A3DF177720C7B8CEC6C2304A34D853B21E2801C74458570421D7CA18F608.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121646.00457677649940752740266288482695:50511014090351:2800:9B66A3DF177720C7B8CEC6C2304A34D853B21E2801C74458570421D7CA18F608.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121651.14922882559349744754556624816864:50511014090351:2800:C313216450FD00A520117C069B0FAE33A31F5D2FEA147668036E1A2EBA10485F.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121651.14922882559349744754556624816864:50511014090351:2800:C313216450FD00A520117C069B0FAE33A31F5D2FEA147668036E1A2EBA10485F.jpg[/IMG]

Image after 3x super-resolution

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121743.95254138633674905643752005722038:50511014090351:2800:227D91D6178E7686CA8203DD7B0DE1A887325A912C2CCE08A522BA7EFED60A1F.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121743.95254138633674905643752005722038:50511014090351:2800:227D91D6178E7686CA8203DD7B0DE1A887325A912C2CCE08A522BA7EFED60A1F.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121747.26086041532260969104542435925223:50511014090351:2800:AC51365065D4C91E32015D48921A46FB2811EE171B890E22BD7FD385E5DB67EF.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200928121747.26086041532260969104542435925223:50511014090351:2800:AC51365065D4C91E32015D48921A46FB2811EE171B890E22BD7FD385E5DB67EF.jpg[/IMG]

Related Links

With HUAWEI ML Kit, you can integrate a wide range of capabilities and services into your apps, including text recognition, card recognition, text translation, facial recognition, speech recognition, text to speech, image classification, image segmentation, and product visual search.

If you’re interested, go to HUAWEI ML Kit - About the Service to learn more.

To find out more about these capabilities and services, here are some useful resources:

https://developer.huawei.com/consumer/cn/doc/31403