Sebastian Gomez
Introduction to Google Vision API
I've always been fascinated by artificial intelligence. In fact, I used to teach a few courses on the subject at universities in Medellín, and the field is so broad and vast that a person's first steps into it can feel overwhelming, and it's likely they give up before building anything tangible with AI. That's why I wanted to write this story. It will also serve as notes for a talk I'm giving at the Google DevFest in Medellín.
Let's start with the basics. I want to show you one of the APIs from the whole suite of cloud services Google offers within its Google Cloud Platform product: Google Cloud Vision API. As its name suggests, it's a REST API that lets you perform detailed analysis and deep processing of images. While it's available for almost every modern programming language, I find it interesting to understand how to get it running with JavaScript, and fortunately Google offers an official library, the @google-cloud/vision SDK, that works perfectly with Node.js.
How does Google Cloud Vision API work?
Here I want to correct a misconception that's very common. Vision API does not search for images similar to yours in a giant gallery and then copy their labels. What it does is run your image through pretrained deep learning models that Google built using huge amounts of labeled images. Those models don't compare your photo against other photos, they recognize the patterns they learned during training and produce predictions directly about your image.
In practice, Vision API isn't a single model but a collection of specialized features you can request separately:
- Label detection: labels the general content of the image (dog, beach, food, vehicle, etc.).
- Object localization: detects objects and also returns the bounding box where they are.
- Text detection and document text detection (OCR): extracts printed or handwritten text from the image.
- Face detection: detects faces and attributes such as expression, without identifying the person.
- Landmark detection: recognizes well known places and monuments.
- Safe search: classifies whether an image contains sensitive content.
When you send an image, you choose which features you want and the API returns each prediction with a confidence score between 0 and 1. Behind all of this is the computing power of Google's servers and years of training these models, so in just a few lines of code you get access to computer vision capabilities that would be very expensive to build from scratch.
Let's get it running in Node.js
Let's get to the practical part, which is exactly what I promised. First you need a Google Cloud project with the Cloud Vision API enabled and a service account whose JSON credentials file you've downloaded. The simplest approach is to point the GOOGLE_APPLICATION_CREDENTIALS environment variable to that file.
Note: never commit your JSON credentials file to version control. Keep it out of Git and, in production, prefer managed identities (Workload Identity) over downloaded keys.
Set up your credentials and create the project:
# Point to your service account file
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"
# Initialize the project and install the official SDK
npm init -y
npm install @google-cloud/visionNow a minimal label detection example. Since the SDK uses promises, async/await is the most comfortable approach:
// index.js
import vision from '@google-cloud/vision';
// The client reads credentials from GOOGLE_APPLICATION_CREDENTIALS
const client = new vision.ImageAnnotatorClient();
async function detectLabels(filePath) {
// labelDetection returns a promise with an array; the first element is the response
const [result] = await client.labelDetection(filePath);
const labels = result.labelAnnotations ?? [];
console.log('Detected labels:');
for (const label of labels) {
// description is the label; score is the confidence between 0 and 1
console.log(`- ${label.description} (${(label.score * 100).toFixed(1)}%)`);
}
}
detectLabels('./dog.jpg').catch(console.error);Run the script with:
node index.jsEach item in labelAnnotations carries a description (the label) and a score (the confidence). That's why we print the score as a percentage: it helps you decide, depending on your use case, the threshold above which you want to trust a label.
What if your image lives on the internet rather than on your disk? You can pass a public URL or a Cloud Storage object directly:
// You can also analyze a remote image by its URL
const [result] = await client.labelDetection(
'https://example.com/dog.jpg',
);Tip: the same client exposes methods for the other features, such as textDetection (OCR), objectLocalization, faceDetection and landmarkDetection. You call them the same way: you pass the image and get back an array whose first element is the response.
Note: the @google-cloud/vision SDK exposes the ImageAnnotatorClient client with methods like labelDetection, textDetection, faceDetection and safeSearchDetection. Response field names can vary across major versions; check the official reference for your version.
How much does it cost?
You're probably wondering whether this costs an arm and a leg. The good news is that, to get started, it doesn't. Google Cloud Vision API offers a monthly free tier per feature (for example, label detection or OCR) and, beyond that threshold, charges by blocks of processed units. At the time of writing, the figure that used to circulate was around USD 1.50 per 1,000 units, but prices and the free tier structure change over time.
Note: check the current figures on the official pricing page, since they change over time: Cloud Vision API pricing (cloud.google.com/vision/pricing). The values above are indicative.
So there's no reason not to at least try this API.
Alternatives
Vision API isn't the only computer vision and image processing option you'll find. There are interesting alternatives you can compare and use:
- Azure AI Vision (formerly Azure Cognitive Services Computer Vision), Microsoft's equivalent.
- OCR.Space, an OCR focused option, useful and affordable.
Some of these alternatives can turn out cheaper, but which one to use and when will depend on your specific needs. I recommend comparing prices and capabilities directly in each provider's official documentation.
Conclusions
- Google Cloud Vision API is a powerful tool for analyzing and processing images using pretrained deep learning models, not similar image lookups.
- It offers a free tier per feature and reasonable pricing, which makes it accessible for anyone who wants to experiment (remember to verify the current figures).
- There are alternatives such as Azure AI Vision and OCR.Space, and it's worth comparing which one best fits your project.
Suggested exercises
- Enable the Cloud Vision API in a Google Cloud project, set
GOOGLE_APPLICATION_CREDENTIALS, and run the label detection example on several images of your own. - Extend the script to use
textDetection(OCR) on an image with text and print the extracted text. - Compare Google Cloud Vision API with Azure AI Vision and OCR.Space, analyzing the pros, cons, and how they fit your needs.
3-point summary
- Vision API runs your image through pretrained deep learning models that produce predictions (labels, objects, text, faces) with a confidence score.
- With the official
@google-cloud/visionSDK and a few lines of Node.js you already have computer vision capabilities running. - The API is cost accessible and has alternatives such as Azure AI Vision and OCR.Space worth evaluating per project.
That's all, I hope this post is useful to you and that you can apply it to a project you have in mind.
Leave me a comment if it helped, if you want to add an opinion, or if you have any questions. And remember, if you liked it, you can also share it using the social links below. Good luck with your next computer vision project.
Sebastian Gomez
Creador de contenido principalmente acerca de tecnología.