以前我试图从图像中提取葡萄牙语文本,并意识到结果出现了一些字母上的某些特殊标记。
即使文档说基于拉丁语的语言,在这种情况下是葡萄牙语,也不需要设置语言提示,我决定添加pt-BR并且它工作得很好。
现在我想用另一种语言获得LABEL_DETECTION的结果,因为它总是用英语,即使是我自己的languageHints首选项。要做到这一点,现在我只是翻译结果。它看起来并不高效,所以我想的是比云端智能支持它更容易的事情。
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);