diff --git a/README.txt b/README.txt index a956647..0ca42c2 100644 --- a/README.txt +++ b/README.txt @@ -34,6 +34,7 @@ Google Vision API offers the following features: Taxonomy tagging using Label Detection -------------------------------------- Label Detection feature of the Google Cloud Vision API has been used for taxonomy tagging purpose. +Note: The number of results returned by the Vision API for labels is configurable. The user can set the value on /admin/config/media/google_vision. If not set, the default value is considered to be 5. Step by step instructions are given below: 1. Create taxonomy vocabulary for your labels (tags). diff --git a/config/install/google_vision.settings.yml b/config/install/google_vision.settings.yml deleted file mode 100644 index a009027..0000000 --- a/config/install/google_vision.settings.yml +++ /dev/null @@ -1,7 +0,0 @@ -max_results_label_detection: 5 -max_results_landmark_detection: 2 -max_results_logo_detection: 2 -max_results_safe_search_detection: 1 -max_results_text_detection: 10 -max_results_face_detection: 25 -max_results_image_properties: 5 diff --git a/config/schema/google_vision.schema.yml b/config/schema/google_vision.schema.yml index 514cc6b..b9511d9 100644 --- a/config/schema/google_vision.schema.yml +++ b/config/schema/google_vision.schema.yml @@ -13,27 +13,9 @@ google_vision.settings: api_key: type: string label: 'Google Vision Api Key' - max_results_label_detection: + max_results_labels: type: integer - label: 'Max results for the Label Detection feature' - max_results_landmark_detection: - type: integer - label: 'Max results for the Landmark Detection feature' - max_results_logo_detection: - type: integer - label: 'Max results for the Logo Detection feature' - max_results_safe_search_detection: - type: integer - label: 'Max results for the Safe Search Detection feature' - max_results_text_detection: - type: integer - label: 'Max results for the Text Detection feature' - max_results_face_detection: - type: integer - label: 'Max results for the Face Detection feature' - max_results_image_properties: - type: integer - label: 'Max results for the Image Properties feature' + label: 'Maximum results for Label Detection' field.field.*.*.*.third_party.google_vision: type: mapping diff --git a/src/Form/GoogleVisionSettingsForm.php b/src/Form/GoogleVisionSettingsForm.php index d52c498..1e61159 100644 --- a/src/Form/GoogleVisionSettingsForm.php +++ b/src/Form/GoogleVisionSettingsForm.php @@ -42,87 +42,32 @@ class GoogleVisionSettingsForm extends ConfigFormBase { $form['api_key'] = [ '#type' => 'textfield', - '#title' => t('Google Vision API key'), + '#title' => $this->t('Google Vision API key'), '#required' => TRUE, - '#description' => t( + '#description' => $this->t( 'To create API key
  1. Visit Google Console and create a project to use.
  2. Enable the Cloud Vision API.
  3. Generate API key with type "Browser key" under the Credentials tab.
', [ - '@url' => 'https://cloud.google.com/console', + '@url' => 'https://cloud.google.com/console' ] ), - '#default_value' => $config->get('api_key'), + '#default_value' => $config->get('api_key') ]; - $form['max_results'] = [ - '#type' => 'details', - '#title' => $this->t('Max number of results on each API call'), - '#open' => FALSE, - ]; - - $form['max_results']['label_detection'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Label Detection feature'), - '#default_value' => $config->get('max_results_label_detection'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, - ]; - - $form['max_results']['landmark_detection'] = [ + $form['max_results_labels'] = [ '#type' => 'number', - '#title' => $this->t('Max results for the Landmark Detection feature'), - '#default_value' => $config->get('max_results_landmark_detection'), + '#title' => $this->t('Max results for Label Detection'), + '#description' => $this->t('Optional. The default value will be set to 5.'), + '#default_value' => !empty($config->get('max_results_labels')) ? $config->get('max_results_labels') : 5, '#min' => 1, '#step' => 1, - '#required' => TRUE, ]; - $form['max_results']['logo_detection'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Logo Detection feature'), - '#default_value' => $config->get('max_results_logo_detection'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, - ]; - - $form['max_results']['safe_search_detection'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Safe Search Detection feature'), - '#default_value' => $config->get('max_results_safe_search_detection'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, - ]; - - $form['max_results']['text_detection'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Text Detection feature'), - '#default_value' => $config->get('max_results_text_detection'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, - ]; - - $form['max_results']['face_detection'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Face Detection feature'), - '#default_value' => $config->get('max_results_face_detection'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, - ]; - - $form['max_results']['image_properties'] = [ - '#type' => 'number', - '#title' => $this->t('Max results for the Image Properties Detection feature'), - '#default_value' => $config->get('max_results_image_properties'), - '#min' => 1, - '#step' => 1, - '#required' => TRUE, + $form['note'] = [ + '#type' => 'markup', + '#markup' => $this->t('Note: The labels are the result of the responses returned by Vision API. It may sometimes happen that the number of results is less than this value.'), ]; return parent::buildForm($form, $form_state); @@ -134,13 +79,7 @@ class GoogleVisionSettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('google_vision.settings') ->set('api_key', $form_state->getValue('api_key')) - ->set('max_results_label_detection', $form_state->getValue('label_detection')) - ->set('max_results_landmark_detection', $form_state->getValue('landmark_detection')) - ->set('max_results_logo_detection', $form_state->getValue('logo_detection')) - ->set('max_results_safe_search_detection', $form_state->getValue('safe_search_detection')) - ->set('max_results_text_detection', $form_state->getValue('text_detection')) - ->set('max_results_face_detection', $form_state->getValue('face_detection')) - ->set('max_results_image_properties', $form_state->getValue('image_properties')) + ->set('max_results_labels', $form_state->getValue('max_results_labels')) ->save(); parent::submitForm($form, $form_state); diff --git a/src/GoogleVisionApi.php b/src/GoogleVisionApi.php index a3242e5..e648de7 100644 --- a/src/GoogleVisionApi.php +++ b/src/GoogleVisionApi.php @@ -47,80 +47,30 @@ class GoogleVisionApi implements GoogleVisionApiInterface { protected $maxResultsLabelDetection; /** - * Stores the maxResults number for the LANDMARK_DETECTION feature. - * - * @var int - */ - protected $maxResultsLandmarkDetection; - - /** - * Stores the maxResults number for the LOGO_DETECTION feature. - * - * @var int - */ - protected $maxResultsLogoDetection; - - /** - * Stores the maxResults number for the SAFE_SEARCH_DETECTION feature. - * - * @var int - */ - protected $maxResultsSafeSearchDetection; - - /** - * Stores the maxResults number for the TEXT_DETECTION feature. - * - * @var int - */ - protected $maxResultsTextDetection; - - /** - * Stores the maxResults number for the FACE_DETECTION feature. - * - * @var int - */ - protected $maxResultsFaceDetection; - - /** - * Stores the maxResults number for the IMAGE_PROPERTIES feature. - * - * @var int - */ - protected $maxResultsImageProperties; - - /** * Construct a GoogleVisionApi object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. + * * @param \GuzzleHttp\ClientInterface $http_client * A Guzzle client object. - * - * @todo Throw the exception when the api key is not set on status page. */ public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) { $this->configFactory = $config_factory; $this->httpClient = $http_client; - $config = $this->configFactory->get('google_vision.settings'); - $this->apiKey = $config->get('api_key'); - $this->maxResultsLabelDetection = $config->get('max_results_label_detection'); - $this->maxResultsLandmarkDetection = $config->get('max_results_landmark_detection'); - $this->maxResultsLogoDetection = $config->get('max_results_logo_detection'); - $this->maxResultsSafeSearchDetection = $config->get('max_results_safe_search_detection'); - $this->maxResultsTextDetection = $config->get('max_results_text_detection'); - $this->maxResultsFaceDetection = $config->get('max_results_face_detection'); - $this->maxResultsImageProperties = $config->get('max_results_image_properties'); + $this->apiKey = $this->configFactory->get('google_vision.settings') + ->get('api_key'); + $this->maxResultsLabelDetection = $this->configFactory->get('google_vision.settings') + ->get('max_results_labels'); } /** * Encode image to send it to the Google Vision Api. * * @param string $filepath - * The full path to the file. */ protected function encodeImage($filepath) { - // It looks pretty dirty. I hope that in future it will be implemented in - // Google SDK. + // It looks pretty dirty. I hope that in future it will be implemented in Google SDK // and we will be able to avoid this approach. $encoded_image = base64_encode(file_get_contents($filepath)); return $encoded_image; @@ -129,11 +79,11 @@ class GoogleVisionApi implements GoogleVisionApiInterface { /** * Function to make request through httpClient service. * - * @param mixed $data - * The object to be passed during the API call. + * @param $data + * The object to be passed during the API call. * * @return array - * An array obtained in response from the API call. + * An array obtained in response from the API call. */ protected function postRequest($data) { $url = static::APIEndpoint . $this->apiKey; @@ -204,7 +154,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'LANDMARK_DETECTION', - 'maxResults' => $this->maxResultsLandmarkDetection + 'maxResults' => 2 ], ], ], @@ -237,7 +187,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'LOGO_DETECTION', - 'maxResults' => $this->maxResultsLogoDetection + 'maxResults' => 2 ], ], ], @@ -270,7 +220,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'SAFE_SEARCH_DETECTION', - 'maxResults' => $this->maxResultsSafeSearchDetection + 'maxResults' => 1 ], ], ], @@ -303,7 +253,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'TEXT_DETECTION', - 'maxResults' => $this->maxResultsTextDetection + 'maxResults' => 10 ], ], ], @@ -336,7 +286,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'FACE_DETECTION', - 'maxResults' => $this->maxResultsFaceDetection + 'maxResults' => 25 ], ], ], @@ -369,7 +319,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'IMAGE_PROPERTIES', - 'maxResults' => $this->maxResultsImageProperties + 'maxResults' => 5 ], ], ], @@ -383,5 +333,4 @@ class GoogleVisionApi implements GoogleVisionApiInterface { return FALSE; } - } diff --git a/src/Tests/MaxResultConfigurableTest.php b/src/Tests/MaxResultConfigurableTest.php new file mode 100644 index 0000000..dbd73fb --- /dev/null +++ b/src/Tests/MaxResultConfigurableTest.php @@ -0,0 +1,62 @@ +adminUser = $this->drupalCreateUser([ + 'administer google vision', + ]); + $this->drupalLogin($this->adminUser); + } + + /** + * Test the default value for the "Maximum results for Label Detection". + */ + public function testDefaultValue() { + $this->drupalGet(Url::fromRoute('google_vision.settings')); + $this->assertResponse(200); + $this->assertFieldByName('max_results_labels', '5', 'The default value is set to 5'); + } + + /** + * Test to verify that "Maximum results for Label Detection" is + * configurable. + */ + public function testNewValue() { + $this->drupalGet(Url::fromRoute('google_vision.settings')); + $this->assertResponse(200); + $max_value = 2; + $edit = ['max_results_labels' => $max_value]; + $this->drupalPostForm(Url::fromRoute('google_vision.settings'), $edit, t('Save configuration')); + $this->assertFieldByName('max_results_labels', $max_value, 'The new value is set'); + } +} diff --git a/tests/modules/google_vision_test/google_vision_test.install b/tests/modules/google_vision_test/google_vision_test.install index 943cf03..0ce4ee5 100644 --- a/tests/modules/google_vision_test/google_vision_test.install +++ b/tests/modules/google_vision_test/google_vision_test.install @@ -6,5 +6,6 @@ function google_vision_test_install() { $config = \Drupal::configFactory()->getEditable('google_vision.settings'); $config->set('api_key', 'test_23key') + ->set('max_results_labels', 5) ->save(); }