From e6892b5ecdc79e2a9c2a6885097501cf9cb2ff4d Mon Sep 17 00:00:00 2001 From: Mike Joseph Date: Mon, 25 Nov 2019 15:00:59 -0500 Subject: [PATCH] support for detection modes support for reading directly from S3 buckets --- src/Image.php | 23 ++++++----- src/Service/DetectService.php | 72 +++++++++++++++++++++++++++++++++-- src/Service/LabelService.php | 25 +++++++++++- src/Service/TextService.php | 27 ++++++++++++- 4 files changed, 131 insertions(+), 16 deletions(-) diff --git a/src/Image.php b/src/Image.php index 6f296b9..ab9a0ae 100644 --- a/src/Image.php +++ b/src/Image.php @@ -12,24 +12,19 @@ class Image /** @var string */ private $binaryContent; + /** @var string */ + private $urlContent; + /** @var Label[] */ private $labels = []; /** @var Text[] */ private $texts = []; - /** - * @param string $binaryContent - */ - public function __construct(string $binaryContent) - { - $this->setBinaryContent($binaryContent); - } - /** * @return string */ - public function getBinaryContent(): string + public function getBinaryContent(): ?string { return $this->binaryContent; } @@ -42,6 +37,16 @@ public function setBinaryContent(string $binaryContent): void $this->binaryContent = $binaryContent; } + public function getUrlContent(): ?string + { + return $this->urlContent; + } + + public function setUrlContent(string $urlContent): void + { + $this->urlContent = $urlContent; + } + /** * @param int $minimumConfidence * @return Label[] diff --git a/src/Service/DetectService.php b/src/Service/DetectService.php index fe0c543..40c92bd 100644 --- a/src/Service/DetectService.php +++ b/src/Service/DetectService.php @@ -18,6 +18,27 @@ class DetectService // From https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html const AWS_REKOGNITION_VERSION = '2016-06-27'; + /** + * Detect both text and labels + * @var string + */ + const DETECT_BOTH = 'detect_both'; + + /** + * Detect text only + * @var string + */ + const DETECT_TEXT = 'detect_text'; + + /** + * Detect labels only + * @var string + */ + const DETECT_LABEL = 'detect_label'; + + /** @var string */ + protected $detectMode; + /** @var RekognitionClient */ protected $rekognitionClient; @@ -34,23 +55,46 @@ public function __construct(array $options = []) { $options = $this->buildOptions($options); + $this->detectMode = self::DETECT_BOTH; + $this->rekognitionClient = new RekognitionClient($options); $this->setLabelService(new LabelService($this->rekognitionClient)); $this->setTextService(new TextService($this->rekognitionClient)); } + /** + * Set detection mode, default is to detect both + * labels and text. + * + * @param string $mode + */ + public function setDetectMode($mode): void + { + $this->detectMode = $mode; + } + /** * @param string $binaryContent * @return Image */ public function detect(string $binaryContent): Image { - $rekognitionImage = new Image($binaryContent); + $rekognitionImage = new Image; + $rekognitionImage->setBinaryContent($binaryContent); - $this->labelService->detectLabels($rekognitionImage); - $this->textService->detectText($rekognitionImage); + return $this->handleDetection($rekognitionImage); + } + + /** + * @param string $s3Url + * @return Image + */ + public function detectFromS3(string $s3Url): Image + { + $rekognitionImage = new Image; + $rekognitionImage->setUrlContent($s3Url); - return $rekognitionImage; + return $this->handleDetection($rekognitionImage); } /** @@ -60,6 +104,9 @@ public function detect(string $binaryContent): Image */ public function detectFromUrl(string $url): Image { + if (strpos($url, 's3.amazonaws.com') !== false) { + return $this->detectFromS3($url); + } $binaryContent = @file_get_contents($url); if ($binaryContent === false) { @@ -146,4 +193,21 @@ protected function getEnvironmentOptions(): array return $environmentOptions; } + + /** + * Run detection, respecting mode + * @param Image $image + * @return Image + */ + private function handleDetection(Image $image): Image + { + if ($this->detectMode == self::DETECT_BOTH || $this->detectMode == self::DETECT_LABEL) { + $this->labelService->detectLabels($image); + } + if ($this->detectMode == self::DETECT_BOTH || $this->detectMode == self::DETECT_TEXT) { + $this->textService->detectText($image); + } + + return $image; + } } diff --git a/src/Service/LabelService.php b/src/Service/LabelService.php index 652fc58..736504c 100644 --- a/src/Service/LabelService.php +++ b/src/Service/LabelService.php @@ -55,10 +55,33 @@ public function detectLabels(Image $rekognitionImage): void */ protected function getResult(Image $rekognitionImage) { + if ($rekognitionImage->getBinaryContent()) { + return $this->rekognitionClient->detectLabels( + [ + 'Image' => [ + 'Bytes' => $rekognitionImage->getBinaryContent(), + ], + 'Attributes' => ['ALL'] + ] + ); + } + + $url = $rekognitionImage->getUrlContent(); + $parts = explode('s3.amazonaws.com', $url); + if (!count($parts)) { + throw new \Exception('Unable to parse S3 url'); + } + + $bucket = trim(substr($parts[1], 0, strpos($parts[1], '/', 1)), '/'); + $name = substr($parts[1], (strpos($parts[1], '/', 1) + 1)); + return $this->rekognitionClient->detectLabels( [ 'Image' => [ - 'Bytes' => $rekognitionImage->getBinaryContent(), + 'S3Object' => [ + 'Bucket' => $bucket, + 'Name' => $name + ] ], 'Attributes' => ['ALL'] ] diff --git a/src/Service/TextService.php b/src/Service/TextService.php index c7c08b6..99daf7b 100644 --- a/src/Service/TextService.php +++ b/src/Service/TextService.php @@ -50,10 +50,33 @@ public function detectText(Image $rekognitionImage): void */ protected function getResult(Image $rekognitionImage) { - return $this->rekognitionClient->detectText( + if ($rekognitionImage->getBinaryContent()) { + return $this->rekognitionClient->detectText( + [ + 'Image' => [ + 'Bytes' => $rekognitionImage->getBinaryContent(), + ], + 'Attributes' => ['ALL'] + ] + ); + } + + $url = $rekognitionImage->getUrlContent(); + $parts = explode('s3.amazonaws.com', $url); + if (!count($parts)) { + throw new \Exception('Unable to parse S3 url'); + } + + $bucket = trim(substr($parts[1], 0, strpos($parts[1], '/', 1)), '/'); + $name = substr($parts[1], (strpos($parts[1], '/', 1) + 1)); + + return $this->rekognitionClient->detectLabels( [ 'Image' => [ - 'Bytes' => $rekognitionImage->getBinaryContent(), + 'S3Object' => [ + 'Bucket' => $bucket, + 'Name' => $name + ] ], 'Attributes' => ['ALL'] ]