示例#1
0
        public async Task <ActionResult> TagImage(HttpPostedFileBase file)
        {
            AnalysisResultViewModel model;

            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Files.Count > 0)
                    {
                        var Inputfile = Request.Files[0];

                        if (Inputfile != null && Inputfile.ContentLength > 0)
                        {
                            var filename = Path.GetFileName(Inputfile.FileName);
                            if (!Directory.Exists(Server.MapPath("~/uploadedfile/")))
                            {
                                Directory.CreateDirectory(Server.MapPath("~/uploadedfile/"));
                            }
                            if (System.IO.File.Exists(Path.Combine(Server.MapPath("~/uploadedfile/"), filename)))
                            {
                                System.IO.File.Delete(Path.Combine(Server.MapPath("~/uploadedfile/"), filename));
                            }
                            var path = Path.Combine(Server.MapPath("~/uploadedfile/"), filename);
                            Inputfile.SaveAs(path);

                            model = new AnalysisResultViewModel();
                            model.ImageLocalPath = "/uploadedfile/" + filename;
                            var analysisResult = await AnalyzeImage(path); //Call Computer vision to Tag images

                            //var predictionResult = await PredictImage(path); // Call custom vision
                            model.Insights = new ImageInsights
                            {
                                Caption    = analysisResult.Caption,
                                Categories = analysisResult.Categories,
                                ImageId    = analysisResult.ImageId,
                                Tags       = analysisResult.Tags//.Union(predictionResult.Tags).ToArray()
                            };
                            ViewBag.FileStatus = "File uploaded successfully.";
                            return(View(model));
                        }
                    }
                    else
                    {
                        ViewBag.FileStatus = "No files uploaded";
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.FileStatus = "Error while file uploading.";;
                }
            }
            return(View());
        }
示例#2
0
        private async Task <ImageMetadata> SaveData(AnalysisResultViewModel data)
        {
            var fileName = Path.GetFileName(data.ImageLocalPath);
            Func <Task <Stream> > imageCB;

            try
            {
                if (blobStorage == null || cosmosDb == null)
                {
                    BlobStorageHelper.ContainerName    = ConfigurationManager.AppSettings["ContainerName"];
                    BlobStorageHelper.ConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
                    blobStorage = await BlobStorageHelper.BuildAsync();

                    CosmosDBHelper.AccessKey      = ConfigurationManager.AppSettings["CosmosDBKey"];
                    CosmosDBHelper.EndpointUri    = ConfigurationManager.AppSettings["CosmosDBEndpointURI"];
                    CosmosDBHelper.DatabaseName   = ConfigurationManager.AppSettings["DatabaseName"];
                    CosmosDBHelper.CollectionName = ConfigurationManager.AppSettings["CollectionName"];
                    cosmosDb = await CosmosDBHelper.BuildAsync();
                }
                var existing = await cosmosDb.FindDocumentByIdAsync <ImageMetadata>(fileName);

                var resized = ImageResizer.ResizeIfRequired(Server.MapPath(data.ImageLocalPath), 750);
                imageCB = async() => System.IO.File.OpenRead(resized.Item2);
                var imageBlob = await blobStorage.UploadImageAsync(imageCB, fileName);

                var metadata = new ImageMetadata(data.ImageLocalPath);
                metadata.AddInsights(data.Insights);
                metadata.BlobUri  = imageBlob.Uri;
                metadata.TaggedBy = User.Identity.Name;
                if (existing == null)
                {
                    metadata = (await cosmosDb.CreateDocumentIfNotExistsAsync(metadata, metadata.Id)).Item2;
                }
                else
                {
                    metadata = await cosmosDb.UpdateDocumentAsync(metadata, metadata.Id);
                }

                return(metadata);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
示例#3
0
        public async Task <ActionResult> UpdateAnalysisData(AnalysisResultViewModel model)
        {
            try
            {
                await SaveData(model);

                ViewBag.Status = new UpdateStatusModel {
                    Success = true, Message = "Your image tag settings successfully saved!"
                };
                return(View("TagImage"));
            }
            catch (Exception ex)
            {
                ViewBag.Status = new UpdateStatusModel {
                    Success = false, Message = "Failed to save image tag settings!"
                };
                return(View("TagImage"));
            }
        }
示例#4
0
        public ActionResult AnalyzeUrl(UrlAnalysisInputModel model)
        {
            var result = new AnalysisResultViewModel()
            {
                Options = model.Options
            };

            if (model.Options.NumberOfWords)
            {
                result.NumberOfWords = seoAnalyzer.GetWordOccurancesFromUrl(model.Url)
                                       .Select(x => new WordCountModel()
                {
                    Word = x.Key, Count = x.Value
                }).ToList();
            }

            if (model.Options.NumberOfMetaKeywords)
            {
                result.NumberOfMetaKeywords = seoAnalyzer.GetMetaKeywordWordOccurancesFromUrl(model.Url)
                                              .Select(x => new WordCountModel()
                {
                    Word = x.Key, Count = x.Value
                }).ToList();
            }

            if (model.Options.ExternalLinks)
            {
                result.ExternalLinks = seoAnalyzer.GetExternalUrlsFromUrl(model.Url)
                                       .Select(x => new LinkModel()
                {
                    Link = x
                }).ToList();
            }

            return(PartialView("AnalysisResult", result));
        }