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 <object> UploadImageToAzure(CustomVisionBatchImage customVisionBatchImage, Guid projectId) { CustomVisionTrainingClient trainingApi = AuthenticateTraining(customVisionBatchImage.Endpoint, customVisionBatchImage.TrainingKey); var currentProject = await trainingApi.GetProjectAsync(projectId); // Create the ImageUrlBatch to send to CustomVision List <ImageUrlCreateEntry> singleUrlEntry = new List <ImageUrlCreateEntry>(); foreach (CustomVisionImage customVisionImage in customVisionBatchImage.BatchImage) { singleUrlEntry.Add(new ImageUrlCreateEntry(customVisionImage.ImageUri)); } ImageUrlCreateBatch batchEntry = new ImageUrlCreateBatch(singleUrlEntry); var isUploaded = trainingApi.CreateImagesFromUrlsAsync(projectId, batchEntry); List <string> createdImageUris = new List <string>(); foreach (ImageCreateResult image in isUploaded.Result.Images) { createdImageUris.Add(image.Image.OriginalImageUri); } var response = new { project_id = projectId, project_name = currentProject.Name, image_count = isUploaded.Result.Images.Count, image_create_result = createdImageUris }; return(response); }
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 })); }
static int Main(string[] args) { Console.WriteLine("Custom Vision Image Trainer V1.0"); if (args.Length < 4 || args.Contains("-?")) { Console.WriteLine("Usage: customvisiontrainer.exe {custom vision account key} {custom vision project} {source image uri} {image tag(s)}"); return(1); } // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(args[0]); TrainingApi trainingApi = new TrainingApi(trainingCredentials); // Get a reference to the project and create a tag var project = trainingApi.GetProjects().First(proj => String.Equals(proj.Name, args[1], StringComparison.OrdinalIgnoreCase)); // Create the specified tag(s), if it doesn't already exist var tags = args[3].Split(';') .Select(tag => { var tagObject = trainingApi.GetTags(project.Id).Tags.FirstOrDefault(projTag => String.Equals(projTag.Name, tag, StringComparison.OrdinalIgnoreCase)); if (tagObject == null) { tagObject = trainingApi.CreateTag(project.Id, tag); } return(tagObject); }) .ToList(); // Enumerate the list of images from the specified root uri var storageUri = new UriBuilder(args[2]); var path = storageUri.Path; storageUri.Path = ""; var blobClient = new CloudBlobClient(storageUri.Uri); var pathSegments = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var container = blobClient.GetContainerReference(pathSegments[0]); var blobUris = container.ListBlobs(String.Join("/", pathSegments.Skip(1))) .Select(blobItem => blobItem.Uri) .ToList(); // Upload the images directly to the custom vision project var images = new ImageUrlCreateBatch(tags.Select(tag => tag.Id).ToList(), blobUris.Select(blobUri => blobUri.ToString()).ToList()); trainingApi.CreateImagesFromUrlsAsync(project.Id, images, new CancellationTokenSource(60000).Token).Wait(); return(0); }
private async void UploadButton_Click(object sender, RoutedEventArgs e) { if (urls.Count > 0) { ImageUrlCreateBatch createBatch = new ImageUrlCreateBatch(); ImageTagModel imageTagModel = (ImageTagModel)tagsComboBox.SelectedItem; createBatch.TagIds = new List <Guid> { imageTagModel.Id }; createBatch.Urls = urls; await MainWindow.currentWindow.UploadOnlineImage(projectId, createBatch); ProjectPage projectPage = (ProjectPage)MainWindow.currentWindow.mainFrame.Content; projectPage.RefreshTags(); Window.GetWindow(this).Close(); } }
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 static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(TrainClassifier))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(TrainClassifier) })) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var j = JObject.Parse(json); var gameId = (string)j["gameId"]; var imageUrls = j["imageUrls"].ToObject <List <string> >(); var tags = (JArray)j["tags"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; var api = new TrainingApi(new TrainingApiCredentials(ConfigManager.Instance.CustomVisionTrainingKey)); ProjectModel project = null; //Get the existing project for this game if there is one if (!string.IsNullOrEmpty(game.CustomVisionProjectId)) { try { project = api.GetProject(Guid.Parse(game.CustomVisionProjectId)); } catch (Exception) { } } //Otherwise create a new project and associate it with the game if (project == null) { project = api.CreateProject(game.Name, game.Id); game.CustomVisionProjectId = project.Id.ToString(); CosmosDataService.Instance.UpdateItemAsync <Game>(game).Wait(); } //Generate tag models for training var tagModels = new List <ImageTagModel>(); foreach (string tag in tags) { var model = api.CreateTag(project.Id, tag.Trim()); tagModels.Add(model); } //Batch the image urls that were sent up from Azure Storage (blob) var batch = new ImageUrlCreateBatch(tagModels.Select(m => m.Id).ToList(), imageUrls); var summary = api.CreateImagesFromUrls(project.Id, batch); if (!summary.IsBatchSuccessful) { return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful")); } //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); return(req.CreateResponse(HttpStatusCode.OK, true)); } catch (Exception e) { analytic.TrackException(e); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } } }
async public static Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(TrainClassifier))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(TrainClassifier) })) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var j = JObject.Parse(json); var gameId = (string)j["gameId"]; var imageUrls = j["imageUrls"].ToObject <List <string> >(); var tags = (JArray)j["tags"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; TrainingApi api = new TrainingApi { ApiKey = ConfigManager.Instance.CustomVisionTrainingKey }; Project project = null; //Get the existing project for this game if there is one if (!string.IsNullOrEmpty(game.CustomVisionProjectId)) { try { project = api.GetProject(Guid.Parse(game.CustomVisionProjectId)); } catch (Exception) { } } //Otherwise create a new project and associate it with the game if (project == null) { project = api.CreateProject($"{game.Name}_{DateTime.Now.ToString()}_{Guid.NewGuid().ToString()}", game.Id); game.CustomVisionProjectId = project.Id.ToString(); CosmosDataService.Instance.UpdateItemAsync <Game>(game).Wait(); } var tagItems = tags.Select(t => api.CreateTag(project.Id, t.ToString().Trim())); var entries = imageUrls.Select(u => new ImageUrlCreateEntry(u)).ToList(); //Batch the image urls that were sent up from Azure Storage (blob) var batch = new ImageUrlCreateBatch(entries, tagItems.Select(t => t.Id).ToList()); var summary = api.CreateImagesFromUrls(project.Id, batch); //if(!summary.IsBatchSuccessful) // return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful"); //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); var data = new Event("Training classifier"); data.Add("project", project.Name); data.Add("iteration", iteration.Id); await EventHubService.Instance.SendEvent(data); return(req.CreateResponse(HttpStatusCode.OK, true)); } catch (Exception e) { analytic.TrackException(e); var baseException = e.GetBaseException(); var operationException = baseException as HttpOperationException; var reason = baseException.Message; if (operationException != null) { var jobj = JObject.Parse(operationException.Response.Content); var code = jobj.GetValue("Code"); if (code != null && !string.IsNullOrWhiteSpace(code.ToString())) { reason = code.ToString(); } } return(req.CreateErrorResponse(HttpStatusCode.BadRequest, reason)); } } }
/// <summary> /// 上传图片 /// </summary> /// <param name="projectId"></param> /// <param name="createBatch"></param> /// <returns></returns> public Task UploadOnlineImage(Guid projectId, ImageUrlCreateBatch createBatch) { return(Task.Run(() => { trainingApi.CreateImagesFromUrls(projectId, createBatch); })); }
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); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var jobj = JObject.Parse(json); var tags = (JArray)jobj["tags"]; var term = jobj["term"].ToString(); var projectId = jobj["projectId"].ToString(); var trainingKey = jobj["trainingKey"].ToString(); var offset = 0; if (jobj["offset"] != null) { offset = (int)jobj["offset"]; } var imageUrls = await SearchForImages(term, offset); var api = new TrainingApi(new TrainingApiCredentials(trainingKey)); var project = api.GetProject(Guid.Parse(projectId)); var tagModels = new List <ImageTagModel>(); var existingTags = api.GetTags(project.Id); foreach (string tag in tags) { ImageTagModel model = existingTags.Tags.SingleOrDefault(t => t.Name == tag); if (model == null) { model = api.CreateTag(project.Id, tag.Trim()); } tagModels.Add(model); } var batch = new ImageUrlCreateBatch(tagModels.Select(m => m.Id).ToList(), imageUrls); var summary = api.CreateImagesFromUrls(project.Id, batch); //if(!summary.IsBatchSuccessful) // return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful"); //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); return(req.CreateResponse(HttpStatusCode.OK, iteration.Id)); } catch (Exception e) { var exception = e.GetBaseException(); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, exception.Message)); } async Task <List <string> > SearchForImages(string term, int offset) { var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "c2adf0e5c057447ea9e0f50cc5202251"); var uri = $"https://api.cognitive.microsoft.com/bing/v7.0/images/search?count=50&q={term}&offset={offset}"; var json = await client.GetStringAsync(uri); var jobj = JObject.Parse(json); var arr = (JArray)jobj["value"]; var list = new List <string>(); foreach (var result in arr) { list.Add(result["contentUrl"].ToString()); } return(list); } }