2016-09-13 120 views
1

我正在嘗試向Google Vision文本檢測API發出批量請求。到目前爲止,我把圖像的路徑放入列表中,發出批量請求並獲得響應。但是,我無法確定哪個結果屬於哪個圖像。爲此,我嘗試在請求中添加一個ID,當我得到結果時,我會比較ID。但是,我無法將任何自定義字段放入請求中。我的方法有什麼問題嗎?我怎麼知道哪個響應屬於哪個圖像?Google Vision Java客戶端批量請求響應識別

這裏是我使用的這些請求的代碼:

private Vision vision; 
private static final String APPLICATION_NAME = "ProjectName"; 

public static Vision getVisionService() throws IOException, GeneralSecurityException { 

    GoogleCredential credential = GoogleCredential.fromStream 
      (new FileInputStream("/project-key.json")) 
      .createScoped(VisionScopes.all()); 
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
    return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
} 
/** 
* Gets up to {@code maxResults} text annotations for images stored at {@code paths}. 
*/ 
public List<String> detectText(List<Path> paths) { 
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder(); 

    try { 
     for (Path path : paths) { 
      byte[] data; 
      data = Files.readAllBytes(path); 
      requests.add(
        new AnnotateImageRequest() 
        .setImage(new Image().encodeContent(data)) 
        .setFeatures(ImmutableList.of(
          new Feature() 
          .setType("TEXT_DETECTION") 
          .setMaxResults(10)))); 
     } 

     Vision.Images.Annotate annotate = 
       vision.images() 
       .annotate(new BatchAnnotateImagesRequest().setRequests(requests.build())); 
     // Due to a bug: requests to Vision API containing large images fail when GZipped. 
     annotate.setDisableGZipContent(true); 
     BatchAnnotateImagesResponse batchResponse = annotate.execute(); 
     assert batchResponse.getResponses().size() == paths.size(); 

     List<String> output = new ArrayList(); 

     for (int i = 0; i < paths.size(); i++) { 
      AnnotateImageResponse response = batchResponse.getResponses().get(i); 
      if(response != null && response.getTextAnnotations() != null){ 
       System.out.println(response.toString()); 
       String result = getDescriptionFromJson(response.getTextAnnotations().toString()); 
       System.out.println(response.get("customField")); 
       output.add(result); 
      } 
     } 
     return output; 
    } catch (IOException ex) { 
     System.out.println("Exception occured: " + ex); 
     return null; 
    } 
} 

public String getDescriptionFromJson(String json){ 
    JSONArray results = new JSONArray(json); 
    JSONObject result = (JSONObject) results.get(0); 
    return result.getString("description"); 
} 

public static void main(String[] args) { 
    GoogleVisionQueryHelper g = new GoogleVisionQueryHelper(); 

    try { 
     g.vision = getVisionService(); 
     List<Path> paths = new ArrayList<>(); 

     String directory = "/images"; 

     File[] files = new File(directory).listFiles(); 
     for(File file : files){ 
      if(file.isFile() && !file.getName().contains("DS_Store") && !file.getName().startsWith(".")){ 
      System.out.println(file.getAbsolutePath()); 
      paths.add(Paths.get(file.getAbsolutePath())); 
      } 
     }   
     System.out.println("Starting..."); 

     for(String s: g.detectText(paths)){ 
      System.out.println(s); 
     } 
    } catch (IOException | GeneralSecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

嗨,兄弟,你能給我一些幫助嗎?我的計劃將停留在 'BatchAnnotateImagesResponse響應= vision.images() .annotate(requestBatch) .setDisableGZipContent(真) .execute();' 你知道原因嗎?我對Google雲視覺api完全陌生 我懷疑請求網址的正確性,因爲它總是顯示「https:// vision.googleapis.com」,而不是「https://vision.googleapis.com/v1」/images:annotate?key =' –

回答

1

BatchAnnotateImageResponse持有相同的順序在BatchAnnotateImageRequest請求列表的響應列表。

+0

你怎麼知道的? – user2604150

相關問題