diff --git a/google_vision.module b/google_vision.module index 51c50c5..3441648 100755 --- a/google_vision.module +++ b/google_vision.module @@ -28,6 +28,7 @@ function google_vision_entity_presave(Drupal\Core\Entity\EntityInterface $entity // Take first referenced vocabulary. $vid = reset($field->getSettings()['handler_settings']['target_bundles']); google_vision_file_entity_add_labels($entity, $field, $vid); + google_vision_file_entity_add_landmark($entity, $field, $vid); } } } @@ -118,3 +119,21 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { } } } + +/** + * Try to identify and detect popular landmarks for the image. + */ +function google_vision_file_entity_add_landmark($file, $field, $vid) { + // Try to retrieve file URI. + $file_uri = $file->getFileUri(); + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $data = \Drupal::service('google_vision.api')->landmarkDetection($filepath); + // If we have retrieved labels. + if (!empty($data['responses'][0]['landmarkAnnotations'])) { + foreach ($data['responses'][0]['landmarkAnnotations'] as $item) { + $landmark = $item['description']; + } + $file->set('field_image_alt_text', $landmark); + } + } +} diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php index 814a1d7..8af1521 100755 --- a/src/GoogleVisionAPI.php +++ b/src/GoogleVisionAPI.php @@ -138,7 +138,7 @@ class GoogleVisionAPI { 'features' => [ [ 'type' => 'LANDMARK_DETECTION', - 'maxResults' => 2 + 'maxResults' => 1 ], ], ],