Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Java on Azure: Leveraging Cloud-Native Services

0.00/5 (No votes)
7 May 2021 1  
In the next part of this series, we look at Azure Cognitive Services and how we can use existing cloud-native services.

Using cloud services, you can easily outsource functionality and concentrate on your core competencies when building a solution. Instead of making new complex functionality, you can take advantage of widely used and tested services that already exist.

Azure offers AI services that you can quickly integrate into your application. Developing this functionality on your own could have a high effort cost in the collection of the data needed to test and train a machine learning solution. We will browse the available functionality and show how to add it to an application.

Azure Cognitive Services has APIs in the categories of vision, speech, language, and decision. For our application, we use the vision service. The vision category aids in recognizing people’s faces and emotional expressions. It recognizes objects and text within images and index videos. The capabilities of this service go beyond what’s described in this article. You can browse the capabilities and test them online in the Features area of the Azure Cognitive Services site.

For the sample application, we use the computer vision service to add descriptive information to a shipment. The application provides a picture to the Azure Vision service. If someone ships a package with a picture, then the computer vision service uses objects detected within the picture as the default description for the package. By automatically adding text based on a picture, the shipment record becomes searchable based on the description, even if a shipper did not enter a description.

Creating Vision Resources

To use the Computer Vision service, log into the Azure Portal and create a Computer Vision resource. To create the resource, type "Cognitive Services" in the search box and select it from the results. Select +Create to open the Azure Marketplace. Type "Computer Vision." The Computer Vision resource shows in the results. Select the result and the option to create it. For testing the feature, select the "Free F0" pricing tier.

After you’ve selected a pricing tier and resource group, continue to create the resource. A few moments later, the resource becomes available. When you view the resource, note that it shows an endpoint and a link to view its keys. You will need this information in a few moments.

Adding the Vision Library to Your Project

The Computer Vision service uses a REST API. The service isn’t specific to any platform, but there are platform-specific client access libraries. You can find the latest version of the libraries for vision here.

If we select the version that we want to use, we can see the dependency to add to our project. Select the copy icon for the Maven dependency and then paste the dependency declaration in the pom.xml file for your project.

Making a Visual Recognition Request

You need a few code imports to use the library.

import com.microsoft.azure.cognitiveservices.vision.computervision.*;
import com.microsoft.azure.cognitiveservices.vision.computervision.implementation.
ComputerVisionImpl;
import com.microsoft.azure.cognitiveservices.vision.computervision.models.*;

Create an instance of the computer vision client. You will need your subscription key and endpoint. This information is on the page for your vision resource in the Azure portal.

ComputerVisionClient client =
ComputerVisionManager.authenticate(subscriptionKey).withEndpoint(endpoint);

We also need a list of the features that we want to extract from the image. There are several categories of information that you could extract. But for our application, we do not need all of them. We only need the DESCRIPTION category. The other categories appear here for demonstration purposes.

List<VisualFeatureTypes> featureList = new ArrayList<>();
  featureList.add(VisualFeatureTypes.DESCRIPTION);
  featureList.add(VisualFeatureTypes.CATEGORIES);
  featureList.add(VisualFeatureTypes.TAGS);
  featureList.add(VisualFeatureTypes.OBJECTS);

The list of features and the bytes that make up the image must be passed to the computer vision client. It returns an ImageAnalysis object.

 byte[] imageBytes = Files.readAllBytes(rawImage.toPath());
ImageAnalysis analysis = compVisClient.computerVision().
  analyzeImageInStream().
  withImage(imageBytes).
  withVisualFeatures(featureList).
  execute();

The analysis object contains a list of descriptive words for the image. The applications adds this text to a space-separated list.

StringBuilder descriptionWords = new StringBuilder();
for (ImageCaption caption : analysis.description().captions()) {
  String description = String.format("%s ", caption.text())
}
String completeDescription = descriptionWords.toString();

Here are a few samples for images and the description response that the vision service returns.

"a video game controller"
"a black and orange camera"

Now when we provide an image to the system, we receive text that describes the contents of the image, which is attached to the package descriptions. From an image, we get searchable text. If we were to build this functionality independently without using a service, it could have cost us hundreds of hours of effort.

Conclusion

Congratulations, you have built your first cloud-native Java application on Azure! This is just the beginning of your journey. To learn more about what Azure Cognitive Services can do for you, visit the Azure Cognitive Services site. You can quickly incorporate the services into a program and try them out for free. To learn more about making Java microservices that take advantage of Azure features, check out the free e-book collection on Azure and Kubernetes services.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here