A function that annotates an image using Google's Cloud Vision API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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