現在、Google Cloud PlatformのCloud Vision APIを試そうとしております。
以下の環境で実装しております。
―――――――――――――――――――――――――――――――――――
Windows
Eclipse
java
Mavenプロジェクト
―――――――――――――――――――――――――――――――――――
こちらの、Google公式のドキュメントを見て、クイックスタートを進めています。
https://cloud.google.com/vision/docs/quickstart-client-libraries?hl=ja
Google Cloud Platform にてプロジェクトを作成
0.
課金を設定
0.
認証情報を作成しJsonファイルを任意のディレクトリに保存
0.
コマンドプロンプトで『set GOOGLE_APPLICATION_CREDENTIALS=<Jsonファイルのパス>』を実行
0.
念のため、Windowsのシステム環境変数にも設定
0.
EclipseでMavenプロジェクトを作成
0.
pom.xmlに、ドキュメントに記載されている
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.85.0</version>
を設定
0.
以下のソースコードをコピーして実行
0.
警告:
Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/. [土 8月 31 16:14:37 JST 2019]
が表示され、エラーにもならずプロセスが終わらない状態でした。
コードは、ドキュメントに記載されている以下の通りです。
public class QuickstartSample { public static void main(String... args) throws Exception { // Instantiates a client try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) { // The path to the image file to annotate String fileName = "<画像ファイルを直接パスで記載しました>"; // Reads the image file into memory Path path = Paths.get(fileName); byte[] data = Files.readAllBytes(path); ByteString imgBytes = ByteString.copyFrom(data); // Builds the image annotation request List<AnnotateImageRequest> requests = new ArrayList<>(); Image img = Image.newBuilder().setContent(imgBytes).build(); Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder() .addFeatures(feat) .setImage(img) .build(); requests.add(request); // Performs label detection on the image file BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); for (AnnotateImageResponse res : responses) { if (res.hasError()) { System.out.printf("Error: %s\n", res.getError().getMessage()); return; } for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { annotation.getAllFields().forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString())); } } } } }
Cloud Vision APIについて詳しい方がいらっしゃいましたら、
教えていただけますと幸いです。
あなたの回答
tips
プレビュー