5

我正在制作一个将利用 Google AutoML Vision API 的 Android 应用程序。我正在寻找一种方法来获取永久访问令牌或在代码中生成它们,这样我就不需要每次想使用我的应用程序时都使用 gcloud。我该怎么做呢?

我已经创建了 AutoML 模型,设置了我的服务帐户,并在 Android Studio 中编写了我的应用程序,以便它使用 Volley 向 API 发出请求。问题是,它们要求您使用 gcloud 生成并传递访问令牌。我可以生成令牌并将其放入我的代码中,但它只持续一个小时然后过期。REST API 需要访问令牌,如下所示。

curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth application-default print-access- 
token)" 

我已经研究了解决这个问题的不同方法。例如,有一些 Google Client Libraries for Java 和 Google Cloud Applications 展示了如何将服务帐户凭据添加到代码中。我很困惑从手机运行它时如何将 Json 密钥文件添加到代码中。我还读到可以使用 Firebase,但我不熟悉该过程是什么。

目前,我将在我的计算机上打开 gcloud,生成访问令牌,将其粘贴到我的代码中并使用标题按如下方式运行应用程序,这会在长达一小时的时间内返回所需的结果,直到访问代码过期。

@Override
public Map<String, String> getHeaders() throws AuthFailureError{
    Map<String, String> headers = new HashMap<>();
    headers.put("Authorization", "Bearer " + accesstoken);
    return headers;
 }

我希望这是一个可以在 Android 手机上运行的独立应用程序。这样做的最佳方法是什么?


更新:我能够将文件添加到 Android Studio 中,然后使用一些函数来获取访问令牌,它似乎在模拟器中工作。我不确定这种方法有多安全,因为带有密钥的 json 文件需要保密。

InputStream is = getAssets().open("app.json");
GoogleCredentials credentials = 
GoogleCredentials.fromStream(i).createScoped(Lists.newArrayList(scope));
credentials.refreshIfExpired();
AccessToken accesstoken = credentials.getAccessToken();
4

1 回答 1

1
  • 将 firebase 添加到您的 android 项目中。https://firebase.google.com/docs/android/setup您将在 Firebase 中创建一个项目并下载一个用于配置的 json 文件并将其添加到 app 目录中。在 gradle 文件中添加依赖项。
  • 在 Firebase 控制台上,转到 ML Kit 部分并使用您的照片创建 AUTML 模型。
  • 训练模型
  • 训练完成后,您可以下载模型并在 assets/model 目录中下载 3 个文件。它可以使用了。通过这种方式,您将使用 Firebase AutoML SDK,并且不需要生成令牌。

  • 使用您的模型并从应用程序中进行预测。步骤是:

    • 准备图像以进行预测
    • 准备模型
    • 获取图像标注器
    • 处理图像进行分类

     public void findLabelsWithAutoML() {
            Bitmap bitmap = null;
            File file = new File(currentPhotoPath);
            System.out.println("file "+file);
            try {
                bitmap = MediaStore.Images.Media
                        .getBitmap(getContentResolver(), Uri.fromFile(file));
            } catch (Exception e) {
                e.printStackTrace();
            }
            FirebaseVisionImageMetadata metadata =  new FirebaseVisionImageMetadata.Builder()
                     .setWidth(480) // 480x360 is typically sufficient for
                    .setHeight(360) // image recognition
                    .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
                    .setRotation(FirebaseVisionImageMetadata.ROTATION_0)
                    .build();

            FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap);

            System.out.println("firebaseVisionImage :"+firebaseVisionImage);
            FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder()
                    .setAssetFilePath("model/manifest.json")
                    .build();
            FirebaseVisionOnDeviceAutoMLImageLabelerOptions labelerOptions = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
                    .setConfidenceThreshold(0.65F)  // Evaluate your model in the Firebase console
                    // to determine an appropriate value.
                    .build();
            FirebaseVisionImageLabeler firebaseVisionImageLabeler = null;
            try {
                firebaseVisionImageLabeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(labelerOptions);
            } catch (Exception e) {
                e.printStackTrace();
            }
            firebaseVisionImageLabeler.processImage(firebaseVisionImage)
                    .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() {
                        @Override
                        public void onSuccess(List<FirebaseVisionImageLabel> labels) {
                            for (FirebaseVisionImageLabel label : labels) {
                                System.out.println("label " + label.getText() + " score: " + (label.getConfidence() * 100));
                            }
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            //
                        }
                    });

        }

于 2020-02-26T08:53:56.113 回答