private async Task ListProjectTags(TrainingApi trainingApi, Guid projectId)
        {
            var tagList = await trainingApi.GetTagsAsync(projectId);

            _tagIndexer = new ObservableCollection <TagValues>();


            foreach (var tag in tagList.Tags)
            {
                TagIndexer.Add(new TagValues
                {
                    TagName = tag.Name.ToString(),
                    TagID   = tag.Id.ToString()
                });
            }

            _tagIndexer = TagIndexer;

            RaisePropertyChanged("ItemsList");
            RaisePropertyChanged("TagIndexer");
            _isTagPickerVisible       = true;
            _isTagButtonButtonVisible = true;
            RaisePropertyChanged("IsTagPickerVisible");
            RaisePropertyChanged("IsTagButtonVisible");
        }
        public async Task <bool> CreateImagesFromData(Memory memory, IList <string> tags)
        {
            try
            {
                if (!tags.Any())
                {
                    return(false);
                }

                var project = await trainingApi.GetProjectAsync(new Guid("a7731c4f-8b9f-4012-ab53-98e4e0c48a5b"));

                var existingTags = await trainingApi.GetTagsAsync(project.Id);

                List <string> tagIds = new List <string>();
                foreach (var tag in tags)
                {
                    TrainingTag trainingTag = existingTags.FirstOrDefault(x => x.Name == tag);
                    if (trainingTag == null)
                    {
                        try
                        {
                            trainingTag = await trainingApi.CreateTagAsync(project.Id, tag);
                        }
                        catch (Microsoft.Rest.HttpOperationException)
                        {
                            // just in case there is a collision of tags getting created
                        }
                    }

                    if (trainingTag != null)
                    {
                        tagIds.Add(trainingTag.Id.ToString());
                    }
                }

                var fileName = Path.Combine(DependencyService.Get <IMediaFolder>().Path, $"{memory.MediaPath}");
                var data     = File.ReadAllBytes(Path.Combine(DependencyService.Get <IMediaFolder>().Path, $"{memory.MediaPath}"));
                using (var s = new MemoryStream(data))
                {
                    var result = await trainingApi.CreateImagesFromDataAsync(project.Id, s, tagIds);

                    return(result.IsBatchSuccessful);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Exception in {nameof(VisionService)}.{nameof(ProcessImageFile)}: {e.Message}");
                Debug.WriteLine(e.StackTrace);
            }

            return(false);
        }
Esempio n. 3
0
        private static async Task ListProjectTags(TrainingApi trainingApi, Guid projectId)
        {
            var tagList = await trainingApi.GetTagsAsync(projectId);

            if (tagList.Tags.Any())
            {
                Console.WriteLine($"Tags: {Environment.NewLine}{string.Join(Environment.NewLine, tagList.Tags.Select(t => $"  {t.Name} (Image count: {t.ImageCount})"))}{Environment.NewLine}");
            }
            else
            {
                Console.WriteLine($"No tags yet...{Environment.NewLine}");
            }
        }
        private async Task LoadTagsInCurrentProject()
        {
            this.TagsInCurrentGroup.Clear();

            try
            {
                this.TagsInCurrentGroup.AddRange((await trainingApi.GetTagsAsync(this.CurrentProject.Id)).OrderBy(t => t.Name));
            }
            catch (Exception e)
            {
                await Util.GenericApiCallExceptionHandler(e, "Failure loading tags in the project");
            }
        }
        private async Task <Tag> GetOrCreateTag(TrainingApi trainingApi, Guid projectId, string name)
        {
            var tagList = await trainingApi.GetTagsAsync(projectId);

            var tag = tagList.Tags.Where(t => t.Name.ToUpper() == name.ToUpper()).SingleOrDefault();

            if (tag == null)
            {
                tag = await trainingApi.CreateTagAsync(projectId, name);
            }

            return(tag);
        }
        private async void PopulateTagSamplesAsync(Guid projectId, TrainingApi trainingEndPoint, ObservableCollection <TagSampleViewModel> collection)
        {
            foreach (var tag in (await trainingEndPoint.GetTagsAsync(projectId)).Tags.OrderBy(t => t.Name))
            {
                if (tag.ImageCount > 0)
                {
                    var imageModelSample = await trainingEndPoint.GetImagesByTagsAsync(projectId, null, new string[] { tag.Id.ToString() }, null, 1);

                    collection.Add(new TagSampleViewModel {
                        TagName = tag.Name, TagSampleImage = imageModelSample.First().ThumbnailUri
                    });
                }
            }
        }
Esempio n. 7
0
        private async void btnSubirNuevosTags_Click(object sender, RoutedEventArgs e)
        {
            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = localSettings.Values["apiKeyCVTraining"] as string
            };
            var domains = await trainingApi.GetDomainsAsync();

            var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection");
            var project            = vs.First().ToString();

            try
            {
                Iteration iteration      = new Iteration();
                var       iterationsList = await trainingApi.GetIterationsAsync(new Guid(vsproject));

                var       iterationUltima = iterationsList.Last().ToString();
                ListaTags listaTags       = new ListaTags();
                var       TagsPred        = await trainingApi.GetTagsAsync(new Guid(vsproject));

                PredictionQueryTag predictionQueryTag = new PredictionQueryTag(new Guid(TagsPred.Last().Id.ToString()), 0.45, 1);

                StoredImagePrediction storedImagePredictiong = new StoredImagePrediction();
                listaTags.Add(predictionQueryTag);

                PredictionQueryToken predictionToken = new PredictionQueryToken()
                {
                    Application = null, Continuation = null, EndTime = null, StartTime = null, IterationId = new Guid(iterationUltima), MaxCount = 100, OrderBy = "Newest", Session = null, Tags = listaTags
                };
                PredictionQueryResult predictionQueryResult = new PredictionQueryResult(predictionToken, StoredImages());

                var imagePath        = System.IO.Path.Combine("Images", "fork");
                var imageFileEntries = new List <ImageIdCreateEntry>();
                ImageIdCreateEntry imageIdCreateEntry = new ImageIdCreateEntry();


                foreach (var item in region)
                {
                    imageIdCreateEntry.Id = new Guid(idPredictedImage);
                    //imageFileEntries.Add(new ImageFileCreateEntry(item, File.ReadAllBytes(item), null, new List<Region>(new Region[] { new Region(tagId, region[0], region[1], region[2], region[3])})));
                }


                ImageRegionCreateBatch imageRegionCreateBatch = new ImageRegionCreateBatch();
            }
            catch (Exception ex)
            {
                var error = ex.Message.ToString();
            }
        }
Esempio n. 8
0
        private static async Task DeleteImagesAndTagsAsync(ParsingOptions options, TrainingApi trainingApi)
        {
            // Delete all tagged images.
            Console.WriteLine("Deleting existing images...");
            var taggedImages = await trainingApi.GetTaggedImagesAsync(options.ProjectId);

            await trainingApi.DeleteImagesAsync(options.ProjectId, taggedImages.Select(i => i.Id.ToString()).ToList());

            // Delete all tags.
            Console.WriteLine("Deleting existing tags...");
            var tags = await trainingApi.GetTagsAsync(options.ProjectId);

            foreach (var tag in tags.Tags)
            {
                await trainingApi.DeleteTagAsync(options.ProjectId, tag.Id);
            }
        }
Esempio n. 9
0
        private async Task LoadTagsInCurrentProject()
        {
            this.TagsInCurrentGroup.Clear();

            try
            {
                ImageTagListModel tagListModel = await trainingApi.GetTagsAsync(this.CurrentProject.Id);

                foreach (ImageTagModel tag in tagListModel.Tags.OrderBy(t => t.Name))
                {
                    this.TagsInCurrentGroup.Add(tag);
                }
            }
            catch (Exception e)
            {
                await Util.GenericApiCallExceptionHandler(e, "Failure loading people in the group");
            }
        }
Esempio n. 10
0
        public async Task <PredictImageResult> PredictImage(Stream testImage)
        {
            string trainingKey   = "6308b3b62b344e3f8e4170c4728deed2";
            string predictionKey = "afdffbaa498445c1830aa18ee9216e0b";

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

            TrainingApi trainingApi = new TrainingApi()
            {
                ApiKey = trainingKey
            };
            var projects = await trainingApi.GetProjectsAsync();

            var project = projects.First(f => f.Name == "WA-SE-AI");

            try
            {
                var result = await endpoint.PredictImageAsync(project.Id, testImage);

                var tags = await trainingApi.GetTagsAsync(project.Id);

                // Loop over each prediction and write out the results
                foreach (var c in result.Predictions)
                {
                    Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
                }
                var topPrediction = result.Predictions.OrderByDescending(m => m.Probability).First();
                PredictImageResult predictImageResult = new PredictImageResult
                {
                    PredictionModel = topPrediction,
                    Tag             = tags.FirstOrDefault(f => f.Id == topPrediction.TagId)
                };
                return(predictImageResult);
            }
            catch (Exception e)
            {
                throw new Exception("PredictImage failed");
            }
        }
Esempio n. 11
0
        private static async Task DeleteImagesAndTagsAsync(ParsingOptions options, TrainingApi trainingApi)
        {
            // Delete all tagged images.
            Console.WriteLine("Deleting existing images...");
            IList <Microsoft.Cognitive.CustomVision.Training.Models.Image> taggedImages;

            while ((taggedImages = await trainingApi.GetTaggedImagesAsync(options.ProjectId, take: 50, skip: 0)).Any())
            {
                await trainingApi.DeleteImagesAsync(options.ProjectId, taggedImages.Select(i => i.Id.ToString()).ToList());
            }

            // Delete all tags.
            Console.WriteLine("Deleting existing tags...");
            var tags = await trainingApi.GetTagsAsync(options.ProjectId);

            foreach (var tag in tags.Tags)
            {
                await trainingApi.DeleteTagAsync(options.ProjectId, tag.Id);
            }
        }
Esempio n. 12
0
        public static async Task <HttpResponseMessage> Tags(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "tags")] HttpRequestMessage req,
            TraceWriter log)
        {
            var CV_ProjectId   = Environment.GetEnvironmentVariable("CV_ProjectId");
            var CV_TrainingKey = Environment.GetEnvironmentVariable("CV_TrainingKey");
            var trainingApi    = new TrainingApi()
            {
                ApiKey = CV_TrainingKey
            };
            var projectId = Guid.Parse(CV_ProjectId);

            var existTags = await trainingApi.GetTagsAsync(projectId);

            var tags = existTags.Select(x =>
            {
                // XXX 早くこういうのやめよう
                var categories = new string[]
                {
                    "FA:G",
                    "メガミデバイス",
                    "メガミ",
                    "HGUC",
                    "MG",
                    "RG"
                };
                return(new
                {
                    Category = categories.Contains(x.Name)
                        ? "Category"
                        : categories.Any(y => x.Name.StartsWith(y))
                            ? "Item"
                            : "Attribute",
                    AttributeType = "None",
                    x.Name
                });
            });

            return(req.CreateJsonResponse(HttpStatusCode.OK, tags));
        }
Esempio n. 13
0
        private async void PopulateTagSamplesAsync(Guid projectId, TrainingApi trainingEndPoint, ObservableCollection <TagSampleViewModel> collection)
        {
            foreach (var tag in (await trainingEndPoint.GetTagsAsync(projectId)).OrderBy(t => t.Name))
            {
                if (tag.ImageCount > 0)
                {
                    var imageModelSample = (await trainingEndPoint.GetTaggedImagesAsync(projectId, null, new string[] { tag.Id.ToString() }, null, 1)).First();

                    var tagRegion = imageModelSample.Regions?.FirstOrDefault(r => r.TagId == tag.Id);
                    if (tagRegion == null || (tagRegion.Width == 0 && tagRegion.Height == 0))
                    {
                        collection.Add(new TagSampleViewModel {
                            TagName = tag.Name, TagSampleImage = new BitmapImage(new Uri(imageModelSample.ThumbnailUri))
                        });
                    }
                    else
                    {
                        // Crop a region from the image that is associated with the tag, so we show something more
                        // relevant than the whole image.
                        ImageSource croppedImage = await Util.DownloadAndCropBitmapAsync(
                            imageModelSample.ImageUri,
                            new Microsoft.ProjectOxford.Face.Contract.FaceRectangle
                        {
                            Left   = (int)(tagRegion.Left *imageModelSample.Width),
                            Top    = (int)(tagRegion.Top *imageModelSample.Height),
                            Width  = (int)(tagRegion.Width *imageModelSample.Width),
                            Height = (int)(tagRegion.Height *imageModelSample.Height)
                        });

                        collection.Add(new TagSampleViewModel {
                            TagName = tag.Name, TagSampleImage = croppedImage
                        });
                    }
                }
            }
        }
Esempio n. 14
0
        public static async Task <HttpResponseMessage> ImageUpload(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "images/upload")] HttpRequestMessage req,
            [Table("Users")] IQueryable <User> users,
            [Table("CreateImageFromUrls")] IQueryable <CreateImageFromUrlsEntity> imageUrls,
            [Queue("create-image-from-urls")] ICollector <CreateImageFromUrlsRequest> queueItems,
            [Table("CreateImageFromUrls")] ICollector <CreateImageFromUrlsEntity> outImageUrlTable,
            [Table("PredictedInfo")] ICollector <PredictedInfo> outPredictedTable,
            TraceWriter log,
            Microsoft.Azure.WebJobs.ExecutionContext context)
        {
            log.Info("C# HTTP trigger function processed a request.");

            // collect input
            dynamic data = await req.Content.ReadAsAsync <object>();

            string imageData          = data.image;
            ICollection <string> tags = data.tags.ToObject <List <string> >();
            string modelName          = data.modelName;

            log.Info($"modelName={modelName}tags={string.Join(",", tags)}, image={imageData}");

            var dataUrlReg = Regex.Match(imageData, @"data:image/(?<type>.+?);base64,(?<data>.+)");
            var image      = Convert.FromBase64String(dataUrlReg.Groups["data"].Value);
            var extension  = dataUrlReg.Groups["type"].Value;

            // client
            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 predictionEndpoint = new PredictionEndpoint()
            {
                ApiKey = CV_PredictionKey
            };
            var projectId = Guid.Parse(CV_ProjectId);

            // collect user
            var user  = User.FromRequest(users, req, Thread.CurrentPrincipal);
            var iuser = user as IUser;

            // 既存のタグのみを受け付ける
            var existTags = await trainingApi.GetTagsAsync(projectId);

            tags = tags.Intersect(existTags.Select(x => x.Name)).ToList();

            // setup blob
            // https://docs.microsoft.com/ja-jp/azure/cosmos-db/table-storage-design-guide#log-tail-pattern
            var invertedTicks = string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);
            var blobName      = invertedTicks + "-" + Guid.NewGuid().ToString();

            var blockBlob          = CommonHelper.PhotoBlobReference(blobName);
            var blockBlobThumbnail = CommonHelper.PhotoThumbnailBlobReference(blobName);

            blockBlob.Properties.ContentType          = "image/jpeg";
            blockBlobThumbnail.Properties.ContentType = "image/jpeg";

            var url    = blockBlob.Uri.ToString();
            var source = "Upload";
            var key    = blockBlob.Name;

            log.Info($"pre upload. blobUri={url}");

            // XXX この辺りの一連の手続きをいい感じに関数分割してフロー処理できると可用性と変更影響範囲が良くなりそう
            // ただ、この程度の規模のサービスで1関数で同期的に処理する平易さ以上のメリットを得られるかは疑問

            // normalize image
            MemoryStream   normalizedImage = NormalizeImage(image);
            AnchorLocation anchor          = DetectFocusAnchor(log, normalizedImage, context.FunctionAppDirectory);

            log.Info($"Anchor: {anchor.ToString()}");
            MemoryStream thumbnailImage = ToThumbnailImage(image, anchor);

            // upload image
            normalizedImage.Position = 0;
            thumbnailImage.Position  = 0;
            await blockBlob.UploadFromStreamAsync(normalizedImage);

            await blockBlobThumbnail.UploadFromStreamAsync(thumbnailImage);

            log.Info($"after upload.");

            // queue image for training
            // 使用しているAPIの都合上、BLOBがアップロード後である必要がある
            TrainingImageLogic.AddImage(
                imageUrls, queueItems, outImageUrlTable, log,
                source, url, key, tags, user, modelName
                );
            log.Info($"after queue image data.");

            // predict image
            // https://docs.microsoft.com/ja-jp/azure/cognitive-services/custom-vision-service/csharp-tutorial
            var imageUrl = new ImageUrl()
            {
                Url = url
            };
            // XXX Storage emurator で通すならNgrock等の工夫が必要。単にDevelop用のStorage Accountを取ってしまった方が楽かも。
            var predictResult = await predictionEndpoint.PredictImageUrlAsync(projectId, imageUrl);

            log.Info($"after prediction.");
            var predicted = new PredictedInfo()
            {
                PartitionKey = source,
                RowKey       = key,
                Result       = predictResult,
                User         = user,
                ModelName    = modelName
            };

            outPredictedTable.Add(predicted);

            return(req.CreateJsonResponse(HttpStatusCode.OK, new
            {
                name = blockBlob.Name,
                url,
                result = predictResult
            }));
        }
Esempio n. 15
0
        private async Task <SelectList> GetTagSelectList(TrainingApi api, Guid?selectedTag = null)
        {
            var tagList = await api.GetTagsAsync(ProjectId);

            return(new SelectList(tagList.Tags, "Id", "Name", selectedTag));
        }
        private static async Task <bool> MainAsync(string[] args)
        {
            try
            {
                trainingApi.ApiKey = CustomVisionTrainingApiKey;

                // Create project
                var projects = await trainingApi.GetProjectsAsync();

                var project = projects.Where(x => x.Name == projectName).FirstOrDefault();

                if (project == null)
                {
                    Console.WriteLine($"\nCreating project '{projectName}'");
                    project = await trainingApi.CreateProjectAsync(projectName);
                }

                // Retrieve all tags
                var tagsList = await trainingApi.GetTagsAsync(project.Id);

                #region Create Object tag
                var objectTag = tagsList.Tags.Where(x => x.Name == objectTagName).FirstOrDefault();

                if (objectTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{objectTagName}'");
                    objectTag = await trainingApi.CreateTagAsync(project.Id, objectTagName);
                }

                // add images to tag
                Console.WriteLine($"\nAdding images to tag '{objectTagName}'");
                List <ImageFileCreateEntry> imageFiles = (Directory.GetFiles(objectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    objectTag.Id
                }));

                #endregion

                #region Create Ocean tag
                var oceanTag = tagsList.Tags.Where(x => x.Name == oceanTagName).FirstOrDefault();

                if (oceanTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{oceanTagName}'");
                    oceanTag = await trainingApi.CreateTagAsync(project.Id, oceanTagName);
                }

                // add images
                Console.WriteLine($"\nAdding images to tag '{oceanTagName}'");
                imageFiles = (Directory.GetFiles(noObjectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    oceanTag.Id
                }));

                #endregion

                #region Train the classifier
                Console.WriteLine("\nTraining");
                var iteration = await trainingApi.TrainProjectAsync(project.Id);

                do
                {
                    Thread.Sleep(1000);
                    iteration = await trainingApi.GetIterationAsync(project.Id, iteration.Id);
                }while (string.Equals("training", iteration.Status, StringComparison.OrdinalIgnoreCase));

                if (!string.Equals(iteration.Status, "completed", StringComparison.OrdinalIgnoreCase))
                {
                    throw new Exception($"An error occurred training the classifier. Iteration status is {iteration.Status}");
                }

                iteration.IsDefault = true;
                await trainingApi.UpdateIterationAsync(project.Id, iteration.Id, iteration);

                #endregion

                Console.WriteLine(
                    $@"\n
Your custom vision project ID is {project.Id.ToString()}.

Copy this Guid and add it to your application settings under the name 'CustomVisionProjectId'
"
                    );

                Console.WriteLine("\nFinished. Press Enter to exit");
                Console.Read();

                return(true);
            }
            catch (Exception e)
            {
                Console.Write(e);
                Console.WriteLine("\nPress Enter to exit");
                Console.Read();

                return(false);
            }
        }