[Huawei Developers] muraliameakula:
Everyone's family has some old photos filed away in an album. Despite the simple backgrounds and casual poses, these photos reveal quite a bit, telling stories and providing insight on what life was like in the past.

In anticipation of Father's Birthday, John, a programmer at Huawei, was racking his brains about what gift to get for his father. He thought about it for quite a while — then suddenly, a glimpse at an old photo album piqued his interest. "Why not using my coding expertise to restore my father's old photo, and shed light on his youthful personality?", he mused. Intrigued by this thought, John started to look into how he could achieve this goal.

Image super-resolution in HUAWEI ML Kit was ultimately what he settled on. With this service, John was able to convert the wrinkled and blurry old photo into a hi-res image, and presented it to his father. His father was deeply touched by the gesture.

Actual Effects:

Image Super-Resolution

This service converts an unclear, low-resolution image into a high-resolution image, increasing pixel intensity and displaying details that were missed when the image was originally taken.

Image super-resolution is ideal in computer vision, where it can help enhance image recognition and analysis capabilities. The Image super-resolution technology has improved rapidly, and weighs more in day-to-day work and life. It can be used to sharpen common images, such as portrait shots, as well as vital images in fields like medical imaging, security surveillance, and satellite imaging.

Image super-resolution offers both 1x and 3x super-resolution capabilities. 1x super-resolution removes compression noise, and 3x super-resolution effectively suppresses compression noise, while also providing a 3x enlargement capability.

The Image super-resolution service can help enhance images for a wide range of objects and items, such as greenery, food, and employee ID cards. You can even use it to enhance low-quality images such as news images obtained from the network into clear, enlarged ones.

Development Preparations

For more details about configuring the Huawei Maven repository and integrating the image super-resolution SDK, please refer to the Development Guide of ML Kit on HUAWEI Developers.

Configuring the Integrated SDK

Open the build.gradle file in the app directory. Add build dependencies for the image super-resolution SDK under the dependencies block.

implementation'com.huawei.hms:ml-computer-vision-imagesuperresolution:2.0.4.300'
implementation'com.huawei.hms:ml-computer-vision-imagesuperresolution-model:2.0.4.300'

Configuring the AndroidManifest.xml File

Open the AndroidManifest.xml file in the main folder. Apply for the storage read permission as needed by adding the following statement before <application>:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Add the following statements in <application>. Then the app, after being installed, will automatically update the machine learning model to the device.

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

Development Procedure

Configuring the Application for the Storage Read Permission

Check whether the app has had the storage read permission in onCreate() of MainActivity. If no, apply for this permission through requestPermissions; if yes, call startSuperResolutionActivity() to start super-resolution processing on the image.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
} else {

startSuperResolutionActivity();

}

Check the permission application results:

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startSuperResolutionActivity();
} else {

Toast.makeText(this, "Permission application failed, you denied the permission", Toast.LENGTH_SHORT).show();
}
}
}

After the application is complete, create a button. Set a configuration that after the button is tapped, the app will read images from the storage.

private void selectLocalImage() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, REQUEST_SELECT_IMAGE);
}

Configuring the Image Super-Resolution Analyzer

Before the app can perform super-resolution processing on the image, create and configure an analyzer. The example below configures two parameters for the 1x super-resolution capability and 3x super-resolution capability respectively. Which one of them is used depends on the value of selectItem.

private MLImageSuperResolutionAnalyzer createAnalyzer() {
if (selectItem == INDEX_1X) {
return MLImageSuperResolutionAnalyzerFactory.getInstance().getImageSuperResolutionAnalyzer();
} else {
MLImageSuperResolutionAnalyzerSetting setting = new MLImageSuperResolutionAnalyzerSetting.Factory()
.setScale(MLImageSuperResolutionAnalyzerSetting.ISR_SCALE_3X)
.create();
return MLImageSuperResolutionAnalyzerFactory.getInstance().getImageSuperResolutionAnalyzer(setting);
}
}

Constructing and Processing the Image

Before the app can perform super-resolution processing on the image, convert the image into a bitmap whose color format is ARGB8888. Create an MLFrame object using the bitmap. After the image is added, obtain its information and override onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE && resultCode == Activity.RESULT_OK) {
if (data != null) {
imageUri = data.getData();
}
reloadAndDetectImage(true, false);
} else if (resultCode == REQUEST_SELECT_IMAGE && resultCode == Activity.RESULT_CANCELED) {

finish();
}
}

Create an MLFrame object using the bitmap.

srcBitmap = BitmapUtils.loadFromPathWithoutZoom(this, imageUri, IMAGE_MAX_SIZE, IMAGE_MAX_SIZE);
MLFrame frame = MLFrame.fromBitmap(srcBitmap);

Call the asynchronous method asyncAnalyseFrame to perform super-resolution processing on the image.

Task<MLImageSuperResolutionResult> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLImageSuperResolutionResult>() {
public void onSuccess(MLImageSuperResolutionResult result) {
// Recognition success.
desBitmap = result.getBitmap();
setImage(desImageView, desBitmap);
setImageSizeInfo(desBitmap.getWidth(), desBitmap.getHeight());
}
}).addOnFailureListener(new OnFailureListener() {
public void onFailure(Exception e) {
// Recognition failure.
Log.e(TAG, "Failed." + e.getMessage());
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

}
});

After the recognition is complete, stop the analyzer.

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

Meanwhile, override onDestroy of the activity to release the bitmap resources.

@Override

protected void onDestroy() {
super.onDestroy();
if (srcBitmap != null) {
srcBitmap.recycle();
}
if (desBitmap != null) {
desBitmap.recycle();
}
if (analyzer != null) {
analyzer.stop();
}
}

References

To learn more, please visit:

Official webpages for Image Super-Resolution and ML Kit

HUAWEI Developers official website

Development Guide

Reddit to join developer discussions

GitHub or Gitee to download the demo and sample code

Stack Overflow to solve integration problems

Follow our official account for the latest HMS Core-related news and updates.

Original source