Skip to content

Add vision ocr for pdf/tiff #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update samples with latest library
  • Loading branch information
nnegrey committed Apr 2, 2018
commit 7b5092f6cb153fff50b9366feeb983ae4882bee3
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<module>vision/beta/cloud-client</module>
<module>vision/cloud-client</module>
<module>vision/face-detection</module>
<module>vision/v1p2beta1</module>
<module>vision/label</module>
<module>vision/landmark-detection</module>
<module>vision/text</module>
Expand Down
9 changes: 2 additions & 7 deletions vision/v1p2beta1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.24.0</version>
<version>1.24.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
<version>1.24.1</version>
</dependency>
<!-- [END dependencies] -->

Expand Down
22 changes: 16 additions & 6 deletions vision/v1p2beta1/src/main/java/com/example/vision/Detect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Blob.BlobSourceOption;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.vision.v1p2beta1.AnnotateFileResponse;
import com.google.cloud.vision.v1p2beta1.AnnotateFileResponse.Builder;
import com.google.cloud.vision.v1p2beta1.AnnotateImageResponse;
import com.google.cloud.vision.v1p2beta1.AsyncAnnotateFileRequest;
import com.google.cloud.vision.v1p2beta1.AsyncAnnotateFileResponse;
Expand All @@ -36,9 +38,14 @@
import com.google.cloud.vision.v1p2beta1.OperationMetadata;
import com.google.cloud.vision.v1p2beta1.OutputConfig;

import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonFormat.Parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -118,7 +125,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio
// Create the configuration for the output with the batch size.
// The batch size sets how many pages should be grouped into each json output file.
OutputConfig outputConfig = OutputConfig.newBuilder()
.setBatchSize(2)
.setBatchSize(1)
.setGcsDestination(gcsDestination)
.build();

Expand All @@ -142,7 +149,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio

// Wait for the request to finish. (The result is not used, since the API saves the result to
// the specified location on GCS.)
List<AsyncAnnotateFileResponse> result = response.get(90, TimeUnit.SECONDS)
List<AsyncAnnotateFileResponse> result = response.get(180, TimeUnit.SECONDS)
.getResponsesList();

// Once the request has completed and the output has been
Expand Down Expand Up @@ -180,9 +187,12 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio
// object. If the Blob is small read all its content in one request
// (Note: the file is a .json file)
// Storage guide: https://cloud.google.com/storage/docs/downloading-objects
AnnotateFileResponse annotateFileResponse = AnnotateFileResponse.newBuilder()
.mergeFrom(firstOutputFile.getContent())
.build();
String jsonContents = new String(firstOutputFile.getContent());
Builder builder = AnnotateFileResponse.newBuilder();
JsonFormat.parser().merge(jsonContents, builder);

// Build the AnnotateFileResponse object
AnnotateFileResponse annotateFileResponse = builder.build();

// Parse through the object to get the actual response for the first page of the input file.
AnnotateImageResponse annotateImageResponse = annotateFileResponse.getResponses(0);
Expand All @@ -191,7 +201,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio
// The response contains more information:
// annotation/pages/blocks/paragraphs/words/symbols
// including confidence score and bounding boxes
System.out.format("\nText: %s\n", annotateImageResponse.getFullTextAnnotation());
System.out.format("\nText: %s\n", annotateImageResponse.getFullTextAnnotation().getText());
} else {
System.out.println("No MATCH");
}
Expand Down