Skip to content

Commit

Permalink
feat: detectLabels functionality is added
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-mizrak committed Jan 17, 2025
1 parent 4e383f4 commit a4187d3
Show file tree
Hide file tree
Showing 29 changed files with 1,552 additions and 30 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
}
],
"require": {
"php": "^8.2"
"php": "^8.2",
"aws/aws-sdk-php": "^3.336",
"spatie/laravel-data": "^4.11"
},
"require-dev": {
"orchestra/testbench": "^9.6.1",
Expand Down Expand Up @@ -52,4 +54,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
33 changes: 33 additions & 0 deletions config/aws-rekognition.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| AWS credentials
|--------------------------------------------------------------------------
|
| Here you may specify the credentials for AWS used to sign requests.
| See https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#method___construct for more information.
|
*/
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
],

/*
|--------------------------------------------------------------------------
| AWS region
|--------------------------------------------------------------------------
|
| Here you may specify the region to connect to.
| See https://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
*/
'region' => env('AWS_REGION', 'us-east-1'),

/*
|--------------------------------------------------------------------------
| AWS version of the webservice to utilize (e.g., 2006-03-01)
|--------------------------------------------------------------------------
|
| Here you may specify the version of the webservice.
| See https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#method___construct for more information.
*/
'version' => env('AWS_VERSION', 'latest'),
];
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@

<php>
<env name="APP_ENV" value="testing"/>
<env name="AWS_ACCESS_KEY_ID" value=""/>
<env name="AWS_SECRET_ACCESS_KEY" value=""/>
<env name="AWS_REGION" value="us-east-1"/>
<env name="AWS_VERSION" value="latest"/>
</php>
</phpunit>
78 changes: 78 additions & 0 deletions src/Data/DetectLabelsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data;

use Spatie\LaravelData\Data;

/**
* DetectLabelsData is for detectLabels method related context data such as image, features, maxLabels, minConfidence and settings.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#detectlabels
*
* @class DetectLabelsData
*/
final class DetectLabelsData extends Data
{
public function __construct(
/*
* The input image as base64-encoded bytes, image resource or an S3 object (Images stored in an S3 Bucket do not need to be base64-encoded).
*
* @param ImageData
*/
public ImageData $image,

/*
* A list of the types of analysis to perform.
* Specifying GENERAL_LABELS uses the label detection feature, while specifying IMAGE_PROPERTIES returns information regarding image color and quality.
* default: GENERAL_LABELS
*
* @param array|null
*/
public ?array $features = null,

/*
* Maximum number of labels you want the service to return in the response. The service returns the specified number of highest confidence labels.
* Only valid when GENERAL_LABELS is specified as a feature type in the Feature input parameter.
*
* @param int|null
*/
public ?int $maxLabels = null,

/*
* Specifies the minimum confidence level for the labels to return.
* Amazon Rekognition doesn't return any labels with confidence lower than this specified value.
* If MinConfidence is not specified, the operation returns labels with a confidence values greater than or equal to 55 percent.
* Only valid when GENERAL_LABELS is specified as a feature type in the Feature input parameter.
*
* @param float|null
*/
public ?float $minConfidence = null,

/*
* A list of the filters to be applied to returned detected labels and image properties.
*
* @param SettingsData|null
*/
public ?SettingsData $settings = null
) {}

/**
* Convert the data to an AWS Rekognition array format, excluding null values.
*
* @return array
*/
public function toRekognitionDataFormat(): array
{
return array_filter(
[
'Image' => $this->image->toRekognitionDataFormat(),
'Features' => $this->features,
'MaxLabels' => $this->maxLabels,
'MinConfidence' => $this->minConfidence,
'Settings' => $this->settings?->toRekognitionDataFormat(),
],
fn ($value) => $value !== null
);
}
}
41 changes: 41 additions & 0 deletions src/Data/DetectLabelsImagePropertiesSettingsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data;

use Spatie\LaravelData\Data;

/**
* DetectLabelsImagePropertiesSettingsData is settings for the IMAGE_PROPERTIES feature type.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#shape-detectlabelsimagepropertiessettings
*
* @class DetectLabelsImagePropertiesSettingsData
*/
final class DetectLabelsImagePropertiesSettingsData extends Data
{
public function __construct(
/*
* The maximum number of dominant colors to return when detecting labels in an image.
* The default value is 10.
*
* @param int|null
*/
public ?int $maxDominantColors = null,
) {}

/**
* Convert the data to an AWS Rekognition array format.
*
* @return array
*/
public function toRekognitionDataFormat(): array
{
return array_filter(
[
'MaxDominantColors' => $this->maxDominantColors,
],
fn ($value) => $value !== null
);
}
}
65 changes: 65 additions & 0 deletions src/Data/GeneralLabelsSettingsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data;

use Spatie\LaravelData\Data;

/**
* Contains filters for the object labels returned by DetectLabels. Filters can be inclusive, exclusive, or a combination of both and can be applied to individual labels or entire label categories.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#shape-generallabelssettings
* To see a list of label categories: https://docs.aws.amazon.com/rekognition/latest/dg/labels.html
*
* @class GeneralLabelsSettingsData
*/
final class GeneralLabelsSettingsData extends Data
{
public function __construct(
/*
* The label categories that should be excluded from the return from DetectLabels.
*
* @param array|null
*/
public ?array $labelCategoryExclusionFilters = null,

/*
* The label categories that should be included in the return from DetectLabels.
*
* @param array|null
*/
public ?array $labelCategoryInclusionFilters = null,

/*
* The labels that should be excluded from the return from DetectLabels.
*
* @param array|null
*/
public ?array $labelExclusionFilters = null,

/*
* The labels that should be included in the return from DetectLabels.
*
* @param array|null
*/
public ?array $labelInclusionFilters = null,
) {}

/**
* Convert the data to an AWS Rekognition array format.
*
* @return array
*/
public function toRekognitionDataFormat(): array
{
return array_filter(
[
'LabelCategoryExclusionFilters' => $this->labelCategoryExclusionFilters,
'LabelCategoryInclusionFilters' => $this->labelCategoryInclusionFilters,
'LabelExclusionFilters' => $this->labelExclusionFilters,
'LabelInclusionFilters' => $this->labelInclusionFilters,
],
fn ($value) => $value !== null
);
}
}
49 changes: 49 additions & 0 deletions src/Data/ImageData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data;

use Spatie\LaravelData\Data;

/**
* Provides the input image either as bytes or an S3 object.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#shape-image
* Also check: https://docs.aws.amazon.com/rekognition/latest/dg/images-information.html
*
* @class ImageData
*/
final class ImageData extends Data
{
public function __construct(
/*
* Blob of image bytes up to 5 MBs.
*
* @param string|resource|StreamInterface|null
*/
public mixed $bytes = null,

/*
* Identifies an S3 object as the image source.
*
* @param S3ObjectData|null
*/
public ?S3ObjectData $s3Object = null
) {}

/**
* Convert the data to an AWS Rekognition array format.
*
* @return array
*/
public function toRekognitionDataFormat(): array
{
return array_filter(
[
'Bytes' => $this->bytes,
'S3Object' => $this->s3Object?->toRekognitionDataFormat(),
],
fn ($value) => $value !== null
);
}
}
25 changes: 25 additions & 0 deletions src/Data/ResultData/AliasData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data\ResultData;

use Spatie\LaravelData\Data;

/**
* A potential alias of for a given label.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#shape-labelalias
*
* @class LabelData
*/
final class AliasData extends Data
{
public function __construct(
/*
* The name of an alias for a given label.
*
* @param string|null
*/
public ?string $name = null,
) {}
}
35 changes: 35 additions & 0 deletions src/Data/ResultData/BackgroundData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace MoeMizrak\Rekognition\Data\ResultData;

use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;

/**
* Information about the properties of an image’s background, including the background’s quality and dominant colors, including the quality and dominant colors of the image.
* For more info: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html#shape-detectlabelsimagebackground
*
* @class BackgroundData
*/
final class BackgroundData extends Data
{
public function __construct(
/*
* A description of the dominant colors in an image.
*
* @param DataCollection|null
*/
#[DataCollectionOf(DominantColorsData::class)]
public ?DataCollection $dominantColors = null,

/*
* The quality of the image background as defined by brightness and sharpness.
*
* @param QualityData|null
*/
public ?QualityData $quality = null,
) {}
}
Loading

0 comments on commit a4187d3

Please sign in to comment.