Created
April 3, 2018 17:57
-
-
Save BatuhanKucukali/0e1efbbfde2334a5ab91e7cb1acdea0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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