Ejemplo n.º 1
0
 private void radioButtonDetection_CheckedChanged(object sender, EventArgs e)
 {
     if (radioButtonDetection.Checked)
     {
         currentPredictionMode = PredictionMode.Detection;
         project             = trainingApi.GetProject(new Guid("e1ec7b7e-ac74-4959-9c8b-4d48d25a7668"));
         buttonClear.Enabled = isPredicting ? false : true;
     }
     pictureBox1.Invalidate();
 }
Ejemplo n.º 2
0
        private string connectToAnalyze()
        {
            // custom vision
            TrainingApi trainingAPI = new TrainingApi()
            {
                ApiKey = "1360135bcfaa45d5b6d69dc06dcb04e4"
            };
            Guid _projectkey = new Guid("ea8591bc-bff1-47d6-b46d-64938d2d8b58");

            project = trainingAPI.GetProject(_projectkey);

            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = "65125a9d5a774acaaeaa0f9705404307"
            };
            // 28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1   iteration key


            ArrayList lists = new ArrayList();

            byte[] imgArr = GetFileAsByteArray(App._file.AbsolutePath);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(imgArr);
            var result = endpoint.PredictImage(project.Id, stream, new Guid("28bf5d1c-1025-4d3f-a7d7-4dfd4b65f4a1"));

            foreach (var prediction in result.Predictions)
            {
                lists.Add($"{prediction.Tag}");
            }
            return(lists.Get(0).ToString());
        }
Ejemplo n.º 3
0
        public Form1()
        {
            InitializeComponent();

            trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };
            currentPredictionMode = PredictionMode.Detection;
            project = trainingApi.GetProject(new Guid("e1ec7b7e-ac74-4959-9c8b-4d48d25a7668"));

            // Create a prediction endpoint, passing in obtained prediction key
            endpoint = new PredictionEndpoint()
            {
                ApiKey = predictionKey
            };

            labelInfo.Text         = textBoxUrl.Text = "";
            percentLevel           = trackBar1.Value;
            selectedItemIndex      = -1;
            isPredicting           = true;
            textBoxTag.Enabled     = buttonAddTag.Enabled = buttonSend.Enabled = buttonClear.Enabled = false;
            labelStatus.Text       = "PREDICTING";
            treeListToUpload       = new List <TreeData>();
            treeListDetection      = new List <TreeData>();
            treeListClassification = new List <TreeData>();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var options = Options.InitializeOptions <TrainingOptions>(args);

            if (options == null)
            {
                return;
            }

            var trainingApi = new TrainingApi()
            {
                ApiKey = options.Trainingkey
            };

            if (options.BaseUri != null)
            {
                Console.WriteLine($"The default base uri is {trainingApi.BaseUri}. Changed it to {options.BaseUri}.");
                trainingApi.BaseUri = options.BaseUri;
            }

            ProjectInfo project;

            if (options.ProjectId != Guid.Empty)
            {
                if (!trainingApi.CheckIfProjectExists(options.ProjectId))
                {
                    Console.WriteLine($"Cannot get the project with id {options.ProjectId}. Please check if the Guid is right.");
                    return;
                }

                project = new ProjectInfo(trainingApi.GetProject(options.ProjectId));
            }
            else
            {
                Console.WriteLine("ProjectId is not provided. Creating a new project...");
                var projectInfo = ProjectInfo.ReadProjectInfo(options.WorkDir + options.ProjectInfoFileName);
                project = trainingApi.CreateProjectAsync(projectInfo).Result;
            }

            Console.WriteLine($"Project Id: {project.Id}");

            if (options.ImageSource != "existing")
            {
                var uploadResult = trainingApi.ReadAndUploadImagesAsync(project, options, options.AllowedTagNames).Result;
                if (!uploadResult.IsBatchSuccessful)
                {
                    Console.WriteLine("No image uploaded successfully. Please check if those images have been uploaded before.");
                    return;
                }
            }

            trainingApi.TrainProject(project.Id);

            Console.WriteLine("The classifier is now training. You can check its status in the web portal.\nPress any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var options = Options.InitializeOptions <ImagesDownloadOptions>(args);

            if (options == null)
            {
                return;
            }

            var trainingApi = new TrainingApi()
            {
                ApiKey = options.Trainingkey
            };

            if (options.BaseUri != null)
            {
                Console.WriteLine($"The default base uri is {trainingApi.BaseUri}. Changed it to {options.BaseUri}.");
                trainingApi.BaseUri = options.BaseUri;
            }

            string errorMessage;

            if (!options.ValidateWithTrainingApi(trainingApi, out errorMessage))
            {
                Console.WriteLine(errorMessage);
                return;
            }

            var imageGetter = new ImagesLoaderFromProject(trainingApi, options.ProjectId, options.IterationId, options.AllowedTagNames);
            var images      = imageGetter.LoadImages();

            var imageFileWriter = ImageFilesWriterGenerator.GenerateImageFileWriter(options);

            imageFileWriter.WriteImagesToDisk(images);

            var project = new ProjectInfo(trainingApi.GetProject(options.ProjectId));

            project.WriteProjectInfo(options.WorkDir + options.ProjectInfoFileName).Wait();
        }
Ejemplo n.º 6
0
        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));
                }
            }
        }
Ejemplo n.º 7
0
        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));
                }
            }
        }
Ejemplo n.º 8
0
        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();
        }
Ejemplo n.º 9
0
        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();
        }
Ejemplo n.º 10
0
        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);
            }
        }