private filterImageDescriptions()

in client/src/services/google/image-recognition.ts [127:155]


  private filterImageDescriptions(response: RecognitionResponse|null): ImageDescription[] {
    if (!response) {
      logger.warn('No Labels detected');
      throw new Error('No Labels detected');
    }
    // check if image as flagged as inappropriate
    const maxLikelihoods = this.baseConfig.maxSafeSearchLikelihoods;
    for (const cat in maxLikelihoods) {
      if (response.safeSearchAnnotation[cat] && maxLikelihoods[cat]
        && GoogleImageRecognitionServiceBase.getSafeSearchLikelihoodIndex(response.safeSearchAnnotation[cat])
        > GoogleImageRecognitionServiceBase.getSafeSearchLikelihoodIndex(maxLikelihoods[cat])) {
        logger.warn('Error loading image descriptions: image is inappropriate');
        throw new InappropriateContentError('Image is inappropriate');
      }
    }
    // check if image has no annotations
    if (!response || !response.labelAnnotations || response.labelAnnotations.length === 0) {
      logger.warn('No Labels detected');
      throw new Error('No Labels detected');
    }
    // filter out any annotations with multiple words
    let annotations = response.labelAnnotations;
    if (this.baseConfig.singleWordDescriptionsOnly) {
      annotations = annotations.filter( GoogleImageRecognitionServiceBase.isSingleWord );
    }
    // filter out duplicates
    annotations = GoogleImageRecognitionService.removeDuplicateWords(annotations);
    return annotations;
  }