Click here to Skip to main content
14,976,321 members
Articles / Hosted Services / Azure
Article
Posted 7 May 2021

Tagged as

Stats

1.3K views
2 bookmarked

Java on Azure: Leveraging Cloud-Native Services

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 May 2021CPOL4 min read
In the next part of this series, we look at Azure Cognitive Services and how we can use existing cloud-native services.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

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.

Image 1

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.

Image 2

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.

Image 3

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.

Image 4 "a video game controller"
Image 5 "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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joel Ivory Johnson
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
-- There are no messages in this forum --