0

Java WebアプリケーションがREST要求を使用するようにLinuxインスタンスを設定して実行しているAmazon EC2があります。問題は、このアプリケーションでGoogle Cloud Visionを使用して、ユーザーの写真の暴力/ヌードを認識しようとしていることです。私のターミナルでEC2へのアクセスGoogleアプリケーションの資格情報が設定されていないと見つかりません

が、私はマニュアルを参照し、次のコマンドによってGOOGLE_APPLICATION_CREDENTIALSを設定します。私は私のサーバーを再起動すると、と「エコーを実行しました:

export GOOGLE_APPLICATION_CREDENTIALS=<my_json_path.json> 

ここに私の最初の問題が来ます$ GOOGLE_APPLICATION_CREDENTIALS '変数は削除されました。さて、私はそれをbash_profileとbashrcに設定しましたが、今は大丈夫です。

しかし、私は私の絵の大人と暴力のステータスを取得するには、上記のコードを消費し、自分のアプリケーションを実行したとき、私は次のエラーを得た:

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. 

私のコードは以下の通りです:

コントローラ

 if(SafeSearchDetection.isSafe(user.getId())) { 
      if(UserDB.updateUserProfile(user)==false){ 
       throw new SQLException("Failed to Update"); 
      } 
     } else { 
      throw new IOException("Explicit Content"); 
     } 

SafeSearchDetection.isSafe(INT idUser)

String path = IMAGES_PATH + idUser + ".jpg"; 

    try { 
     mAdultMedicalViolence = detectSafeSearch(path); 
     if(mAdultMedicalViolence.get(0) > 3) 
      return false; 
     else if(mAdultMedicalViolence.get(1) > 3) 
      return false; 
     else if(mAdultMedicalViolence.get(2) > 3) 
      return false; 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return true; 

detectSafeSearch(文字列のパス)

答えて

0
List<AnnotateImageRequest> requests = new ArrayList<AnnotateImageRequest>(); 
    ArrayList<Integer> adultMedicalViolence = new ArrayList<Integer>(); 

    ByteString imgBytes = ByteString.readFrom(new FileInputStream(path)); 

    Image img = Image.newBuilder().setContent(imgBytes).build(); 
    Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); 
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); 
    requests.add(request); 

    ImageAnnotatorClient client = ImageAnnotatorClient.create(); 

    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); 
    List<AnnotateImageResponse> responses = response.getResponsesList(); 

    for (AnnotateImageResponse res : responses) { 
     if (res.hasError()) { 
      System.out.println("Error: "+res.getError().getMessage()+"\n"); 
      return null; 
     } 

     SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); 
     adultMedicalViolence.add(annotation.getAdultValue()); 
     adultMedicalViolence.add(annotation.getMedicalValue()); 
     adultMedicalViolence.add(annotation.getViolenceValue()); 

    } 
    for(int content : adultMedicalViolence) 
     System.out.println(content + "\n"); 

    return adultMedicalViolence; 

マイRESTアプリケーションはTomcat8上に構築されました。正常に実行されなかった場合は、次のコマンドを実行してください。

System.getenv("GOOGLE_APPLICATION_CREDENTIALS") 

私の問題は環境変数からTomcatへのインストールにあったことに気付きました。これを修正するために、私は/ binに新しいファイルsetenv.shを作成しました:

GOOGLE_APPLICATION_CREDENTIALS=<my_json_path.json> 

そして、それは機能しました!

関連する問題