From c6070b62fd4622336f068ac6b58e921cac218057 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Mon, 11 Jun 2018 09:30:25 +0100 Subject: [PATCH] Initial commit --- .gitattributes | 2 + README.md | 170 ++++++++++++++++++++++++++++++ composer.json | 23 ++++ src/Comprehend.php | 53 ++++++++++ src/ComprehendFacade.php | 12 +++ src/ComprehendServiceProvider.php | 38 +++++++ src/config/main.php | 10 ++ 7 files changed, 308 insertions(+) create mode 100644 .gitattributes create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Comprehend.php create mode 100644 src/ComprehendFacade.php create mode 100644 src/ComprehendServiceProvider.php create mode 100644 src/config/main.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/README.md b/README.md new file mode 100644 index 0000000..dffcb7d --- /dev/null +++ b/README.md @@ -0,0 +1,170 @@ +# fork of larareko/aws-rekognition + +A Laravel package/facade for the Rekognition API PHP SDK. + +This repository implements a simple Service Provider of the AWS Rekognition client, and makes it easily accessible via a Facade in Laravel >= 5. + +* Now updated for Rekognition video (startLabelDetection, getLabelDetection, startContentModeration, getContentModeration, startCelebrityRecognition, getCelebrityRecognition) + +See [AWS Rekognition](https://aws.amazon.com/rekognition/) for more information. + +## Requirements + +Create an account at [AWS](https://aws.amazon.com/console/) and take note of your API keys. + +## Installation using [Composer](https://getcomposer.org) + +In your terminal application move to the root directory of your laravel project using the cd command and require the project as a dependency using composer. + +composer require michaeljwright/aws-rekognition + +This will add the following lines to your composer.json and download the project and its dependencies to your projects ./vendor directory: + +```javascript +// + +./composer.json +{ + "name": "michaeljwright/larareko-demo", + "description": "A dummy project used to test the Laravel Larareko (AWS Rekognition) Facade.", + + // ... + + "require": { + "php": ">=5.5.9", + "laravel/framework": "5.2.*", + "michaeljwright/aws-rekognition": "0.1*", + // ... + }, + + //... +} +``` + +## Usage + +In order to use the static interface we must customize the application configuration to tell the system where it can find the new service. Open the file config/app.php and add the following lines ([a], [b]): + +```php + +// config/app.php + +return [ + + // ... + + 'providers' => [ + + // ... + + /* + * Package Service Providers... + */ + MichaelJWright\Rekognition\RekognitionServiceProvider::class, // [a] + + /* + * Application Service Providers... + */ + App\Providers\AppServiceProvider::class, + App\Providers\AuthServiceProvider::class, + App\Providers\EventServiceProvider::class, + App\Providers\RouteServiceProvider::class, + + ], + + // ... + + 'aliases' => [ + + 'App' => Illuminate\Support\Facades\App::class, + 'Artisan' => Illuminate\Support\Facades\Artisan::class, + + // ... + + 'Rekognition' => 'MichaelJWright\Rekognition\RekognitionFacade', // [b] + 'Hash' => Illuminate\Support\Facades\Hash::class, + + // ... + ], + +]; + + +``` + +## Publish Vendor + +aws-rekognition requires a connection configuration. To get started, you'll need to publish all vendor assets running: + +php artisan vendor:publish + +This will create a config/rekognition.php file in your app that you can modify to set your configuration. Make sure you check for changes compared to the original config file after an upgrade. + +Now you should be able to use the facade within your application. Ex: + +```php + +class LabelDetectionImage extends Model +{ + /** + * Upload image to S3 + * + * @param Illuminate\Http\UploadedFile $file + * + * @return string + */ + public function upload(UploadedFile $file) : string + { + $name = time() . $file->getClientOriginalName(); + + \Rekognition::uploadImageToS3(file_get_contents($file), null, self::BUCKET, $name); + + return $name; + } +} + +``` + +## Example for video label recognition + +```php + +// FIRST call startLabelDetection to create a rekognition job + +$config = [ + 'MinConfidence' => 80, //set confidence level for probability of correct labels + 'Video' => [ + 'S3Object' => [ + 'Bucket' => 'YOUR_BUCKET_NAME', + 'Name' => 'YOUR_VIDEO_FILE_NAME', + ], + ], + ]; +$job = \Rekognition::startLabelDetection($config); //start a job in rekognition for specific video file +dd($job['JobId']); //output job id so you can use it to get the labels + +// THEN call getLabelDetection to get the labels for the specific job + +$config = [ + 'JobId' => 'YOUR_JOB_ID', + 'SortBy' => 'NAME', //set to whatever you want to sort the labels by + ]; +$job = \Rekognition::getLabelDetection($config); +dd($job['Labels']); //output the labels + +``` + +## Testing + +Unit Tests are created with PHPunit and orchestra/testbench, they can be ran with ./vendor/bin/phpunit. + +## Contributing + +Find an area you can help with and do it. Open source is about collaboration and open participation. +Try to make your code look like what already exists or better and submit a pull request. Also, if +you have any ideas on how to make the code better or on improving the scope and functionality please +contact any of the contributors. + +## License + +MIT License. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0007293 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "michaeljwright/aws-comprehend", + "description": "A Laravel package for the AWS Comprehend", + "require-dev": { + "phpunit/phpunit": "~5.7", + "orchestra/testbench": "~3.0" + }, + "license": "MIT", + "authors": [ + { + "name": "Mike Wright", + "email": "mike.filmworks@gmail.com" + } + ], + "require": { + "aws/aws-sdk-php":"~3.0" + }, + "autoload": { + "psr-4": { + "MichaelJWright\\Comprehend\\": "src/" + } + } +} diff --git a/src/Comprehend.php b/src/Comprehend.php new file mode 100644 index 0000000..57a21ba --- /dev/null +++ b/src/Comprehend.php @@ -0,0 +1,53 @@ +args = [ + 'credentials' => config('comprehend.credentials'), + 'region' => config('comprehend.region'), + 'version' => config('comprehend.version') + ]; + + $this->client = new ComprehendClient($this->args); + } + + /** + * @return ComprehendClient + */ + public function getClient() + { + return $this->client; + } + + /** + * Inspects text and returns an inference of the prevailing sentiment (POSITIVE, NEUTRAL, MIXED, or NEGATIVE). + * + * @param array $params + * @return array + */ + public function detectSentiment(array $params = []) + { + return $this->client->detectSentiment($params); + } + +} diff --git a/src/ComprehendFacade.php b/src/ComprehendFacade.php new file mode 100644 index 0000000..daa07b6 --- /dev/null +++ b/src/ComprehendFacade.php @@ -0,0 +1,12 @@ +publishes([ + __DIR__ . '/config/main.php' => config_path('comprehend.php'), + ]); + + $file = __DIR__ . '/../vendor/autoload.php'; + + if (file_exists($file)) { + require $file; + } + } + + /** + * Register the application services. + * + * @return void + */ + public function register() + { + $this->app->bind('aws-comprehend', function() { + return new Comprehend; + }); + } +} diff --git a/src/config/main.php b/src/config/main.php new file mode 100644 index 0000000..f551b33 --- /dev/null +++ b/src/config/main.php @@ -0,0 +1,10 @@ + [ + 'key' => env('YOUR_AWS_ACCESS_KEY_ID'), + 'secret' => env('YOUR_AWS_SECRET_ACCESS_KEY'), + ], + 'region' => env('YOUR_AWS_REGION', 'us-east-1'), + 'version' => 'latest' +];