检测 Cloud Storage 文件中的多个对象(Beta 版)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对 Cloud Storage 中存储的文件上的图片中的多个对象执行对象检测(Beta 版发布)。
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Detect multiple objects in a Cloud Storage file (beta)\n\nPerform object detection for multiple objects in an image on a file stored in Cloud Storage (for beta launch).\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Vision quickstart using\nclient libraries](/vision/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Vision Java API\nreference documentation](/java/docs/reference/google-cloud-vision/latest/overview).\n\n\nTo authenticate to Vision, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Detects localized objects in a remote image on Google Cloud Storage.\n *\n * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects\n * on.\n * @param out A {@link PrintStream} to write detected objects to.\n * @throws Exception on errors while closing the client.\n * @throws IOException on Input/Output errors.\n */\n public static void detectLocalizedObjectsGcs(String gcsPath, PrintStream out)\n throws Exception, IOException {\n List\u003cAnnotateImageRequest\u003e requests = new ArrayList\u003c\u003e();\n\n ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();\n Image img = Image.newBuilder().setSource(imgSource).build();\n\n AnnotateImageRequest request =\n AnnotateImageRequest.newBuilder()\n .addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION))\n .setImage(img)\n .build();\n requests.add(request);\n\n // Perform the request\n try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {\n BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);\n List\u003cAnnotateImageResponse\u003e responses = response.getResponsesList();\n client.close();\n // Display the results\n for (AnnotateImageResponse res : responses) {\n for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {\n out.format(\"Object name: %s\\n\", entity.getName());\n out.format(\"Confidence: %s\\n\", entity.getScore());\n out.format(\"Normalized Vertices:\\n\");\n entity\n .getBoundingPoly()\n .getNormalizedVerticesList()\n .forEach(vertex -\u003e out.format(\"- (%s, %s)\\n\", vertex.getX(), vertex.getY()));\n }\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Vision quickstart using\nclient libraries](/vision/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Vision Python API\nreference documentation](/python/docs/reference/vision/latest).\n\n\nTo authenticate to Vision, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def localize_objects_uri(uri):\n \"\"\"Localize objects in the image on Google Cloud Storage\n\n Args:\n uri: The path to the file in Google Cloud Storage (gs://...)\n \"\"\"\n from google.cloud import vision_v1p3beta1 as vision\n\n client = vision.ImageAnnotatorClient()\n\n image = vision.Image()\n image.source.image_uri = uri\n\n objects = client.object_localization(image=image).localized_object_annotations\n\n print(f\"Number of objects found: {len(objects)}\")\n for object_ in objects:\n print(f\"\\n{object_.name} (confidence: {object_.score})\")\n print(\"Normalized bounding polygon vertices: \")\n for vertex in object_.bounding_poly.normalized_vertices:\n print(f\" - ({vertex.x}, {vertex.y})\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=vision)."]]