node.jsmachine-learninggoogle-cloud-storagegoogle-cloud-automl

How to solve , Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information


I am implementing google automl in NodeJS to predict the image level. I have created model, level and uploaded images manually. Now I want to predict level of an image using NodeJS. I wrote a function but always getting the below error,

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information

the code is below-

async function addfile() {
  console.log("add file called")
  const projectId = "project-name";
  const computeRegion = "us-central1";
  const modelId = "modelid";
  const filePath = "./src/assets/uploads/micro.jpeg";
  const scoreThreshold = "0.9";

  const client = new automl.PredictionServiceClient();
  const modelFullId = client.modelPath(projectId, computeRegion, modelId);
  try {
    const content = fs.readFileSync(filePath, 'base64');
    const params = {};
    if (scoreThreshold) {
      params.score_threshold = scoreThreshold;
    }
    const payload = {};
    payload.image = { imageBytes: content };
    console.log("try block is running")
    var [response] = await client.predict({
      name: modelFullId,
      payload: payload,
      params: params,
      keyFilename: "./src/assets/uploads/service_account_key.json"
    });
    console.log('Prediction results: ' + JSON.stringify(response));
    response.payload.forEach(result => {
      console.log('Predicted class name: ${result.displayName}');
      console.log('Predicted class score: ${result.classification.score}');
    });
  } catch (exception) {
    console.log("exception occur = " + exception);
  }
}

Any solution for that will be appreciated.


Solution

  • As mentioned by @Rakesh Saini , this error occurs when environment variables are not set or missing.The environment can be set by adding application credentials in the project and adding other required environment variables like Project ID and location.