Hi all - thought I'd share my little project here in case it helps someone. I think v4 of Chevereto might have this (?), but in the meantime here's one way to do it.
Overview
Amazon Rekognition is a deep-learning algorithm that can detect objects in scenes, faces, and nudity. I'm using it for the latter, but you could easily modify this code to support auto tagging and more. At the moment new users get 5,000 image credits a month for free for the first year, then it's $1-ish per 1,000 after that.
Step 1: Sign up and sign into your AWS account
You can follow this guide, which I sort of sum up in the next four steps: https://docs.aws.amazon.com/rekognition/latest/dg/setting-up.html
Step 2: Create an Identity and Access Management (IAM) user
Info for that is also at the previous link.
Step 3: After creating the IAM user, grant them the AmazonRekognitionFullAccess permission
More info: https://docs.aws.amazon.com/rekognition/latest/dg/authentication-and-access-control.html
Step 4: Make a note of the key and secret, under the "Security credentials" tab for that user
Navigating AWS is the toughest part, but once you have the key and secret, and the proper permissions set up, you can continue.
Step 5: Download and install the AWS SDK for PHP (these guys are in love with acronyms)
https://aws.amazon.com/sdk-for-php/
Step 6: Copy route.json.php to overrides
Copy the /app/routes/route.json.php file to the overrides folder, e.g. /app/routes/overrides/route.json.php
Step 7: Edit the OVERRIDE version as follows (read comments and edit as necessary):
At the top of the script, right below the copyright disclaimer, add:
(The above assumes you've downloaded the SDK zip from step 5 and placed it in a folder on your site. There are other ways to install it if you'd prefer, at the link in step 5.)
ABOVE the line of code
Hope that's useful to someone! I'd recommend if you have a massive site though to keep an eye on the billing, because it could add up fast.
Overview
Amazon Rekognition is a deep-learning algorithm that can detect objects in scenes, faces, and nudity. I'm using it for the latter, but you could easily modify this code to support auto tagging and more. At the moment new users get 5,000 image credits a month for free for the first year, then it's $1-ish per 1,000 after that.
Step 1: Sign up and sign into your AWS account
You can follow this guide, which I sort of sum up in the next four steps: https://docs.aws.amazon.com/rekognition/latest/dg/setting-up.html
Step 2: Create an Identity and Access Management (IAM) user
Info for that is also at the previous link.
Step 3: After creating the IAM user, grant them the AmazonRekognitionFullAccess permission
More info: https://docs.aws.amazon.com/rekognition/latest/dg/authentication-and-access-control.html
Step 4: Make a note of the key and secret, under the "Security credentials" tab for that user
Navigating AWS is the toughest part, but once you have the key and secret, and the proper permissions set up, you can continue.
Step 5: Download and install the AWS SDK for PHP (these guys are in love with acronyms)
https://aws.amazon.com/sdk-for-php/
Step 6: Copy route.json.php to overrides
Copy the /app/routes/route.json.php file to the overrides folder, e.g. /app/routes/overrides/route.json.php
Step 7: Edit the OVERRIDE version as follows (read comments and edit as necessary):
At the top of the script, right below the copyright disclaimer, add:
PHP:
require($_SERVER['DOCUMENT_ROOT'] . '/PATH_TO_REKOGNIZE/aws-autoloader.php');
use Aws\S3\S3Client;
(The above assumes you've downloaded the SDK zip from step 5 and placed it in a folder on your site. There are other ways to install it if you'd prefer, at the link in step 5.)
ABOVE the line of code
$uploaded_id = CHV\Image::uploadToWebsite($source, $logged_user, $_REQUEST);
add this:
PHP:
// If this user claims the image is SFW, let's double check
// You could remove this if you wanted to check all images, even ones marked as NSFW
// like if you wanted to fix user input errors or something
if($_REQUEST['nsfw'] == 0){
// Is the file coming from a local source?
if(isset($source['tmp_name'])){
$img = file_get_contents($source['tmp_name']);
}
// ... or a remote source?
else{
$img = file_get_contents($source);
}
// Let's make sure that worked
if($img !== false){
// Fire up Rekognition
$s3 = new \Aws\Rekognition\RekognitionClient([
'version' => 'latest',
// Modify the region depending on your AWS setup
'region' => 'us-west-2',
// You don't have to hard-code these here, but that's the way I roll
'credentials' => [
'key' => 'KEY_CODE_HERE',
'secret' => 'SECRET_CODE_HERE'
]
]);
// Here's where we check for nudity. You can also replace or add object detection (https://docs.aws.amazon.com/rekognition/latest/dg/labels-detect-labels-image.html)
// and face detection (https://docs.aws.amazon.com/rekognition/latest/dg/faces-detect-images.html), etc.
$result = $s3->DetectModerationLabels([
'Image' => [ // REQUIRED
'Bytes' => $img
]
]);
// If the image has some level of tawdriness :)
// I'm not worried about details/level of the confidence score, because I'm just looking for a yes or no if there's explicit nudity or not (see below)
if(isset($result['ModerationLabels'][0]['Confidence'])){
// Personally I'm just interested in marking nudity as NSFW, but it also detects things like bikinis, partial nudity, etc.
if($result['ModerationLabels'][0]['Name'] == 'Explicit Nudity'){
// Boobies or weenies found ... let's mark the sucker
// Here's where you could add tags, etc. to be entered into the DB as well if you used detect labels
$_REQUEST['nsfw'] = 1;
}
}
}
}
Hope that's useful to someone! I'd recommend if you have a massive site though to keep an eye on the billing, because it could add up fast.