Exécuter l'application pour la détection de visages
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Exécutez l'application qui dessine des cadres autour des visages détectés dans une image.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez la page suivante :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]