Cow*_*ord 5 java android google-cloud-platform gcloud automl
我正在制作一个将利用 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)"
Run Code Online (Sandbox Code Playgroud)
我已经研究了解决这个问题的不同方法。例如,有一些适用于 Java 和 Google Cloud 应用程序的 Google 客户端库展示了如何将服务帐户凭据添加到代码中。我很困惑从手机运行时如何将 Json 密钥文件添加到代码中。我还读到可以使用 Firebase,但我不熟悉该过程是什么。
目前,我将在我的计算机上打开 gcloud,生成访问令牌,将其粘贴到我的代码中,并按如下方式使用标题运行应用程序,这将返回所需的结果长达一个小时,直到访问代码过期。
@Override
public Map<String, String> getHeaders() throws AuthFailureError{
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accesstoken);
return headers;
}
Run Code Online (Sandbox Code Playgroud)
我希望这是一个可以在 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();
Run Code Online (Sandbox Code Playgroud)
小智 1
训练完成后,您可以下载模型,并在 asset/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) {
//
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
583 次 |
| 最近记录: |