|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +let request = require('request') |
| 7 | + |
| 8 | +/** |
| 9 | + * This sample uses the Computer Vision API to detect printed text in an image, |
| 10 | + * then returns the text and its properties, such as language, text angle, orientation. |
| 11 | + * Bounding boxes are calculated around portions of the text. |
| 12 | + * |
| 13 | + * Computer Vision API - v2 .1: |
| 14 | + * https: //westus.dev.cognitive.microsoft.com/docs/services/5cd27ec07268f6c679a3e641/operations/56f91f2e778daf14a499f20d |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +// Add your Azure Computer Vision subscription key and endpoint to your environment variables. |
| 19 | +// Also note v5 and v7 require separate subscription keys. |
| 20 | +let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'] |
| 21 | +let endpoint = process.env['COMPUTER_VISION_ENDPOINT'] + '/vision/v2.1/ocr' |
| 22 | + |
| 23 | +// An image with printed text; or replace with your own. |
| 24 | +let url = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg' |
| 25 | + |
| 26 | +// Request parameters. |
| 27 | +const params = { |
| 28 | + 'language': 'unk', // auto-detects language |
| 29 | + 'detectOrientation': 'true', |
| 30 | +}; |
| 31 | + |
| 32 | +// Options |
| 33 | +let request_params = { |
| 34 | + method: 'POST', |
| 35 | + uri: endpoint, |
| 36 | + qs: params, |
| 37 | + body: '{"url": ' + '"' + url + '"}', |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + 'Ocp-Apim-Subscription-Key': subscriptionKey |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Make request |
| 45 | +request(request_params, function (error, response, body) { |
| 46 | + console.error('error:', error) |
| 47 | + console.log('statusCode:', response && response.statusCode) |
| 48 | + console.log('original image: ' + url.substring(url.lastIndexOf('/') + 1)) |
| 49 | + console.log() |
| 50 | + console.log(JSON.stringify(JSON.parse(body), null, 2)) |
| 51 | +}) |
0 commit comments