protected override bool TagSelector(TrainingModels.Tag tag, SkiVideoEntity video) { if (tag == null || video == null) { return(false); } else { return(tag.Name == video.RopeLengthM.ToString()); } }
protected override bool TagSelector(TrainingModels.Tag tag, SkiVideoEntity video) { if (tag == null || video == null) { return(false); } else { return(tag.Name == video.Skier.Trim()); } }
protected abstract bool TagSelector(TrainingModels.Tag tag, SkiVideoEntity video);
private TrainingModels.Tag CreateTag(SkiVideoEntity video, string tagName) { TrainingModels.Tag tag = trainingApi.CreateTag(ProjectId, tagName); tags.Add(tag); return(tag); }
public object Process() { if (!Directory.Exists(Options.Folder)) { Console.WriteLine($"Folder not found: {Options.Folder}"); return(null); } var tagDirectories = Directory.GetDirectories(Options.Folder); if (tagDirectories.Length == 0) { Console.WriteLine($"Not subfolders found in {Options.Folder}. Subfolders are required to define tags and associated images."); return(null); } var cropRect = new Rectangle(); if (!string.IsNullOrEmpty(Options.CropArea)) { var dims = Options.CropArea.Split(","); try { var x = int.Parse(dims[0]); var y = int.Parse(dims[1]); var w = int.Parse(dims[2]); var h = int.Parse(dims[3]); cropRect = new Rectangle(x, y, w, h); } catch { Console.WriteLine($"Unable to parse {Options.CropArea} into a rectangle. Required format is: x,y,width, height"); return(new object()); } Console.WriteLine($"Crop area is [{cropRect.Left},{cropRect.Top}], {cropRect.Width}x{cropRect.Height}"); } else { Console.WriteLine("No cropping will be performed."); } Console.WriteLine(); var settings = JsonConvert.DeserializeObject <Settings>(File.ReadAllText(Program.SETTINGS_FILE)); var trainingApi = CvService.AuthenticateTraining(settings.CvTrainingEndpoint, settings.CvTrainingKey, settings.CvPredictionKey); // Explore subdirectories var totalFileCount = 0; Console.WriteLine($"Base folder: {Options.Folder}"); foreach (var directory in tagDirectories) { var files = Directory.GetFiles(directory); totalFileCount += files.Length; Console.WriteLine($" /{directory.Split(Path.DirectorySeparatorChar).Last()}, {files.Length} image(s)"); } Console.WriteLine(" --------"); Console.WriteLine($" {totalFileCount} image(s) in total"); if (totalFileCount == 0) { Console.WriteLine("Nothing to do..."); } else if (Options.IsInteractive) { Console.WriteLine(); Console.WriteLine(Program.CONFIRM_PHRASE); Console.ReadLine(); } else { Console.WriteLine(); } var project = trainingApi.GetProject(Options.ProjectId); Console.WriteLine($"Target project is '{project.Name}'"); Console.WriteLine(); if (totalFileCount == 0) { return(new object()); } // Go through each subdirectory var fileCountFormat = string.Empty; for (int i = 0; i < totalFileCount.ToString().Length; i++) { fileCountFormat += "0"; } var fileCount = 1; foreach (var directory in tagDirectories) { var targetTagName = directory.Split(Path.DirectorySeparatorChar).Last(); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Tag targetTag = null; try { targetTag = trainingApi.GetTags(Options.ProjectId).SingleOrDefault(t => t.Name == targetTagName); // If a tag with this name does not already exist, create it if (targetTag == null) { if (targetTagName.ToLowerInvariant() == "negative") { targetTag = trainingApi.CreateTag(Options.ProjectId, targetTagName, type: "Negative"); } else { targetTag = trainingApi.CreateTag(Options.ProjectId, targetTagName); } Console.WriteLine($"Created tag '{targetTagName}' (id={targetTag.Id})"); } else { Console.WriteLine($"Reusing existing tag '{targetTag.Name}' (id={targetTag.Id})"); } } catch (CustomVisionErrorException ex) { Console.WriteLine($"Error: {ex.DetailedMessage()}"); return(new object()); } Console.WriteLine(); if (cropRect == Rectangle.Empty) { fileCount = UploadFolder(trainingApi, fileCount, directory, targetTag); } else { // Crops into temporary files //foreach (var file in Directory.GetFiles(directory)) //{ // Console.Write($"[{fileCount++.ToString(fileCountFormat)}/{totalFileCount}] Processing {Path.Combine(directory, file)}..."); // using (var image = SixLabors.ImageSharp.Image.Load(file)) // { // image.Mutate(x => x.Crop(cropRect)); // using (var stream = new MemoryStream()) // { // //image.Save(stream, new JpegEncoder() { Quality = 100 }); // var tmpFile = Path.GetTempFileName(); // try // { // var res = trainingApi.CreateImagesFromData(Options.ProjectId, stream, new List<Guid>() { targetTag.Id }); // var errorCount = PrintBatchErrors(res); // } // catch (CustomVisionErrorException ex) { Console.WriteLine($"Error: {ex.DetailedMessage()}"); } // } // } //} var tempDir = Path.Combine(Path.GetTempPath(), targetTagName); if (Directory.Exists(tempDir) && Options.IsInteractive) { Console.WriteLine($"Temporary folder {tempDir} already exists."); Console.WriteLine(Program.CONFIRM_PHRASE); Console.ReadLine(); } else { Directory.CreateDirectory(tempDir); Console.Write($"Created temporary folder {tempDir}"); } foreach (var file in Directory.GetFiles(directory)) { Console.Write($"Cropping {Path.Combine(directory.Split(Path.DirectorySeparatorChar).Last(), file)}..."); using (var image = SixLabors.ImageSharp.Image.Load(file)) { image.Mutate(x => x.Crop(cropRect)); image.Save(Path.Combine(tempDir, Path.GetFileName(file)), new JpegEncoder() { Quality = 100 }); } Console.WriteLine("ok"); } fileCount = UploadFolder(trainingApi, fileCount, tempDir, targetTag); Directory.Delete(tempDir, true); Console.WriteLine($"Deleted {tempDir}"); } } return(new object()); }
private int UploadFolder(CustomVisionTrainingClient trainingApi, int fileCount, string directory, Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Tag targetTag) { Console.WriteLine($"Batch uploading content from {directory}...."); var imageFiles = Directory.GetFiles(directory).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); try { while (imageFiles.Any()) { var batchSize = Math.Min(imageFiles.Count, BATCH_SIZE); var ifcb = new ImageFileCreateBatch(imageFiles.Take(batchSize).ToList(), new List <Guid>() { targetTag.Id }); var res = trainingApi.CreateImagesFromFiles(Options.ProjectId, ifcb); var errorCount = PrintBatchErrors(res); imageFiles.RemoveRange(0, batchSize); fileCount += batchSize; Console.WriteLine($" {batchSize} files uploaded with {errorCount} errors ({fileCount} total)"); } } catch (CustomVisionErrorException ex) { Console.WriteLine($"Error: {ex.DetailedMessage()}"); } fileCount += imageFiles.Count; return(fileCount); }
public async Task <bool> UploadImageAsync(Stream data, string fileName, string tagName) { CustomVisionTrainingClient trainingApi = AuthenticateTraining(_configuration.TrainingEndpoint, _configuration.TrainingKey); Guid projectIdGuid = Guid.Parse(_configuration.ProjectId); Project project = await trainingApi.GetProjectAsync(projectIdGuid); IList <Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Tag> tags = await trainingApi.GetTagsAsync(projectIdGuid); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Tag existedTag = tags.SingleOrDefault(t => t.Name == tagName); if (existedTag == null) { existedTag = trainingApi.CreateTag(projectIdGuid, tagName); } ImageCreateSummary result = trainingApi.CreateImagesFromData(projectIdGuid, data, new List <Guid>() { existedTag.Id }); return(result.IsBatchSuccessful); }