I am trying to train an Azure Custom Vision model by following the instructions on this Learn page: Quickstart: Create an image classification project with the Custom Vision client library or REST API using Java. When I finally try to run the Gradle build, I am getting the below error
Exception in thread "main" com.microsoft.azure.cognitiveservices.vision.customvision.training.models.CustomVisionErrorException: Status code 404, {"error":{"code":"404","message": "Resource not found"}}
I am getting this error specifically in the following line:
Project project=createProject(trainClient);
The createProject function which I have taken from the Learn page I linked earlier is:
public static Project createProject(CustomVisionTrainingClient trainClient)
{
Trainings trainer=trainClient.trainings();
System.out.println("Creating Project");
Project project=trainer.createProject().withName("Moon-Phases-Trainer").execute();
return project;
}
The line that is causing an issue in specific in the function is
Project project=trainer.createProject().withName("Moon-Phases-Trainer").execute();
Here's the full code for your reference:
class CustomVisionQuickstart
{
final static String trainingApiKey=System.getenv("VISION_TRAINING_KEY");
final static String trainingEndpoint=System.getenv("VISION_TRAINING_ENDPOINT");
final static String predictionApiKey=System.getenv("VISION_PREDICTION_KEY");
final static String predictionEndpoint=System.getenv("VISION_PREDICTION_ENDPOINT");
final static String predictionResourceID=System.getenv("VISION_PREDICTION_RESOURCE_ID");
private static Tag newMoonTag, waxingCrescentTag, firstQuarterTag, waxingGibbousTag,
fullTag, waningGibbousTag, thirdQuarterTag, waningCrescentTag;
public static void main(String[] args) throws InterruptedException
{
// System.out.println(trainingApiKey);
// System.out.println(trainingEndpoint);
CustomVisionTrainingClient trainClient=CustomVisionTrainingManager
.authenticate(trainingEndpoint,trainingApiKey)
.withEndpoint(trainingEndpoint);
CustomVisionPredictionClient predictor=CustomVisionPredictionManager
.authenticate(predictionEndpoint,predictionApiKey)
.withEndpoint(predictionEndpoint);
Project project=createProject(trainClient);
addTags(trainClient, project);
uploadImages(trainClient, project);
trainProject(trainClient, project);
publishIteration(trainClient, project);
testProject(predictor, project);
}
public static Project createProject(CustomVisionTrainingClient trainClient)
{
Trainings trainer=trainClient.trainings();
System.out.println("Creating Project");
Project project=trainer.createProject().withName("Moon-Phases-Trainer").execute();
return project;
}
public static void addTags(CustomVisionTrainingClient trainClient, Project project)
{
Trainings trainer = trainClient.trainings();
newMoonTag = trainer.createTag().withProjectId(project.id()).withName("New Moon").execute();
waxingCrescentTag = trainer.createTag().withProjectId(project.id()).withName("Waxing Crescent Moon").execute();
firstQuarterTag = trainer.createTag().withProjectId(project.id()).withName("First Quarter Moon").execute();
waxingGibbousTag = trainer.createTag().withProjectId(project.id()).withName("Waxing Gibbous Moon").execute();
fullTag = trainer.createTag().withProjectId(project.id()).withName("Full Moon").execute();
waningGibbousTag = trainer.createTag().withProjectId(project.id()).withName("Waning Gibbous Moon").execute();
thirdQuarterTag = trainer.createTag().withProjectId(project.id()).withName("Third Quarter Moon").execute();
waningCrescentTag = trainer.createTag().withProjectId(project.id()).withName("Waning Crescent Moon").execute();
}
public static void uploadImages(CustomVisionTrainingClient trainClient, Project project)
{
Trainings trainer=trainClient.trainings();
for (int x=0;x<4;x++)
{
String fileName="New_Moon_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,newMoonTag.id(),null);
}
for (int x=0;x<9;x++)
{
String fileName="Waxing_Crescent_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,waxingCrescentTag.id(),null);
}
for (int x=0;x<8;x++)
{
String fileName="First_Quarter_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,firstQuarterTag.id(),null);
}
for (int x=0;x<13;x++)
{
String fileName="Waxing_Gibbous_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,waxingGibbousTag.id(),null);
}
for (int x=0;x<5;x++)
{
String fileName="Full_Moon_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,fullTag.id(),null);
}
for (int x=0;x<15;x++)
{
String fileName="Waning_Gibbous_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,waningGibbousTag.id(),null);
}
for (int x=0;x<6;x++)
{
String fileName="Third_Quarter_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,thirdQuarterTag.id(),null);
}
for (int x=0;x<13;x++)
{
String fileName="Waning_Crescent_"+x+".jpg";
byte contents[]=getImage("/Moons",fileName);
addImageToProject(trainer,project,fileName,contents,waningCrescentTag.id(),null);
}
}
private static void addImageToProject(Trainings trainer, Project project, String fileName, byte[] contents, UUID tag, double[] regionValues)
{
System.out.println("Adding image: "+fileName);
ImageFileCreateEntry file=new ImageFileCreateEntry().withName(fileName).withContents(contents);
ImageFileCreateBatch batch=new ImageFileCreateBatch().withImages(Collections.singletonList(file));
if(regionValues!=null)
{
Region region=new Region().withTagId(tag).withLeft(regionValues[0]).withTop(regionValues[1]).withWidth(regionValues[2]).withHeight(regionValues[3]);
file=file.withRegions(Collections.singletonList(region));
}
else
batch = batch.withTagIds(Collections.singletonList(tag));
trainer.createImagesFromFiles(project.id(),batch);
}
private static byte[] getImage(String folder, String fileName)
{
try
{
return ByteStreams.toByteArray(CustomVisionQuickstart.class.getResourceAsStream(folder+"/"+fileName));
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public static Iteration trainProject(CustomVisionTrainingClient trainClient, Project project) throws InterruptedException
{
System.out.println("Training...");
Trainings trainer=trainClient.trainings();
Iteration iteration=trainer.trainProject(project.id(),new TrainProjectOptionalParameter());
while(iteration.status().equals("Training"))
{
System.out.println("Training Status: " + iteration.status());
Thread.sleep(1000);
iteration = trainer.getIteration(project.id(), iteration.id());
}
System.out.println("Training Status: " + iteration.status());
return iteration;
}
public static void publishIteration(CustomVisionTrainingClient trainClient, Project project) throws InterruptedException
{
Trainings trainer=trainClient.trainings();
Iteration iteration=trainProject(trainClient,project);
String publishedModelName="myMoonPhaseModel";
trainer.publishIteration(project.id(),iteration.id(),publishedModelName,predictionResourceID);
}
public static void testProject(CustomVisionPredictionClient predictor, Project project)
{
byte[] testImeage=getImage("/Moons","testImage.jpg");
String publishedModelName = "myMoonPhaseModel";
ImagePrediction results=predictor.predictions().classifyImage().withProjectId(project.id()).withPublishedName(publishedModelName).withImageData(testImeage).execute();
for (Prediction prediction : results.predictions())
System.out.println(String.format("\t%s: %.2f%%", prediction.tagName(), prediction.probability() * 100.0f));
}
}
I am trying to connect to the Azure Custom vision training API but am getting a resource not found error when I should be seeing a project being created in the vision studio. Please let me what is causing this error and how to resolve it. Thank you.