We are trying to create a project on CustomVision using SDK. We are using the following libraries:
import { TrainingAPIClient } from "@azure/cognitiveservices-customvision-training"; import { PredictionAPIClient } from "@azure/cognitiveservices-customvision-prediction"; import { ApiKeyCredentials } from "@azure/ms-rest-js";
here are the versions:
"@azure/cognitiveservices-customvision-prediction": "^5.1.2", "@azure/cognitiveservices-customvision-training": "^5.2.0", "@azure/ms-rest-azure-js": "^2.1.0",
Unfortunately, no matter what i do, i keep getting this error:
TypeError: Expected signal to be an instanceof AbortSignal
We tried switching to "@azure/ms-rest-azure-js"; which did not help.
We tried passing AbortSignal but this did not work as well.
Current versions of these libraries do not support modern libs like core-auth so we can't use that.
Any other ideas?
The error Expected signal to be an instance of AbortSignal
typically occurs when using abort-controller
in the following format:
const controller = new AbortController();
doAsyncWork({ abortSignal: controller.signal });
controller.abort();
Note :
Refer to these git and MSDOC On the same issue .
To avoid the conflict, install the AbortSignal
library (or remove the part of the code that contains it).AbortSignal
.
npm install @azure/abort-controller
npm install abort-controller
By referring to the doc, I have executed the following code for creating a project, adding tags, uploading images, training the model, and publishing the iteration using Azure Custom Vision.
const util = require('util');
const fs = require('fs');
const TrainingApi = require("@azure/cognitiveservices-customvision-training");
const PredictionApi = require("@azure/cognitiveservices-customvision-prediction");
const msRest = require("@azure/ms-rest-js");
const trainingKey = "VISION_TRAINING_KEY";
const trainingEndpoint = "VISION_TRAINING_ENDPOINT";
const VISIONKey = "VISION_PREDICTION_KEY";
const predictionResourceId ="VISION_PREDICTION_RESOURCE_ID";
const predictionEndpoint = "VISION_PREDICTION_ENDPOINT";
const publishIterationName = "classifyModel";
const setTimeoutPromise = util.promisify(setTimeout);
(async () => {
console.log("Creating project...");
const credentials = new msRest.ApiKeyCredentials({ inHeader: { "Training-key": trainingKey } });
const trainer = new TrainingApi.TrainingAPIClient(credentials, trainingEndpoint);
const sampleProject = await trainer.createProject("Sample Project");
console.log("Adding tags...");
const hemlockTag = await trainer.createTag(sampleProject.id, "Hemlock");
const cherryTag = await trainer.createTag(sampleProject.id, "Japanese Cherry");
console.log("Adding images...");
const sampleDataRoot = "Images";
let fileUploadPromises = [];
const hemlockDir = `${sampleDataRoot}/Hemlock`;
const hemlockFiles = fs.readdirSync(hemlockDir);
hemlockFiles.forEach(file => {
fileUploadPromises.push(trainer.createImagesFromData(sampleProject.id, fs.readFileSync(`${hemlockDir}/${file}`), { tagIds: [hemlockTag.id] }));
});
const cherryDir = `${sampleDataRoot}/Japanese_Cherry`;
const japaneseCherryFiles = fs.readdirSync(cherryDir);
japaneseCherryFiles.forEach(file => {
fileUploadPromises.push(trainer.createImagesFromData(sampleProject.id, fs.readFileSync(`${cherryDir}/${file}`), { tagIds: [cherryTag.id] }));
});
await Promise.all(fileUploadPromises);
console.log("Training...");
let trainingIteration = await trainer.trainProject(sampleProject.id);
console.log("Training started...");
while (trainingIteration.status == "Training") {
console.log("Training status: " + trainingIteration.status);
await setTimeoutPromise(1000, null);
trainingIteration = await trainer.getIteration(sampleProject.id, trainingIteration.id);
}
console.log("Training status: " + trainingIteration.status);
await trainer.publishIteration(sampleProject.id, trainingIteration.id, publishIterationName, predictionResourceId);
const predictor_credentials = new msRest.ApiKeyCredentials({ inHeader: { "Prediction-key": VISIONKey } });
const predictor = new PredictionApi.PredictionAPIClient(predictor_credentials, predictionEndpoint);
const testFile = fs.readFileSync(`${sampleDataRoot}/Test/test_image.jpg`);
const results = await predictor.classifyImage(sampleProject.id, publishIterationName, testFile);
console.log("Results:");
results.predictions.forEach(predictedResult => {
console.log(`\t ${predictedResult.tagName}: ${(predictedResult.probability * 100.0).toFixed(2)}%`);
});
})();