Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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