Enviar uma solicitação de detecção facial
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Envie uma solicitação à API Cloud Vision para detectar rostos em uma imagem.
Mais informações
Para ver a documentação detalhada que inclui este exemplo de código, consulte:
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,["# Send a request for face detection\n\nSend a request to the Cloud Vision API to detected faces in an image.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Face detection tutorial](/vision/docs/face-tutorial)\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 /** Gets up to {@code maxResults} faces for an image stored at {@code path}. */\n public List\u003cFaceAnnotation\u003e detectFaces(Path path, int maxResults) throws IOException {\n byte[] data = Files.readAllBytes(path);\n\n AnnotateImageRequest request =\n new AnnotateImageRequest()\n .setImage(new Image().encodeContent(data))\n .setFeatures(\n ImmutableList.of(\n new Feature().setType(\"FACE_DETECTION\").setMaxResults(maxResults)));\n Vision.Images.Annotate annotate =\n vision\n .images()\n .annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));\n // Due to a bug: requests to Vision API containing large images fail when GZipped.\n annotate.setDisableGZipContent(true);\n\n BatchAnnotateImagesResponse batchResponse = annotate.execute();\n assert batchResponse.getResponses().size() == 1;\n AnnotateImageResponse response = batchResponse.getResponses().get(0);\n if (response.getFaceAnnotations() == null) {\n throw new IOException(\n response.getError() != null\n ? response.getError().getMessage()\n : \"Unknown error getting image annotations\");\n }\n return response.getFaceAnnotations();\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[Vision quickstart using\nclient libraries](/vision/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Vision Node.js API\nreference documentation](https://googleapis.dev/nodejs/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 async function detectFaces(inputFile) {\n // Make a call to the Vision API to detect the faces\n const request = {image: {source: {filename: inputFile}}};\n const results = await client.faceDetection(request);\n const faces = results[0].faceAnnotations;\n const numFaces = faces.length;\n console.log(`Found ${numFaces} face${numFaces === 1 ? '' : 's'}.`);\n return faces;\n }\n\n### PHP\n\n\nBefore trying this sample, follow the PHP setup instructions in the\n[Vision quickstart using\nclient libraries](/vision/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Vision PHP API\nreference documentation](/php/docs/reference/cloud-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 # annotate the image\n // $path = 'path/to/your/image.jpg'\n $image = file_get_contents($path);\n $response = $imageAnnotator-\u003efaceDetection($image);\n $faces = $response-\u003egetFaceAnnotations();\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 detect_face(face_file, max_results=4):\n \"\"\"Uses the Vision API to detect faces in the given file.\n\n Args:\n face_file: A file-like object containing an image with faces.\n\n Returns:\n An array of Face objects with information about the picture.\n \"\"\"\n client = vision.ImageAnnotatorClient()\n\n content = face_file.read()\n image = vision.Image(content=content)\n\n return client.face_detection(image=image, max_results=max_results).face_annotations\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)."]]