|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const request = require('request') |
| 7 | + |
| 8 | +/** |
| 9 | + * This sample uses the Computer Vision API to analyze a remote image. |
| 10 | + * It returns image properties such as categories, description (tags), color, and size. |
| 11 | + * |
| 12 | + * Computer Vision API - v2 .1: |
| 13 | + * https: //westus.dev.cognitive.microsoft.com/docs/services/5cd27ec07268f6c679a3e641/operations/56f91f2e778daf14a499f20d |
| 14 | + */ |
| 15 | + |
| 16 | +// Add your Computer Vision subscription key and endpoint to your environment variables. |
| 17 | +let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'] |
| 18 | +let endpoint = process.env['COMPUTER_VISION_ENDPOINT'] + '/vision/v2.1/analyze' |
| 19 | + |
| 20 | +// Image to be analyzed; you may use your own URL image. |
| 21 | +const url = |
| 22 | + 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg' |
| 23 | + |
| 24 | +// Request parameters. |
| 25 | +const params = { |
| 26 | + 'visualFeatures': 'Categories,Description,Color', |
| 27 | + 'details': '', |
| 28 | + 'language': 'en' |
| 29 | +} |
| 30 | + |
| 31 | +// Options |
| 32 | +const options = { |
| 33 | + uri: endpoint, |
| 34 | + qs: params, |
| 35 | + body: '{"url": ' + '"' + url + '"}', |
| 36 | + headers: { |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + 'Ocp-Apim-Subscription-Key' : subscriptionKey |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Make the request. |
| 43 | +request.post(options, (error, response, body) => { |
| 44 | + console.error('error:', error) |
| 45 | + console.log('statusCode:', response && response.statusCode) |
| 46 | + console.log('original image:', url.substring(url.lastIndexOf('/') + 1)) |
| 47 | + console.log() |
| 48 | + console.log(JSON.stringify(JSON.parse(body), null, 2)) |
| 49 | +}) |
0 commit comments