use of com.google.cloud.automl.v1.Image in project openj9 by eclipse.
the class XMLIndexReader method _createCoreImageAfterParseError.
/**
* This is like the setJ9DumpData method but is to be used when we fail to parse the file. It is meant to try to construct the
* Image object with what it was able to get
*
* @param e The cause of the error
*/
private void _createCoreImageAfterParseError(Exception e) {
Builder builder = null;
if (_stream == null) {
// extract directly from the file
builder = new Builder(_coreFile, _reader, 0, _fileResolvingAgent);
} else {
// extract using the data stream
builder = new Builder(_coreFile, _stream, 0, _fileResolvingAgent);
}
_coreFile.extract(builder);
String osType = builder.getOSType();
String cpuType = builder.getCPUType();
String cpuSubType = builder.getCPUSubType();
long creationTime = builder.getCreationTime();
_coreImage = new Image(osType, null, cpuType, cpuSubType, 0, 0, creationTime);
Iterator spaces = builder.getAddressSpaces();
while (spaces.hasNext()) {
ImageAddressSpace addressSpace = (ImageAddressSpace) spaces.next();
// Set all processes as having invalid runtimes
for (Iterator processes = addressSpace.getProcesses(); processes.hasNext(); ) {
ImageProcess process = (ImageProcess) processes.next();
process.runtimeExtractionFailed(e);
}
_coreImage.addAddressSpace(addressSpace);
}
}
use of com.google.cloud.automl.v1.Image in project java-vision by googleapis.
the class ITSystemTest method detectLandmarksUrlTest.
@Test
public void detectLandmarksUrlTest() throws Exception {
ImageSource imgSource = ImageSource.newBuilder().setImageUri(SAMPLE_URI + "landmark/pofa.jpg").build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
List<String> actual = new ArrayList<>();
int tryCount = 0;
int maxTries = 3;
while (tryCount < maxTries) {
try {
actual = addResponsesToList(request);
break;
} catch (StatusRuntimeException ex) {
tryCount++;
System.out.println("retrying due to request throttling or DOS prevention...");
TimeUnit.SECONDS.sleep(30);
}
}
assertThat(actual).contains("Palace of Fine Arts");
}
use of com.google.cloud.automl.v1.Image in project java-vision by googleapis.
the class ITSystemTest method detectWebEntitiesIncludeGeoResultsTest.
@Test
public void detectWebEntitiesIncludeGeoResultsTest() throws IOException {
ByteString imgBytes = ByteString.readFrom(new FileInputStream(RESOURCES + "city.jpg"));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImageContext(imageContext).setImage(img).build();
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
List<AnnotateImageResponse> responses = response.getResponsesList();
List<String> actual = new ArrayList<>();
for (AnnotateImageResponse imgResponse : responses) {
for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
actual.add(entity.getDescription());
}
}
List<String> expectedResults = new ArrayList<>();
expectedResults.add("Skyscraper");
expectedResults.add("Suburb");
assertThat(actual).containsAnyIn(expectedResults);
}
use of com.google.cloud.automl.v1.Image in project java-vision by googleapis.
the class ITSystemTest method detectWebEntitiesIncludeGeoResultsGcsTest.
@Test
public void detectWebEntitiesIncludeGeoResultsGcsTest() {
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + "landmark/pofa.jpg").build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImageContext(imageContext).setImage(img).build();
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
List<AnnotateImageResponse> responses = response.getResponsesList();
List<String> actual = new ArrayList<>();
System.out.println("WebEntitiesGeo SIZE");
System.out.println(actual.size());
for (AnnotateImageResponse imgResponse : responses) {
for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
actual.add(entity.getDescription());
}
}
assertThat(actual).contains("The Palace Of Fine Arts");
}
use of com.google.cloud.automl.v1.Image 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());
}
}
}
Aggregations