|
46 | 46 | import com.google.cloud.vision.v1.ImageContext;
|
47 | 47 | import com.google.cloud.vision.v1.ImageSource;
|
48 | 48 | import com.google.cloud.vision.v1.InputConfig;
|
| 49 | +import com.google.cloud.vision.v1.LocalizedObjectAnnotation; |
49 | 50 | import com.google.cloud.vision.v1.LocationInfo;
|
50 | 51 | import com.google.cloud.vision.v1.OperationMetadata;
|
51 | 52 | import com.google.cloud.vision.v1.OutputConfig;
|
@@ -103,6 +104,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception,
|
103 | 104 | + "Commands:\n"
|
104 | 105 | + "\tfaces | labels | landmarks | logos | text | safe-search | properties"
|
105 | 106 | + "| web | web-entities | web-entities-include-geo | crop | ocr \n"
|
| 107 | + + "| object-localization \n" |
106 | 108 | + "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage "
|
107 | 109 | + "resource (gs://...)\n"
|
108 | 110 | + "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,
|
190 | 192 | } else if (command.equals("ocr")) {
|
191 | 193 | String destPath = args.length > 2 ? args[2] : "";
|
192 | 194 | 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 | + } |
193 | 201 | }
|
194 | 202 | }
|
195 | 203 |
|
@@ -1452,4 +1460,93 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio
|
1452 | 1460 | }
|
1453 | 1461 | }
|
1454 | 1462 | // [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] |
1455 | 1552 | }
|
0 commit comments