use of com.google.cloud.dialogflow.cx.v3beta1.Page in project java-dialogflow-cx by googleapis.
the class ListPages method listPages.
// DialogFlow API List Pages Sample.
// Lists all pages from the provided parameters
public static void listPages(String projectId, String agentId, String flowId, String location) throws IOException {
PagesClient client = PagesClient.create();
Builder listRequestBuilder = ListPagesRequest.newBuilder();
listRequestBuilder.setParent("projects/" + projectId + "/locations/" + location + "/agents/" + agentId + "/flows/" + flowId);
listRequestBuilder.setLanguageCode("en");
// Make API request to list all pages in the project
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
System.out.println(element);
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Page in project java-vision by googleapis.
the class DetectBeta method detectHandwrittenOcrGcs.
// [END vision_handwritten_ocr_beta]
// [START vision_handwritten_ocr_gcs_beta]
/**
* Performs handwritten text detection on a remote image on Google Cloud Storage.
*
* @param gcsPath The path to the remote file on Google Cloud Storage to detect handwritten text
* on.
* @param out A {@link PrintStream} to write the results to.
* @throws Exception on errors while closing the client.
* @throws IOException on Input/Output errors.
*/
public static void detectHandwrittenOcrGcs(String gcsPath, PrintStream out) throws Exception {
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
// Set the parameters for the image
ImageContext imageContext = ImageContext.newBuilder().addLanguageHints("en-t-i0-handwrit").build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).setImageContext(imageContext).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page : annotation.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();
out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), symbol.getConfidence());
}
out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence());
paraText = String.format("%s %s", paraText, wordText);
}
// Output Example using Paragraph:
out.println("\nParagraph: \n" + paraText);
out.format("Paragraph Confidence: %f\n", para.getConfidence());
blockText = blockText + paraText;
}
pageText = pageText + blockText;
}
}
out.println("\nComplete annotation:");
out.println(annotation.getText());
}
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Page in project java-vision by googleapis.
the class Detect method detectDocumentText.
/**
* Performs document text detection on a local image file.
*
* @param filePath The path to the local file to detect document text on.
* @throws Exception on errors while closing the client.
* @throws IOException on Input/Output errors.
*/
// [START vision_fulltext_detection]
public static void detectDocumentText(String filePath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page : annotation.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(annotation.getText());
}
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Page in project java-vision by googleapis.
the class DetectBatchAnnotateFiles method detectBatchAnnotateFiles.
// Performs document feature detection on a local PDF/TIFF/GIF file.
public static void detectBatchAnnotateFiles(String filePath) {
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);
ByteString pdfBytes = ByteString.readFrom(new FileInputStream(filePath));
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").setContent(pdfBytes).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.dialogflow.cx.v3beta1.Page 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());
}
}
Aggregations