Detete vários objetos num ficheiro local (beta)
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Realize a deteção de objetos para vários objetos numa imagem através de um ficheiro local (para o lançamento beta).
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],[],[],[],null,["# Detect multiple objects in a local file (beta)\n\nPerform object detection for multiple objects in an image using on a local file (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 the specified local image.\n *\n * @param filePath The path to the file to perform localized object detection 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 detectLocalizedObjects(String filePath, PrintStream out)\n throws Exception, IOException {\n List\u003cAnnotateImageRequest\u003e requests = new ArrayList\u003c\u003e();\n\n ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));\n\n Image img = Image.newBuilder().setContent(imgBytes).build();\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\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(path):\n \"\"\"Localize objects in the local image.\n\n Args:\n path: The path to the local file.\n \"\"\"\n from google.cloud import vision_v1p3beta1 as vision\n\n client = vision.ImageAnnotatorClient()\n\n with open(path, \"rb\") as image_file:\n content = image_file.read()\n image = vision.Image(content=content)\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)."]]