private void btnCreate_Click(object sender, RoutedEventArgs e) { // Create a new project Log("Creating new project:"); project = trainingApi.CreateProject("planogram3"); Log($"Project {project.Name} - {project.Id} created "); // Make two tags in the new project validTag = trainingApi.CreateTag(project.Id, "valid"); invalidTag = trainingApi.CreateTag(project.Id, "invalid"); Log($"Tags {validTag.Name} - {invalidTag.Name} created "); Log($"Creation Completed"); }
/// <summary> /// Train a Model given a set of trainings /// </summary> /// <param name="project"></param> /// <param name="trainings"></param> /// <param name="token"></param> /// <returns></returns> public void TrainModel(ref ProjectModel project, IList <Models.Training> trainings) { // Save all Training Tasks to be performed //var tagCreationTasks = (from training in trainings // from tag in training.Tags // select TrainingApi.CreateTagAsync(project.Id, tag.ToString(), cancellationToken: token)). // Cast<Task>().ToList(); var tagNames = trainings.Select(t => t.TagName).Distinct(); var tagNameToId = new Dictionary <string, Guid>(); foreach (var tagName in tagNames) { var tagModel = TrainingApi.CreateTag(project.Id, tagName); tagNameToId.Add(tagName, tagModel.Id); } // Execute Trainings //await Task.WhenAny(tagCreationTasks); foreach (var training in trainings) { TrainingApi.CreateImagesFromData(project.Id, training.ImageFileStream, new List <string> { tagNameToId[training.TagName].ToString() }); } //var imageCreationTasks = (from training in trainings // from tag in training.Tags // select TrainingApi.CreateImagesFromDataAsync(project.Id, training.ImageFileStream, new List<string> { tag.ToString() }, cancellationToken: token)).Cast<Task>().ToList(); //await Task.WhenAny(imageCreationTasks); var iterationModel = TrainingApi.TrainProject(project.Id); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iterationModel.Status == "Training") { Thread.Sleep(1000); // Re-query the iteration to get it's updated status iterationModel = TrainingApi.GetIteration(project.Id, iterationModel.Id); } iterationModel.IsDefault = true; // Complete Training TrainingApi.UpdateIteration(project.Id, iterationModel.Id, iterationModel); }
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 static void UploadTrainingData(Guid projectId, TrainingApi trainingApi) { Console.WriteLine("Uploading images"); foreach (var tagName in new[] { "surface-pro", "surface-studio" }) { var tag = trainingApi.CreateTag(projectId, tagName); var images = Directory.GetFiles($@"..\..\..\..\dataset\Training\{tagName}") .Select(f => new ImageFileCreateEntry(Path.GetFileName(f), File.ReadAllBytes(f))) .ToList(); var batch = new ImageFileCreateBatch(images, new List <Guid> { tag.Id }); trainingApi.CreateImagesFromFiles(projectId, batch); } }
private static void CreateTags(TrainingApi trainingApi, ProjectModel project, List <string> tags, string tagName, ImageTagListModel tagdata) { bool foundtag = false; foreach (var tag in tagdata.Tags) { if (tag.Name == tagName) { tags.Add(tag.Id.ToString()); foundtag = true; break; } } if (!foundtag) { var tagId = trainingApi.CreateTag(project.Id, tagName); tagCount++; tags.Add(tagId.Id.ToString()); } }
public static void CreateImageFromUrls([QueueTrigger("create-image-from-urls")] CreateImageFromUrlsRequest imageCreateRequest, TraceWriter log) { log.Info($"C# Queue trigger function processed: {imageCreateRequest}"); // https://docs.microsoft.com/ja-jp/azure/cognitive-services/custom-vision-service/csharp-tutorial var CV_ProjectId = Environment.GetEnvironmentVariable("CV_ProjectId"); var CV_TrainingKey = Environment.GetEnvironmentVariable("CV_TrainingKey"); //var CV_PredictionKey = Environment.GetEnvironmentVariable("CV_PredictionKey"); var trainingApi = new TrainingApi() { ApiKey = CV_TrainingKey }; var projectId = Guid.Parse(CV_ProjectId); //var project = new ProjectInfo(trainingApi.GetProject(Guid.Parse(CV_ProjectId))); var requestTags = imageCreateRequest.Tags.ToList(); var existTags = trainingApi.GetTags(projectId); var nonExistTagNames = requestTags.Except(existTags.Select(x => x.Name)); var tagIds = existTags.Where(x => requestTags.Contains(x.Name)).Select(x => x.Id).ToList(); nonExistTagNames.ToList().ForEach(tagName => { // XXX 同時に同じタグ名を指定された複数のキューを処理すると、重複してタグ作成してしまう。 // 現状、重複エラー後のリトライで処理 var tag = trainingApi.CreateTag(projectId, tagName); tagIds.Add(tag.Id); }); var images = new List <ImageUrlCreateEntry>() { new ImageUrlCreateEntry() { Url = imageCreateRequest.Url } }; trainingApi.CreateImagesFromUrls(Guid.Parse(CV_ProjectId), new ImageUrlCreateBatch(images, tagIds)); }
public void Classify() { Console.WriteLine("thread starting for {0}", f); WebClient client = new WebClient(); var thisFoodTag = trainingApi.CreateTag(project.Id, f); Console.WriteLine("training for {0}", f); SearchResult res = BingImageSearch(f); SearchResultJson result = JsonConvert.DeserializeObject <SearchResultJson>(res.jsonResult); List <Stream> ms = new List <Stream>(); Console.WriteLine("downloading {0} images", result.value.Count); foreach (var g in result.value) { string img = g.thumbnailUrl; /*try * {*/ Console.WriteLine("url: {0}", img); Stream s = new MemoryStream(client.DownloadData(img)); Console.WriteLine("{0} downloaded ok", img); trainingApi.CreateImagesFromData(project.Id, s, new List <String> { thisFoodTag.Id.ToString() }); //trainingApi.TrainProject(project.Id); /*} * catch (Exception ex) * { * Console.WriteLine("errored for {0}", img); * }*/ } Console.WriteLine("download OK, training"); Console.WriteLine("training done for {0}", f); }
static void Main(string[] args) { // Agregue su clave de entrenamiento desde la página de configuración del portale string trainingKey = "8f15a89a49a44979bf478d9391a45cd6"; // Crea el Api, pasando la clave de entrenamiento TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My New Project"); // Crea un nuevo proyecto var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); // Las imágenes se pueden cargar de a una por vez foreach (var image in hemlockImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { hemlockTag.Id.ToString() }); } } // O subido en un solo lote var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { japaneseCherryTag.Id })); // Ahora hay imágenes con etiquetas que comienzan a entrenar el proyecto Console.WriteLine("\tTraining"); var iteration = trainingApi.TrainProject(project.Id); // La iteración devuelta estará en progreso, y se puede consultar periódicamente para ver cuándo se completó while (iteration.Status == "Training") { Thread.Sleep(1000); // Vuelva a consultar la iteración para obtener su estado actualizado iteration = trainingApi.GetIteration(project.Id, iteration.Id); } // La iteración ahora está entrenada. Convertirlo en el punto final predeterminado del proyecto iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine("Done!\n"); // Ahora hay un punto final entrenado, se puede usar para hacer una predicción // Agregue su clave de predicción desde la página de configuración del portal // La clave de predicción se usa en lugar de la clave de entrenamiento cuando se hacen predicciones string predictionKey = "559018cc3d434cef8095da2e8b8dd30c"; // Crear un punto final de predicción, pasando la clave de predicción obtenida PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Hacer una predicción contra el nuevo proyecto Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Pasa el cursor sobre cada predicción y escribe los resultados foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
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> /// 创建Tag /// </summary> /// <param name="projectId"></param> /// <param name="tagName"></param> /// <returns></returns> public ImageTagModel NewTag(Guid projectId, string tagName) { return(trainingApi.CreateTag(projectId, tagName)); }
static void Main(string[] args) { // Add your training key from the settings page of the portal string trainingKey = "d34ef84918894544889d7136f32d4e67"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Getting Project or create project Console.WriteLine("Getting project"); var project = trainingApi.GetProject(new Guid("06d4aeff-fb9d-453f-8912-5b2e89d1f1d4")); //var project = trainingApi.CreateProject("MurdersWeapons"); // Make two tags in the new project var batTag = trainingApi.CreateTag(project.Id, "Bat"); var candleTag = trainingApi.CreateTag(project.Id, "Candle"); var flashlightTag = trainingApi.CreateTag(project.Id, "Flashlight"); var gitarTag = trainingApi.CreateTag(project.Id, "Gitar"); var hammerTag = trainingApi.CreateTag(project.Id, "Hammer"); var screwdriverTag = trainingApi.CreateTag(project.Id, "Screwdriver"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); var imageFiles = batImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { batTag.Id })); imageFiles = candleImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { candleTag.Id })); imageFiles = flashlightImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { flashlightTag.Id })); imageFiles = gitarImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { gitarTag.Id })); imageFiles = hammerImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { hammerTag.Id })); imageFiles = screwdriverImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { screwdriverTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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); Console.WriteLine("Done!\n"); Console.ReadKey(); }
static void Main(string[] args) { // You can either add your training key here, pass it on the command line, or type it in when the program runs string trainingKey = GetTrainingKey(trainingKeyString, args); // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject(projectName); // Create some tags, you need at least two var tag1 = trainingApi.CreateTag(project.Id, "tag1"); var tag2 = trainingApi.CreateTag(project.Id, "tag2"); // Add some images to the tags Console.Write("\n\tProcessing images"); // Upload using the path to the images, a reference to the training API, a ference to your project and a tag UploadImages(@"..\..\..\Images\1", trainingApi, project, new List <string>() { tag1.Id.ToString() }); UploadImages(@"..\..\..\Images\1", trainingApi, project, new List <string>() { tag2.Id.ToString() }); // Or uploaded in a single batch //trainingApi.CreateImagesFromData(project.Id, japaneseCherryImages, new List<Guid>() { japaneseCherryTag.Id }); // Now there are images with tags start training the project Console.WriteLine("\tStarting training"); IterationModel iteration = null; try { iteration = trainingApi.TrainProject(project.Id); } catch (Exception e) { Console.WriteLine($"Trainig could not be completed. Error: {e.Message}"); } if (iteration != null) { // 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); } Console.WriteLine($"\tFinished training iteration {iteration.Id}"); // The iteration is now trained. Make it the default project endpoint iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Get the prediction key, which is used in place of the training key when making predictions //var account = trainingApi.GetAccountInfo(); //var predictionKey = account.Keys.PredictionKeys.PrimaryKey; //// Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key //PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); //PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); //// Make a prediction against the new project //Console.WriteLine("Making a prediction:"); //var result = endpoint.PredictImage(project.Id, testImage); //// Loop over each prediction and write out the results //foreach (var c in result.Predictions) //{ // Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); //} } Console.ReadKey(); }
private static void CreateTheModel(string trainingSetPath, Project project) { var trainingKey = ConfigurationManager.AppSettings["CustomVision_TrainingKey"]; var trainingApi = new TrainingApi() { ApiKey = trainingKey }; var trainingModel = new List <Model>(); bool performImageAugmentation = true; try { performImageAugmentation = Convert.ToBoolean(ConfigurationManager.AppSettings["PerformImageAugmentation"]); } catch { } int widthHeight = 299; try { widthHeight = Convert.ToInt32(ConfigurationManager.AppSettings["WidthHeight"]); } catch { } int padImages = 10; try { padImages = Convert.ToInt32(ConfigurationManager.AppSettings["PadImages"]); } catch { } var trainingSet = Directory.GetDirectories(trainingSetPath); foreach (var subdirectory in trainingSet) { var dir = new DirectoryInfo(subdirectory); var name = dir.Name; Console.WriteLine($"\tAdding Tag - {name}"); var tag = trainingApi.CreateTag(project.Id, name); var images = Directory.GetFiles($"{subdirectory}").Select(f => { trainingModel.Add(new Model() { Label = name, Path = f }); return(new MemoryStream(File.ReadAllBytes(f))); }).ToList(); foreach (var image in images) { try { Console.WriteLine($"\tUploading image with tag: {tag.Name}"); if (performImageAugmentation) { // flip from RGB to BGR System.Drawing.Bitmap img = new System.Drawing.Bitmap(image); Image <Bgr, byte> ogImg = new Image <Bgr, byte>(img); // perform Intensity Image Equalization Image <Ycc, byte> ycrcb = ogImg.Convert <Ycc, byte>(); ycrcb._EqualizeHist(); ogImg = ycrcb.Convert <Bgr, byte>(); //replace original image with equalized image int top = 0; int bottom = 0; int left = 0; int right = 0; if (img.Width != img.Height) { // we need to pad our image if the width and height aren't set already in a previous smart crop step if (img.Width < img.Height) { int dif = img.Height - img.Width; left = dif / 2; right = dif - left; } if (img.Height < img.Width) { int dif = img.Width - img.Height; top = dif / 2; bottom = dif - top; } } if (padImages > 0) { top += padImages; bottom += padImages; left += padImages; right += padImages; } if ((top > 0) || (bottom > 0) || (left > 0) || (right > 0)) { Image <Bgr, byte> padImg = new Image <Bgr, byte>(img.Width + left + right, img.Height + top + bottom); CvInvoke.CopyMakeBorder(ogImg, padImg, top, bottom, left, right, Emgu.CV.CvEnum.BorderType.Constant, new MCvScalar(255, 255, 255)); // pad the image with a white background ogImg = padImg; } if (ogImg.Width != widthHeight) { // resize the padded image ogImg = ogImg.Resize(widthHeight, widthHeight, Emgu.CV.CvEnum.Inter.Linear); } trainStream(ogImg.ToBitmap(), trainingApi, project.Id, tag.Id.ToString()); for (var i = 0; i < 3; i++) { if ((new Random().Next(1, 11)) <= 5) { // 50% of the time flip the image horizontally before rotation ogImg = ogImg.Flip(Emgu.CV.CvEnum.FlipType.Horizontal); } trainStream(ogImg.Rotate(new Random().Next(-45, 45), new Bgr(255, 255, 255)).ToBitmap(), trainingApi, project.Id, tag.Id.ToString()); // rotate with a white background } } else { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { tag.Id.ToString() }); } } catch (Exception e) { //kill exception and carry on Console.WriteLine(e); } } } try { using (TextWriter writer = new StreamWriter($"{trainingSetPath}\\trainingModel.csv")) { var csv = new CsvWriter(writer); csv.WriteRecords(trainingModel); } } catch (Exception e) { Console.WriteLine(e); } }
static void Main(string[] args) { // Create the Api, passing in the training key string trainingKey = ""; if (String.IsNullOrEmpty(trainingKey)) { Console.WriteLine("The custom vision training key needs to be set."); Environment.Exit(1); } TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Find the object detection project var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); var project = trainingApi.GetProjects().FirstOrDefault(p => p.Settings.DomainId == objDetectionDomain.Id); string modelPath = Path.Combine(Environment.CurrentDirectory, "..", "model"); string dataPath = Path.Combine(modelPath, "dataset.json"); string imagesPath = Path.Combine(modelPath, "images"); var images = JsonConvert.DeserializeObject <IEnumerable <Image> >(File.ReadAllText(dataPath)); // Create tags, unless they already exist var existingTags = trainingApi.GetTags(project.Id); var tagsToImport = images .SelectMany(i => i.Tags .Select(t => t.TagName)) .Distinct() .Where(t => !existingTags.Any(e => string.Compare(e.Name, t, ignoreCase: true))); if (tagsToImport.Any()) { foreach (var tag in tagsToImport) { Console.WriteLine($"Importing {tag}"); var newTag = trainingApi.CreateTag(project.Id, tag); existingTags.Add(newTag); } } // Upload images with region data, in batches of 50 while (images.Any()) { var currentBatch = images.Take(BATCH_SIZE); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var imageRef in currentBatch) { var regions = new List <Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Region>(); foreach (var region in imageRef.Regions) { regions.Add(new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Region() { Height = region.Height, Width = region.Width, Top = region.Top, Left = region.Left, TagId = existingTags.First(t => t.Name == region.TagName).Id }); } string imagePath = Path.Combine(imagesPath, string.Concat(imageRef.Id, ".png")); imageFileEntries.Add(new ImageFileCreateEntry() { Name = imagePath, Contents = File.ReadAllBytes(imagePath), Regions = regions }); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); images = images.Skip(BATCH_SIZE); } Console.WriteLine("Training data upload complete!"); }
static void Main(string[] args) { // Add your training and prediction key from the settings page of the portal string trainingKey = "<add your training key here>"; string predictionKey = "<add your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My First Project"); // Make two tags in the new project var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\\tUploading images"); LoadImagesFromDisk(); // Images are then uploaded foreach (var image in hemlockImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { hemlockTag.Id.ToString() }); } foreach (var image in japaneseCherryImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { japaneseCherryTag.Id.ToString() }); } // Now there are images with tags start training the project Console.WriteLine("\\tTraining"); 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); Console.WriteLine("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } Console.ReadKey(); }
public async Task Main() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TrainingApi trainingApi = new TrainingApi() { ApiKey = CustomVisionAPIKey }; trainingApi.HttpClient.Timeout = TimeSpan.FromMinutes(TimeoutMins); if (!QuickTest) { Console.WriteLine($"Creating Custom Vision Project: {ProjectName}"); var project = trainingApi.CreateProject(ProjectName); Console.WriteLine($"Scanning subfolders within: {ImagePath}"); List <Tag> allTags = new List <Tag>(); foreach (var folder in Directory.EnumerateDirectories(ImagePath)) { string folderName = Path.GetFileName(folder); var tagNames = folderName.Contains(",") ? folderName.Split(',').Select(t => t.Trim()).ToArray() : new string[] { folderName }; // Create tag for each comma separated value in subfolder name foreach (var tag in tagNames) { // Check we've not already created this tag from another subfolder if (!allTags.Any(t => t.Name.Equals(tag))) { Console.WriteLine($"Creating Tag: {tag}"); var imageTag = trainingApi.CreateTag(project.Id, tag); allTags.Add(imageTag); } } } foreach (var currentFolder in Directory.EnumerateDirectories(ImagePath)) { string folderName = Path.GetFileName(currentFolder); var tagNames = folderName.Contains(",") ? folderName.Split(',').Select(t => t.Trim()).ToArray() : new string[] { folderName }; try { // Load the images to be uploaded from disk into memory Console.WriteLine($"Uploading: {ImagePath}\\{folderName} images..."); var images = Directory.GetFiles(currentFolder).ToList(); var folderTags = allTags.Where(t => tagNames.Contains(t.Name)).Select(t => t.Id).ToList(); var imageFiles = images.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img), folderTags)).ToList(); var imageBatch = new ImageFileCreateBatch(imageFiles); var summary = await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles)); // List any images that didn't make it foreach (var imageResult in summary.Images.Where(i => !i.Status.Equals("OK"))) { Console.WriteLine($"{ImagePath}\\{folderName}\\{imageResult.SourceUrl}: {imageResult.Status}"); } Console.WriteLine($"Uploaded {summary.Images.Where(i => i.Status.Equals("OK")).Count()}/{images.Count()} images successfully from {ImagePath}\\{folderName}"); } catch (Exception exp) { Console.WriteLine($"Error processing {currentFolder}: {exp.Source}:{exp.Message}"); } } try { // Train CV model and set iteration to the default Console.WriteLine($"Training model"); var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status.Equals("Training")) { Thread.Sleep(1000); iteration = trainingApi.GetIteration(project.Id, iteration.Id); Console.WriteLine($"Model status: {iteration.Status}"); } if (iteration.Status.Equals("Completed")) { iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine($"Iteration: {iteration.Id} set as default"); } else { Console.WriteLine($"Iteration status: {iteration.Status}"); } } catch (Exception exp) { Console.WriteLine($"Error training model (check you have at least 5 images per tag and 2 tags)"); Console.WriteLine($"Error {exp.Source}: {exp.Message}"); } } else { // Quick test existing (trained) model Console.WriteLine($"Custom Vision Quick test: {ProjectName} with image {ImagePath}"); // Retrieve CV project var projects = trainingApi.GetProjects(); var project = projects.Where(p => p.Name.Equals(ProjectName)).FirstOrDefault(); if (project == null) { Console.WriteLine($"Can't find Custom Vision Project: {ProjectName}"); return; } // Read test image if (!File.Exists(ImagePath)) { Console.WriteLine($"Can't find image: {ImagePath}"); return; } var image = new MemoryStream(File.ReadAllBytes(ImagePath)); // Get the default iteration to test against and check results var iterations = trainingApi.GetIterations(project.Id); var defaultIteration = iterations.Where(i => i.IsDefault == true).FirstOrDefault(); if (defaultIteration == null) { Console.WriteLine($"No default iteration has been set"); return; } var result = trainingApi.QuickTestImage(project.Id, image, defaultIteration.Id); foreach (var prediction in result.Predictions) { Console.WriteLine($"Tag: {prediction.Tag} Probability: {prediction.Probability}"); } } // fin stopwatch.Stop(); Console.WriteLine($"Done."); Console.WriteLine($"Total time: {stopwatch.Elapsed}"); }
static void Main(string[] args) { // Add your training key from the settings page of the portal string trainingKey = "YOUR TRAINING KEY"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Find the object detection domain var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("CSharp Office", null, objDetectionDomain.Id); //var project = trainingApi.GetProject(); using (StreamReader r = new StreamReader("Office.json")) { string json = r.ReadToEnd(); Console.Write(json); var data = JObject.Parse(json); //Export Tags from code var inputTags = data["inputTags"].ToString().Split(','); foreach (string t in inputTags) { Console.WriteLine(t); trainingApi.CreateTag(project.Id, t); } // Get all tagsIDs created on custom vision and add into a dictionary Dictionary <string, Guid> imgtags = new Dictionary <string, Guid>(); foreach (Tag t in trainingApi.GetTags(project.Id)) { Console.WriteLine(t.Name + " - " + t.Id); imgtags.Add(t.Name, t.Id); } // Create Image TagIDs with normalized points Dictionary <string, double[]> imgtagdic = new Dictionary <string, double[]>(); foreach (var a in data["visitedFrames"]) { Console.WriteLine(a); try { foreach (var key in data["frames"][a.ToString()]) { double x1 = Convert.ToDouble(key["x1"].ToString()); double y1 = Convert.ToDouble(key["y1"].ToString()); double x2 = Convert.ToDouble(key["x2"].ToString()); double y2 = Convert.ToDouble(key["y2"].ToString()); int h = Convert.ToInt32(key["height"].ToString()); int w = Convert.ToInt32(key["width"].ToString()); double tleft = (double)x1 / (double)w; double ttop = (double)y1 / (double)h; double twidth = (double)(x2 - x1) / (double)w; double theight = (double)(y2 - y1) / (double)h; try { string tag = key["tags"][0].ToString(); // Defining UniqueID per tags in photo below imgtagdic.Add(imgtags[tag].ToString() + "," + a.ToString() + ',' + key["name"].ToString() + tag, new double[] { tleft, ttop, twidth, theight }); } catch { Console.WriteLine("An Error occured on imtagdic"); } } } catch { Console.WriteLine("An Error occured on json parsing"); } } // Add all images for fork var imagePath = Path.Combine("", "Office"); string[] allphotos = Directory.GetFiles(imagePath); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var key in imgtagdic) { Guid tagguid = Guid.Parse(key.Key.Split(',')[0]); var fileName = allphotos[Convert.ToInt32(key.Key.Split(',')[1])]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(tagguid, key.Value[0], key.Value[1], key.Value[2], key.Value[3]) }))); // //Tried the add list of tags //List<Guid> listtags = new List<Guid> { tagguid }; //imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), listtags, new List<Region>(new Region[] { new Region(tagguid, key.Value[0], key.Value[1], key.Value[2], key.Value[3]) }))); } Console.WriteLine("\tUpload has started!"); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); Console.WriteLine("\tUpload is done!"); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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 != "Completed") { Thread.Sleep(1000); // Re-query the iteration to get its 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); Console.WriteLine("Done!\n"); } }
public Boolean Train(string tagName, string description = null) { // Add your training & prediction key from the settings page of the portal string trainingKey = "6308b3b62b344e3f8e4170c4728deed2"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; var project = trainingApi.GetProjects().First(f => f.Name == "WA-SE-AI"); // Make two tags in the new project Tag trainTag; try { trainTag = trainingApi.GetTags(project.Id).First(f => f.Name == tagName); } catch (Exception) { trainTag = trainingApi.CreateTag(project.Id, tagName, description); } // Add some images to the tags Console.WriteLine("Start load image into memory"); List <string> TrainImages = LoadImagesFromDisk(tagName); if (TrainImages != null) { Console.WriteLine("Uploading " + TrainImages.Count + "images"); var trainImageFiles = TrainImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(trainImageFiles, new List <Guid>() { trainTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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); Console.WriteLine("Training Done!\n"); return(true); } else { Console.WriteLine("No image found!\n"); return(true); } }
static void Main(string[] args) { // Add your training key from the settings page of the portal //76af0d521ed848ce8d4c8c70b7fb1f2b //5d0b18588d934e1293008dcf75dd0606 string trainingKey = "76af0d521ed848ce8d4c8c70b7fb1f2b"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("SilverFernAI"); // Load all image folders imageLists = Directory.GetDirectories(@"..\..\..\Images").ToList(); List <Tag> imageTags = new List <Tag>(); foreach (string imageList in imageLists) { string imageClass = imageList.Split('\\').LastOrDefault(); Tag imageTag = trainingApi.CreateTag(project.Id, imageClass); imageTags.Add(imageTag); DirectoryInfo d = new DirectoryInfo(@imageList); FileInfo[] infos = d.GetFiles(); List <string> images = Directory.GetFiles(@imageList).ToList(); foreach (var image in images) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { imageTag.Id.ToString() }); } } } // // Now there are images with tags start training the project // Console.WriteLine("\tTraining"); // 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); // Console.WriteLine("Done!\n"); // // Now there is a trained endpoint, it can be used to make a prediction // // Add your prediction key from the settings page of the portal // // The prediction key is used in place of the training key when making predictions // string predictionKey = "<your key here>"; // // Create a prediction endpoint, passing in obtained prediction key // PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // // Make a prediction against the new project // Console.WriteLine("Making a prediction:"); // var result = endpoint.PredictImage(project.Id, testImage); // // Loop over each prediction and write out the results // foreach (var c in result.Predictions) // { // Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); // } // Console.ReadKey(); //} //private static void LoadImagesFromDisk() //{ // // this loads the images to be uploaded from disk into memory // hemlockImages = Directory.GetFiles(@"..\..\..\Images\Hemlock").ToList(); // japaneseCherryImages = Directory.GetFiles(@"..\..\..\Images\Japanese Cherry").ToList(); // testImage = new MemoryStream(File.ReadAllBytes(@"..\..\..\Images\Test\test_image.jpg")); //} }
static void Main(string[] args) { string chaveClassificacao = "<Chave de treinamento>"; string idProjeto = "<ID Projeto>"; // Crio o cliente para o Azure Custom View TrainingApi trainingApi = new TrainingApi() { ApiKey = chaveClassificacao }; Console.WriteLine("Iniciando as tags."); // Crio a tag shitzu Tag shitzuTag = trainingApi.CreateTag(new Guid(idProjeto), "Shitzu"); // Crio a tag poodle Tag poodleTag = trainingApi.CreateTag(new Guid(idProjeto), "Poodle"); // Carrego as imagens de shitzu string[] imagensShitzu = Directory.GetFiles(Path.Combine("Amostras", "Shitzu")).ToArray(); // Carrego as imagens de poodle string[] imagensPoodle = Directory.GetFiles(Path.Combine("Amostras", "Poodle")).ToArray(); Console.WriteLine("Cadastrando as imagens de shitzu."); // Cadastro das imagens de shitzu foreach (var imagem in imagensShitzu) { using (var stream = new MemoryStream(File.ReadAllBytes(imagem))) { trainingApi.CreateImagesFromData(new Guid(idProjeto), stream, new List <string>() { shitzuTag.Id.ToString() }); } } Console.WriteLine("Cadastrando as imagens de poodle."); // Cadastro das imagens de poodle foreach (var imagem in imagensPoodle) { using (var stream = new MemoryStream(File.ReadAllBytes(imagem))) { trainingApi.CreateImagesFromData(new Guid(idProjeto), stream, new List <string>() { poodleTag.Id.ToString() }); } } // Inicio o treinamento com as imagens cadastradas. Iteration interacao = trainingApi.TrainProject(new Guid(idProjeto)); Console.WriteLine("Treinando."); // Verifico periodicamente até concluir while (interacao.Status == "Training") { Thread.Sleep(1000); // Verifico novamente o status interacao = trainingApi.GetIteration(new Guid(idProjeto), interacao.Id); } // Agora que o treinamento está concluído, configuro como o endpoint padrão. interacao.IsDefault = true; trainingApi.UpdateIteration(new Guid(idProjeto), interacao.Id, interacao); Console.WriteLine("Finalizado."); }
private static void CreateTheModel(string trainingSetPath, Project project) { var trainingKey = ConfigurationManager.AppSettings["CustomVision_TrainingKey"]; var trainingApi = new TrainingApi() { ApiKey = trainingKey }; var trainingModel = new List <Model>(); var trainingSet = Directory.GetDirectories(trainingSetPath); foreach (var subdirectory in trainingSet) { var dir = new DirectoryInfo(subdirectory); var name = dir.Name; Console.WriteLine($"\tAdding Tag - {name}"); var tag = trainingApi.CreateTag(project.Id, name); var images = Directory.GetFiles($"{subdirectory}").Select(f => { trainingModel.Add(new Model() { Label = name, Path = f }); return(new MemoryStream(File.ReadAllBytes(f))); }).ToList(); foreach (var image in images) { try { Console.WriteLine($"\tUploading image with tag: {tag.Name}"); trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { tag.Id.ToString() }); } catch (Exception e) { //kill exception and carry on Console.WriteLine(e); } } } try { using (TextWriter writer = new StreamWriter($"{trainingSetPath}\\trainingModel.csv")) { var csv = new CsvWriter(writer); csv.WriteRecords(trainingModel); } } catch (Exception e) { Console.WriteLine(e); } }
static void Main(string[] args) { string trainingKey = GetTrainingKey(configTrainingKey, args); TrainingApi trainingApi = new TrainingApi { ApiKey = trainingKey }; Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("Bike Type"); var MbikesTag = trainingApi.CreateTag(project.Id, "Mountain"); var RbikesTag = trainingApi.CreateTag(project.Id, "Racing"); Console.WriteLine("\tUploading images"); LoadImages(); foreach (var image in bikesImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { MbikesTag.Id.ToString() }); } foreach (var image in rBikesImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { RbikesTag.Id.ToString() }); } trainingApi.CreateImagesFromData(project.Id, testImage, new List <string>() { MbikesTag.Id.ToString() }); Console.WriteLine("\tTraining"); var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status.Equals("Training")) { Thread.Sleep(1000); iteration = trainingApi.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine("Done!\n"); var predictionKey = GetPredictionKey(configPredictionKey, args); PredictionEndpoint endpoint = new PredictionEndpoint { ApiKey = predictionKey }; Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
static void Main(string[] args) { // You can either add your training key here, pass it on the command line, or type it in when the program runs string trainingKey = GetTrainingKey("<your key here>", args); // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("Car Assessment"); // Make two tags in the new project var WriteOffTag = trainingApi.CreateTag(project.Id, "WriteOff"); var DentTag = trainingApi.CreateTag(project.Id, "Dent"); // Add some images to the tags Console.WriteLine("\\tUploading images"); LoadImagesFromDisk(); // Images can be uploaded one at a time foreach (var image in WriteOffImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string> () { WriteOffTag.Id.ToString() }); } // Or uploaded in a single batch trainingApi.CreateImagesFromData(project.Id, DentImages, new List <Guid> () { DentTag.Id }); // Now there are images with tags start training the project Console.WriteLine("\\tTraining"); 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); Console.WriteLine("Done!\\n"); // Now there is a trained endpoint, it can be used to make a prediction // Get the prediction key, which is used in place of the training key when making predictions var account = trainingApi.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
private void buttonSend_Click(object sender, EventArgs e) { if (pictureBox1.BackgroundImage == null) { MessageBox.Show("Missing image or selected regions"); return; } if (currentPredictionMode == PredictionMode.Detection) { if (treeListToUpload.Count == 0) { MessageBox.Show("Missing selected regions"); return; } try { var imageFileEntries = new List <ImageFileCreateEntry>(); List <Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Region> regions = new List <Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Region>(); foreach (var item in treeListToUpload) { var tagList = trainingApi.GetTags(project.Id); Tag tag = tagList.FirstOrDefault(x => x.Name.ToLower().Equals(item.TagName.ToLower())); if (tag == null) { tag = trainingApi.CreateTag(project.Id, System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item.TagName.ToLower())); } var region = new double[] { item.BoundingBox.Left, item.BoundingBox.Top, item.BoundingBox.Width, item.BoundingBox.Height }; regions.Add(new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.Region(tag.Id, region[0], region[1], region[2], region[3])); } imageFileEntries.Add(new ImageFileCreateEntry(null, GetStream(pictureBox1.BackgroundImage).ToArray(), null, regions)); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); treeListToUpload.Clear(); pictureBox1.Invalidate(); MessageBox.Show("The image is uploaded successfully"); } catch (Exception ex) { labelInfo.Text = ex.ToString(); } } else if (currentPredictionMode == PredictionMode.Classification) { if (imageTagToUpload == "") { MessageBox.Show("Missing tag"); return; } try { var tagList = trainingApi.GetTags(project.Id); Tag tag = tagList.FirstOrDefault(x => x.Name.ToLower().Equals(imageTagToUpload.ToLower())); if (tag == null) { tag = trainingApi.CreateTag(project.Id, imageTagToUpload.ToLower()); } trainingApi.CreateImagesFromData(project.Id, GetStream(pictureBox1.BackgroundImage), new List <string>() { tag.Id.ToString() }); MessageBox.Show("The image is uploaded successfully"); } catch (Exception ex) { labelInfo.Text = ex.ToString(); } } }
public void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "d7ba782c8051443c8557ff464418949f"; //string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); //var project = trainingApi.CreateProject("My New Project"); // Make two tags in the new project var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); // Images can be uploaded one at a time foreach (var image in hemlockImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { hemlockTag.Id.ToString() }); } } // Or uploaded in a single batch var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { japaneseCherryTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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); Console.WriteLine("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Create a prediction endpoint, passing in obtained prediction key PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } Console.ReadKey(); }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "<your training key here>"; string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Find the object detection domain var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My New Project", null, objDetectionDomain.Id); // Make two tags in the new project var forkTag = trainingApi.CreateTag(project.Id, "fork"); var scissorsTag = trainingApi.CreateTag(project.Id, "scissors"); Dictionary <string, double[]> fileToRegionMap = new Dictionary <string, double[]>() { // FileName, Left, Top, Width, Height { "scissors_1", new double[] { 0.4007353, 0.194068655, 0.259803921, 0.6617647 } }, { "scissors_2", new double[] { 0.426470578, 0.185898721, 0.172794119, 0.5539216 } }, { "scissors_3", new double[] { 0.289215684, 0.259428144, 0.403186262, 0.421568632 } }, { "scissors_4", new double[] { 0.343137264, 0.105833367, 0.332107842, 0.8055556 } }, { "scissors_5", new double[] { 0.3125, 0.09766343, 0.435049027, 0.71405226 } }, { "scissors_6", new double[] { 0.379901975, 0.24308826, 0.32107842, 0.5718954 } }, { "scissors_7", new double[] { 0.341911763, 0.20714055, 0.3137255, 0.6356209 } }, { "scissors_8", new double[] { 0.231617644, 0.08459154, 0.504901946, 0.8480392 } }, { "scissors_9", new double[] { 0.170343131, 0.332957536, 0.767156839, 0.403594762 } }, { "scissors_10", new double[] { 0.204656869, 0.120539248, 0.5245098, 0.743464053 } }, { "scissors_11", new double[] { 0.05514706, 0.159754932, 0.799019635, 0.730392158 } }, { "scissors_12", new double[] { 0.265931368, 0.169558853, 0.5061275, 0.606209159 } }, { "scissors_13", new double[] { 0.241421565, 0.184264734, 0.448529422, 0.6830065 } }, { "scissors_14", new double[] { 0.05759804, 0.05027781, 0.75, 0.882352948 } }, { "scissors_15", new double[] { 0.191176474, 0.169558853, 0.6936275, 0.6748366 } }, { "scissors_16", new double[] { 0.1004902, 0.279036, 0.6911765, 0.477124184 } }, { "scissors_17", new double[] { 0.2720588, 0.131977156, 0.4987745, 0.6911765 } }, { "scissors_18", new double[] { 0.180147052, 0.112369314, 0.6262255, 0.6666667 } }, { "scissors_19", new double[] { 0.333333343, 0.0274019931, 0.443627447, 0.852941155 } }, { "scissors_20", new double[] { 0.158088237, 0.04047389, 0.6691176, 0.843137264 } }, { "fork_1", new double[] { 0.145833328, 0.3509314, 0.5894608, 0.238562092 } }, { "fork_2", new double[] { 0.294117659, 0.216944471, 0.534313738, 0.5980392 } }, { "fork_3", new double[] { 0.09191177, 0.0682516545, 0.757352948, 0.6143791 } }, { "fork_4", new double[] { 0.254901975, 0.185898721, 0.5232843, 0.594771266 } }, { "fork_5", new double[] { 0.2365196, 0.128709182, 0.5845588, 0.71405226 } }, { "fork_6", new double[] { 0.115196079, 0.133611143, 0.676470637, 0.6993464 } }, { "fork_7", new double[] { 0.164215669, 0.31008172, 0.767156839, 0.410130739 } }, { "fork_8", new double[] { 0.118872553, 0.318251669, 0.817401946, 0.225490168 } }, { "fork_9", new double[] { 0.18259804, 0.2136765, 0.6335784, 0.643790841 } }, { "fork_10", new double[] { 0.05269608, 0.282303959, 0.8088235, 0.452614367 } }, { "fork_11", new double[] { 0.05759804, 0.0894935, 0.9007353, 0.3251634 } }, { "fork_12", new double[] { 0.3345588, 0.07315363, 0.375, 0.9150327 } }, { "fork_13", new double[] { 0.269607842, 0.194068655, 0.4093137, 0.6732026 } }, { "fork_14", new double[] { 0.143382356, 0.218578458, 0.7977941, 0.295751631 } }, { "fork_15", new double[] { 0.19240196, 0.0633497, 0.5710784, 0.8398692 } }, { "fork_16", new double[] { 0.140931368, 0.480016381, 0.6838235, 0.240196079 } }, { "fork_17", new double[] { 0.305147052, 0.2512582, 0.4791667, 0.5408496 } }, { "fork_18", new double[] { 0.234068632, 0.445702642, 0.6127451, 0.344771236 } }, { "fork_19", new double[] { 0.219362751, 0.141781077, 0.5919118, 0.6683006 } }, { "fork_20", new double[] { 0.180147052, 0.239820287, 0.6887255, 0.235294119 } } }; // Add all images for fork var imagePath = Path.Combine("Images", "fork"); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(forkTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // Add all images for scissors imagePath = Path.Combine("Images", "scissors"); imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(scissorsTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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 its 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); Console.WriteLine("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Create a prediction endpoint, passing in the obtained prediction key PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var imageFile = Path.Combine("Images", "test", "test_image.jpg"); using (var stream = File.OpenRead(imageFile)) { var result = endpoint.PredictImage(project.Id, File.OpenRead(imageFile)); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]"); } } Console.ReadKey(); }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "d7ba782c8051443c8557ff464418949f"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.GetProject(new Guid("65d4860d-036d-4e2a-97af-50c3afaec972")); var sourceDirectory = System.Configuration.ConfigurationSettings.AppSettings["ImageSourece"]; var imageDirectories = Directory.GetDirectories(sourceDirectory).ToList(); foreach (var directory in imageDirectories) { var diseaseTags = directory.Split(new string[] { "___" }, StringSplitOptions.None); if (diseaseTags.Count() < 2) { continue; } var diseaseTag = diseaseTags[1].Replace('_', ' '); if (diseaseTag == "healthy") { continue; } var plantName = diseaseTags[0].Substring(diseaseTags[0].LastIndexOf('\\') + 1).Replace('_', ' '); var imagefiles = Directory.GetFiles(Path.Combine(sourceDirectory, directory)).ToList(); var imagePerTag = 190; var tagName = string.Format("{0} {1}", plantName, diseaseTag); if (trainingApi.GetTags(project.Id).Any(t => t.Name == tagName)) { continue; } var tag = trainingApi.CreateTag(project.Id, tagName); var randomFiles = GetRandomFiles(imagefiles, imagePerTag); //max upload size 64 in a batch var batchStartIndex = 0; var batchsize = 64; while (batchStartIndex < imagePerTag) { var itemsCountToTake = (imagePerTag - batchStartIndex >= batchsize) ? batchsize : imagePerTag - batchStartIndex; var batchImages = randomFiles.Skip(batchStartIndex).Take(itemsCountToTake); var imageFilesToUpload = batchImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFilesToUpload, new List <Guid>() { tag.Id })); batchStartIndex += itemsCountToTake; } } // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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") { System.Threading.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); Console.WriteLine("Done!\n"); Console.ReadKey(); }
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); } }