Skip to content

Instantly share code, notes, and snippets.

@SametSahin10
Created March 21, 2022 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SametSahin10/32b24f3ded9fd7779c0c7c5436fdecca to your computer and use it in GitHub Desktop.
Save SametSahin10/32b24f3ded9fd7779c0c7c5436fdecca to your computer and use it in GitHub Desktop.
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 = {
type: "LABEL_DETECTION",
maxResults: 50,
};
const safeSearchDetectionFeature = {
type: "SAFE_SEARCH_DETECTION",
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