Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2022 10:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A function that annotates an image using Google's Cloud Vision API
* Annotates an image.
* @param {AnnotateImageParams} params The params.
export async function annotateImage(params: AnnotateImageParams) {
const imageAnnotatorClient = new vision.ImageAnnotatorClient();
const labelDetectionFeature = {
maxResults: 50,
const safeSearchDetectionFeature = {
maxResults: 50,
const features = [labelDetectionFeature, safeSearchDetectionFeature];
const image = {source: {imageUri: params.imageURI}};
const request = {image, features: features};
const [annotateImageResponse] =
await imageAnnotatorClient.annotateImage(request);
const safeSearchAnnotation = annotateImageResponse.safeSearchAnnotation;
const labelAnnotations = annotateImageResponse.labelAnnotations;
logger.debug("labelAnnotations: ", JSON.stringify(labelAnnotations));
logger.debug("safeSearchAnnotation: ", JSON.stringify(safeSearchAnnotation));
// ImageAnnotationResult is just a utility class that includes methods
// that determines if the image is appropriate or not
// based the values of safeSearchAnnotation and labelAnnotations.
return new ImageAnnotationResult(safeSearchAnnotation, labelAnnotations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment