use of com.google.cloud.translate.v3.GcsSource in project java-vision by googleapis.
the class DetectBatchAnnotateFilesGcs method detectBatchAnnotateFilesGcs.
// Performs document feature detection on a remote PDF/TIFF/GIF file on Google Cloud Storage.
public static void detectBatchAnnotateFilesGcs(String gcsPath) {
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
// Annotate the first two pages and the last one (max 5 pages)
// First page starts at 1, and not 0. Last page is -1.
List<Integer> pages = Arrays.asList(1, 2, -1);
GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsPath).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
// Other supported mime types : 'image/tiff' or 'image/gif'
InputConfig inputConfig = InputConfig.newBuilder().setMimeType("application/pdf").setGcsSource(gcsSource).build();
AnnotateFileRequest request = AnnotateFileRequest.newBuilder().addFeatures(feat).setInputConfig(inputConfig).addAllPages(pages).build();
List<AnnotateFileRequest> requests = new ArrayList<>();
requests.add(request);
BatchAnnotateFilesRequest batchAnnotateFilesRequest = BatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build();
ApiFuture<BatchAnnotateFilesResponse> future = client.batchAnnotateFilesCallable().futureCall(batchAnnotateFilesRequest);
BatchAnnotateFilesResponse response = future.get();
// Getting the first response
AnnotateFileResponse annotateFileResponse = response.getResponses(0);
// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation textAnnotation = annotateFileResponse.getResponses(0).getFullTextAnnotation();
for (Page page : textAnnotation.getPagesList()) {
String pageText = "";
for (Block block : page.getBlocksList()) {
String blockText = "";
for (Paragraph para : block.getParagraphsList()) {
String paraText = "";
for (Word word : para.getWordsList()) {
String wordText = "";
for (Symbol symbol : word.getSymbolsList()) {
wordText = wordText + symbol.getText();
System.out.format("Symbol text: %s (Confidence: %f)\n", symbol.getText(), symbol.getConfidence());
}
System.out.format("Word text: %s (Confidence: %f)\n\n", wordText, word.getConfidence());
paraText = String.format("%s %s", paraText, wordText);
}
// Output Example using Paragraph:
System.out.println("\nParagraph: \n" + paraText);
System.out.format("Paragraph Confidence: %f\n", para.getConfidence());
blockText = blockText + paraText;
}
pageText = pageText + blockText;
}
}
System.out.println("\nComplete annotation:");
System.out.println(textAnnotation.getText());
} catch (Exception e) {
System.out.println("Error during detectPdfText: \n" + e.toString());
}
}
use of com.google.cloud.translate.v3.GcsSource in project java-automl by googleapis.
the class DatasetApi method importData.
// [START automl_translate_import_data]
/**
* Import sentence pairs to the dataset.
*
* @param projectId the Google Cloud Project ID.
* @param computeRegion the Region name. (e.g., "us-central1").
* @param datasetId the Id of the dataset.
* @param path the remote Path of the training data csv file.
*/
public static void importData(String projectId, String computeRegion, String datasetId, String path) throws IOException, InterruptedException, ExecutionException {
// Instantiates a client
try (AutoMlClient client = AutoMlClient.create()) {
// Get the complete path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
GcsSource.Builder gcsSource = GcsSource.newBuilder();
// Get multiple Google Cloud Storage URIs to import data from
String[] inputUris = path.split(",");
for (String inputUri : inputUris) {
gcsSource.addInputUris(inputUri);
}
// Import data from the input URI
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
System.out.println("Processing import...");
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
System.out.println(String.format("Dataset imported. %s", response));
}
}
use of com.google.cloud.translate.v3.GcsSource in project java-translate by googleapis.
the class BatchTranslateTextWithModel method batchTranslateTextWithModel.
// Batch translate text using AutoML Translation model
public static void batchTranslateTextWithModel(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String modelId) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
String location = "us-central1";
LocationName parent = LocationName.of(projectId, location);
// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
// Configure where to store the output in a GCS bucket
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
// Configure the model used in the request
String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
// Build the request that will be sent to the API
BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).putModels(targetLanguage, modelPath).build();
// Start an asynchronous request
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between 300 - 450 (maximum allowed seconds)
long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
// Display the translation for each input text provided
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
}
}
use of com.google.cloud.translate.v3.GcsSource in project java-translate by googleapis.
the class BatchTranslateText method batchTranslateText.
// Batch translate text
public static void batchTranslateText(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `us-central1`
LocationName parent = LocationName.of(projectId, "us-central1");
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between 300 - 450 (maximum allowed seconds)
long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
}
}
use of com.google.cloud.translate.v3.GcsSource in project java-retail by googleapis.
the class ImportProductsGcs method getImportProductsGcsRequest.
public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName) {
GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Collections.singleton(String.format("%s/%s", GCS_BUCKET, gcsObjectName))).build();
ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
System.out.println("GRS source: " + gcsSource.getInputUrisList());
ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(GCS_ERROR_BUCKET).build();
ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).setReconciliationMode(ReconciliationMode.INCREMENTAL).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
System.out.println("Import products from google cloud source request: " + importRequest);
return importRequest;
}
Aggregations