private static Project CreateProject(string projectName) { try { var trainingKey = ConfigurationManager.AppSettings["CustomVision_TrainingKey"]; var trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project IList <Project> projects = trainingApi.GetProjects(); foreach (Project p in projects) { if (p.Name == projectName) { Console.WriteLine("\tFound project: " + projectName); return(p); } } // A project isn't found with this name create it. Console.WriteLine("\tCreating new project:"); return(trainingApi.CreateProject(projectName)); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Initialize Model from preloaded Data /// </summary> /// <param name="token"></param> /// <returns></returns> public void InitializeModel() { if (string.IsNullOrWhiteSpace(_trainingKey)) { throw new Exception("Call SetTrainingKey before training the Model"); } var trainingCredentials = new TrainingApiCredentials(_trainingKey); TrainingApi = new TrainingApi(trainingCredentials); // Check if project already exists var projects = TrainingApi.GetProjects(); var duplicate = projects.FirstOrDefault(p => p.Name == _projectName); if (duplicate != null) { TrainingApi.DeleteProject(duplicate.Id); } // Create a new CV Project var project = TrainingApi.CreateProject(_projectName); // Save Project Guid ProjectGuid = project.Id; // Retrieve Model Initialization Data from Folder "Training" var trainings = RetrieveTrainings(); TrainModel(ref project, trainings); }
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"); }
private static void Train() { string trainingKey = "fdf8998652a44b5ea1380439f25d71ca"; string projectKey = "INat-ImageClassifier"; string trainPath = @"c:\data\Images\train\"; TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); var project = trainingApi.CreateProject(projectKey); var files = new DirectoryInfo(trainPath).GetFiles("*", SearchOption.AllDirectories); foreach (var image in files) { if (tagCount >= 50) { break; } List <string> tags = new List <string>(); string tagName = image.Directory.Name.Replace("_", " "); var tagdata = trainingApi.GetTags(project.Id); CreateTags(trainingApi, project, tags, tagName, tagdata); var imagestream = new MemoryStream(File.ReadAllBytes(image.FullName)); Console.WriteLine("Sending image " + image.Name + " To Custom Vision AI for training..."); trainingApi.CreateImagesFromData(project.Id, imagestream, tags); } var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(100); iteration = trainingApi.GetIteration(project.Id, iteration.Id); Console.WriteLine("Training..."); } iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); }
private static Project CreateProject(string projectName) { try { var trainingKey = ConfigurationManager.AppSettings["CustomVision_TrainingKey"]; var trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("\tCreating new project:"); var project = trainingApi.CreateProject(projectName); return(project); } catch (Exception e) { Console.WriteLine(e); throw; } }
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) { // 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> /// 创建Project /// </summary> /// <param name="projectName"></param> /// <param name="description"></param> /// <param name="domainId"></param> public void NewProject(string projectName, string description, Guid?domainId) { var project = trainingApi.CreateProject(projectName, description, domainId); }
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(); }
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"); } }
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 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 // 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 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(); }
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(); }
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) { // 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(); }
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 key from the settings page of the portal string trainingKey = "b97174f6bded4d1c871247f533824bf8"; // 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"); var bottletag = trainingApi.CreateTag(project.Id, "Bottle"); var iceteatag = trainingApi.CreateTag(project.Id, "Ice tea"); // 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() }); } } //## uploading normal bottles foreach (var image in BottleImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { bottletag.Id.ToString() }); } } //## foreach (var image in IceTeaImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { iceteatag.Id.ToString(), bottletag.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 // 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 = "5eb55e0982ed420b9fea340675da17b9"; // 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(); }
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(); }