Skip to content

Commit 02bd4fc

Browse files
nirupa-kumarchingor13
authored andcommitted
samples: Vision GA - library update (#1213)
vision_localize_objects vision_localize_objects_gcs
1 parent 224a4cd commit 02bd4fc

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

vision/snippets/resources/puppies.jpg

280 KB
Loading

vision/snippets/src/main/java/com/example/vision/Detect.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.cloud.vision.v1.ImageContext;
4747
import com.google.cloud.vision.v1.ImageSource;
4848
import com.google.cloud.vision.v1.InputConfig;
49+
import com.google.cloud.vision.v1.LocalizedObjectAnnotation;
4950
import com.google.cloud.vision.v1.LocationInfo;
5051
import com.google.cloud.vision.v1.OperationMetadata;
5152
import com.google.cloud.vision.v1.OutputConfig;
@@ -103,6 +104,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception,
103104
+ "Commands:\n"
104105
+ "\tfaces | labels | landmarks | logos | text | safe-search | properties"
105106
+ "| web | web-entities | web-entities-include-geo | crop | ocr \n"
107+
+ "| object-localization \n"
106108
+ "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage "
107109
+ "resource (gs://...)\n"
108110
+ "Path to File:\n\tA path to the remote file on Cloud Storage (gs://...)\n"
@@ -190,6 +192,12 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception,
190192
} else if (command.equals("ocr")) {
191193
String destPath = args.length > 2 ? args[2] : "";
192194
detectDocumentsGcs(path, destPath);
195+
} else if (command.equals("object-localization")) {
196+
if (path.startsWith("gs://")) {
197+
detectLocalizedObjectsGcs(path, out);
198+
} else {
199+
detectLocalizedObjects(path, out);
200+
}
193201
}
194202
}
195203

@@ -1452,4 +1460,93 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio
14521460
}
14531461
}
14541462
// [END vision_text_detection_pdf_gcs]
1463+
1464+
// [START vision_localize_objects]
1465+
/**
1466+
* Detects localized objects in the specified local image.
1467+
*
1468+
* @param filePath The path to the file to perform localized object detection on.
1469+
* @param out A {@link PrintStream} to write detected objects to.
1470+
* @throws Exception on errors while closing the client.
1471+
* @throws IOException on Input/Output errors.
1472+
*/
1473+
public static void detectLocalizedObjects(String filePath, PrintStream out)
1474+
throws Exception, IOException {
1475+
List<AnnotateImageRequest> requests = new ArrayList<>();
1476+
1477+
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
1478+
1479+
Image img = Image.newBuilder().setContent(imgBytes).build();
1480+
AnnotateImageRequest request =
1481+
AnnotateImageRequest.newBuilder()
1482+
.addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION))
1483+
.setImage(img)
1484+
.build();
1485+
requests.add(request);
1486+
1487+
// Perform the request
1488+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
1489+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
1490+
List<AnnotateImageResponse> responses = response.getResponsesList();
1491+
1492+
// Display the results
1493+
for (AnnotateImageResponse res : responses) {
1494+
for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
1495+
out.format("Object name: %s\n", entity.getName());
1496+
out.format("Confidence: %s\n", entity.getScore());
1497+
out.format("Normalized Vertices:\n");
1498+
entity
1499+
.getBoundingPoly()
1500+
.getNormalizedVerticesList()
1501+
.forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY()));
1502+
}
1503+
}
1504+
}
1505+
}
1506+
// [END vision_localize_objects]
1507+
1508+
// [START vision_localize_objects_gcs]
1509+
/**
1510+
* Detects localized objects in a remote image on Google Cloud Storage.
1511+
*
1512+
* @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
1513+
* on.
1514+
* @param out A {@link PrintStream} to write detected objects to.
1515+
* @throws Exception on errors while closing the client.
1516+
* @throws IOException on Input/Output errors.
1517+
*/
1518+
public static void detectLocalizedObjectsGcs(String gcsPath, PrintStream out)
1519+
throws Exception, IOException {
1520+
List<AnnotateImageRequest> requests = new ArrayList<>();
1521+
1522+
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
1523+
Image img = Image.newBuilder().setSource(imgSource).build();
1524+
1525+
AnnotateImageRequest request =
1526+
AnnotateImageRequest.newBuilder()
1527+
.addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION))
1528+
.setImage(img)
1529+
.build();
1530+
requests.add(request);
1531+
1532+
// Perform the request
1533+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
1534+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
1535+
List<AnnotateImageResponse> responses = response.getResponsesList();
1536+
client.close();
1537+
// Display the results
1538+
for (AnnotateImageResponse res : responses) {
1539+
for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
1540+
out.format("Object name: %s\n", entity.getName());
1541+
out.format("Confidence: %s\n", entity.getScore());
1542+
out.format("Normalized Vertices:\n");
1543+
entity
1544+
.getBoundingPoly()
1545+
.getNormalizedVerticesList()
1546+
.forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY()));
1547+
}
1548+
}
1549+
}
1550+
}
1551+
// [END vision_localize_objects_gcs]
14551552
}

vision/snippets/src/test/java/com/example/vision/DetectIT.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,26 @@ public void testDetectDocumentsGcs() throws Exception {
373373
blob.delete();
374374
}
375375
}
376+
377+
@Test
378+
public void testDetectLocalizedObjects() throws Exception {
379+
// Act
380+
String[] args = {"object-localization", "./resources/puppies.jpg"};
381+
Detect.argsHelper(args, out);
382+
383+
// Assert
384+
String got = bout.toString();
385+
assertThat(got).contains("Dog");
386+
}
387+
388+
@Test
389+
public void testDetectLocalizedObjectsGcs() throws Exception {
390+
// Act
391+
String[] args = {"object-localization", "gs://cloud-samples-data/vision/puppies.jpg"};
392+
Detect.argsHelper(args, out);
393+
394+
// Assert
395+
String got = bout.toString();
396+
assertThat(got).contains("Dog");
397+
}
376398
}

0 commit comments

Comments
 (0)