以前我试图从图像中提取葡萄牙语文本,并意识到结果在某些字母上缺少一些特殊标记。
甚至文档说基于拉丁语的语言,在这种情况下是葡萄牙语,不需要设置语言提示,我决定添加 pt-BR 并且它工作得很好。
现在我想以不同的语言获得 LABEL_DETECTION 的结果,因为它总是以英语提供,即使有我自己的语言提示偏好。为此,现在我只是在翻译结果。它似乎效率不高,所以我在考虑比这更容易的事情,以防 Cloud Vision 支持它。
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
builder.setVisionRequestInitializer(new
VisionRequestInitializer(CLOUD_VISION_API_KEY));
Vision vision = builder.build();
BatchAnnotateImagesRequest batchAnnotateImagesRequest =
new BatchAnnotateImagesRequest();
batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();
// Add the image
Image base64EncodedImage = new Image();
// Convert the bitmap to a JPEG
// Just in case it's a format that Android understands but Cloud Vision
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
// Base64 encode the JPEG
base64EncodedImage.encodeContent(imageBytes);
annotateImageRequest.setImage(base64EncodedImage);
//this part I thought would be enough to get LABEL_DETECTION results in Portuguese, but it's not.
ImageContext imageContext = new ImageContext();
String [] languages = { "pt-BR" };
imageContext.setLanguageHints(Arrays.asList(languages));
annotateImageRequest.setImageContext(imageContext);
// add the features we want
annotateImageRequest.setFeatures(array);
// Add the list of one thing to the request
add(annotateImageRequest);}});
Vision.Images.Annotate annotateRequest =
vision.images().annotate(batchAnnotateImagesRequest);
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotateRequest.setDisableGZipContent(true);
Log.d(TAG, "created Cloud Vision request object, sending request");
BatchAnnotateImagesResponse response = annotateRequest.execute();
//function to get Strings from response
photoDescriptor.callTextAdaptation(response);