What Is The Computer Vision API? Reference Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 270 [warning: Documents this large are best viewed by clicking the View PDF Link!]

ContentsContents
Computer Vision API Documentation
Overview
What is Computer Vision?
Quickstarts
Using the REST API
Analyze a remote image
cURL
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Analyze a local image
C#
Python
Generate a thumbnail
C#
cURL
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Extract printed text
C#
cURL
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Extract handwritten text
C#
Java
JavaScript
Python
Use a domain model
PHP
Python
Using the .NET SDK
Analyze an image
Generate a thumbnail
Extract text
Using the Python SDK
Tutorials
Generate metadata for images
Concepts
Tagging images
Detecting objects
Detecting brands
Categorizing images
Describing images
Detecting faces
Detecting image types
Detecting domain-specific content
Detecting color schemes
Generating thumbnails
Recognize printed and handwritten text
Detecting adult and racy content
How-to guides
Use Computer Vision
Java
JavaScript
Python
Call the Computer Vision API
Use containers
Install and run containers
Configure containers
Use the Computer Vision Connected Service
Analyze videos in real time
Reference
Azure CLI
Azure PowerShell
Computer Vision API v2.0
Computer Vision API v1.0
SDKs
.NET
Node.js
Python
Go
Android (Java)
Swift
Resources
Samples
Explore an image processing app
Other Computer Vision samples
FAQ
Category taxonomy
Language support
Pricing and limits
UserVoice
Stack Overflow
Azure roadmap
Regional availability
Compliance
What is Computer Vision?
5/29/2019 5 minutes to read Edit Online
Analyze images for insight
ACTION DESCRIPTION
Tag visual features Identify and tag visual features in an image, from a set of
thousands of recognizable objects, living things, scenery, and
actions. When the tags are ambiguous or not common
knowledge, the API response provides 'hints' to clarify the
meaning of the tag in the context of a known setting. Tagging
isn't limited to the main subject, such as a person in the
foreground, but also includes the setting (indoor or outdoor),
furniture, tools, plants, animals, accessories, gadgets, and so
on.
Detect objects Object detection is similar to tagging, but the API returns the
bounding box coordinates for each tag applied. For example, if
an image contains a dog, cat and person, the Detect operation
will list those objects together with their coordinates in the
image. You can use this functionality to process further
relationships between the objects in an image. It also lets you
know when there are multiple instances of the same tag in an
image.
Detect brands Identify commercial brands in images or videos from a
database of thousands of global logos. You can use this
feature, for example, to discover which brands are most
popular on social media or most prevalent in media product
placement.
Categorize an image Identify and categorize an entire image, using a category
taxonomy with parent/child hereditary hierarchies. Categories
can be used alone, or with our new tagging models.
Currently, English is the only supported language for tagging
and categorizing images.
Azure's Computer Vision service provides developers with access to advanced algorithms that process images and
return information. To analyze an image, you can either upload an image or specify an image URL. The images
processing algorithms can analyze content in several different ways, depending on the visual features you're
interested in. For example, Computer Vision can determine if an image contains adult or racy content, or it can find
all of the human faces in an image.
You can use Computer Vision in your application by using either a native SDK or invoking the REST API directly.
This page broadly covers what you can do with Computer Vision.
You can analyze images to detect and provide insights about their visual features and characteristics. All of the
features in the table below are provided by the Analyze Image API.
Describe an image Generate a description of an entire image in human-readable
language, using complete sentences. Computer Vision's
algorithms generate various descriptions based on the objects
identified in the image. The descriptions are each evaluated
and a confidence score generated. A list is then returned
ordered from highest confidence score to lowest.
Detect faces Detect faces in an image and provide information about each
detected face. Computer Vision returns the coordinates,
rectangle, gender, and age for each detected face.
Computer Vision provides a subset of the functionality that
can be found in Face, and you can use the Face service for
more detailed analysis, such as facial identification and pose
detection.
Detect image types Detect characteristics about an image, such as whether an
image is a line drawing or the likelihood of whether an image
is clip art.
Detect domain-specific content Use domain models to detect and identify domain-specific
content in an image, such as celebrities and landmarks. For
example, if an image contains people, Computer Vision can use
a domain model for celebrities included with the service to
determine if the people detected in the image match known
celebrities.
Detect the color scheme Analyze color usage within an image. Computer Vision can
determine whether an image is black & white or color and, for
color images, identify the dominant and accent colors.
Generate a thumbnail Analyze the contents of an image to generate an appropriate
thumbnail for that image. Computer Vision first generates a
high-quality thumbnail and then analyzes the objects within
the image to determine the area of interest. Computer Vision
then crops the image to fit the requirements of the area of
interest. The generated thumbnail can be presented using an
aspect ratio that is different from the aspect ratio of the
original image, depending on your needs.
Get the area of interest Analyze the contents of an image to return the coordinates of
the area of interest. This is the same function that is used to
generate a thumbnail, but instead of cropping the image,
Computer Vision returns the bounding box coordinates of the
region, so the calling application can modify the original image
as desired.
ACTION DESCRIPTION
Extract text from images
You can use Computer Vision to extract text from an image into a machine-readable character stream using optical
character recognition (OCR). If needed, OCR corrects the rotation of the recognized text and provides the frame
coordinates of each word. OCR supports 25 languages and automatically detects the language of the recognized
text.
You can also use the Read API to extract both printed and handwritten text from images and text-heavy documents.
The Read API uses updated models and works for a variety objects with different surfaces and backgrounds, such
as receipts, posters, business cards, letters, and whiteboards. Currently, English is the only supported language.
Moderate content in images
Use containers
Image requirements
Data privacy and security
Next steps
You can use Computer Vision to detect adult and racy content in an image and return a confidence score for both.
The filter for adult and racy content detection can be set on a sliding scale to accommodate your preferences.
Use Computer Vision containers to recognize printed and handwritten text locally by installing a standardized
Docker container closer to your data.
Computer Vision can analyze images that meet the following requirements:
The image must be presented in JPEG, PNG, GIF, or BMP format
The file size of the image must be less than 4 megabytes (MB)
The dimensions of the image must be greater than 50 x 50 pixels
For OCR, the dimensions of the image must be between 50 x 50 and 4200 x 4200 pixels
As with all of the Cognitive Services, developers using the Computer Vision service should be aware of Microsoft's
policies on customer data. See the Cognitive Services page on the Microsoft Trust Center to learn more.
Get started with Computer Vision by following a quickstart guide:
Quickstart: Analyze an image
Quickstart: Extract handwritten text
Quickstart: Generate a thumbnail
Quickstart: Analyze a remote image using the REST
API and cURL in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample command
curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -H "Content-Type: application/json"
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?
visualFeatures=Categories,Description&details=Landmarks&language=en" -d "
{\"url\":\"http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg\"}"
Examine the response
In this quickstart, you analyze a remotely stored image to extract visual features using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have cURL.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following command into a text editor.
2. Make the following changes in the command where needed:
3. Open a command prompt window.
4. Paste the command from the text editor into the command prompt window, and then run the command.
a. Replace the value of <subscriptionKey> with your subscription key.
b. Replace the request URL (https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze ) with
the endpoint URL for the Analyze Image method from the Azure region where you obtained your
subscription keys, if necessary.
c. Optionally, change the language parameter of the request URL (language=en ) to use a different
supported language.
d. Optionally, change the image URL in the request body (
http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg\ ) to the URL of a different
image to be analyzed.
A successful response is returned in JSON. The sample application parses and displays a successful response in the
command prompt window, similar to the following example:
{
"categories": [
{
"name": "outdoor_water",
"score": 0.9921875,
"detail": {
"landmarks": []
}
}
],
"description": {
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
],
"captions": [
{
"text": "a large waterfall over a rocky cliff",
"confidence": 0.916458423253597
}
]
},
"requestId": "b6e33879-abb2-43a0-a96e-02cb5ae0b795",
"metadata": {
"height": 959,
"width": 1280,
"format": "Jpeg"
}
}
Next steps
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Analyze a remote image using the REST
API and Go in Computer Vision
4/18/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
func main() {
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = "<Subscription Key>"
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have Go installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the below code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with a .go extension. For example, analyze-image.go .
4. Open a command prompt window.
5. At the prompt, run the go build command to compile the package from the file. For example,
go build analyze-image.go .
6. At the prompt, run the compiled package. For example, analyze-image .
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze"
const imageUrl =
"https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg"
const params = "?visualFeatures=Description&details=Landmarks&language=en"
const uri = uriBase + params
const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"
reader := strings.NewReader(imageUrlEnc)
// Create the HTTP client
client := &http.Client{
Timeout: time.Second * 2,
}
// Create the POST request, passing the image URL in the request body
req, err := http.NewRequest("POST", uri, reader)
if err != nil {
panic(err)
}
// Add request headers
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
// Send the request and retrieve the response
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Read the response body
// Note, data is a byte array
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Parse the JSON data from the byte array
var f interface{}
json.Unmarshal(data, &f)
// Format and display the JSON result
jsonFormatted, _ := json.MarshalIndent(f, "", " ")
fmt.Println(string(jsonFormatted))
}
Examine the response
A successful response is returned in JSON. The sample application parses and displays a successful response in the
command prompt window, similar to the following example:
{
"categories": [
{
"detail": {
"landmarks": []
},
"name": "outdoor_water",
"score": 0.9921875
}
],
"description": {
"captions": [
{
"confidence": 0.916458423253597,
"text": "a large waterfall over a rocky cliff"
}
],
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
]
},
"metadata": {
"format": "Jpeg",
"height": 959,
"width": 1280
},
"requestId": "a92f89ab-51f8-4735-a58d-507da2213fc2"
}
Next steps
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Analyze a remote image using the
Computer Vision REST API and Java
4/18/2019 4 minutes to read Edit Online
Prerequisites
Create and run the sample application
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have Java™ Platform, Standard Edition Development Kit 7 or 8 (JDK 7 or 8) installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from
a command line application template.
2. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are
provided for each library.
Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
Apache HTTP core (org.apache.httpcomponents:httpcore:4.4.9)
JSON library (org.json:json:20180130)
3. Add the following import statements to the file that contains the Main public class for your project.
4. Replace the Main public class with the following code, then make the following changes in code where
needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageToAnalyze with the URL of a different image that you want to
analyze.
public class Main {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
private static final String subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
private static final String uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";
private static final String imageToAnalyze =
"https://upload.wikimedia.org/wikipedia/commons/" +
"1/12/Broadway_and_Times_Square_by_night.jpg";
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
URIBuilder builder = new URIBuilder(uriBase);
// Request parameters. All of them are optional.
builder.setParameter("visualFeatures", "Categories,Description,Color");
builder.setParameter("language", "en");
// Prepare the URI for the REST API method.
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request body.
StringEntity requestEntity =
new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
request.setEntity(requestEntity);
// Call the REST API method and get the response entity.
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
// Format and display the JSON response.
String jsonString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonString);
System.out.println("REST Response:\n");
System.out.println(json.toString(2));
}
} catch (Exception e) {
// Display error message.
System.out.println(e.getMessage());
}
}
}
Compile and run the program
Examine the response
1. Save, then build the Java project.
2. If you're using an IDE, run Main .
Alternately, if you're running the program from a command line window, run the following commands. These
commands presume your libraries are in a folder named libs that is in the same folder as Main.java ; if not, you
will need to replace libs with the path to your libraries.
javac -cp ".;libs/*" Main.java
java -cp ".;libs/*" Main
1. Compile the file Main.java .
2. Run the program. It will send the request to the QnA Maker API to create the KB, then it will poll for the
results every 30 seconds. Each response is printed to the command line window.
A successful response is returned in JSON. The sample application parses and displays a successful response in the
console window, similar to the following example:
REST Response:
{
"metadata": {
"width": 1826,
"format": "Jpeg",
"height": 2436
},
"color": {
"dominantColorForeground": "Brown",
"isBWImg": false,
"accentColor": "B74314",
"dominantColorBackground": "Brown",
"dominantColors": ["Brown"]
},
"requestId": "bbffe1a1-4fa3-4a6b-a4d5-a4964c58a811",
"description": {
"captions": [{
"confidence": 0.8241405091548035,
"text": "a group of people on a city street filled with traffic at night"
}],
"tags": [
"outdoor",
"building",
"street",
"city",
"busy",
"people",
"filled",
"traffic",
"many",
"table",
"car",
"group",
"walking",
"bunch",
"crowded",
"large",
"night",
"light",
"standing",
"man",
"tall",
"umbrella",
"riding",
"sign",
"crowd"
]
},
"categories": [{
"score": 0.625,
"name": "outdoor_street"
}]
}
Clean up resources
Next steps
When no longer needed, delete the Java project, including the compiled class and imported libraries.
Explore a Java Swing application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API Java Tutorial
Quickstart: Analyze a remote image using the REST
API and JavaScript in Computer Vision
4/19/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
<!DOCTYPE html>
<html>
<head>
<title>Analyze Sample</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function processImage() {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
var subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services. Or,
follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .html extension. For example, analyze-image.html .
4. Open a browser window.
5. In the browser, drag and drop the file into the browser window.
6. When the webpage is displayed in the browser, choose the Analyze Image button.
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of the value attribute for the inputImage control with the URL of a
different image that you want to analyze.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
var uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";
// Request parameters.
var params = {
"visualFeatures": "Categories,Description,Color",
"details": "",
"language": "en",
};
// Display the image.
var sourceImageUrl = document.getElementById("inputImage").value;
document.querySelector("#sourceImage").src = sourceImageUrl;
// Make the REST API call.
$.ajax({
url: uriBase + "?" + $.param(params),
// Request headers.
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader(
"Ocp-Apim-Subscription-Key", subscriptionKey);
},
type: "POST",
// Request body.
data: '{"url": ' + '"' + sourceImageUrl + '"}',
})
.done(function(data) {
// Show formatted JSON on webpage.
$("#responseTextArea").val(JSON.stringify(data, null, 2));
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Display error message.
var errorString = (errorThrown === "") ? "Error. " :
errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" :
jQuery.parseJSON(jqXHR.responseText).message;
alert(errorString);
});
};
</script>
<h1>Analyze image:</h1>
Enter the URL to an image, then click the <strong>Analyze image</strong> button.
<br><br>
Image to analyze:
<input type="text" name="inputImage" id="inputImage"
value="https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg" />
<button onclick="processImage()">Analyze image</button>
<br><br>
<div id="wrapper" style="width:1020px; display:table;">
<div id="jsonOutput" style="width:600px; display:table-cell;">
Response:
<br><br>
<textarea id="responseTextArea" class="UIInput"
style="width:580px; height:400px;"></textarea>
</div>
<div id="imageDiv" style="width:420px; display:table-cell;">
Source image:
<br><br>
<img id="sourceImage" width="400" />
</div>
</div>
</div>
</body>
</html>
Examine the response
A successful response is returned in JSON. The sample webpage parses and displays a successful response in the
browser window, similar to the following example:
{
"categories": [
{
"name": "outdoor_water",
"score": 0.9921875,
"detail": {
"landmarks": []
}
}
],
"description": {
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
],
"captions": [
{
"text": "a large waterfall over a rocky cliff",
"confidence": 0.916458423253597
}
]
},
"color": {
"dominantColorForeground": "Grey",
"dominantColorBackground": "Green",
"dominantColors": [
"Grey",
"Green"
],
"accentColor": "4D5E2F",
"isBwImg": false
},
"requestId": "73ef10ce-a4ea-43c6-aee7-70325777e4b3",
"metadata": {
"height": 959,
"width": 1280,
"format": "Jpeg"
}
}
Next steps
Explore a JavaScript application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API JavaScript Tutorial
Quickstart: Analyze a remote image using the REST
API with Node.js in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have Node.js 4.x or later installed.
You must have npm installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Install the npm request package.
npm install request
a. Open a command prompt window as an administrator.
b. Run the following command:
c. After the package is successfully installed, close the command prompt window.
2. Copy the following code into a text editor.
3. Make the following changes in code where needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
d. Optionally, replace the value of the language request parameter with a different language.
4. Save the code as a file with a .js extension. For example, analyze-image.js .
5. Open a command prompt window.
6. At the prompt, use the node command to run the file. For example, node analyze-image.js .
'use strict';
const request = require('request');
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = '<Subscription Key>';
// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
const uriBase =
'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze';
const imageUrl =
'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';
// Request parameters.
const params = {
'visualFeatures': 'Categories,Description,Color',
'details': '',
'language': 'en'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error);
return;
}
let jsonResponse = JSON.stringify(JSON.parse(body), null, ' ');
console.log('JSON Response\n');
console.log(jsonResponse);
});
Examine the response
A successful response is returned in JSON. The sample parses and displays a successful response in the command
prompt window, similar to the following example:
{
"categories": [
{
"name": "outdoor_water",
"score": 0.9921875,
"detail": {
"landmarks": []
}
}
],
"description": {
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
],
"captions": [
{
"text": "a large waterfall over a rocky cliff",
"confidence": 0.916458423253597
}
]
},
"color": {
"dominantColorForeground": "Grey",
"dominantColorBackground": "Green",
"dominantColors": [
"Grey",
"Green"
],
"accentColor": "4D5E2F",
"isBwImg": false
},
"requestId": "81b4e400-e3c1-41f1-9020-e6871ad9f0ed",
"metadata": {
"height": 959,
"width": 1280,
"format": "Jpeg"
}
}
Clean up resources
Next steps
When no longer needed, delete the file, and then uninstall the npm request package. To uninstall the package, do
the following steps:
npm uninstall request
1. Open a command prompt window as an administrator.
2. Run the following command:
3. After the package is successfully uninstalled, close the command prompt window.
Explore the Computer Vision APIs used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Analyze a remote image using the REST
API and PHP in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have PHP installed.
You must have Pear installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Install the PHP5 HTTP_Request2 package.
pear install HTTP_Request2
a. Open a command prompt window as an administrator.
b. Run the following command:
c. After the package is successfully installed, close the command prompt window.
2. Copy the following code into a text editor.
3. Make the following changes in code where needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
d. Optionally, replace the value of the language request parameter with a different language.
4. Save the code as a file with a .php extension. For example, analyze-image.php .
5. Open a browser window with PHP support.
6. Drag and drop the file into the browser window.
<html>
<head>
<title>Analyze Image Sample</title>
</head>
<body>
<?php
// Replace <Subscription Key> with a valid subscription key.
$ocpApimSubscriptionKey = '<Subscription Key>';
// You must use the same location in your REST call as you used to obtain
// your subscription keys. For example, if you obtained your subscription keys
// from westus, replace "westcentralus" in the URL below with "westus".
$uriBase = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/';
$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';
require_once 'HTTP/Request2.php';
$request = new Http_Request2($uriBase . '/analyze');
$url = $request->getUrl();
$headers = array(
// Request headers
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
'visualFeatures' => 'Categories,Description',
'details' => '',
'language' => 'en'
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_POST);
// Request body parameters
$body = json_encode(array('url' => $imageUrl));
// Request body
$request->setBody($body);
try
{
$response = $request->send();
echo "<pre>" .
json_encode(json_decode($response->getBody()), JSON_PRETTY_PRINT) . "</pre>";
}
catch (HttpException $ex)
{
echo "<pre>" . $ex . "</pre>";
}
?>
</body>
</html>
Examine the response
A successful response is returned in JSON. The sample website parses and displays a successful response in the
browser window, similar to the following example:
{
"categories": [
{
"name": "outdoor_water",
"score": 0.9921875,
"detail": {
"landmarks": []
}
}
],
"description": {
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
],
"captions": [
{
"text": "a large waterfall over a rocky cliff",
"confidence": 0.916458423253597
}
]
},
"requestId": "ebf5a1bc-3ba2-4c56-99b4-bbd20ba28705",
"metadata": {
"height": 959,
"width": 1280,
"format": "Jpeg"
}
}
Clean up resources
When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2 package. To uninstall the
package, do the following steps:
1. Open a command prompt window as an administrator.
2. Run the following command:
Next steps
pear uninstall HTTP_Request2
3. After the package is successfully uninstalled, close the command prompt window.
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Analyze a remote image using the REST
API and Python in Computer Vision
4/19/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
You can run this quickstart in a step-by step fashion using a Jupyter notebook on MyBinder. To launch Binder,
select the following button:
launch
launch binder
binder
If you don't have an Azure subscription, create a free account before you begin.
You must have Python installed if you want to run the sample locally.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
You must have the following Python packages installed. You can use pip to install Python packages.
requests
matplotlib
pillow
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .py extension. For example, analyze-image.py .
4. Open a command prompt window.
5. At the prompt, use the python command to run the sample. For example, python analyze-image.py .
a. Replace the value of subscription_key with your subscription key.
b. Replace the value of vision_base_url with the endpoint URL for the Computer Vision resource in the
Azure region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of image_url with the URL of a different image that you want to analyze.
import requests
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "<Subscription Key>"
assert subscription_key
# You must use the same region in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from
# westus, replace "westcentralus" in the URI below with "westus".
#
# Free trial subscription keys are generated in the "westus" region.
# If you use a free trial subscription key, you shouldn't need to change
# this region.
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/"
analyze_url = vision_base_url + "analyze"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/" + \
"Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key }
params = {'visualFeatures': 'Categories,Description,Color'}
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()
Examine the response
{
"categories": [
{
"name": "outdoor_",
"score": 0.00390625,
"detail": {
"landmarks": []
}
},
{
"name": "outdoor_street",
"score": 0.33984375,
"detail": {
"landmarks": []
A successful response is returned in JSON. The sample webpage parses and displays a successful response in the
command prompt window, similar to the following example:
"landmarks": []
}
}
],
"description": {
"tags": [
"building",
"outdoor",
"street",
"city",
"people",
"busy",
"table",
"walking",
"traffic",
"filled",
"large",
"many",
"group",
"night",
"light",
"crowded",
"bunch",
"standing",
"man",
"sign",
"crowd",
"umbrella",
"riding",
"tall",
"woman",
"bus"
],
"captions": [
{
"text": "a group of people on a city street at night",
"confidence": 0.9122243847383961
}
]
},
"color": {
"dominantColorForeground": "Brown",
"dominantColorBackground": "Brown",
"dominantColors": [
"Brown"
],
"accentColor": "B54316",
"isBwImg": false
},
"requestId": "c11894eb-de3e-451b-9257-7c8b168073d1",
"metadata": {
"height": 600,
"width": 450,
"format": "Jpeg"
}
}
Next steps
Explore a Python application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API Python Tutorial
Quickstart: Analyze a remote image using the REST
API and Ruby in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have Ruby 2.4.x or later installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .rb extension. For example, analyze-image.rb .
4. Open a command prompt window.
5. At the prompt, use the ruby command to run the sample. For example, ruby analyze-image.rb .
a. Replace <Subscription Key> with your subscription key.
b. Replace https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze with the endpoint URL
for the Analyze Image method in the Azure region where you obtained your subscription keys, if
necessary.
c. Optionally, replace the value of the language request parameter with a different language.
d. Optionally, replace http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg\ with the
URL of a different image that you want to analyze.
require 'net/http'
# You must use the same location in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from westus,
# replace "westcentralus" in the URL below with "westus".
uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze')
uri.query = URI.encode_www_form({
# Request parameters
'visualFeatures' => 'Categories, Description',
'details' => 'Landmarks',
'language' => 'en'
})
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
# Replace <Subscription Key> with your valid subscription key.
request['Ocp-Apim-Subscription-Key'] = '<Subscription Key>'
request['Content-Type'] = 'application/json'
request.body =
"{\"url\": \"http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg\"}"
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts response.body
Examine the response
{
"categories": [
{
"name": "abstract_",
"score": 0.00390625
},
{
"name": "people_",
"score": 0.83984375,
"detail": {
"celebrities": [
{
"name": "Satya Nadella",
"faceRectangle": {
"left": 597,
"top": 162,
"width": 248,
"height": 248
},
"confidence": 0.999028444
}
]
}
}
],
"adult": {
"isAdultContent": false,
"isRacyContent": false,
"adultScore": 0.0934349000453949,
"racyScore": 0.068613491952419281
},
A successful response is returned in JSON. The sample parses and displays a successful response in the command
prompt window, similar to the following example:
},
"tags": [
{
"name": "person",
"confidence": 0.98979085683822632
},
{
"name": "man",
"confidence": 0.94493889808654785
},
{
"name": "outdoor",
"confidence": 0.938492476940155
},
{
"name": "window",
"confidence": 0.89513939619064331
}
],
"description": {
"tags": [
"person",
"man",
"outdoor",
"window",
"glasses"
],
"captions": [
{
"text": "Satya Nadella sitting on a bench",
"confidence": 0.48293603002174407
}
]
},
"requestId": "0dbec5ad-a3d3-4f7e-96b4-dfd57efe967d",
"metadata": {
"width": 1500,
"height": 1000,
"format": "Jpeg"
},
"faces": [
{
"age": 44,
"gender": "Male",
"faceRectangle": {
"left": 593,
"top": 160,
"width": 250,
"height": 250
}
}
],
"color": {
"dominantColorForeground": "Brown",
"dominantColorBackground": "Brown",
"dominantColors": [
"Brown",
"Black"
],
"accentColor": "873B59",
"isBWImg": false
},
"imageType": {
"clipArtType": 0,
"lineDrawingType": 0
}
}
Next steps
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Analyze a local image using the REST API
and C# in Computer Vision
4/18/2019 4 minutes to read Edit Online
Prerequisites
Create and run the sample application
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CSHttpClientSample
{
static class Program
{
// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
In this quickstart, you will analyze a locally stored image to extract visual features by using Computer Vision's
REST API. With the Analyze Image method, you can extract visual feature information based on image content.
If you don't have an Azure subscription, create a free account before you begin.
You must have Visual Studio 2015 or later.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create the sample in Visual Studio, do the following steps:
1. Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App (.NET Framework)
template.
2. Install the Newtonsoft.Json NuGet package.
3. Replace the code in Program.cs with the following code, and then make the following changes in code where
needed:
4. Run the program.
5. At the prompt, enter the path to a local image.
a. On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for
Solution.
b. Click the Browse tab, and in the Search box type "Newtonsoft.Json".
c. Select Newtonsoft.Json when it displays, then click the checkbox next to your project name, and Install.
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Analyze Image method from the Azure
region where you obtained your subscription keys, if necessary.
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Analyze an image:");
Console.Write(
"Enter the path to the image you wish to analyze: ");
string imageFilePath = Console.ReadLine();
if (File.Exists(imageFilePath))
{
// Call the REST API method.
Console.WriteLine("\nWait a moment for the results to appear.\n");
MakeAnalysisRequest(imageFilePath).Wait();
}
else
{
Console.WriteLine("\nInvalid file path");
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
/// <summary>
/// Gets the analysis of the specified image file by using
/// the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file to analyze.</param>
static async Task MakeAnalysisRequest(string imageFilePath)
{
try
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
// The Analyze Image method returns information about the following
// visual features:
// Categories: categorizes image content according to a
// taxonomy defined in documentation.
// Description: describes the image content with a complete
// sentence in supported languages.
// Color: determines the accent color, dominant color,
// and whether an image is black & white.
string requestParameters =
"visualFeatures=Categories,Description,Color";
// Assemble the URI for the REST API method.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Read the contents of the specified local image
// into a byte array.
byte[] byteData = GetImageAsByteArray(imageFilePath);
// Add the byte array as an octet stream to the request body.
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
{
// This example uses the "application/octet-stream" content type.
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Asynchronously call the REST API method.
response = await client.PostAsync(uri, content);
}
// Asynchronously get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n\n{0}\n",
JToken.Parse(contentString).ToString());
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
// Open a read-only file stream for the specified file.
using (FileStream fileStream =
new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
// Read the file's contents into a byte array.
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
}
Examine the response
A successful response is returned in JSON. The sample application parses and displays a successful response in the
console window, similar to the following example:
{
"categories": [
{
"name": "abstract_",
"score": 0.00390625
},
{
"name": "others_",
"score": 0.0234375
},
{
"name": "outdoor_",
"score": 0.00390625
}
],
"description": {
"tags": [
"road",
"building",
"outdoor",
"street",
"night",
"black",
"city",
"white",
"light",
"sitting",
"riding",
"man",
"side",
"empty",
"rain",
"corner",
"traffic",
"lit",
"hydrant",
"stop",
"board",
"parked",
"bus",
"tall"
],
"captions": [
{
"text": "a close up of an empty city street at night",
"confidence": 0.7965622853462756
}
]
},
"requestId": "dddf1ac9-7e66-4c47-bdef-222f3fe5aa23",
"metadata": {
"width": 3733,
"height": 1986,
"format": "Jpeg"
},
"color": {
"dominantColorForeground": "Black",
"dominantColorBackground": "Black",
"dominantColors": [
"Black",
"Grey"
],
"accentColor": "666666",
"isBWImg": true
}
}
Clean up resources
Next steps
When no longer needed, delete the Visual Studio solution. To do so, open File Explorer, navigate to the folder in
which you created the Visual Studio solution, and delete the folder.
Explore a basic Windows application that uses Computer Vision to perform optical character recognition (OCR);
create smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an
image.
Computer Vision API C# Tutorial
Quickstart: Analyze a local image using the REST API
and Python in Computer Vision
4/19/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you analyze a locally stored image to extract visual features by using Computer Vision's REST
API. With the Analyze Image method, you can extract visual features based on image content.
You can run this quickstart in a step-by step fashion using a Jupyter notebook on MyBinder. To launch Binder,
select the following button:
launch
launch binder
binder
If you don't have an Azure subscription, create a free account before you begin.
You must have Python installed if you want to run the sample locally.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
You must have the following Python packages installed. You can use pip to install Python packages.
requests
matplotlib
pillow
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .py extension. For example, analyze-local-image.py .
4. Open a command prompt window.
5. At the prompt, use the python command to run the sample. For example, python analyze-local-image.py .
a. Replace the value of subscription_key with your subscription key.
b. Replace the value of vision_base_url with the endpoint URL for the Computer Vision resource in the
Azure region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of image_path with the path and file name of a different image that you
want to analyze.
import requests
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "<Subscription Key>"
assert subscription_key
# You must use the same region in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from
# westus, replace "westcentralus" in the URI below with "westus".
#
# Free trial subscription keys are generated in the "westus" region.
# If you use a free trial subscription key, you shouldn't need to change
# this region.
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/"
analyze_url = vision_base_url + "analyze"
# Set image_path to the local path of an image that you want to analyze.
image_path = "C:/Documents/ImageToAnalyze.jpg"
# Read the image into a byte array
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Categories,Description,Color'}
response = requests.post(
analyze_url, headers=headers, params=params, data=image_data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(analysis)
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(image_data))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
Examine the response
{
"categories": [
{
"name": "outdoor_",
"score": 0.00390625,
"detail": {
"landmarks": []
}
},
{
"name": "outdoor_street",
"score": 0.33984375,
"detail": {
"landmarks": []
A successful response is returned in JSON. The sample webpage parses and displays a successful response in the
command prompt window, similar to the following example:
"landmarks": []
}
}
],
"description": {
"tags": [
"building",
"outdoor",
"street",
"city",
"people",
"busy",
"table",
"walking",
"traffic",
"filled",
"large",
"many",
"group",
"night",
"light",
"crowded",
"bunch",
"standing",
"man",
"sign",
"crowd",
"umbrella",
"riding",
"tall",
"woman",
"bus"
],
"captions": [
{
"text": "a group of people on a city street at night",
"confidence": 0.9122243847383961
}
]
},
"color": {
"dominantColorForeground": "Brown",
"dominantColorBackground": "Brown",
"dominantColors": [
"Brown"
],
"accentColor": "B54316",
"isBwImg": false
},
"requestId": "c11894eb-de3e-451b-9257-7c8b168073d1",
"metadata": {
"height": 600,
"width": 450,
"format": "Jpeg"
}
}
Clean up resources
Next steps
When no longer needed, delete the file.
Explore a Python application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API Python Tutorial
Quickstart: Generate a thumbnail using the REST API
and C# in Computer Vision
4/18/2019 5 minutes to read Edit Online
Prerequisites
Create and run the sample application
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CSHttpClientSample
{
static class Program
{
// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. With the Get
Thumbnail method, you can generate a thumbnail of an image. You specify the height and width, which can differ
from the aspect ratio of the input image. Computer Vision uses smart cropping to intelligently identify the area of
interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have Visual Studio 2015 or later.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create the sample in Visual Studio, do the following steps:
1. Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App template.
2. Install the Newtonsoft.Json NuGet package.
3. Replace the code in Program.cs with the following code, and then make the following changes in code where
needed:
4. Run the program.
5. At the prompt, enter the path to a local image.
a. On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for
Solution.
b. Click the Browse tab, and in the Search box type "Newtonsoft.Json".
c. Select Newtonsoft.Json when it displays, then click the checkbox next to your project name, and Install.
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Thumbnail:");
Console.Write(
"Enter the path to the image you wish to use to create a thumbnail image: ");
string imageFilePath = Console.ReadLine();
if (File.Exists(imageFilePath))
{
// Call the REST API method.
Console.WriteLine("\nWait a moment for the results to appear.\n");
MakeThumbNailRequest(imageFilePath).Wait();
}
else
{
Console.WriteLine("\nInvalid file path");
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
/// <summary>
/// Gets a thumbnail image from the specified image file by using
/// the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file to use to create the thumbnail image.</param>
static async Task MakeThumbNailRequest(string imageFilePath)
{
try
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters.
// The width and height parameters specify a thumbnail that's
// 200 pixels wide and 150 pixels high.
// The smartCropping parameter is set to true, to enable smart cropping.
string requestParameters = "width=200&height=150&smartCropping=true";
// Assemble the URI for the REST API method.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Read the contents of the specified local image
// into a byte array.
byte[] byteData = GetImageAsByteArray(imageFilePath);
// Add the byte array as an octet stream to the request body.
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses the "application/octet-stream" content type.
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Asynchronously call the REST API method.
response = await client.PostAsync(uri, content);
}
// Check the HTTP status code of the response. If successful, display
// display the response and save the thumbnail.
if (response.IsSuccessStatusCode)
{
// Display the response data.
Console.WriteLine("\nResponse:\n{0}", response);
// Get the image data for the thumbnail from the response.
byte[] thumbnailImageData =
await response.Content.ReadAsByteArrayAsync();
// Save the thumbnail to the same folder as the original image,
// using the original name with the suffix "_thumb".
// Note: This will overwrite an existing file of the same name.
string thumbnailFilePath =
imageFilePath.Insert(imageFilePath.Length - 4, "_thumb");
File.WriteAllBytes(thumbnailFilePath, thumbnailImageData);
Console.WriteLine("\nThumbnail written to: {0}", thumbnailFilePath);
}
else
{
// Display the JSON error data.
string errorString = await response.Content.ReadAsStringAsync();
Console.WriteLine("\n\nResponse:\n{0}\n",
JToken.Parse(errorString).ToString());
}
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
// Open a read-only file stream for the specified file.
using (FileStream fileStream =
new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
// Read the file's contents into a byte array.
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
}
Examine the response
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
succeeds, the thumbnail is saved to the same folder as the local image, using the original name with the suffix
"_thumb". If the request fails, the response contains an error code and a message to help determine what went
wrong.
Response:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
apim-request-id: 131eb5b4-5807-466d-9656-4c1ef0a64c9b
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Cache-Control: no-cache
Date: Tue, 06 Jun 2017 20:54:07 GMT
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 5800
Content-Type: image/jpeg
Expires: -1
}
Clean up resources
Next steps
The sample application displays a successful response in the console window, similar to the following example:
When no longer needed, delete the Visual Studio solution. To do so, open File Explorer, navigate to the folder in
which you created the Visual Studio solution, and delete the folder.
Explore a basic Windows application that uses Computer Vision to perform optical character recognition (OCR);
create smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an
image. To rapidly experiment with the Computer Vision APIs, try the Open API testing console.
Computer Vision API C# Tutorial
Quickstart: Generate a thumbnail using the REST API
and cURL in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Get Thumbnail request
NOTENOTE
Create and run the sample command
In this quickstart, you generate a thumbnail from an image using Computer Vision's REST API. You specify the
desired height and width, which can differ in aspect ration from the input image. Computer Vision uses smart
cropping to intelligently identify the area of interest and generate cropping coordinates around that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have cURL.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
With the Get Thumbnail method, you can generate a thumbnail of an image.
To run the sample, do the following steps:
1. Copy the following code into an editor.
2. Replace <Subscription Key> with your valid subscription key.
3. Replace <File> with the path and filename to save the thumbnail.
4. Change the Request URL (https://westcentralus.api.cognitive.microsoft.com/vision/v2.0 ) to use the location
where you obtained your subscription keys, if necessary.
5. Optionally, change the image ({\"url\":\"... ) to analyze.
6. Open a command window on a computer with cURL installed.
7. Paste the code in the window and run the command.
You must use the same location in your REST call as you used to obtain your subscription keys. For example, if you obtained
your subscription keys from westus, replace "westcentralus" in the URL below with "westus".
To create and run the sample, do the following steps:
1. Copy the following command into a text editor.
2. Make the following changes in the command where needed:
a. Replace the value of <subscriptionKey> with your subscription key.
b. Replace the value of <thumbnailFile> with the path and name of the file in which to save the thumbnail.
c. Replace the request URL (
https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail ) with the endpoint
Examine the response
Next steps
curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -o <thumbnailFile> -H "Content-Type:
application/json" "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail?
width=100&height=100&smartCropping=true" -d "
{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-
Shorkie_Poo_Puppy.jpg\"}"
URL for the Get Thumbnail method from the Azure region where you obtained your subscription keys, if
necessary.
d. Optionally, change the image URL in the request body (
https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-
Shorkie_Poo_Puppy.jpg\
) to the URL of a different image from which to generate a thumbnail.
3. Open a command prompt window.
4. Paste the command from the text editor into the command prompt window, and then run the command.
A successful response writes the thumbnail image to the file specified in <thumbnailFile> . If the request fails, the
response contains an error code and a message to help determine what went wrong. If the request seems to
succeed but the created thumbnail is not a valid image file, it might be that your subscription key is not valid.
Explore the Computer Vision API to how to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Generate a thumbnail using the REST API
and Go in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
func main() {
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = "<Subscription Key>"
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
In this quickstart, you generate a thumbnail from an image using Computer Vision's REST API. You specify the
height and width, which can differ in aspect ratio from the input image. Computer Vision uses smart cropping to
intelligently identify the area of interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have Go installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with a .go extension. For example, get-thumbnail.go .
4. Open a command prompt window.
5. At the prompt, run the go build command to compile the package from the file. For example,
go build get-thumbnail.go .
6. At the prompt, run the compiled package. For example, get-thumbnail .
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image from which you want to
generate a thumbnail.
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail"
const imageUrl =
"https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
const params = "?width=100&height=100&smartCropping=true"
const uri = uriBase + params
const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"
reader := strings.NewReader(imageUrlEnc)
// Create the HTTP client
client := &http.Client{
Timeout: time.Second * 2,
}
// Create the POST request, passing the image URL in the request body
req, err := http.NewRequest("POST", uri, reader)
if err != nil {
panic(err)
}
// Add headers
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
// Send the request and retrieve the response
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Read the response body.
// Note, data is a byte array
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Parse the JSON data
var f interface{}
json.Unmarshal(data, &f)
// Format and display the JSON result
jsonFormatted, _ := json.MarshalIndent(f, "", " ")
fmt.Println(string(jsonFormatted))
}
Examine the response
Next steps
A successful response contains the thumbnail image binary data. If the request fails, the response contains an error
code and a message to help determine what went wrong.
Explore the Computer Vision API to analyze an image, detect celebrities and landmarks, create a thumbnail, and
extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing
console.
Explore the Computer Vision API
Quickstart: Generate a thumbnail using the REST API
and Java in Computer Vision
4/18/2019 4 minutes to read Edit Online
Prerequisites
Create and run the sample application
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. You specify the
height and width, which can differ from the aspect ratio of the input image. Computer Vision uses smart cropping
to intelligently identify the area of interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have Java™ Platform, Standard Edition Development Kit 7 or 8 (JDK 7 or 8) installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
import java.awt.*;
import javax.swing.*;
import java.net.URI;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from
a command line application template.
2. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are
provided for each library.
Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
Apache HTTP core (org.apache.httpcomponents:httpcore:4.4.9)
JSON library (org.json:json:20180130)
3. Add the following import statements to the file that contains the Main public class for your project.
4. Replace the Main public class with the following code, then make the following changes in code where
needed:
a. Replace the value of subscriptionKey with your subscription key.
// This sample uses the following libraries:
// - Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
// - Apache HTTP core (org.apache.httpcomponents:httpccore:4.4.9)
// - JSON library (org.json:json:20180130).
public class Main {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
private static final String subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
private static final String uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail";
private static final String imageToAnalyze =
"https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg";
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
URIBuilder uriBuilder = new URIBuilder(uriBase);
// Request parameters.
uriBuilder.setParameter("width", "100");
uriBuilder.setParameter("height", "150");
uriBuilder.setParameter("smartCropping", "true");
// Prepare the URI for the REST API method.
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request body.
StringEntity requestEntity =
new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
request.setEntity(requestEntity);
// Call the REST API method and get the response entity.
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
// Check for success.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageToAnalyze with the URL of a different image for which you want to
generate a thumbnail.
5. Save, then build the Java project.
6. If you're using an IDE, run Main . Otherwise, open a command prompt window and then use the java
command to run the compiled class. For example, java Main .
// Check for success.
if (response.getStatusLine().getStatusCode() == 200) {
// Display the thumbnail.
System.out.println("\nDisplaying thumbnail.\n");
displayImage(entity.getContent());
} else {
// Format and display the JSON error message.
String jsonString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonString);
System.out.println("Error:\n");
System.out.println(json.toString(2));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Displays the given input stream as an image.
private static void displayImage(InputStream inputStream) {
try {
BufferedImage bufferedImage = ImageIO.read(inputStream);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
JFrame jFrame = new JFrame();
jFrame.setLayout(new FlowLayout());
jFrame.setSize(100, 150);
jFrame.add(jLabel);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Examine the response
Next steps
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
succeeds, the thumbnail is generated from the binary data in the response and displayed in a separate window
created by the sample application. If the request fails, the response is displayed in the console window. The
response for the failed request contains an error code and a message to help determine what went wrong.
Explore a Java Swing application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API Java Tutorial
Quickstart: Generate a thumbnail using the REST API
and JavaScript in Computer Vision
4/18/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
<!DOCTYPE html>
<html>
<head>
<title>Thumbnail Sample</title>
</head>
<body>
<script type="text/javascript">
function processImage() {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
var subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. You specify the
height and width, which can differ in aspect ratio from the input image. Computer Vision uses smart cropping to
intelligently identify the area of interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services. Or,
follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .html extension. For example, get-thumbnail.html .
4. Open a browser window.
5. In the browser, drag and drop the file into the browser window.
6. When the webpage is displayed in the browser, choose the Generate thumbnail button.
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of the value attribute for the inputImage control with the URL of a
different image that you want to analyze.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
var uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail";
// Request parameters.
var params = "?width=100&height=150&smartCropping=true";
// Display the source image.
var sourceImageUrl = document.getElementById("inputImage").value;
document.querySelector("#sourceImage").src = sourceImageUrl;
// Prepare the REST API call:
// Create the HTTP Request object.
var xhr = new XMLHttpRequest();
// Identify the request as a POST, with the URL and parameters.
xhr.open("POST", uriBase + params);
// Add the request headers.
xhr.setRequestHeader("Content-Type","application/json");
xhr.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Set the response type to "blob" for the thumbnail image data.
xhr.responseType = "blob";
// Process the result of the REST API call.
xhr.onreadystatechange = function(e) {
if(xhr.readyState === XMLHttpRequest.DONE) {
// Thumbnail successfully created.
if (xhr.status === 200) {
// Show response headers.
var s = JSON.stringify(xhr.getAllResponseHeaders(), null, 2);
document.getElementById("responseTextArea").value =
JSON.stringify(xhr.getAllResponseHeaders(), null, 2);
// Show thumbnail image.
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(this.response);
document.querySelector("#thumbnailImage").src = imageUrl;
} else {
// Display the error message. The error message is the response
// body as a JSON string. The code in this code block extracts
// the JSON string from the blob response.
var reader = new FileReader();
// This event fires after the blob has been read.
reader.addEventListener('loadend', (e) => {
document.getElementById("responseTextArea").value =
JSON.stringify(JSON.parse(e.srcElement.result), null, 2);
});
// Start reading the blob as text.
reader.readAsText(xhr.response);
}
}
}
// Make the REST API call.
xhr.send('{"url": ' + '"' + sourceImageUrl + '"}');
};
</script>
<h1>Generate thumbnail image:</h1>
Enter the URL to an image to use in creating a thumbnail image,
then click the <strong>Generate thumbnail</strong> button.
<br><br>
<br><br>
Image for thumbnail:
<input type="text" name="inputImage" id="inputImage"
value="https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-
Shorkie_Poo_Puppy.jpg" />
<button onclick="processImage()">Generate thumbnail</button>
<br><br>
<div id="wrapper" style="width:1160px; display:table;">
<div id="jsonOutput" style="width:600px; display:table-cell;">
Response:
<br><br>
<textarea id="responseTextArea" class="UIInput"
style="width:580px; height:400px;"></textarea>
</div>
<div id="imageDiv" style="width:420px; display:table-cell;">
Source image:
<br><br>
<img id="sourceImage" width="400" />
</div>
<div id="thumbnailDiv" style="width:140px; display:table-cell;">
Thumbnail:
<br><br>
<img id="thumbnailImage" />
</div>
</div>
</body>
</html>
Examine the response
Next steps
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
succeeds, the thumbnail is generated from the binary data in the response and displayed in the browser window. If
the request fails, the response is displayed in the console window. The response for the failed request contains an
error code and a message to help determine what went wrong.
Explore a JavaScript application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API JavaScript Tutorial
Quickstart: Generate a thumbnail using the REST API
and Node.js in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. With the Get
Thumbnail method, you can generate a thumbnail of an image. You specify the height and width, which can differ
from the aspect ratio of the input image. Computer Vision uses smart cropping to intelligently identify the area of
interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have Node.js 4.x or later installed.
You must have npm installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Install the npm request package.
npm install request
a. Open a command prompt window as an administrator.
b. Run the following command:
c. After the package is successfully installed, close the command prompt window.
2. Copy the following code into a text editor.
3. Make the following changes in code where needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
4. Save the code as a file with a .js extension. For example, get-thumbnail.js .
5. Open a command prompt window.
6. At the prompt, use the node command to run the file. For example, node get-thumbnail.js .
'use strict';
const request = require('request');
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = '<Subscription Key>';
// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
const uriBase =
'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';
const imageUrl =
'https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg';
// Request parameters.
const params = {
'width': '100',
'height': '100',
'smartCropping': 'true'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error);
return;
}
});
Examine the response
Next steps
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
fails, the response is displayed in the console window. The response for the failed request contains an error code
and a message to help determine what went wrong.
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Generate a thumbnail using the REST API
and PHP in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. With the Get
Thumbnail method, you can generate a thumbnail of an image. You specify the height and width, which can differ
from the aspect ratio of the input image. Computer Vision uses smart cropping to intelligently identify the area of
interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have PHP installed.
You must have Pear installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Install the PHP5 HTTP_Request2 package.
pear install HTTP_Request2
a. Open a command prompt window as an administrator.
b. Run the following command:
c. After the package is successfully installed, close the command prompt window.
2. Copy the following code into a text editor.
3. Make the following changes in code where needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure
region where you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image for which you want to
generate a thumbnail.
4. Save the code as a file with a .php extension. For example, get-thumbnail.php .
5. Open a browser window with PHP support.
6. Drag and drop the file into the browser window.
<html>
<head>
<title>Get Thumbnail Sample</title>
</head>
<body>
<?php
// Replace <Subscription Key> with a valid subscription key.
$ocpApimSubscriptionKey = '<Subscription Key>';
// You must use the same location in your REST call as you used to obtain
// your subscription keys. For example, if you obtained your subscription keys
// from westus, replace "westcentralus" in the URL below with "westus".
$uriBase = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/';
$imageUrl =
'https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg';
require_once 'HTTP/Request2.php';
$request = new Http_Request2($uriBase . 'generateThumbnail');
$url = $request->getUrl();
$headers = array(
// Request headers
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
'width' => '100', // Width of the thumbnail.
'height' => '100', // Height of the thumbnail.
'smartCropping' => 'true',
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_POST);
// Request body parameters
$body = json_encode(array('url' => $imageUrl));
// Request body
$request->setBody($body);
try
{
$response = $request->send();
echo "<pre>" .
json_encode(json_decode($response->getBody()), JSON_PRETTY_PRINT) . "</pre>";
}
catch (HttpException $ex)
{
echo "<pre>" . $ex . "</pre>";
}
?>
</body>
</html>
Examine the response
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
fails, the response is displayed in the browser window. The response for the failed request contains an error code
and a message to help determine what went wrong.
Clean up resources
Next steps
When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2 package. To uninstall the
package, do the following steps:
pear uninstall HTTP_Request2
1. Open a command prompt window as an administrator.
2. Run the following command:
3. After the package is successfully uninstalled, close the command prompt window.
Explore the Computer Vision API to analyze an image, detect celebrities and landmarks, create a thumbnail, and
extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing
console.
Explore the Computer Vision API
Quickstart: Generate a thumbnail using the REST API
and Python in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you will generate a thumbnail from an image using Computer Vision's REST API. With the Get
Thumbnail method, you can specify the desired height and width, and Computer Vision uses smart cropping to
intelligently identify the area of interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
A code editor such as Visual Studio Code
To create and run the sample, copy the following code into the code editor.
import requests
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
# Replace <Subscription Key> with your valid subscription key.
subscription_key = "<Subscription Key>"
assert subscription_key
# You must use the same region in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from
# westus, replace "westcentralus" in the URI below with "westus".
#
# Free trial subscription keys are generated in the "westus" region.
# If you use a free trial subscription key, you shouldn't need to change
# this region.
vision_base_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/"
thumbnail_url = vision_base_url + "generateThumbnail"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'width': '50', 'height': '50', 'smartCropping': 'true'}
data = {'url': image_url}
response = requests.post(thumbnail_url, headers=headers, params=params, json=data)
response.raise_for_status()
thumbnail = Image.open(BytesIO(response.content))
# Display the thumbnail.
plt.imshow(thumbnail)
plt.axis("off")
# Verify the thumbnail size.
print("Thumbnail is {0}-by-{1}".format(*thumbnail.size))
Examine the response
Run in Jupyter (optional)
Next, do the following:
1. Replace the value of subscription_key with your subscription key.
2. Replace the value of vision_base_url with the endpoint URL for the Computer Vision resource in the Azure
region where you obtained your subscription keys, if necessary.
3. Optionally, replace the value of image_url with the URL of a different image for which you want to generate a
thumbnail.
4. Save the code as a file with an .py extension. For example, get-thumbnail.py .
5. Open a command prompt window.
6. At the prompt, use the python command to run the sample. For example, python get-thumbnail.py .
A successful response is returned as binary data which represents the image data for the thumbnail. The sample
should display this image. If the request fails, the response is displayed in the command prompt window and
should contain an error code.
You can optionally run this quickstart in a step-by step fashion using a Jupyter notebook on MyBinder. To launch
Next steps
Binder, select the following button:
launch
launch binder
binder
Next, learn more detailed information about the thumbnail generation feature.
Generating thumbnails
Quickstart: Generate a thumbnail using the REST API
and Ruby in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample
In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. With the Get
Thumbnail method, you can generate a thumbnail of an image. You specify the height and width, which can differ
from the aspect ratio of the input image. Computer Vision uses smart cropping to intelligently identify the area of
interest and generate cropping coordinates based on that region.
If you don't have an Azure subscription, create a free account before you begin.
You must have Ruby 2.4.x or later installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with an .rb extension. For example, get-thumbnail.rb .
4. Open a command prompt window.
5. At the prompt, use the ruby command to run the sample. For example, ruby get-thumbnail.rb .
a. Replace <Subscription Key> with your subscription key.
b. Replace https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze with the endpoint URL
for the Get Thumbnail method in the Azure region where you obtained your subscription keys, if
necessary.
c. Optionally, replace
https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-
Shorkie_Poo_Puppy.jpg\
with the URL of a different image for which you want to generate a thumbnail.
require 'net/http'
# You must use the same location in your REST call as you used to get your
# subscription keys. For example, if you got your subscription keys from westus,
# replace "westcentralus" in the URL below with "westus".
uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail')
uri.query = URI.encode_www_form({
# Request parameters
'width' => '100',
'height' => '100',
'smartCropping' => 'true'
})
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
# Replace <Subscription Key> with your valid subscription key.
request['Ocp-Apim-Subscription-Key'] = '<Subscription Key>'
request['Content-Type'] = 'application/json'
request.body =
"{\"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/" +
"Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\"}"
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
#puts response.body
Examine the response
Next steps
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request
fails, the response is displayed in the console window. The response for the failed request contains an error code
and a message to help determine what went wrong.
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Extract printed text (OCR) using the REST
API and C# in Computer Vision
4/18/2019 4 minutes to read Edit Online
Prerequisites
Create and run the sample application
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CSHttpClientSample
{
static class Program
{
// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
In this quickstart, you will extract printed text with optical character recognition (OCR) from an image by using
Computer Vision's REST API. With the OCR feature, you can detect printed text in an image and extract recognized
characters into a machine-usable character stream.
If you don't have an Azure subscription, create a free account before you begin.
You must have Visual Studio 2015 or later.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create the sample in Visual Studio, do the following steps:
1. Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App template.
2. Install the Newtonsoft.Json NuGet package.
3. Replace the code in Program.cs with the following code, and then make the following changes in code where
needed:
4. Run the program.
5. At the prompt, enter the path to a local image.
a. On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for
Solution.
b. Click the Browse tab, and in the Search box type "Newtonsoft.Json".
c. Select Newtonsoft.Json when it displays, then click the checkbox next to your project name, and Install.
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the OCR method from the Azure region where
you obtained your subscription keys, if necessary.
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Optical Character Recognition:");
Console.Write("Enter the path to an image with text you wish to read: ");
string imageFilePath = Console.ReadLine();
if (File.Exists(imageFilePath))
{
// Call the REST API method.
Console.WriteLine("\nWait a moment for the results to appear.\n");
MakeOCRRequest(imageFilePath).Wait();
}
else
{
Console.WriteLine("\nInvalid file path");
}
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
/// <summary>
/// Gets the text visible in the specified image file by using
/// the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file with printed text.</param>
static async Task MakeOCRRequest(string imageFilePath)
{
try
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters.
// The language parameter doesn't specify a language, so the
// method detects it automatically.
// The detectOrientation parameter is set to true, so the method detects and
// and corrects text orientation before detecting text.
string requestParameters = "language=unk&detectOrientation=true";
// Assemble the URI for the REST API method.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Read the contents of the specified local image
// into a byte array.
byte[] byteData = GetImageAsByteArray(imageFilePath);
// Add the byte array as an octet stream to the request body.
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses the "application/octet-stream" content type.
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Asynchronously call the REST API method.
response = await client.PostAsync(uri, content);
}
// Asynchronously get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n\n{0}\n",
JToken.Parse(contentString).ToString());
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message);
}
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
// Open a read-only file stream for the specified file.
using (FileStream fileStream =
new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
// Read the file's contents into a byte array.
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
}
Examine the response
{
"language": "en",
"textAngle": -1.5000000000000335,
"orientation": "Up",
"regions": [
{
"boundingBox": "154,49,351,575",
"lines": [
{
"boundingBox": "165,49,340,117",
"words": [
{
"boundingBox": "165,49,63,109",
"text": "A"
},
{
"boundingBox": "261,50,244,116",
"text": "GOAL"
}
]
},
{
"boundingBox": "165,169,339,93",
"words": [
A successful response is returned in JSON. The sample application parses and displays a successful response in the
console window, similar to the following example:
{
"boundingBox": "165,169,339,93",
"text": "WITHOUT"
}
]
},
{
"boundingBox": "159,264,342,117",
"words": [
{
"boundingBox": "159,264,64,110",
"text": "A"
},
{
"boundingBox": "255,266,246,115",
"text": "PLAN"
}
]
},
{
"boundingBox": "161,384,338,119",
"words": [
{
"boundingBox": "161,384,86,113",
"text": "IS"
},
{
"boundingBox": "274,387,225,116",
"text": "JUST"
}
]
},
{
"boundingBox": "154,506,341,118",
"words": [
{
"boundingBox": "154,506,62,111",
"text": "A"
},
{
"boundingBox": "248,508,247,116",
"text": "WISH"
}
]
}
]
}
]
}
Clean up resources
Next steps
When no longer needed, delete the Visual Studio solution. To do so, open File Explorer, navigate to the folder in
which you created the Visual Studio solution, and delete the folder.
Explore a basic Windows application that uses Computer Vision to perform optical character recognition (OCR);
create smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an
image.
Computer Vision API C# Tutorial
Quickstart: Extract printed text (OCR) using the REST
API and cURL in Computer Vision
4/18/2019 2 minutes to read Edit Online
Prerequisites
Create and run the sample command
curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -H "Content-Type: application/json"
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr?language=unk&detectOrientation=true" -d "
{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-
Atomist_quote_from_Democritus.png\"}"
Examine the response
{
"language": "en",
"orientation": "Up",
"textAngle": 0,
"regions": [
{
In this quickstart, you extract printed text with optical character recognition (OCR) from an image by using
Computer Vision's REST API. With the OCR method, you can detect printed text in an image and extract
recognized characters into a machine-usable character stream.
If you don't have an Azure subscription, create a free account before you begin.
You must have cURL.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following command into a text editor.
2. Make the following changes in the command where needed:
3. Open a command prompt window.
4. Paste the command from the text editor into the command prompt window, and then run the command.
a. Replace the value of <subscriptionKey> with your subscription key.
b. Replace the request URL (https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr ) with the
endpoint URL for the OCR method from the Azure region where you obtained your subscription keys, if
necessary.
c. Optionally, change the image URL in the request body (
https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-
Atomist_quote_from_Democritus.png\
) to the URL of a different image to be analyzed.
A successful response is returned in JSON. The sample application parses and displays a successful response in the
command prompt window, similar to the following example:
{
"boundingBox": "21,16,304,451",
"lines": [
{
"boundingBox": "28,16,288,41",
"words": [
{
"boundingBox": "28,16,288,41",
"text": "NOTHING"
}
]
},
{
"boundingBox": "27,66,283,52",
"words": [
{
"boundingBox": "27,66,283,52",
"text": "EXISTS"
}
]
},
{
"boundingBox": "27,128,292,49",
"words": [
{
"boundingBox": "27,128,292,49",
"text": "EXCEPT"
}
]
},
{
"boundingBox": "24,188,292,54",
"words": [
{
"boundingBox": "24,188,292,54",
"text": "ATOMS"
}
]
},
{
"boundingBox": "22,253,297,32",
"words": [
{
"boundingBox": "22,253,105,32",
"text": "AND"
},
{
"boundingBox": "144,253,175,32",
"text": "EMPTY"
}
]
},
{
"boundingBox": "21,298,304,60",
"words": [
{
"boundingBox": "21,298,304,60",
"text": "SPACE."
}
]
},
{
"boundingBox": "26,387,294,37",
"words": [
{
"boundingBox": "26,387,210,37",
"text": "Everything"
},
{
"boundingBox": "249,389,71,27",
"boundingBox": "249,389,71,27",
"text": "else"
}
]
},
{
"boundingBox": "127,431,198,36",
"words": [
{
"boundingBox": "127,431,31,29",
"text": "is"
},
{
"boundingBox": "172,431,153,36",
"text": "opinion."
}
]
}
]
}
]
}
Next steps
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Extract printed text (OCR) using the REST
API and Go in Computer Vision
4/18/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
func main() {
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = "<Subscription Key>"
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
In this quickstart, you extract printed text with optical character recognition (OCR) from an image by using
Computer Vision's REST API. With the OCR method, you can detect printed text in an image and extract
recognized characters into a machine-usable character stream.
If you don't have an Azure subscription, create a free account before you begin.
You must have Go installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
1. Copy the following code into a text editor.
2. Make the following changes in code where needed:
3. Save the code as a file with a .go extension. For example, get-printed-text.go .
4. Open a command prompt window.
5. At the prompt, run the go build command to compile the package from the file. For example,
go build get-printed-text.go .
6. At the prompt, run the compiled package. For example, get-printed-text .
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the OCR method from the Azure region where
you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr"
const imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/" +
"Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png"
const params = "?language=unk&detectOrientation=true"
const uri = uriBase + params
const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"
reader := strings.NewReader(imageUrlEnc)
// Create the Http client
client := &http.Client{
Timeout: time.Second * 2,
}
// Create the Post request, passing the image URL in the request body
req, err := http.NewRequest("POST", uri, reader)
if err != nil {
panic(err)
}
// Add headers
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
// Send the request and retrieve the response
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Read the response body.
// Note, data is a byte array
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Parse the Json data
var f interface{}
json.Unmarshal(data, &f)
// Format and display the Json result
jsonFormatted, _ := json.MarshalIndent(f, "", " ")
fmt.Println(string(jsonFormatted))
}
Examine the response
{
"language": "en",
"orientation": "Up",
"regions": [
{
"boundingBox": "21,16,304,451",
"lines": [
A successful response is returned in JSON. The sample application parses and displays a successful response in the
command prompt window, similar to the following example:
"lines": [
{
"boundingBox": "28,16,288,41",
"words": [
{
"boundingBox": "28,16,288,41",
"text": "NOTHING"
}
]
},
{
"boundingBox": "27,66,283,52",
"words": [
{
"boundingBox": "27,66,283,52",
"text": "EXISTS"
}
]
},
{
"boundingBox": "27,128,292,49",
"words": [
{
"boundingBox": "27,128,292,49",
"text": "EXCEPT"
}
]
},
{
"boundingBox": "24,188,292,54",
"words": [
{
"boundingBox": "24,188,292,54",
"text": "ATOMS"
}
]
},
{
"boundingBox": "22,253,297,32",
"words": [
{
"boundingBox": "22,253,105,32",
"text": "AND"
},
{
"boundingBox": "144,253,175,32",
"text": "EMPTY"
}
]
},
{
"boundingBox": "21,298,304,60",
"words": [
{
"boundingBox": "21,298,304,60",
"text": "SPACE."
}
]
},
{
"boundingBox": "26,387,294,37",
"words": [
{
"boundingBox": "26,387,210,37",
"text": "Everything"
},
{
"boundingBox": "249,389,71,27",
"text": "else"
}
}
]
},
{
"boundingBox": "127,431,198,36",
"words": [
{
"boundingBox": "127,431,31,29",
"text": "is"
},
{
"boundingBox": "172,431,153,36",
"text": "opinion."
}
]
}
]
}
],
"textAngle": 0
}
Next steps
Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail,
and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API
testing console.
Explore the Computer Vision API
Quickstart: Extract printed text (OCR) using the REST
API and Java in Computer Vision
4/18/2019 3 minutes to read Edit Online
Prerequisites
Create and run the sample application
In this quickstart, you extract printed text with optical character recognition (OCR) from an image by using
Computer Vision's REST API. With the OCR method, you can detect printed text in an image and extract
recognized characters into a machine-usable character stream.
If you don't have an Azure subscription, create a free account before you begin.
You must have Java™ Platform, Standard Edition Development Kit 7 or 8 (JDK 7 or 8) installed.
You must have a subscription key for Computer Vision. You can get a free trial key from Try Cognitive Services.
Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your
key.
To create and run the sample, do the following steps:
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from
a command line application template.
2. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are
provided for each library.
Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
Apache HTTP core (org.apache.httpcomponents:httpcore:4.4.9)
JSON library (org.json:json:20180130)
3. Add the following import statements to the file that contains the Main public class for your project.
4. Replace the Main public class with the following code, then make the following changes in code where
needed:
a. Replace the value of subscriptionKey with your subscription key.
b. Replace the value of uriBase with the endpoint URL for the OCR method from the Azure region where
you obtained your subscription keys, if necessary.
c. Optionally, replace the value of imageToAnalyze with the URL of a different image from which you want
to extract printed text.
5. Save, then build the Java project.
6. If you're using an IDE, run Main . Otherwise, open a command prompt window and then use the java
command to run the compiled class. For example, java Main .
public class Main {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
private static final String subscriptionKey = "<Subscription Key>";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
private static final String uriBase =
"https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr";
private static final String imageToAnalyze =
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/" +
"Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png";
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
URIBuilder uriBuilder = new URIBuilder(uriBase);
uriBuilder.setParameter("language", "unk");
uriBuilder.setParameter("detectOrientation", "true");
// Request parameters.
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request body.
StringEntity requestEntity =
new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
request.setEntity(requestEntity);
// Call the REST API method and get the response entity.
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
// Format and display the JSON response.
String jsonString = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonString);
System.out.println("REST Response:\n");
System.out.println(json.toString(2));
}
} catch (Exception e) {
// Display error message.
System.out.println(e.getMessage());
}
}
}
Examine the response
REST Response:
{
"orientation": "Up",
"regions": [{
"boundingBox": "21,16,304,451",
"lines": [
{
"boundingBox": "28,16,288,41",
"words": [{
"boundingBox": "28,16,288,41",
"text": "NOTHING"
}]
},
{
"boundingBox": "27,66,283,52",
"words": [{
"boundingBox": "27,66,283,52",
"text": "EXISTS"
}]
},
{
"boundingBox": "27,128,292,49",
"words": [{
"boundingBox": "27,128,292,49",
"text": "EXCEPT"
}]
},
{
"boundingBox": "24,188,292,54",
"words": [{
"boundingBox": "24,188,292,54",
"text": "ATOMS"
}]
},
{
"boundingBox": "22,253,297,32",
"words": [
{
"boundingBox": "22,253,105,32",
"text": "AND"
},
{
"boundingBox": "144,253,175,32",
"text": "EMPTY"
}
]
},
{
"boundingBox": "21,298,304,60",
"words": [{
"boundingBox": "21,298,304,60",
"text": "SPACE."
}]
},
{
"boundingBox": "26,387,294,37",
"words": [
{
"boundingBox": "26,387,210,37",
"text": "Everything"
},
{
"boundingBox": "249,389,71,27",
"text": "else"
A successful response is returned in JSON. The sample application parses and displays a successful response in the
console window, similar to the following example:
"text": "else"
}
]
},
{
"boundingBox": "127,431,198,36",
"words": [
{
"boundingBox": "127,431,31,29",
"text": "is"
},
{
"boundingBox": "172,431,153,36",
"text": "opinion."
}
]
}
]
}],
"textAngle": 0,
"language": "en"
}
Clean up resources
Next steps
When no longer needed, delete the Java project, including the compiled class and imported libraries.
Explore a Java Swing application that uses Computer Vision to perform optical character recognition (OCR); create
smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To
rapidly experiment with the Computer Vision API, try the Open API testing console.
Computer Vision API Java Tutorial
Quickstart: Extract printed text (OCR) using the REST
API and JavaScript in Computer Vision
4/18/2019 3 minutes to read