use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.
the class DocumentOcrTemplate method extractOcrResultFuture.
private ListenableFuture<DocumentOcrResultSet> extractOcrResultFuture(OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> grpcFuture) {
SettableListenableFuture<DocumentOcrResultSet> result = new SettableListenableFuture<>();
ApiFutures.addCallback(grpcFuture, new ApiFutureCallback<AsyncBatchAnnotateFilesResponse>() {
@Override
public void onFailure(Throwable throwable) {
result.setException(throwable);
}
@Override
public void onSuccess(AsyncBatchAnnotateFilesResponse asyncBatchAnnotateFilesResponse) {
String outputLocationUri = asyncBatchAnnotateFilesResponse.getResponsesList().get(0).getOutputConfig().getGcsDestination().getUri();
GoogleStorageLocation outputFolderLocation = new GoogleStorageLocation(outputLocationUri);
result.set(readOcrOutputFileSet(outputFolderLocation));
}
}, this.executor);
return result;
}
use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.
the class CloudVisionTemplate method extractTextFromFile.
/**
* Extract the text out of a file and return the result as a String.
*
* @param fileResource the file one wishes to analyze
* @param mimeType the mime type of the fileResource. Currently, only "application/pdf",
* "image/tiff" and "image/gif" are supported.
* @return the text extracted from the pdf as a string per page
* @throws CloudVisionException if the image could not be read or if text extraction failed
*/
public List<String> extractTextFromFile(Resource fileResource, String mimeType) {
AnnotateFileResponse response = analyzeFile(fileResource, mimeType, Type.DOCUMENT_TEXT_DETECTION);
List<AnnotateImageResponse> annotateImageResponses = response.getResponsesList();
if (annotateImageResponses.isEmpty()) {
throw new CloudVisionException(EMPTY_RESPONSE_ERROR_MESSAGE);
}
List<String> result = annotateImageResponses.stream().map(annotateImageResponse -> annotateImageResponse.getFullTextAnnotation().getText()).collect(Collectors.toList());
if (result.isEmpty() && response.getError().getCode() != Code.OK.getNumber()) {
throw new CloudVisionException(response.getError().getMessage());
}
return result;
}
use of com.google.cloud.vision.v1.ProductSearchResults.Result in project TweetwallFX by TweetWallFX.
the class GoogleVisionCache method load.
private Map<String, ImageContentAnalysis> load(final Stream<String> imageUris) throws IOException {
if (null == getClient()) {
return Collections.emptyMap();
}
final List<AnnotateImageRequest> requests = imageUris.filter(Objects::nonNull).distinct().map(this::createImageRequest).peek(air -> LOG.info("Prepared {}", air)).collect(Collectors.toList());
if (requests.isEmpty()) {
return Collections.emptyMap();
}
LOG.info("Executing analysis for {} AnnotateImageRequests", requests.size());
final BatchAnnotateImagesResponse batchResponse = getClient().batchAnnotateImages(requests);
final Iterator<AnnotateImageResponse> itResponse = batchResponse.getResponsesList().iterator();
final Iterator<AnnotateImageRequest> itRequest = requests.iterator();
final Map<String, ImageContentAnalysis> result = new LinkedHashMap<>(requests.size());
while (itRequest.hasNext() && itResponse.hasNext()) {
final AnnotateImageRequest request = itRequest.next();
final AnnotateImageResponse response = itResponse.next();
final String uri = request.getImage().getSource().getImageUri();
final ImageContentAnalysis ica = new ImageContentAnalysis(response);
LOG.info("Image('{}') was evaluated as {}", uri, ica);
result.put(uri, ica);
cache.put(uri, ica);
}
if (itRequest.hasNext()) {
throw new IllegalStateException("There are still annotate Responses available!");
} else if (itRequest.hasNext()) {
throw new IllegalStateException("There are still annotate Requests available!");
} else {
return Collections.unmodifiableMap(result);
}
}
use of com.google.cloud.vision.v1.ProductSearchResults.Result in project java-vision by googleapis.
the class ProductSearch method getSimilarProductsFile.
// [START vision_product_search_get_similar_products]
/**
* Search similar products to image in local file.
*
* @param projectId - Id of the project.
* @param computeRegion - Region name.
* @param productSetId - Id of the product set.
* @param productCategory - Category of the product.
* @param filePath - Local file path of the image to be searched
* @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
* color = blue) AND style = kids It will search on all products with the following labels:
* color:red AND style:kids color:blue AND style:kids
* @throws IOException - on I/O errors.
*/
public static void getSimilarProductsFile(String projectId, String computeRegion, String productSetId, String productCategory, String filePath, String filter) throws IOException {
try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
// Get the full path of the product set.
String productSetPath = ProductSearchClient.formatProductSetName(projectId, computeRegion, productSetId);
// Read the image as a stream of bytes.
File imgPath = new File(filePath);
byte[] content = Files.readAllBytes(imgPath.toPath());
// Create annotate image request along with product search feature.
Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
// The input image can be a HTTPS link or Raw image bytes.
// Example:
// To use HTTP link replace with below code
// ImageSource source = ImageSource.newBuilder().setImageUri(imageUri).build();
// Image image = Image.newBuilder().setSource(source).build();
Image image = Image.newBuilder().setContent(ByteString.copyFrom(content)).build();
ImageContext imageContext = ImageContext.newBuilder().setProductSearchParams(ProductSearchParams.newBuilder().setProductSet(productSetPath).addProductCategories(productCategory).setFilter(filter)).build();
AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().addFeatures(featuresElement).setImage(image).setImageContext(imageContext).build();
List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);
// Search products similar to the image.
BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);
List<Result> similarProducts = response.getResponses(0).getProductSearchResults().getResultsList();
System.out.println("Similar Products: ");
for (Result product : similarProducts) {
System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
System.out.println(String.format("Product display name: %s", product.getProduct().getDisplayName()));
System.out.println(String.format("Product description: %s", product.getProduct().getDescription()));
System.out.println(String.format("Score(Confidence): %s", product.getScore()));
System.out.println(String.format("Image name: %s", product.getImage()));
}
}
}
use of com.google.cloud.vision.v1.ProductSearchResults.Result in project java-vision by googleapis.
the class Detect method detectDocumentsGcs.
// [END vision_fulltext_detection_gcs]
// [START vision_text_detection_pdf_gcs]
/**
* Performs document text OCR with PDF/TIFF as source files on Google Cloud Storage.
*
* @param gcsSourcePath The path to the remote file on Google Cloud Storage to detect document
* text on.
* @param gcsDestinationPath The path to the remote file on Google Cloud Storage to store the
* results on.
* @throws Exception on errors while closing the client.
*/
public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinationPath) throws Exception {
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
List<AsyncAnnotateFileRequest> requests = new ArrayList<>();
// Set the GCS source path for the remote file.
GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsSourcePath).build();
// Create the configuration with the specified MIME (Multipurpose Internet Mail Extensions)
// types
InputConfig inputConfig = InputConfig.newBuilder().setMimeType(// Supported MimeTypes: "application/pdf", "image/tiff"
"application/pdf").setGcsSource(gcsSource).build();
// Set the GCS destination path for where to save the results.
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(gcsDestinationPath).build();
// Create the configuration for the System.output with the batch size.
// The batch size sets how many pages should be grouped into each json System.output file.
OutputConfig outputConfig = OutputConfig.newBuilder().setBatchSize(2).setGcsDestination(gcsDestination).build();
// Select the Feature required by the vision API
Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
// Build the OCR request
AsyncAnnotateFileRequest request = AsyncAnnotateFileRequest.newBuilder().addFeatures(feature).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
requests.add(request);
// Perform the OCR request
OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> response = client.asyncBatchAnnotateFilesAsync(requests);
System.out.println("Waiting for the operation to finish.");
// 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(180, TimeUnit.SECONDS).getResponsesList();
// Once the request has completed and the System.output has been
// written to GCS, we can list all the System.output files.
Storage storage = StorageOptions.getDefaultInstance().getService();
// Get the destination location from the gcsDestinationPath
Pattern pattern = Pattern.compile("gs://([^/]+)/(.+)");
Matcher matcher = pattern.matcher(gcsDestinationPath);
if (matcher.find()) {
String bucketName = matcher.group(1);
String prefix = matcher.group(2);
// Get the list of objects with the given prefix from the GCS bucket
Bucket bucket = storage.get(bucketName);
com.google.api.gax.paging.Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));
Blob firstOutputFile = null;
// List objects with the given prefix.
System.out.println("Output files:");
for (Blob blob : pageList.iterateAll()) {
System.out.println(blob.getName());
// the first two pages of the input file.
if (firstOutputFile == null) {
firstOutputFile = blob;
}
}
// Get the contents of the file and convert the JSON contents to an AnnotateFileResponse
// 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
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);
// Here we print the full text from the first page.
// 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().getText());
} else {
System.out.println("No MATCH");
}
}
}
Aggregations