|
1 | 1 | /**
|
2 |
| - * Copyright 2018, Google, Inc. |
| 2 | + * Copyright 2019, Google, Inc. |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
5 | 5 | * You may obtain a copy of the License at
|
|
15 | 15 |
|
16 | 16 | 'use strict';
|
17 | 17 |
|
18 |
| -// [START automl_quickstart] |
19 |
| -// TBD |
20 |
| -// [END automl_quickstart] |
| 18 | +async function main( |
| 19 | + projectId, |
| 20 | + computeRegion, |
| 21 | + modelId, |
| 22 | + filePath, |
| 23 | + scoreThreshold |
| 24 | +) { |
| 25 | + // [START automl_quickstart] |
| 26 | + const automl = require('@google-cloud/automl'); |
| 27 | + const fs = require('fs'); |
21 | 28 |
|
| 29 | + // Create client for prediction service. |
| 30 | + const client = new automl.PredictionServiceClient(); |
| 31 | + |
| 32 | + /** |
| 33 | + * TODO(developer): Uncomment the following line before running the sample. |
| 34 | + */ |
| 35 | + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; |
| 36 | + // const computeRegion = `region-name, e.g. "us-central1"`; |
| 37 | + // const modelId = `id of the model, e.g. “ICN723541179344731436”`; |
| 38 | + // const filePath = `local text file path of content to be classified, e.g. "./resources/flower.png"`; |
| 39 | + // const scoreThreshold = `value between 0.0 and 1.0, e.g. "0.5"`; |
| 40 | + |
| 41 | + // Get the full path of the model. |
| 42 | + const modelFullId = client.modelPath(projectId, computeRegion, modelId); |
| 43 | + |
| 44 | + // Read the file content for prediction. |
| 45 | + const content = fs.readFileSync(filePath, 'base64'); |
| 46 | + |
| 47 | + const params = {}; |
| 48 | + |
| 49 | + if (scoreThreshold) { |
| 50 | + params.score_threshold = scoreThreshold; |
| 51 | + } |
| 52 | + |
| 53 | + // Set the payload by giving the content and type of the file. |
| 54 | + const payload = {}; |
| 55 | + payload.image = {imageBytes: content}; |
| 56 | + |
| 57 | + // params is additional domain-specific parameters. |
| 58 | + // currently there is no additional parameters supported. |
| 59 | + const [response] = await client.predict({ |
| 60 | + name: modelFullId, |
| 61 | + payload: payload, |
| 62 | + params: params, |
| 63 | + }); |
| 64 | + console.log(`Prediction results:`); |
| 65 | + response.payload.forEach(result => { |
| 66 | + console.log(`Predicted class name: ${result.displayName}`); |
| 67 | + console.log(`Predicted class score: ${result.classification.score}`); |
| 68 | + }); |
| 69 | + // [END automl_quickstart] |
| 70 | +} |
| 71 | +main(...process.argv.slice(2)).catch(err => { |
| 72 | + console.error(err); |
| 73 | + process.exitCode = 1; |
| 74 | +}); |
0 commit comments