CloudVision API*
Pentru enable -> Google Cloud Platform Dashboard -> Search for Cloud Vision API -> Enable (proces foarte asemanator cu cel necesar pentru enable TranslateAPI)
Cheia pe care o folositi la translateAPI poate fi folosita si pentru CloudVision API
În proiectul asocial Backend-ului -> terminal -> instalam @google-cloud/vision (npm i @google-cloud/vision)
Fisier nou in folderul utils: imageRecognitionFunctions.js
// imageRecognitionFunctions.js
const vision = require('@google-cloud/vision');
const dotenv = require("dotenv");
dotenv.config();
const imageRecognitionAPI = new vision.ImageAnnotatorClient({
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS
});
async function detectLabels(file) {
try{
const [result] = await imageRecognitionAPI.labelDetection(file);
const labels = result.labelAnnotations;
const labelsDescriptions = labels.map((label) => {
return label.description;
}
);
return labelsDescriptions;
}
catch(error){
console.log(error);
return;
}
}
detectLabels('https://www.impact.ro/wp-content/uploads/2021/12/New-York.jpg')
// detectLabels('./resources/waldo.jpeg')
module.exports = {
detectLabels
}
Drept parametru puteti trimite un fisier local sau chiar un link (atentie, pentru a introduce un link spre o poza trebuie sa apasati click dreapta pe o poza -> copy image adress -> paste ca parametru)
Exemplu de endpoint care utilizeaza aceasta metoda
// utilsRouter.js
router.get("/labels", async (req, res) => {
const { link } = req.body;
if (!link) {
return res.status(400).send("Bad request. Missing parametres.");
}
const labels = await detectLabels(link);
console.log(labels);
return res.json({
labels,
});
}
);
Last updated