Example #1
0
        static void Main(string[] args)
        {
            var options = Options.InitializeOptions <TrainingOptions>(args);

            if (options == null)
            {
                return;
            }

            var trainingApi = new TrainingApi()
            {
                ApiKey = options.Trainingkey
            };

            if (options.BaseUri != null)
            {
                Console.WriteLine($"The default base uri is {trainingApi.BaseUri}. Changed it to {options.BaseUri}.");
                trainingApi.BaseUri = options.BaseUri;
            }

            ProjectInfo project;

            if (options.ProjectId != Guid.Empty)
            {
                if (!trainingApi.CheckIfProjectExists(options.ProjectId))
                {
                    Console.WriteLine($"Cannot get the project with id {options.ProjectId}. Please check if the Guid is right.");
                    return;
                }

                project = new ProjectInfo(trainingApi.GetProject(options.ProjectId));
            }
            else
            {
                Console.WriteLine("ProjectId is not provided. Creating a new project...");
                var projectInfo = ProjectInfo.ReadProjectInfo(options.WorkDir + options.ProjectInfoFileName);
                project = trainingApi.CreateProjectAsync(projectInfo).Result;
            }

            Console.WriteLine($"Project Id: {project.Id}");

            if (options.ImageSource != "existing")
            {
                var uploadResult = trainingApi.ReadAndUploadImagesAsync(project, options, options.AllowedTagNames).Result;
                if (!uploadResult.IsBatchSuccessful)
                {
                    Console.WriteLine("No image uploaded successfully. Please check if those images have been uploaded before.");
                    return;
                }
            }

            trainingApi.TrainProject(project.Id);

            Console.WriteLine("The classifier is now training. You can check its status in the web portal.\nPress any key to exit.");
            Console.ReadKey();
        }
Example #2
0
        private static async Task <Project> GetOrCreateProject(TrainingApi trainingApi, string name)
        {
            var projects = await trainingApi.GetProjectsAsync();

            var project = projects.Where(p => p.Name.ToUpper() == name.ToUpper()).SingleOrDefault();

            if (project == null)
            {
                project = await trainingApi.CreateProjectAsync(name);
            }

            return(project);
        }
        private async void OnAddProjectButtonClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                string  name  = this.projectNameTextBox.Text;
                Project model = await trainingApi.CreateProjectAsync(name);

                this.Projects.Add(model);
                this.projectsListView.SelectedValue = model;

                this.projectNameTextBox.Text = "";
                this.addProjectFlyout.Hide();

                this.needsTraining = true;
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure creating project");
            }
        }
        private static async Task <bool> MainAsync(string[] args)
        {
            try
            {
                trainingApi.ApiKey = CustomVisionTrainingApiKey;

                // Create project
                var projects = await trainingApi.GetProjectsAsync();

                var project = projects.Where(x => x.Name == projectName).FirstOrDefault();

                if (project == null)
                {
                    Console.WriteLine($"\nCreating project '{projectName}'");
                    project = await trainingApi.CreateProjectAsync(projectName);
                }

                // Retrieve all tags
                var tagsList = await trainingApi.GetTagsAsync(project.Id);

                #region Create Object tag
                var objectTag = tagsList.Tags.Where(x => x.Name == objectTagName).FirstOrDefault();

                if (objectTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{objectTagName}'");
                    objectTag = await trainingApi.CreateTagAsync(project.Id, objectTagName);
                }

                // add images to tag
                Console.WriteLine($"\nAdding images to tag '{objectTagName}'");
                List <ImageFileCreateEntry> imageFiles = (Directory.GetFiles(objectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    objectTag.Id
                }));

                #endregion

                #region Create Ocean tag
                var oceanTag = tagsList.Tags.Where(x => x.Name == oceanTagName).FirstOrDefault();

                if (oceanTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{oceanTagName}'");
                    oceanTag = await trainingApi.CreateTagAsync(project.Id, oceanTagName);
                }

                // add images
                Console.WriteLine($"\nAdding images to tag '{oceanTagName}'");
                imageFiles = (Directory.GetFiles(noObjectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    oceanTag.Id
                }));

                #endregion

                #region Train the classifier
                Console.WriteLine("\nTraining");
                var iteration = await trainingApi.TrainProjectAsync(project.Id);

                do
                {
                    Thread.Sleep(1000);
                    iteration = await trainingApi.GetIterationAsync(project.Id, iteration.Id);
                }while (string.Equals("training", iteration.Status, StringComparison.OrdinalIgnoreCase));

                if (!string.Equals(iteration.Status, "completed", StringComparison.OrdinalIgnoreCase))
                {
                    throw new Exception($"An error occurred training the classifier. Iteration status is {iteration.Status}");
                }

                iteration.IsDefault = true;
                await trainingApi.UpdateIterationAsync(project.Id, iteration.Id, iteration);

                #endregion

                Console.WriteLine(
                    $@"\n
Your custom vision project ID is {project.Id.ToString()}.

Copy this Guid and add it to your application settings under the name 'CustomVisionProjectId'
"
                    );

                Console.WriteLine("\nFinished. Press Enter to exit");
                Console.Read();

                return(true);
            }
            catch (Exception e)
            {
                Console.Write(e);
                Console.WriteLine("\nPress Enter to exit");
                Console.Read();

                return(false);
            }
        }