Last active
October 2, 2022 12:52
-
-
Save thoughtspeed7/6230fe01747e1a2c1b1809049f84168d to your computer and use it in GitHub Desktop.
Azure Cognitive Services Computer Vision Read API Using Axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios').default; | |
const { setTimeout } = require('timers/promises'); | |
module.exports = async function (context, req) { | |
// Replace your_sub_domain with your Computer Vision's endpoint sub domain | |
const analyzeURL = | |
'https://your_sub_domain.cognitiveservices.azure.com/vision/v3.2/read/analyze'; | |
// Get imageURL from query string | |
// Example: https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg | |
const imageURL = req.query.imageURL; | |
// Set apiKey to your Computer Vision's key | |
const apiKey = 'your_key'; | |
let res = await axios.post( | |
analyzeURL, | |
{ | |
url: imageURL, | |
}, | |
{ | |
headers: { | |
'Ocp-Apim-Subscription-Key': apiKey, | |
}, | |
} | |
); | |
const analyzeResultsURL = res.headers['operation-location']; | |
// Wait for one second | |
await setTimeout(1000); | |
while (true) { | |
res = await axios.get(analyzeResultsURL, { | |
headers: { | |
'Ocp-Apim-Subscription-Key': apiKey, | |
}, | |
}); | |
if (res.data.status === 'succeeded') { | |
break; | |
} | |
// Keep waiting for one second until status = succeeded | |
await setTimeout(1000); | |
} | |
context.res = { | |
body: res.data, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment