2016-09-13 4 views
1

Google Vision Text Detection APIにバッチリクエストを作成しようとしています。これまでは、画像のパスをリストに入れ、バッチリクエストを行い、応答を取得しました。しかし、どの結果がどの画像に属するのかを判断することはできません。これを行うために、リクエストにIDを入れようとしましたが、結果が返ってくるとIDを比較します。しかし、私はリクエストにカスタムフィールドを置くことはできません。私のアプローチに何か問題がありますか?どのような応答がどの画像に属するのかをどのように知ることができますか?ここでGoogle Vision JavaクライアントバッチリクエストレスポンスID

は、私はこれらの要求に使用するコードです:

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/v1ではなく' https:// vision.googleapis.com'を常に表示するので、リクエストのURLが間違っていると思います/ images:annotate?key = ' –

答えて

1

BatchAnnotateImageResponseはBatchAnnotateImageRequestで要求リストと同じ順序で応答リストを保持しています。

+0

あなたはそれをどのように知っていますか? – user2604150

関連する問題