Skip to content

Instantly share code, notes, and snippets.

@BatuhanKucukali
Created April 3, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BatuhanKucukali/0e1efbbfde2334a5ab91e7cb1acdea0f to your computer and use it in GitHub Desktop.
Save BatuhanKucukali/0e1efbbfde2334a5ab91e7cb1acdea0f to your computer and use it in GitHub Desktop.
@Slf4j
@Service
public class AmazonRekognitionServiceImpl implements ImageRecognitionService {
private final AwsConfig config;
private AmazonRekognition client;
public AmazonRekognitionServiceImpl(AwsConfig config) {
this.config = config;
}
@PostConstruct
public void init() {
BasicAWSCredentials credentials = new BasicAWSCredentials(config.getApiKey(), config.getSecretKey());
client = AmazonRekognitionClientBuilder
.standard()
.withRegion(Regions.EU_WEST_1)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
@Override
public String detectText(byte[] image) {
DetectTextRequest request = new DetectTextRequest()
.withImage(new Image()
.withBytes(ByteBuffer.wrap(image)));
try {
DetectTextResult result = client.detectText(request);
List<TextDetection> textDetections = result.getTextDetections();
if (textDetections.isEmpty()) {
return null;
}
StringBuilder content = new StringBuilder();
for (TextDetection text : textDetections) {
if ("WORD".equals(text.getType())) {
content.append(text.getDetectedText());
}
log.info("Detected: " + text.getDetectedText());
log.info("Confidence: " + text.getConfidence().toString());
log.info("Id : " + text.getId());
log.info("Parent Id: " + text.getParentId());
log.info("Type: " + text.getType());
}
return content.toString();
} catch (AmazonRekognitionException e) {
log.error("Detect Text Exception :", e);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment