( projectId = 'YOUR_PROJECT_ID', location = 'us-central1', modelId = 'YOUR_MODEL_ID', filePath = 'path_to_local_file.jpg' )
| 15 | 'use strict'; |
| 16 | |
| 17 | function main( |
| 18 | projectId = 'YOUR_PROJECT_ID', |
| 19 | location = 'us-central1', |
| 20 | modelId = 'YOUR_MODEL_ID', |
| 21 | filePath = 'path_to_local_file.jpg' |
| 22 | ) { |
| 23 | // [START automl_vision_object_detection_predict] |
| 24 | /** |
| 25 | * TODO(developer): Uncomment these variables before running the sample. |
| 26 | */ |
| 27 | // const projectId = 'YOUR_PROJECT_ID'; |
| 28 | // const location = 'us-central1'; |
| 29 | // const modelId = 'YOUR_MODEL_ID'; |
| 30 | // const filePath = 'path_to_local_file.jpg'; |
| 31 | |
| 32 | // Imports the Google Cloud AutoML library |
| 33 | const {PredictionServiceClient} = require('@google-cloud/automl').v1; |
| 34 | const fs = require('fs'); |
| 35 | |
| 36 | // Instantiates a client |
| 37 | const client = new PredictionServiceClient(); |
| 38 | |
| 39 | // Read the file content for translation. |
| 40 | const content = fs.readFileSync(filePath); |
| 41 | |
| 42 | async function predict() { |
| 43 | // Construct request |
| 44 | // params is additional domain-specific parameters. |
| 45 | // score_threshold is used to filter the result |
| 46 | const request = { |
| 47 | name: client.modelPath(projectId, location, modelId), |
| 48 | payload: { |
| 49 | image: { |
| 50 | imageBytes: content, |
| 51 | }, |
| 52 | }, |
| 53 | params: { |
| 54 | score_threshold: '0.8', |
| 55 | }, |
| 56 | }; |
| 57 | |
| 58 | const [response] = await client.predict(request); |
| 59 | |
| 60 | for (const annotationPayload of response.payload) { |
| 61 | console.log(`Predicted class name: ${annotationPayload.displayName}`); |
| 62 | console.log( |
| 63 | `Predicted class score: ${annotationPayload.imageObjectDetection.score}` |
| 64 | ); |
| 65 | console.log('Normalized vertices:'); |
| 66 | for (const vertex of annotationPayload.imageObjectDetection.boundingBox |
| 67 | .normalizedVertices) { |
| 68 | console.log(`\tX: ${vertex.x}, Y: ${vertex.y}`); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | predict(); |
| 74 | // [END automl_vision_object_detection_predict] |
no test coverage detected