Basics of Watson Visual Recognition service
This chapter gets you started with using the Watson Visual Recognition service.
The Watson Visual Recognition service uses deep learning algorithms to analyze images for scenes, objects, faces, and other content. The response includes keywords that provide information about the content. A set of built-in classes provides highly accurate results without training. You can train custom classifiers to create specialized classes.
This chapter introduces the two main tasks that the IBM Watson Visual Recognition service performs:
Classify a picture and get image details. For example, you might have an image of any entity, such as a cat, and use the Watson Visual Recognition classify method to get the details for that image. For more information, see the Classify an image topic in Watson Developer Cloud.
Detect faces, gender, and age in a picture by using the Watson Visual Recognition detectFaces method. For more information, see the Detect faces topic in Watson Developer Cloud.
This chapter provides simple code examples in Java and Node.js that use the Watson SDKs and Eclipse IDE and Node.js Express framework.
The following topics are covered in this chapter:
1.1 Use case examples
IBM Watson Visual Recognition is a service that allows users to understand the content of images and classify images into logical categories. In addition to classifying images, Visual Recognition also offers facial detection.
The Visual Recognition service can be used for diverse applications and industries, such as these:
Manufacturing: Use images from a manufacturing setting to make sure products are being positioned correctly on an assembly line.
Visual Auditing: Look for visual compliance or deterioration in a fleet of trucks, planes, or windmills in the field, train custom classifiers to understand what defects look like.
Insurance: Rapidly process claims by using images to classify claims into different categories.
Social listening: Use images from your product line or your logo to track buzz about your company on social media.
Social commerce: Use an image of a plated dish to find out which restaurant serves it and find reviews, use a travel photo to find vacation suggestions based on similar experiences, use a house image to find similar homes that are for sale.
Retail: Take a photo of a favorite outfit to find stores with those clothes in stock or on sale, use a travel image to find retail suggestions in that area, use the photo of an item to find out its price in different stores.
Education: Create image-based applications to educate about taxonomies, use pictures to find educational material on similar subjects.
Public safety: Automated, real-time video stream analysis to include targeted observations such as facial recognition and automated licence-plate reading, identify a suspect’s car with unknown whereabouts to locate instances of that model, parked or in motion, in any surveilled part of the country.
1.2 Creating a Watson Visual Recognition service instance and getting the API key
Bluemix provides resources to your applications through a service instance. Before you can use the Watson APIs, you must create an instance of the corresponding service; you will need to create a Watson Visual Recognition service instance for use in all the examples in this book.
To create an instance of the Visual Recognition service, complete these steps:
1. Create a Bluemix account.
You must have a Bluemix account to access the Watson APIs. You can create a trial Bluemix account, valid for a specified number days.
2. Log in to Bluemix and click Catalog.
3. From the left menu, select Services  Watson.
4. Click Visual Recognition (Figure 1-1 on page 3).
Figure 1-1 Create Visual Recognition service instance
5. Change the service and credential names or accept the default values. Confirm that the pricing plan Free is selected and click Create.
6. Select the Service credentials tab and click View Credentials (Figure 1-2).
7. Copy the API key for later use.
Figure 1-2 View Credential
1.3 Image classification and face detection examples
The examples in this chapter do the following tasks by using the Watson Visual Recognition service:
Classify an image using the pre-trained classifier for general classification
For each image, the response, in JSON format, describes the image content.
Detect faces in an image
Detect faces in image, analyze the detected faces, and get data about them, such as estimated age, gender. If a celebrity’s face is detected, provide the names of celebrities. Images must be in JPEG or PNG file format.
For more information, see the Visual Recognition getting started tutorials.
1.3.1 Expected results
By following the examples in this chapter, you should be able to submit images to the application and obtain results after the image has been analyzed by the Watson Visual Recognition services.
Image classification results
Figure 1-3 represents the image used as input to the classification.
Figure 1-3 A sample for image classification: Fruit dish image
Figure 1-4 on page 5 shows the response, in JSON format. It describes the image content and for each image, the response includes a score for each class.
Figure 1-4 Image classification results
Face detection results
Figure 1-5 represents the image used as input in face detection.
Figure 1-5 Sample image for face detection: Barak Obama
Figure 1-6 shows the response in JSON format; it shows that a face was detected and recognized it as an image of a celebrity, former President Barak Obama. It also detected gender as Male and the estimated age.
Figure 1-6 Results of running the face detection service
1.4 Classifying images and detecting faces: Use Watson Java SDK and Eclipse IDE
By the end of this section, you should be able to accomplish these objectives:
Use the Watson Java SDK to call Watson APIs for image classification.
Use the Watson Java SDK to call Watson APIs to detect faces and get additional data about them such as gender and estimated age.
Implementing this use case using the Watson Java SDK and Eclipse IDE involves the following steps:
1. Creating a Bluemix account (see step 1 on page 2)
1.4.1 Getting started with Eclipse and Java
In this use case, Eclipse IDE is used to build the Java application. Install and become familiar with Eclipse and Java before you follow the implementation steps:
Download Eclipse:
Getting Started with Eclipse:
Getting Started with Java Programming:
1.4.2 Downloading the Watson Java SDK
IBM Watson services offer Software Development Kits (SDKs) that simplify application development for a variety of programming languages and platforms.
In this chapter, the focus is on developing a Java sample application. Therefore, the Watson Java SDK must be downloaded:
1. Go to GitHub:
2. Scroll to the Downloads section and click java-sdk-3.7.0-jar-with-dependencies.jar (Figure 1-7).
Figure 1-7 Download Watson Java SDK
1.4.3 Classifying images
In this section, you will use the Watson Java SDK to classify image content. It describes how to call the Watson service and how to interpret the response.
Complete these steps:
1. Launch Eclipse.
After you complete 1.4.1, “Getting started with Eclipse and Java” on page 7, you should have Eclipse installed in your workstation. Launch Eclipse by double-clicking the application icon.
2. Select a workspace directory and click OK (Figure 1-8 on page 8).
Figure 1-8 Select an Eclipse workspace
The Eclipse Welcome page opens (Figure 1-9).
Figure 1-9 Eclipse Welcome page
3. Create a new Java project. Select File  New  Java Project (Figure 1-10).
Figure 1-10 Create new Java project
4. Enter the project name (vrproject in this example), accept the default values for other fields, and click Finish (Figure 1-11).
Figure 1-11 Setting your Java project name
5. Close the Welcome page in order to view your project in Package Explorer.
6. Import the Watson Java SDK so you can use it in your application. Right-click the vrproject project and select Build Path  Configure Build Path (Figure 1-12).
Figure 1-12 Go to your project build path
7. Select the Libraries tab and click Add External JARs.
Browse to and select the Watson Java SDK (JAR file) that you downloaded in 1.4.2, “Downloading the Watson Java SDK” on page 7. Click OK.
The Watson Java SDK is successfully added to your project (Figure 1-13).
Figure 1-13 Import the Watson Java SDK to the Eclipse project
8. Create a Java class to classify your image. Right-click the vrproject project and select New  Class (Figure 1-14).
Figure 1-14 Create a new Java class
9. The New Java Class window opens (Figure 1-15). Provide the class details: Add a class name (ClassifyImage in this example) and select the public static void main(String[] args) check box. Click Finish.
Figure 1-15 Set your Java class name
The ClassifyImage class is created (Figure 1-16).
Figure 1-16 ClassifyImage Java class
10. Edit the ClassifyImage.java content by adding the code in Example 1-1. Spend several minutes to read through the code snippet to understand it.
Example 1-1 Code snippet for image classification
package com.vr;
 
//Here you import Watson Java SDK to make it available in your code.
import com.ibm.watson.developer_cloud.visual_recognition.v3.*;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*;
 
 
public class ClassifyImage {
 
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
service.setEndPoint("https://gateway-a.watsonplatform.net/visual-recognition/api");
 
//Here you replace "your_api_key_here" by the API Key you created in "Creating //a Watson Visual Recognition service instance and getting the API key"
service.setApiKey("your_api_key_here");
 
//Here you add the URL of your image. The image size should not exceed 2MB.
String imageURL = new String("https://raw.githubusercontent.com/watson-developer-cloud/doc-tutorial-downloads/master/visual-recognition/fruitbowl.jpg");
ClassifyImagesOptions options = new ClassifyImagesOptions.Builder().url(imageURL).build();
VisualClassification result = service.classify(options).execute();
System.out.println("Classification Results:");
System.out.println(result);
 
}
 
}
11. Run the code and check results. Right-click ClassImage.java and then select
Run As  Java Application (Figure 1-17).
Figure 1-17 Run ClassifyImage
12. View the results in the Console, which by default is under ClassifyImage. You can double-click the Console tab to maximize the view and check the results (Figure 1-18).
Figure 1-18 Console view displays classification results
The response, in JSON format, describes the image content. For each image, the response includes a score for each class.
Figure 1-19 represents the image used as input to the classification.
Figure 1-19 A sample for image classification: Fruit dish image
1.4.4 Detecting faces
In this section, you use the Watson Java SDK to detect faces in an image. The API also provides data about the detected faces, such as estimated age, gender, and names of celebrities.
1. Right-click ClassifyImage.java (Figure 1-14 on page 13), click Copy and Paste in the same directory.
2. The Name Conflict dialog opens (Figure 1-20). Enter DetectFaces as the new class name and click OK.
Figure 1-20 Create DetectFaces class
The new class DetectFaces.java is listed in Package Explorer (Figure 1-21).
Figure 1-21 DetectFaces java class in Package Explorer
3. Update the code to call the face detection Watson API.
Double-click DetectFace.java to open the class.
Apply the changes that are highlighted (outlined in red in Example 1-2).
Example 1-2 Code changes to perform face detection
package com.vr;
 
import com.ibm.watson.developer_cloud.visual_recognition.v3.*;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*;
 
 
public class ClassifyImage {
 
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
service.setEndPoint("https://gateway-a.watsonplatform.net/visual-recognition/api");
 
//Here you replace "your_api_key_here" by the API Key you created in "Creating a //Watson Visual Recognition service instance and getting the API key"
 
service.setApiKey("your_api_key_here");
//Here you add the URL of your image. The image size should not exceed 2MB.
String imageURL = new String("https://raw.githubusercontent.com/watson-developer-cloud/doc-tutorial-downloads/master/visual-recognition/prez.jpg");
VisualRecognitionOptions options = new VisualRecognitionOptions.Builder().url(imageURL).build(); DetectedFaces result = service.detectFaces(options).execute();
System.out.println("Detections Results:");
System.out.println(result);
}
 
}
4. Run code and check the results. Right-click DetectFaces.java and select Run As  Java Application (see Figure 1-17 on page 16).
5. View the results on your Console, which by default is under DetectFaces. To maximize the view and see the results (Figure 1-22), double-click the Console tab. It shows the response is in JSON format and that a face was detected and recognized as an image of former President Barak Obama. It also detected gender as Male and the estimated age.
Figure 1-22 Results of running the face detection service
Figure 1-23 represents the image used in face detection.
Figure 1-23 Sample image for face detection: Barak Obama
1.5 Classifying images and detecting faces: Use Watson Node.js SDK and Node.js Express framework
By the end of this section, you should be able to accomplish these objectives:
Use the Watson Node.js SDK to call Watson APIs for image classification.
Use the Watson Node.js SDK to call Watson APIs to detect faces, gender, and age in an image.
Implementing this use case using the Watson Node.js SDK involves the following steps:
For more information about the Node.js client library to use the Watson services, see the Watson Developer Cloud Node.js SDK web page.
You can find several Node.js usage examples of the Watson APIs on GitHub.
1.5.1 Installing the Watson Node.js SDK into your project
For Node.js you need to enable the Watson API by installing the SDK into your local Node.js installation and the project you are currently working on:
1. You need a text editor to enter and edit the code. Use your favorite text editor or download Brackets or Atom, which are two very popular code editors.
2. Install Node.js runtime and node package manager (npm) on your system from the Node.js website.
3. After you initiate your Node.js project, install the Watson Node SDK into your local installation and Node.js project:
npm install -g watson-developer-cloud
npm install --save watson-developer-cloud
1.5.2 Classifying images
The Node.js sample code in Example 1-3 does the following tasks:
1. Gets an image from a website URL
2. Sets the API key of the Visual Recognition service
3. Sends the image to the classify method of the Visual Recognition service for processing.
4. Returns the results in JSON format.
Example 1-3 Image classification: Node.js sample code
var parameters = {
"apikey" : "",
"url" : "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama%5B1%5D.jpg"
};
 
var watson = require('watson-developer-cloud');
var fs = require('fs');
var http = require('http');
 
var visual_recognition = new watson.VisualRecognitionV3({
api_key: parameters.api_key, //SET YOUR API KEY
version_date: '2016-05-20'
});
 
visual_recognition.classify(parameters, (err, response) => {
if (err) {
console.log('error:', err);
if (typeof callback !== 'undefined' && typeof callback=="function") return callback(err);
}
else {
console.log(JSON.stringify(response, null, 2));
if (typeof callback !== 'undefined' && typeof callback=="function") return callback(response);
}
});
Note the following important lines in the Node.js code snippet, shown in Figure 1-24:
Line 3: The URL that supplies the image as input for processing.
Line 11: Set your api_key of the Visual Recognition service created in 1.2, “Creating a Watson Visual Recognition service instance and getting the API key” on page 2.
Line 15: Call the classify method passing the image url and api_key.
Figure 1-24 Classify object: JSON snippet highlights
Figure 1-25 shows the response, in JSON format. It describes the image content and includes a score for each class.
Figure 1-25 Results
1.5.3 Detecting faces
In this section, you use the Watson Node.js SDK to detect faces in an image. The API also provides data about the detected faces, such as estimated age, gender, and names of celebrities.
The Node.js sample code in Example 1-4 on page 25 performs the following tasks:
1. Gets an image from a website URL.
2. Sets the API key of the Visual Recognition service.
3. Sends the image to the detectFaces method of the Visual Recognition service for processing.
4. Returns the results in JSON format.
Example 1-4 Face detection: Node.js sample code
var parameters = {
"apikey" : "",
"url" : "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/first-family/44_barack_obama%5B1%5D.jpg"
};
 
var watson = require('watson-developer-cloud');
var fs = require('fs');
var http = require('http');
 
var visual_recognition = new watson.VisualRecognitionV3({
api_key: parameters.api_key, //SET YOUR API KEY
version_date: '2016-05-20'
});
 
visual_recognition. detectFaces (parameters, (err, response) => {
if (err) {
console.log('error:', err);
if (typeof callback !== 'undefined' && typeof callback=="function") return callback(err);
}
else {
console.log(JSON.stringify(response, null, 2));
if (typeof callback !== 'undefined' && typeof callback=="function") return callback(response);
}
});
Note the following important lines in the Node.js code snippet, shown in Figure 1-26:
Line 3: The URL that supplies the image as input for processing.
Line 11: Set your api_key of Visual Recognition service created in 1.2, “Creating a Watson Visual Recognition service instance and getting the API key” on page 2.
Line 15: Call the detectFaces method passing the image url and api_key.
Figure 1-26 Face detection: JSON snippet highlights
Figure 1-27 shows the results in JSON format. The face of a celebrity, former President Barak Obama, was detected and data about the face is provided (gender, estimated age).
Figure 1-27 Expected output
1.6 References
See the following resources:
Overview of the IBM Watson Visual Recognition service:
Watson Developer Cloud: Visual Recognition:
Classify an image:
Detect faces:
Visual Recognition getting started tutorials:
Watson Developer Cloud Node.js SDK:
Node.js usage examples of the Watson APIs:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.111.33