Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package hackthesix2019;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.util.regex.Pattern;
  9.  
  10. import com.amazonaws.regions.Regions;
  11. import com.amazonaws.services.rekognition.AmazonRekognition;
  12. import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
  13. import com.amazonaws.services.rekognition.model.DetectTextRequest;
  14. import com.amazonaws.services.rekognition.model.DetectTextResult;
  15. import com.amazonaws.services.rekognition.model.Image;
  16. import com.amazonaws.services.rekognition.model.TextDetection;
  17.  
  18. //import org.apache.commons.lang3.math.NumberUtils;
  19.  
  20. public class TextInterpreter {
  21. public static void main(String[] args) throws Exception {
  22. getText("ReactineInfo.jpg");
  23. }
  24.  
  25. public static String getText(String imgPath) throws Exception {
  26. String text = "Fuck off";
  27.  
  28. System.setProperty("aws.accessKeyId", "AKIAZTV6LSMNHTQV6WFK");
  29. System.setProperty("aws.secretKey", "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");
  30.  
  31. AmazonRekognition rk = AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
  32. byte[] bytes = Files.readAllBytes(new File(imgPath).toPath());
  33. File n = new File(text);
  34. ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
  35.  
  36. Image img = new Image().withBytes(byteBuffer);
  37. DetectTextRequest req = new DetectTextRequest();
  38. req.withImage(img);
  39.  
  40. DetectTextResult res = rk.detectText(req);
  41.  
  42. for ( TextDetection d : res.getTextDetections() ) {
  43. text = d.getDetectedText();
  44. if (text.contains("DIN") && text.length() == 11) {
  45. text = text.replace("DIN", "");
  46. print("Found DIN and Number");
  47. print(text);
  48. break;
  49. } else if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2 && text.length() == 8) {
  50. print("Found Number");
  51. print(text);
  52. break;
  53. }
  54. //float width = d.getGeometry().getBoundingBox().getWidth();
  55. }
  56. print(res.toString());
  57. return text;
  58. }
  59.  
  60. public static void print(String text) {
  61. System.out.println(text);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement