Running the app for face detection
Stay organized with collections
Save and categorize content based on your preferences.
Run the app that draws boxes around detected faces in an image.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Running the app for face detection\n\nRun the app that draws boxes around 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 /** Annotates an image using the Vision API. */\n public static void main(String[] args) throws IOException, GeneralSecurityException {\n if (args.length != 2) {\n System.err.println(\"Usage:\");\n System.err.printf(\n \"\\tjava %s inputImagePath outputImagePath\\n\", FaceDetectApp.class.getCanonicalName());\n System.exit(1);\n }\n Path inputPath = Paths.get(args[0]);\n Path outputPath = Paths.get(args[1]);\n if (!outputPath.toString().toLowerCase().endsWith(\".jpg\")) {\n System.err.println(\"outputImagePath must have the file extension 'jpg'.\");\n System.exit(1);\n }\n\n FaceDetectApp app = new FaceDetectApp(getVisionService());\n List\u003cFaceAnnotation\u003e faces = app.detectFaces(inputPath, MAX_RESULTS);\n System.out.printf(\"Found %d face%s\\n\", faces.size(), faces.size() == 1 ? \"\" : \"s\");\n System.out.printf(\"Writing to file %s\\n\", outputPath);\n app.writeWithFaces(inputPath, outputPath, faces);\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 main(inputFile, outputFile) {\n const PImage = require('pureimage');\n outputFile = outputFile || 'out.png';\n const faces = await detectFaces(inputFile);\n console.log('Highlighting...');\n await highlightFaces(inputFile, faces, outputFile, PImage);\n console.log('Finished!');\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 call_user_func($imageWriteFunc[$ext], $outputImage, $outFile);\n printf('Output image written to %s' . PHP_EOL, $outFile);\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 main(input_filename, output_filename, max_results):\n with open(input_filename, \"rb\") as image:\n faces = detect_face(image, max_results)\n print(\"Found {} face{}\".format(len(faces), \"\" if len(faces) == 1 else \"s\"))\n\n print(f\"Writing to file {output_filename}\")\n # Reset the file pointer, so we can read the file again\n image.seek(0)\n highlight_faces(image, faces, output_filename)\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)."]]