2017-12-15 9 views
0

ゴーグルクラウドプラットフォームで動作しているアップロード済みのMLモデルを持っています(Pythonとgcloud ml-engineの予測でテスト済み)。予測のためにGoogle Cloud Machine Learning Engineクライアントライブラリを使用するには

現在、このライブラリを使用してAndroidから予測を取得しようとしています:Client Library for Java with this javadoc私はこのようになりますAsyncTaskにアクセスし、Androidのコードのサービスアカウントを使用し :

JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
      HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport(); 
      GoogleCredential credential = GoogleCredential.fromStream(is, httpTransport, jsonFactory); 
      CloudMachineLearningEngine ml = new CloudMachineLearningEngine.Builder(httpTransport,jsonFactory,credential) 
        .setApplicationName("myCloudApplication") 
        .build(); 
      Log.i(TAG,"Successfully set up !!"); 

ある私のサービスアカウントキーを含むJSONファイルへのInputStreamです。 私は訓練されたMLモデルに対して予測をするためにここから多くのことを試してきました。私はオンラインの例を見つけることができません。 これも可能ですか?

すべてのサポートに深く感謝します。

+0

エラーが発生しましたか。これは、問題が資格情報なのかどうかを判断するのに役立ちます。 – rhaertel80

+0

私はエラーを持っていません私は厳密な意味を持っています。 –

答えて

0

これは間違いなくサポートされています。 this sample

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.FileContent; 
import com.google.api.client.http.GenericUrl; 
import com.google.api.client.http.HttpContent; 
import com.google.api.client.http.HttpRequest; 
import com.google.api.client.http.HttpRequestFactory; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.UriTemplate; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.services.discovery.Discovery; 
import com.google.api.services.discovery.model.JsonSchema; 
import com.google.api.services.discovery.model.RestDescription; 
import com.google.api.services.discovery.model.RestMethod; 
import java.io.File; 

/* 
* Sample code for doing Cloud Machine Learning Engine online prediction in Java. 
*/ 
public class OnlinePredictionSample { 

  public static void main(String[] args) throws Exception { 

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
    Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build(); 

    RestDescription api = discovery.apis().getRest("ml", "v1").execute(); 
    RestMethod method = api.getResources().get("projects").getMethods().get("predict"); 

    JsonSchema param = new JsonSchema(); 
    String projectId = "YOUR_PROJECT_ID"; 
    // You should have already deployed a model and a version. 
    // For reference, see https://cloud.google.com/ml-engine/docs/how-tos/deploying-models. 
    String modelId = "YOUR_MODEL_ID"; 
    String versionId = "YOUR_VERSION_ID"; 
    param.set(
        "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId)); 

    GenericUrl url = 
        new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true)); 
    System.out.println(url); 

    String contentType = "application/json"; 
    File requestBodyFile = new File("input.txt"); 
    HttpContent content = new FileContent(contentType, requestBodyFile); 
    System.out.println(content.getLength()); 

    GoogleCredential credential = GoogleCredential.getApplicationDefault(); 
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); 
    HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content); 

    String response = request.execute().parseAsString(); 
    System.out.println(response); 
  } 
} 
+0

このリンクは質問に答えるかもしれませんが、答えの本質的な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューの投稿](レビュー/低品質の投稿/ 18271275) – Aryan

+0

良い点。追加されました! – rhaertel80

+0

ありがとうございますが、そのソリューションではcom.google.api.services.ml.v1.CloudMachineLearningEngine APIは使用されていません。 –

関連する問題