Image Properties is another feature provided by the Vision API. It provides us with properties like the amount of red, green, blue colors in an image, pixels, etc. The most important and useful of them being the detection of the dominant color component in a particular image.

This property would be implemented in a way so as to provide the end user, a functionality to get the content nodes grouped together under the link /node/{node_id}/relatedcontent. The grouping would be on the basis of similar dominant color component(Red, Green or Blue) in the images of the nodes.

Remaining Tasks:
Develop patch for the issue which fulfills the requirement.

(This is one of my tasks of the GSOC 2016 Project.)

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

ajalan065 created an issue. See original summary.

ajalan065’s picture

Here is the patch in progress.
At present, the patch creates a taxonomy vocabulary for the dominant colors and list the colors.
A controller is also added, which finds out the nodes which share the same dominant color.
A new page routed at /node/{nid}/relatedcontent would contain the links to these nodes.

Task Remaining:
The page needs to be built in the controller.

ajalan065’s picture

Status: Active » Needs review
FileSize
10.99 KB
1.91 KB

Here is the patch which groups all the nodes which have same dominant colors in their images.
It creates a tab on the node/{nid} page under the name 'Similar Content' which displays the list of items sharing same dominant color.

Please review the work :)

ajalan065’s picture

Issue summary: View changes
ajalan065’s picture

Issue summary: View changes
eugene.ilyin’s picture

Status: Needs review » Needs work

Mhh, I think that if nodes have images with the same colors, then these nodes are not actually similar. I don't know how it can help to the end user.
In my opinion, more useful functionality is the search of the similar images by colors.
What do you think? Can you try to propose another useful cases?

We shouldn't just write the code, we should help to the end users.

ajalan065’s picture

You mean to say that instead of nodes, I should group the image files together?
Can you please elaborate your idea a bit on how to go about executing your idea? Sorry, but I could not extract much information from your suggestion above.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
10.47 KB

Here is the patch which groups the related images together, instead of nodes, based on the similar dominant color.
Edit the settings for the image file entity, and select the Dominant Color vocabulary also, in addition to the dedicated vocabulary for the labels.
The value for "Store new labels in " should refer to the vocabulary of the labels, and not Dominant Color.

Each image file would have a tab named as "Similar Content" which displays the required result list.

Please review the patch :)

naveenvalecha’s picture

Status: Needs review » Needs work
  1. +++ b/google_vision.module
    @@ -29,6 +31,21 @@ function google_vision_entity_presave(EntityInterface $entity) {
    +            $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
    

    use this class above and then only use "Vocabulary::loadMultiple"

  2. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,73 @@
    +    $query = \Drupal::database()->select('file_managed', 'fm');
    

    inject the connection object.

  3. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,73 @@
    +    $query->condition('vid', "dominant_color");
    

    why double quotes and single quotes

  4. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,73 @@
    +    $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
    

    use the Term class at the top and only use "Term::loadMultiple($tids)"

  5. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,73 @@
    +      $query = \Drupal::database()->select('file__field_labels', 'ffl');
    

    inject the connection object here.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
20.48 KB
14.52 KB

Here is the patch implementing the suggestions of #9. Also the patch contains tests for the issue.

Please review :)

naveenvalecha’s picture

Status: Needs review » Needs work
+++ b/google_vision.module
@@ -29,6 +31,23 @@ function google_vision_entity_presave(EntityInterface $entity) {
+              $vocabulary = \Drupal\taxonomy\Entity\Vocabulary::create(array(

use this class and only use "Vocabulary::create" method instead.

  1. +++ b/google_vision.links.task.yml
    index 3d722fa..551e03b 100644
    --- a/google_vision.module
    

    While you are on this file. Please either remove the @file doc or add the description about the file.

  2. +++ b/google_vision.module
    @@ -102,6 +121,53 @@ function google_vision_form_field_config_form_image_builder($entity_type, FieldC
    +  $query = \Drupal::database()->select('taxonomy_term_field_data', 'tfd');
    

    can we use the \Drupal::entityQuery here ?

  3. +++ b/google_vision.module
    @@ -102,6 +121,53 @@ function google_vision_form_field_config_form_image_builder($entity_type, FieldC
    +  $file->set($field->getName(), $values);
    

    declare $values as an empty [] before foreach. if none of the condition met then it will throw error.

  4. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,105 @@
    +  /**
    

    Add a new line before doc comment.

  5. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,105 @@
    +   * Get the file title.
    

    Add the @param & @return types here.

  6. +++ b/src/Controller/SimilarContentController.php
    @@ -0,0 +1,105 @@
    +   * Returns the list of image links which share the same dominant color.
    

    Same as above. Add the @param and @return type here.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
20.91 KB
2.67 KB

Here is the patch following the suggestions in #11

Please review :)

  • eugene.ilyin committed d791064 on 8.x-1.x
    Issue #2754391 by ajalan065: Implement Image Properties feature of the...
  • eugene.ilyin committed f249239 on 8.x-1.x authored by ajalan065
    Issue #2754391 by ajalan065: Implement Image Properties feature of the...
eugene.ilyin’s picture

Status: Needs review » Needs work

Applied with some corrections.
Be careful, better to try to load vocabulary directly $vocabulary = Vocabulary::load($vid); instead of loading all the vocabularies.

      foreach ($files as $key => $value) {
        $build[$key] = [
          '#prefix' => '<li>',
          '#type' => 'link',
          '#title' => $this->getFileTitle($key),
          '#url' => Url::fromRoute('entity.file.canonical', ['file' => $key]),
          '#suffix' => '</li>',
        ];
      }

Multiple queries to DB in the cycle are awful. Never do it. Please correct to one query.

      $build['#prefix'] = '<ul>';
      $build['#suffix'] = '</ul>';

      foreach ($files as $key => $value) {
        $build[$key] = [
          '#prefix' => '<li>',
          '#type' => 'link',
          '#title' => $this->getFileTitle($key),
          '#url' => Url::fromRoute('entity.file.canonical', ['file' => $key]),
          '#suffix' => '</li>',
        ];
      }

I believe that you can render the list not through prefix and suffix but through something like "item_list" please investigate this point.

return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();

db_query is deprecated. Don't use it.

I think that for route google_vision.related_content you should use permission 'administer files', because actually it's the list of the files and it's not related to administration of the google vision. I did it.

ajalan065’s picture

Multiple queries to DB in the cycle are awful. Never do it. Please correct to one query.

Sorry, I could not get which part are you referring to.

Please highlight the part which you refer, so that I may work on it.

ajalan065’s picture

Assigned: ajalan065 » eugene.ilyin
Status: Needs work » Needs review
FileSize
7.36 KB

Here is the patch implementing the suggestions in #14.
1. db_query() is replaced.
2. prefix and suffix replaced by item_list
3. Multiple queries in the cycle of $build have been corrected to a single query.

Please review :)

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

looks better
Thanks

naveenvalecha’s picture

Assigned: eugene.ilyin » Unassigned
Status: Reviewed & tested by the community » Fixed

Patch committed and pushed to 8.x-1.x
Thanks!

eugene.ilyin’s picture

Status: Fixed » Needs work

I think that the name of this feature is confusing. Dandelion and sand can have the same color. But hardly ever somebody will think that these images are similar.
Please improve instruction, info on the module page and text in interface to let the end user understand this feature better.

naveenvalecha’s picture

Status: Needs work » Fixed

Filed a followup for the same https://www.drupal.org/node/2783939

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.