Amazon Rekognition
Some time we have requirement that user who can upload image should not have text (telephone number/ email id) and also user should not upload celebrities image.
So we can use AWS Rekognition Api for resolve this problem.
Rekognition having following features:
So we can use AWS Rekognition Api for resolve this problem.
Rekognition having following features:
- Rekognition automatically labels objects, concepts and scenes in your images, and provides a confidence score.
- Rekognition automatically detects explicit or suggestive adult content, or violent content in your images, and provides confidence scores.
- Get a complete analysis of facial attributes, including confidence scores.
- Rekognition automatically recognizes celebrities in images and provides confidence scores.
- Compare faces to see how closely they match based on a similarity percentage.
- Rekognition automatically detects and extracts text in your images.
I will consecrate on Text in image and Celebrities in image.
I am Assuming you have AWS account and know how to create IAM user.
In my blog i have created user group and assign roles to group.
For Recognition you required following role to user.
Maven dependecy:
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-rekognition -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-rekognition</artifactId>
<version>1.11.750</version>
</dependency>
Download Access key and secret key of IAM user. It will use when we will call API.
Java Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.AmazonRekognitionException;
import com.amazonaws.services.rekognition.model.BoundingBox;
import com.amazonaws.services.rekognition.model.Celebrity;
import com.amazonaws.services.rekognition.model.DetectTextRequest;
import com.amazonaws.services.rekognition.model.DetectTextResult;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.RecognizeCelebritiesRequest;
import com.amazonaws.services.rekognition.model.RecognizeCelebritiesResult;
import com.amazonaws.services.rekognition.model.TextDetection;
import com.amazonaws.util.IOUtils;
public class RecognizeWithLocalFile {
public static void main(String[] args) {
String photo = "C:\\Users\\hp\\Downloads\\Salman_Khan_filmfare.jpg";
ByteBuffer imageBytes=null;
try (InputStream inputStream = new FileInputStream(new File(photo))) {
imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
}
catch(Exception e)
{
System.out.println("Failed to load file " + photo);
System.exit(1);
}
// this code you can create Bean for get AmazonRekognition client -- Start --
final BasicAWSCredentials credentials;
try {
credentials = new BasicAWSCredentials("AKIAXKM5STPO", "A+M4UajF+sI");
} catch (Exception e) {
throw new AmazonClientException("Cannot load the credentials from "
+ "the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (/Users/userid/.aws/credentials), and is in valid format.", e);
}
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.standard()
.withRegion(Regions.US_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
// --------------END -------------------------//
// Celebrities Request
RecognizeCelebritiesRequest request = new RecognizeCelebritiesRequest()
.withImage(new Image()
.withBytes(imageBytes));
System.out.println("Looking for celebrities in image " + photo + "\n");
RecognizeCelebritiesResult result = rekognitionClient.recognizeCelebrities(request);
//Display recognized celebrity information
List<Celebrity> celebs=result.getCelebrityFaces();
System.out.println(celebs.size() + " celebrity(s) were recognized.\n");
for (Celebrity celebrity: celebs) {
System.out.println("Celebrity recognized: " + celebrity.getName());
System.out.println("Celebrity ID: " + celebrity.getId());
BoundingBox boundingBox=celebrity.getFace().getBoundingBox();
System.out.println("position: " +
boundingBox.getLeft().toString() + " " +
boundingBox.getTop().toString());
System.out.println("Further information (if available):");
for (String url: celebrity.getUrls()){
System.out.println(url);
}
System.out.println();
}
// Detect Text request
DetectTextRequest detectTextRequest = new DetectTextRequest()
.withImage(new Image()
.withBytes(imageBytes));
try {
//Display recognized Text information
DetectTextResult detectTextResult = rekognitionClient.detectText(detectTextRequest);
List<TextDetection> textDetections = detectTextResult.getTextDetections();
System.out.println("Detected lines and words for " + photo);
for (TextDetection text: textDetections) {
System.out.println("Detected: " + text.getDetectedText());
System.out.println("Confidence: " + text.getConfidence().toString());
System.out.println("Id : " + text.getId());
System.out.println("Parent Id: " + text.getParentId());
System.out.println("Type: " + text.getType());
System.out.println();
}
} catch(AmazonRekognitionException e) {
e.printStackTrace();
}
}
}
Update code which is yellow marked and run with main.
You will get text or celebrities recognition on image.
Happy Coding :)
Comments
Post a Comment