// Cloned by Evan Mackey on 20 Nov 2024 from World "Azure Image Description Generator" by Suraksha Shetty
// Please leave this clone trail here.
let bgImage;
let message = "";
const objectsize = 130; // size of object
const anglechange = 0.01; // how much the rotate angle changes each step
var angle = 0;
let apiKey = ''; // User will enter their Azure Cognitive Services API key
function preload() {
bgImage = loadImage('https://ancientbrain.com/uploads/mackeye4/1732121894.png');
}
async function setup() {
createCanvas(1280, 853);
noStroke();
// Validate API key before proceeding
if (!apiKey) {
message += "<p1>Error: Please set your Azure Cognitive Services API key before analysis.</p1>";
AB.msg(message);
return;
}
// Analyze image using Azure Cognitive Services Computer Vision API
try {
const imageAnalysis = await analyzeImageWithAzureCV('https://ancientbrain.com/uploads/paraisr2/chess.jpg');
message += "<p1>Welcome to the Image Analysis demonstration!<p1><br>";
message += `<b>Image Description:</b> ${imageAnalysis.description.captions[0].text}<br>`;
message += `<b>Confidence:</b> ${(imageAnalysis.description.captions[0].confidence * 100).toFixed(2)}%<br>`;
// Display tags
message += "<b>Tags:</b> " + imageAnalysis.description.tags.join(", ") + "<br>";
// Display additional metadata
message += `<b>Image Format:</b> ${imageAnalysis.metadata.format}<br>`;
message += `<b>Image Width:</b> ${imageAnalysis.metadata.width}<br>`;
message += `<b>Image Height:</b> ${imageAnalysis.metadata.height}<br>`;
AB.msg(message);
} catch (error) {
message += `<p1>Error analyzing image: ${error.message}</p1>`;
AB.msg(message);
}
}
async function analyzeImageWithAzureCV(imageUrl) {
const endpoint = 'https://suraksha.cognitiveservices.azure.com/vision/v3.0/analyze';
const params = new URLSearchParams({
visualFeatures: 'Description',
details: 'Landmarks',
language: 'en'
});
const response = await fetch(`${endpoint}?${params}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiKey
},
body: JSON.stringify({ url: imageUrl })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
function draw() {
background(bgImage); // background color
}
// Optional: Function to set API key
function setAzureAPIKey(key) {
apiKey = key;
}