Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
  2. import com.google.cloud.vision.v1.*;
  3. import com.google.protobuf.ByteString;
  4. import nl.remcobuddelmeijer.config.Config;
  5. import nl.remcobuddelmeijer.config.Configs;
  6.  
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.concurrent.ExecutorService;
  14. import java.util.concurrent.Executors;
  15. import java.util.concurrent.Future;
  16. import java.util.concurrent.locks.ReentrantLock;
  17.  
  18. /**
  19. * Created by Remco on 25-6-2017.
  20. */
  21. public class VisionInstance {
  22.  
  23. //Client for VisionAPI
  24. private final ImageAnnotatorClient client = ImageAnnotatorClient.create();
  25.  
  26. private final ExecutorService executor = Executors.newWorkStealingPool();
  27. private final ReentrantLock lock = new ReentrantLock();
  28. private volatile byte[] content;
  29.  
  30. //API Key used for Authentication
  31. private final String key;
  32.  
  33. //Configurations for VisionAPI (Hardcoded)
  34. @Configs({
  35. //API Key based annotation
  36. @Config(key = "key", serializedValue = "AIzaSyAcEdI1whFqiYjlqGhFm8fKybuzNO60XdU")
  37. })
  38.  
  39. /**
  40. * Constructor for one of the VisionAPI instances running.
  41. */
  42. public VisionInstance() throws IOException {
  43. this.key = "";
  44. }
  45.  
  46. public Future<?> fetch(URL url) throws InterruptedException {
  47. executor.execute(() -> {
  48. lock.lock();
  49. URLConnection connection = new URLConnection(url) {
  50. @Override
  51. public void connect() throws IOException {
  52. OutputStream stream = this.getOutputStream();
  53. byte[] b = new byte[2048];
  54. int length;
  55. while((length = this.getInputStream().read(b)) != -1){
  56. stream.write(b, 0, length);
  57. }
  58. content = b;
  59. try {
  60. this.getInputStream().close();
  61. this.getOutputStream().close();
  62. }finally{
  63. lock.unlock();
  64. }
  65. }
  66. };
  67. });
  68. while (lock.isLocked()) {
  69. wait();
  70. }
  71. return executor.submit(() -> {
  72. List<AnnotateImageRequest> requests = new ArrayList<>();
  73. ByteString imgBytes = ByteString.copyFrom(content);
  74. Image image = Image.newBuilder().setContent(imgBytes).build();
  75. Feature feature = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
  76. AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
  77. .addFeatures(feature).setImage(image).build();
  78. requests.add(request);
  79. BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
  80. return response.getResponsesList();
  81. });
  82. }
  83.  
  84. /**
  85. * VisionAPI client bound to instance.
  86. * Can only be used by one thread.
  87. *
  88. * @return
  89. */
  90. public synchronized ImageAnnotatorClient getClient() {
  91. return client;
  92. }
  93.  
  94. /**
  95. * Key assigned to identify identity for Google Auth.
  96. *
  97. * @return
  98. */
  99. public String getKey() {
  100. return key;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement