2016-10-31 3 views
-2

私はGoogleクラウドビジョンAPI(Text_Detection)を使用し、それは、通常の作業が、時にGoogleからのリターン答えをiはメッセージのスタイルıはどのように応答テキストを修正できますか?画像のような

私はちょうど1つのテキストなどをしたい「ACADEMIC PLANNER」はとてもどのようにすることができますは、学術の前を削除します"null:"と他の言葉?

画像例えば

enter image description here

そして、ここに私のコードです。

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 

    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     for (EntityAnnotation label : labels) { 
      message += String.format("%.3f: %s", label.getScore(), label.getDescription()); 
      message += "\n"; 
     } 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 

答えて

0

回答:

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 
    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     message += labels.get(0).getDescription(); 
    } else { 
     message += "nothing"; 
    } 
    return message; 
} 
-1

私が正しくあなたの質問を理解していれば、あなたはちょうどStringクラスのsubstring()trim()方法と、ラベルの説明をサニタイズする必要があります。

private String convertResponseToString(BatchAnnotateImagesResponse response) { 

    String message = "I found these things:\n\n"; 
    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     for (EntityAnnotation label : labels) { 
      // 5=index of the character after "null:" (zero-based array counting), trim() removes whitespace on both sides of the string. 
      message += String.format("%.3f: %s", label.getScore(), label.getDescription().substring(5).trim()); 
      message += "\n"; 
     } 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 

PS:ここにあなたのコードの修正版である文書からは、getDescription()メソッドは文字列を返し、getScore()はフロートを返します。私はスコアがあなたに何か問題を与えていないと確信しています。私はあなたの実際のデータをテストしていません。

+0

いやそれありえない作品。私は試してみるが、私はerorを得る。 imgur.com/uEckW8Z Androidスタジオはこのコードを許可していない、別のアイデアですか? –

+0

ここに必要なのはフルコードです:https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android/CloudVision –

+0

これらのメソッドは文字列でのみ利用でき、 'EntityAnnotation'は文字列ではありません。 ..私はひもの配列で私の微調整をテストした、申し訳ありません。編集された答えを見てください。 – Dut

0

あなたのスコアはおそらくnullです。これを行う:

message += String.format("%s", label.getDescription()); 

は、1つの単語だけを持っているために、あなたはあなたの方法は次のようになります

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 

    List<EntityAnnotation> label = response.getResponses().get(0).getTextAnnotations().get(0); 
    if (label != null) { 
      message += String.format("%s", label.getDescription()); 
      message += "\n"; 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 
+0

Yeap。本当にありがとうございます。他の問題はありますか? –

+0

@EmreAkbaki私の更新を参照してください。 – BlackHatSamurai

+0

それは受け入れないでください "label.getScore()、label.getDescription());"なぜ2回書いたのですか.get(0) ? –

関連する問題