Skip to content

Commit

Permalink
'Unsafe content detection' functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew72ru committed Nov 5, 2023
1 parent f7ec091 commit 3d39e99
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to

## [4.1.2]
- Possibility to store conversion details in a separate group.
- Possibility to run "Unsafe content detection" functionality.

## [4.1.1]
- Retrieve the Metadata without additional requests when using `getMetadata`.
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ while (($status = $api->addons()->checkAwsRecognition($token)) !== 'done') {
$recognitionData = $api->file()->fileInfo($file->getUuid())->getAppdata()->getAwsRekognitionDetectLabels(); // Instance of \Uploadcare\Interfaces\File\AppData\AwsRecognitionLabelsInterface
```

### [Unsafe content detection](https://uploadcare.com/docs/unsafe-content/)

To call unsafe content detection from the library:

```php
$configuration = \Uploadcare\Configuration::create($_ENV['UPLOADCARE_PUBLIC_KEY'], $_ENV['UPLOADCARE_SECRET_KEY']);

$api = new \Uploadcare\Api($configuration);
/** @var \Uploadcare\Interfaces\File\FileInfoInterface $file */
$file = $api->file()->listFiles()->getResults()->first();

# Request recognition, get token to check status
$token = $api->addons()->requestAwsRecognitionModeration($file);
while (($status = $api->addons()->checkAwsRecognitionModeration($token)) !== 'done') {
\usleep(1000);
if ($status === 'error') {
throw new \Exception('Error in process');
}
}

$recognitionModerationData = $api->file()->fileInfo($file->getUuid())->getAppdata()->getAwsRekognitionDetectModerationLabels() // Instance of \Uploadcare\Interfaces\File\AppData\AwsModerationLabelInterface
```

### [Remove background](https://uploadcare.com/docs/remove-bg/)

Remove background from image:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$api = (new Uploadcare\Api($configuration))->addons();
$status = $api->checkAwsRecognitionModeration('request-id');

echo \sprintf('Recognition status: %s', $status);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$api = (new Uploadcare\Api($configuration))->addons();
$resultKey = $api->requestAwsRecognitionModeration('1bac376c-aa7e-4356-861b-dd2657b5bfd2');

echo \sprintf('Recognition requested. Key is \'%s\'', $resultKey);
17 changes: 17 additions & 0 deletions src/Apis/AddonsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

class AddonsApi extends AbstractApi implements AddonsApiInterface
{
public function requestAwsRecognitionModeration($id): string
{
$response = $this->request('POST', '/addons/aws_rekognition_detect_moderation_labels/execute/', [
'body' => ['target' => (string) $id],
])->getBody()->getContents();

return $this->getResponseParameter($response, 'request_id');
}

public function checkAwsRecognitionModeration(string $id): string
{
$uri = \sprintf('/addons/aws_rekognition_detect_moderation_labels/execute/status/?request_id=%s', $id);
$response = $this->request('GET', $uri)->getBody()->getContents();

return $this->getResponseParameter($response, 'status');
}

public function requestAwsRecognition($id): string
{
$response = $this->request('POST', '/addons/aws_rekognition_detect_labels/execute/', [
Expand Down
15 changes: 15 additions & 0 deletions src/File/AppData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Uploadcare\File;

use Uploadcare\File\AppData\AwsRecognitionLabels;
use Uploadcare\File\AppData\AwsRecognitionModerationLabels;
use Uploadcare\File\AppData\ClamAvVirusScan;
use Uploadcare\File\AppData\RemoveBg;
use Uploadcare\Interfaces\File\AppDataInterface;
Expand All @@ -11,18 +12,32 @@
final class AppData implements AppDataInterface, SerializableInterface
{
private ?AwsRecognitionLabels $awsRecognitionLabels = null;
private ?AwsRecognitionModerationLabels $awsRekognitionDetectModerationLabels = null;
private ?ClamAvVirusScan $clamAvVirusScan = null;
private ?RemoveBg $removeBg = null;

public static function rules(): array
{
return [
'awsRekognitionDetectLabels' => AwsRecognitionLabels::class,
'awsRekognitionDetectModerationLabels' => AwsRecognitionModerationLabels::class,
'ucClamavVirusScan' => ClamAvVirusScan::class,
'removeBg' => RemoveBg::class,
];
}

public function getAwsRekognitionDetectModerationLabels(): ?AwsRecognitionModerationLabels
{
return $this->awsRekognitionDetectModerationLabels;
}

public function setAwsRekognitionDetectModerationLabels(?AwsRecognitionModerationLabels $labels): self
{
$this->awsRekognitionDetectModerationLabels = $labels;

return $this;
}

public function getAwsRekognitionDetectLabels(): ?AwsRecognitionLabels
{
return $this->awsRecognitionLabels;
Expand Down
46 changes: 46 additions & 0 deletions src/File/AppData/AbstractAwsLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

abstract class AbstractAwsLabels
{
protected ?string $version = null;
protected ?\DateTimeInterface $datetimeCreated = null;
protected ?\DateTimeInterface $datetimeUpdated = null;

public function getVersion(): ?string
{
return $this->version;
}

public function setVersion(?string $version): self
{
$this->version = $version;

return $this;
}

public function getDatetimeCreated(): ?\DateTimeInterface
{
return $this->datetimeCreated;
}

public function setDatetimeCreated(?\DateTimeInterface $datetimeCreated): self
{
$this->datetimeCreated = $datetimeCreated;

return $this;
}

public function getDatetimeUpdated(): ?\DateTimeInterface
{
return $this->datetimeUpdated;
}

public function setDatetimeUpdated(?\DateTimeInterface $datetimeUpdated): self
{
$this->datetimeUpdated = $datetimeUpdated;

return $this;
}
}
58 changes: 58 additions & 0 deletions src/File/AppData/AwsModerationLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

use Uploadcare\Interfaces\File\AppData\AwsRecognitionData\AwsModerationLabelInterface;
use Uploadcare\Interfaces\SerializableInterface;

class AwsModerationLabel implements AwsModerationLabelInterface, SerializableInterface
{
private ?float $confidence = null;
private ?string $name = null;
private ?string $parentName = null;

public static function rules(): array
{
return [
'confidence' => 'float',
'name' => 'string',
'parentName' => 'string',
];
}

public function getConfidence(): ?float
{
return $this->confidence;
}

public function setConfidence(?float $confidence): self
{
$this->confidence = $confidence;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getParentName(): ?string
{
return $this->parentName;
}

public function setParentName(?string $parentName): self
{
$this->parentName = $parentName;

return $this;
}
}
41 changes: 1 addition & 40 deletions src/File/AppData/AwsRecognitionLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
use Uploadcare\Interfaces\File\AppData\AwsRecognitionLabelsInterface;
use Uploadcare\Interfaces\SerializableInterface;

class AwsRecognitionLabels implements AwsRecognitionLabelsInterface, SerializableInterface
class AwsRecognitionLabels extends AbstractAwsLabels implements AwsRecognitionLabelsInterface, SerializableInterface
{
private ?string $version = null;
private ?\DateTimeInterface $datetimeCreated = null;
private ?\DateTimeInterface $datetimeUpdated = null;
private ?AwsRecognitionData $awsRecognitionData = null;

public static function rules(): array
Expand All @@ -23,42 +20,6 @@ public static function rules(): array
];
}

public function getVersion(): ?string
{
return $this->version;
}

public function setVersion(?string $version): self
{
$this->version = $version;

return $this;
}

public function getDatetimeCreated(): ?\DateTimeInterface
{
return $this->datetimeCreated;
}

public function setDatetimeCreated(?\DateTimeInterface $datetimeCreated): self
{
$this->datetimeCreated = $datetimeCreated;

return $this;
}

public function getDatetimeUpdated(): ?\DateTimeInterface
{
return $this->datetimeUpdated;
}

public function setDatetimeUpdated(?\DateTimeInterface $datetimeUpdated): self
{
$this->datetimeUpdated = $datetimeUpdated;

return $this;
}

public function getData(): ?AwsRecognitionDataInterface
{
return $this->awsRecognitionData;
Expand Down
53 changes: 53 additions & 0 deletions src/File/AppData/AwsRecognitionModerationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

use Uploadcare\Interfaces\File\AppData\AwsRecognitionData\AwsRecognitionDataInterface;
use Uploadcare\Interfaces\SerializableInterface;

class AwsRecognitionModerationData implements AwsRecognitionDataInterface, SerializableInterface
{
private ?string $labelModelVersion = null;

/**
* @var AwsModerationLabel[]
*/
private array $labels = [];

public static function rules(): array
{
return [
'labelModelVersion' => 'string',
'labels' => [AwsModerationLabel::class],
];
}

public function getLabelModelVersion(): ?string
{
return $this->labelModelVersion;
}

public function setLabelModelVersion(?string $labelModelVersion): self
{
$this->labelModelVersion = $labelModelVersion;

return $this;
}

/**
* @return AwsModerationLabel[]
*/
public function getLabels(): iterable
{
return $this->labels;
}

public function addLabel(AwsModerationLabel $label): self
{
if (!\in_array($label, $this->labels, true)) {
$this->labels[] = $label;
}

return $this;
}
}
34 changes: 34 additions & 0 deletions src/File/AppData/AwsRecognitionModerationLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

use Uploadcare\Interfaces\File\AppData\AwsRecognitionData\AwsRecognitionDataInterface;
use Uploadcare\Interfaces\File\AppData\AwsRecognitionLabelsInterface;
use Uploadcare\Interfaces\SerializableInterface;

class AwsRecognitionModerationLabels extends AbstractAwsLabels implements AwsRecognitionLabelsInterface, SerializableInterface
{
private ?AwsRecognitionModerationData $awsRecognitionData = null;

public static function rules(): array
{
return [
'version' => 'string',
'datetimeCreated' => \DateTime::class,
'datetimeUpdated' => \DateTime::class,
'data' => AwsRecognitionModerationData::class,
];
}

public function getData(): ?AwsRecognitionDataInterface
{
return $this->awsRecognitionData;
}

public function setData(?AwsRecognitionModerationData $data): self
{
$this->awsRecognitionData = $data;

return $this;
}
}
Loading

0 comments on commit 3d39e99

Please sign in to comment.