diff --git a/google_vision.module b/google_vision.module index 6d26c90..8d1b7cf 100755 --- a/google_vision.module +++ b/google_vision.module @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\FieldConfigInterface; use Drupal\google_vision\Connector; +use Drupal\google_vision\GoogleVisionAPI; /** * Implements hook_entity_presave(). @@ -67,7 +68,7 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { // Try to retrieve file URI. $file_uri = $file->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { - $data = Connector::makeRequestForLabels($filepath); + $data = \Drupal::service('google_vision.connector')->labelDetection($filepath); // If we have retrieved labels. if (!empty($data['responses'][0]['labelAnnotations'])) { $labels = []; diff --git a/google_vision.services.yml b/google_vision.services.yml new file mode 100755 index 0000000..f165465 --- /dev/null +++ b/google_vision.services.yml @@ -0,0 +1,7 @@ +services: + google_vision.api: + class: Drupal\google_vision\GoogleVisionAPI + arguments: ['@config.factory'] + google_vision.connector: + class: Drupal\google_vision\Connector + arguments: ['@google_vision.api'] diff --git a/src/Connector.php b/src/Connector.php index 1d03323..2e19dfc 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -8,54 +8,91 @@ namespace Drupal\google_vision; use Drupal\Component\Serialization\Json; +use Drupal\google_vision\GoogleVisionAPI; +use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Response; -class Connector { +class Connector extends ControllerBase { + + protected $imageRequest; /** + * Constructor + */ + public function __construct(GoogleVisionAPI $image_Request) { + + $this->imageRequest = $image_Request; + } + + /* * Function to retrieve labels for given image. */ - public static function makeRequestForLabels($filepath) { - $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { - return FALSE; - } - - // 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)); - - // Prepare JSON. - $data = '{ - "requests":[ - { - "image":{ - "content":"' . $encoded_image . '" - }, - "features":[ - { - "type":"LABEL_DETECTION", - "maxResults":5 - } - ] - } - ] - }'; - - $ch = curl_init('https://vision.googleapis.com/v1/images:annotate?key=' . $api_key); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Content-Type: application/json', - 'Content-Length: ' . strlen($data), - ) - ); + public function labelDetection($filepath) { + + $req = $this->imageRequest->makeRequestForLabels($filepath); + return $req; + } + + /* + * Function to detect landmarks within a given image. + */ + public function landmarkDetection($filepath) { + + $req = $this->imageRequest->makeRequestForLandmark($filepath); + return $req; + } + + /* + * Function to detect logos of famous brands within a given image. + */ + public function logoDetection($filepath) { + + $req = $this->imageRequest->makeRequestForLogo($filepath); + return $req; + } + + /* + * Function to detect explicit content within a given image. + */ + public function safeSearchDetection($filepath) { - $result = Json::decode(curl_exec($ch)); - if (empty($result['error'])) { - return $result; - } - return FALSE; + $req = $this->imageRequest->makeRequestForSafeSearch($filepath); + return $req; } + + /* + * Function to retrieve texts within a given image. + */ + public function opticalCharacterRecognition($filepath) { + + $req = $this->imageRequest->makeRequestForOCR($filepath); + return $req; + } + + /* + * Function to detect faces from a given image. + */ + public function faceDetection($filepath) { + + $req = $this->imageRequest->makeRequestForFaceDetection($filepath); + return $req; + } + + /* + * Function to retrieve image attributes from a given image. + */ + public function imageAttributes($filepath) { + + $req = $this->imageRequest->makeRequestForImageAttributes($filepath); + return $req; + } + + public static function create(ContainerInterface $container) { + + return new static($container->get('google_vision.api') + ); + } + } diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php new file mode 100755 index 0000000..67b19ef --- /dev/null +++ b/src/GoogleVisionAPI.php @@ -0,0 +1,327 @@ +configFactory = $config_factory; + } + + /** + * Function to retrieve labels for given image. + */ + public function makeRequestForLabels($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'LABEL_DETECTION', + 'maxResults' => 5 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to detect landmarks within a given image. + */ + public function makeRequestForLandmark($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'LANDMARK_DETECTION', + 'maxResults' => 2 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to detect logos of famous brands within a given image. + */ + public function makeRequestForLogo($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'LOGO_DETECTION', + 'maxResults' => 2 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to detect explicit content within a given image. + */ + public function makeRequestForSafeSearch($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'SAFE_SEARCH_DETECTION', + 'maxResults' => 1 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to retrieve texts for given image. + */ + public function makeRequestForOCR($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'TEXT_DETECTION', + 'maxResults' => 10 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to fetch faces from a given image. + */ + public function makeRequestForFaceDetection($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'FACE_DETECTION', + 'maxResults' => 25 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } + + /** + * Function to retrieve image attributes for given image. + */ + public function makeRequestForImageAttributes($filepath) { + $config = $this->configFactory->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + // 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)); + + // Prepare JSON. + $data = [ + 'requests' => [ + [ + 'image' => [ + 'content' => $encoded_image + ], + 'features' => [ + [ + 'type' => 'IMAGE_PROPERTIES', + 'maxResults' => 5 + ], + ], + ], + ], + ]; + + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; + $client = \Drupal::httpClient(); + $response = $client->post($url, [ + RequestOptions::JSON => $data, + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + ]); + + return json_decode($response->getBody(), TRUE); + + } +}