public async void CreateImageFromUrl() { string imageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Hemlock/hemlock_1.jpg"; using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "CreateImageFromUrl", RecorderMode); using (ICustomVisionTrainingClient client = GetTrainingClient()) { var newProject = client.CreateProjectAsync(projName, projDescription, ProjectBuilderHelper.FoodDomain).Result; var tag = client.CreateTagAsync(newProject.Id, tagName).Result; var urlImages = new ImageUrlCreateEntry[] { new ImageUrlCreateEntry(imageUrl) }; var tags = new Guid[] { tag.Id }; var imageCreatedFromUrl = client.CreateImagesFromUrlsAsync(newProject.Id, new ImageUrlCreateBatch(urlImages, tags)).Result; Assert.True(imageCreatedFromUrl.IsBatchSuccessful); Assert.Equal(1, imageCreatedFromUrl.Images.Count); Assert.Equal(imageUrl, imageCreatedFromUrl.Images[0].SourceUrl); Assert.Equal("OK", imageCreatedFromUrl.Images[0].Status); Assert.NotEqual(Guid.Empty, imageCreatedFromUrl.Images[0].Image.Id); Assert.NotEqual(0, imageCreatedFromUrl.Images[0].Image.Width); Assert.NotEqual(0, imageCreatedFromUrl.Images[0].Image.Height); Assert.NotEmpty(imageCreatedFromUrl.Images[0].Image.OriginalImageUri); Assert.NotEmpty(imageCreatedFromUrl.Images[0].Image.ResizedImageUri); Assert.NotEmpty(imageCreatedFromUrl.Images[0].Image.ThumbnailUri); await client.DeleteProjectAsync(newProject.Id); } } }
public async Task <IActionResult> AcceptLearningImage(Guid projectId, AcceptLearningImageViewModel acceptLearningImageViewModel) { if (!ModelState.IsValid) { if (acceptLearningImageViewModel.MyTagId == null) { TempData["Warning"] = "You must choose a tag for the photo."; } return(RedirectToAction(nameof(ImageController.AcceptLearningImageFind))); } var imageUrl = _imageStorageService.UriFor(acceptLearningImageViewModel.ImageId); ImageUrlCreateEntry imageUrlCreateEntry = new ImageUrlCreateEntry(imageUrl, new List <Guid>() { acceptLearningImageViewModel.MyTagId }); IList <ImageUrlCreateEntry> imageUrlCreateEntries = new List <ImageUrlCreateEntry>() { imageUrlCreateEntry }; ImageUrlCreateBatch url = new ImageUrlCreateBatch(imageUrlCreateEntries); trainingApi.CreateImagesFromUrls(projectId, url); DeleteImageWaitingToConfirm(acceptLearningImageViewModel.ImageWaitingToConfirmId); TempData["Success"] = $"The image has been successfully accepted."; return(RedirectToAction(nameof(ImageController.AcceptLearningImageFind), "Image")); }
public async Task <IActionResult> Create(Guid projectId, IFormFile image, CreateViewModel createViewModel) { if (!ModelState.IsValid) { if (createViewModel.TagId == null) { TempData["Warning"] = "You must choose a tag for the photo."; } return(RedirectToAction(nameof(ImageController.Create), new { projectId })); } string imageId = null; try { if (image == null) { throw new Exception("Choose an image to send."); } using (var stream = image.OpenReadStream()) { imageId = await _imageStorageService.SaveImageAsync(stream, image.FileName); } } catch (Exception ex) { TempData["Error"] = ex.Message; return(RedirectToAction(nameof(ImageController.Create), new { projectId })); } var imageUrl = _imageStorageService.UriFor(imageId); ImageUrlCreateEntry imageUrlCreateEntry = new ImageUrlCreateEntry(imageUrl, new List <Guid>() { Guid.Parse(createViewModel.TagId) }); IList <ImageUrlCreateEntry> imageUrlCreateEntries = new List <ImageUrlCreateEntry>() { imageUrlCreateEntry }; ImageUrlCreateBatch url = new ImageUrlCreateBatch(imageUrlCreateEntries); trainingApi.CreateImagesFromUrls(projectId, url); TempData["Success"] = $"The image has been successfully uploaded."; return(RedirectToAction(nameof(ProjectController.Details), "Project", new { projectId })); }
public async void DeleteImages() { using (MockContext context = MockContext.Start(this.GetType().Name)) { HttpMockServer.Initialize(this.GetType().Name, "DeleteImages", RecorderMode); ICustomVisionTrainingClient client = GetTrainingClient(); var projectId = (await client.CreateProjectAsync(projName)).Id; string imageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg"; var urlImages = new ImageUrlCreateEntry[] { new ImageUrlCreateEntry(imageUrl) }; var result = client.CreateImagesFromUrlsAsync(projectId, new ImageUrlCreateBatch(urlImages)).Result; Assert.True(result.IsBatchSuccessful); Assert.Equal(1, result.Images.Count); await client.DeleteImagesAsync(projectId, new string[] { result.Images[0].Image.Id.ToString() }); await client.DeleteProjectAsync(projectId); } }
public static ImageUrlCreateEntry GetImageUrlEntry(string url, List <Tag> tags) { List <Guid> tagsIDs = new List <Guid>(); ImageUrlCreateEntry imageUrl; foreach (Tag t in tags) { tagsIDs.Add(t.Id); } try { imageUrl = new ImageUrlCreateEntry(url, tagsIDs); } catch (Exception e) { Console.WriteLine($"\n{e.GetType().Name}: Could not create image url entry for image '{url}'."); throw; } return(imageUrl); }
public static void uploadImages() { CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient() { ApiKey = trainingKey, Endpoint = SouthCentralUsEndpoint }; Guid projectId = new Guid(ConfigurationManager.AppSettings["CustomVisionProjectID"]); var project = trainingApi.GetProject(projectId); projectID = project.Id; foreach (String t in tags) { Images resultImages = ImageSearch(t); List <Guid> tagList = new List <Guid>(); if (t != "") { try { Tag tg = trainingApi.CreateTag(project.Id, t); tagList.Add(tg.Id); } catch (Exception e) { List <Tag> oldTagList = new List <Tag>(trainingApi.GetTags(project.Id)); foreach (Tag sampleTag in oldTagList) { if (sampleTag.Name.Equals(t)) { tagList.Add(sampleTag.Id); break; } } } } List <ImageUrlCreateEntry> imageUrlCreateEntries = new List <ImageUrlCreateEntry>(); foreach (var img in resultImages.Value) { ImageUrlCreateEntry imageUrlCreateEntry = new ImageUrlCreateEntry(img.ContentUrl, tagList); imageUrlCreateEntries.Add(imageUrlCreateEntry); } ImageUrlCreateBatch batchImages = new ImageUrlCreateBatch(imageUrlCreateEntries); trainingApi.CreateImagesFromUrls(project.Id, batchImages); var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = trainingApi.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); } }
public async Task <int> LoadWithCOCO(COCODatasetFactory imageSet, bool isDetectionModel) { var imageUrlBatches = new List <List <ImageUrlCreateEntry> >(); var imageUrlEntries = new List <ImageUrlCreateEntry>(); var tagsInBatch = new List <Guid>(); Dictionary <int, Guid> categoriesToTags = new Dictionary <int, Guid>(); int imagesIgnored = 0; foreach (var cat in imageSet.categoriesAndIds.Keys) { string name = imageSet.categoriesAndIds[cat]; var tags = configuration.tagList.Where(tag => tag.Name == name); if (tags.Any()) { categoriesToTags.Add(cat, tags.First().Id); } else { var tag = await configuration.AddTagAsync(name); categoriesToTags.Add(cat, tag.Id); } } for (int i = 0; i < imageSet.traningSet.Values.Count; i++) { var image = imageSet.traningSet.Values.ElementAt(i); List <Region> regions = null; IList <Guid> tags = null; if (isDetectionModel) { regions = new List <Region>(); foreach (var bb in image.bounding_boxes) { Region region = new Region(); region.TagId = categoriesToTags[bb.category_id]; if (!tagsInBatch.Contains(region.TagId)) { tagsInBatch.Add(region.TagId); } // normalized bounding box region.Left = bb.bboxArray[0] / image.imageWidth; region.Width = bb.bboxArray[2] / image.imageWidth; region.Top = bb.bboxArray[1] / image.imageHeight; region.Height = bb.bboxArray[3] / image.imageHeight; //x1, x2, y1, y2 //[bb[0], bb[0]+bb[2], bb[1], bb[1]+bb[3]] regions.Add(region); } if (regions.Count == 0) { continue; } } else { tags = image.bounding_boxes.Select(bb => categoriesToTags[bb.category_id]).ToList(); if (tags.Count == 0) { continue; } foreach (var t in tags) { if (!tagsInBatch.Contains(t)) { tagsInBatch.Add(t); } } } // Traning batch cannot have more than 20 different tags (across all regions) bool tooManyTags = false; if (tagsInBatch.Count > 19) { //If more than 20 tags, "rewind" one image back, we will add this image into the next batch i--; tooManyTags = true; } if (!tooManyTags) // if not too many tags with this image included, add this image { ImageUrlCreateEntry entry = isDetectionModel ? new ImageUrlCreateEntry(image.coco_url, null, regions) : new ImageUrlCreateEntry(image.coco_url, tags); imageUrlEntries.Add(entry); } // 64 is maximim batch size for Custom Vision, 20 is maximum number of distinct tags per batch // If exceeds, add this batch to the list, and start a new batch if (imageUrlEntries.Count > 63 || tooManyTags) { imageUrlBatches.Add(imageUrlEntries); tagsInBatch.Clear(); imageUrlEntries = new List <ImageUrlCreateEntry>(); } } if (imageUrlEntries.Count > 0) { imageUrlBatches.Add(imageUrlEntries); } foreach (var batch in imageUrlBatches) { if (batch.Count == 0) { continue; } var createImagesResult = await configuration.trainingApi.CreateImagesFromUrlsWithHttpMessagesAsync(configuration.project.Id, new ImageUrlCreateBatch(batch)); if (createImagesResult.Response.IsSuccessStatusCode) { if (!createImagesResult.Body.IsBatchSuccessful) { //Maybe it's actually OK or we submitted duplicates (OKDuplicate Status) var notOKImages = createImagesResult.Body.Images.Where(im => (im.Status != "OKDuplicate" && im.Status != "OK" && im.Status != "ErrorTagLimitExceed")); // Custom Vision may decline an image with too many regions, but reports it as too many tags if (notOKImages != null && notOKImages.Count() > 0) { var message = await createImagesResult.Response.Content.ReadAsStringAsync(); throw new Exception($"Failed to create a trainig image batch (batch is not successful). Result:\n {message}"); } else { var tooManyTags = createImagesResult.Body.Images.Where(im => (im.Status == "ErrorTagLimitExceed")); if (tooManyTags != null) { imagesIgnored += tooManyTags.Count(); } } } } else { var message = createImagesResult.Response.Content.ReadAsStringAsync(); throw new Exception($"Failed to create a trainig image batch ({createImagesResult.Response.ReasonPhrase})."); } } return(imagesIgnored); }
public static void uploadImages(CustomVisionTrainingClient trainingApi) { //getting the project details via projectId Guid projectId = new Guid("83867a3b-64ae-4c1d-994c-1fc75b7f0e5a"); var project = trainingApi.GetProject(projectId); projectID = project.Id; foreach (String t in tags) { Images resultImages = ImageSearch(t); List <Guid> tagList = new List <Guid>(); //Spliting the query string for getting the tags for the images string[] splitString = t.Split(new Char[] { ' ', ',', '.', '-', '\n', '\t' }); foreach (string y in splitString) { if (y.Trim() != "") { try { Tag tg = trainingApi.CreateTag(project.Id, y.Trim()); tagList.Add(tg.Id); } catch (Exception e) { //Tag is already stored in the model, so we are fetching its TagID List <Tag> oldTagList = new List <Tag>(trainingApi.GetTags(project.Id)); foreach (Tag sampleTag in oldTagList) { if (sampleTag.Name.Equals(y.Trim())) { tagList.Add(sampleTag.Id); break; } } } } } List <ImageUrlCreateEntry> imageUrlCreateEntries = new List <ImageUrlCreateEntry>(); foreach (var img in resultImages.Value) { //ImageUrl imageUrl = new ImageUrl(img.ContentUrl); ImageUrlCreateEntry imageUrlCreateEntry = new ImageUrlCreateEntry(img.ContentUrl, tagList); imageUrlCreateEntries.Add(imageUrlCreateEntry); } //Creating the images batch with their tag ids ImageUrlCreateBatch batchImages = new ImageUrlCreateBatch(imageUrlCreateEntries); trainingApi.CreateImagesFromUrls(project.Id, batchImages); //training the classifier var iteration = trainingApi.TrainProject(project.Id); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iteration.Status == "Training") { Thread.Sleep(1000); // Re-query the iteration to get it's updated status iteration = trainingApi.GetIteration(project.Id, iteration.Id); } // The iteration is now trained. Make it the default project endpoint iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); } }