diff --git a/google_vision.module b/google_vision.module index 8d1b7cf..51c50c5 100755 --- a/google_vision.module +++ b/google_vision.module @@ -6,7 +6,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\FieldConfigInterface; -use Drupal\google_vision\Connector; use Drupal\google_vision\GoogleVisionAPI; /** @@ -68,7 +67,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 = \Drupal::service('google_vision.connector')->labelDetection($filepath); + $data = \Drupal::service('google_vision.api')->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 index 3e2457c..91ddb31 100755 --- a/google_vision.services.yml +++ b/google_vision.services.yml @@ -2,6 +2,3 @@ services: google_vision.api: class: Drupal\google_vision\GoogleVisionAPI arguments: ['@config.factory', '@http_client'] - google_vision.connector: - class: Drupal\google_vision\Connector - arguments: ['@google_vision.api'] diff --git a/src/Connector.php b/src/Connector.php deleted file mode 100755 index b8d59fa..0000000 --- a/src/Connector.php +++ /dev/null @@ -1,120 +0,0 @@ -googleVisionAPI = $google_vision; - } - - /** - * Function to retrieve labels for given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function labelDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLabels($filepath); - return $req; - } - - /** - * Function to detect landmarks within a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function landmarkDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLandmark($filepath); - return $req; - } - - /** - * Function to detect logos of famous brands within a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function logoDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLogo($filepath); - return $req; - } - - /** - * Function to detect explicit content within a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function safeSearchDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForSafeSearch($filepath); - return $req; - } - - /** - * Function to retrieve texts within a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function opticalCharacterRecognition($filepath) { - $req = $this->googleVisionAPI->makeRequestForOCR($filepath); - return $req; - } - - /** - * Function to detect faces from a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function faceDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForFaceDetection($filepath); - return $req; - } - - /** - * Function to retrieve image attributes from a given image. - * - * @param string $filepath. - * - * @return array $req. - */ - public function imageAttributes($filepath) { - $req = $this->googleVisionAPI->makeRequestForImageAttributes($filepath); - return $req; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static($container->get('google_vision.api') - ); - } - -} diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php index abd4a85..9dc905e 100755 --- a/src/GoogleVisionAPI.php +++ b/src/GoogleVisionAPI.php @@ -59,7 +59,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForLabels($filepath) { + public function labelDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -91,7 +91,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -102,7 +106,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForLandmark($filepath) { + public function landmarkDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -134,7 +138,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -145,7 +153,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForLogo($filepath) { + public function logoDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -177,7 +185,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -188,7 +200,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForSafeSearch($filepath) { + public function safeSearchDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -220,7 +232,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -231,7 +247,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForOCR($filepath) { + public function opticalCharacterRecognition($filepath) { if (!$this->apiKey) { return FALSE; } @@ -263,7 +279,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -274,7 +294,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForFaceDetection($filepath) { + public function faceDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -306,7 +326,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } @@ -317,7 +341,7 @@ class GoogleVisionAPI { * * @return Array|bool. */ - public function makeRequestForImageAttributes($filepath) { + public function imageAttributesDetection($filepath) { if (!$this->apiKey) { return FALSE; } @@ -349,7 +373,11 @@ class GoogleVisionAPI { RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); - return Json::decode($response->getBody(), TRUE); + $result = Json::decode($response->getBody()); + if (empty($result['error'])) { + return $result; + } + return FALSE; } }