Exemplo n.º 1
0
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

            _customVisionPredictionKey = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionPredictionKey");
            _customVisionApiProjectId  = umbracoHelper.TypedContent(1127).GetPropertyValue <string>("customVisionProjectId");


            if (!string.IsNullOrWhiteSpace(_customVisionPredictionKey) && !string.IsNullOrWhiteSpace(_customVisionApiProjectId))
            {
                PredictionEndpoint endpoint = new PredictionEndpoint()
                {
                    ApiKey = _customVisionPredictionKey
                };

                foreach (IMedia media in e.SavedEntities.Where(a => a.ContentType.Name.Equals(Constants.Conventions.MediaTypes.Image)))
                {
                    string relativeImagePath = ImagePathHelper.GetImageFilePath(media);

                    using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                    {
                        var result = endpoint.PredictImage(Guid.Parse(_customVisionApiProjectId), imageFileStream);
                        IEnumerable <string> tags = result.Predictions.Where(a => a.Probability > 0.75).Select(a => a.Tag);
                        media.SetTags("customVisionTags", tags, true);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Update(
            string name,
            string description,
            string streetAddress,
            double?latitude,
            double?longitude,
            string[] images)
        {
            Name          = name ?? throw new ArgumentNullException(nameof(name));
            Description   = description;
            StreetAddress = streetAddress ?? throw new ArgumentNullException(streetAddress);
            Location      = (latitude != null && longitude != null) ? new Location(latitude.Value, longitude.Value) : null;
            Images        = ImagePathHelper.Merge(Images, images);

            AddEvent(new FacilityUpdatedEvent
            {
                Id            = Id,
                Name          = Name,
                Description   = Description,
                StreetAddress = StreetAddress,
                Owner         = Owner,
                Latitude      = latitude,
                Longitude     = longitude,
                Images        = Images
            });
        }
Exemplo n.º 3
0
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            VisionServiceClient visionServiceClient = new VisionServiceClient(_visionApiKey, _visionApiUrl);

            foreach (IMedia media in e.SavedEntities)
            {
                string relativeImagePath = ImagePathHelper.GetImageFilePath(media);
                string fullPath          = _fs.GetFullPath(relativeImagePath);

                // Computer Vision API
                using (Stream imageFileStream = File.OpenRead(fullPath))
                {
                    // Call the Computer Vision API
                    Task <AnalysisResult> analysisResultTask = visionServiceClient
                                                               .AnalyzeImageAsync(
                        imageFileStream,
                        new []
                    {
                        VisualFeature.Description,
                        VisualFeature.Adult,
                        VisualFeature.Tags,
                        VisualFeature.Categories
                    },
                        new[]
                    {
                        "celebrities", "landmarks"
                    }
                        );
                    analysisResultTask.Wait();

                    var computervisionResult = analysisResultTask.Result;


                    // Get the result and set the values of the ContentItem
                    var celebrityTags = new List <string>();
                    var landmarksTags = new List <string>();

                    foreach (Category category in computervisionResult.Categories.Where(a => a.Detail != null))
                    {
                        var detailResult = JsonConvert.DeserializeObject <DetailsModel>(category.Detail.ToString());
                        celebrityTags.AddRange(detailResult.Celebrities.Select(a => a.Name));
                        landmarksTags.AddRange(detailResult.Landmarks.Select(a => a.Name));
                    }

                    IEnumerable <string> tags = computervisionResult.Tags.Select(a => a.Name);
                    string caption            = computervisionResult.Description.Captions.First().Text;
                    bool   isAdult            = computervisionResult.Adult.IsAdultContent;
                    bool   isRacy             = computervisionResult.Adult.IsRacyContent;

                    media.SetTags("tags", tags, true);
                    media.SetTags("celebrities", celebrityTags, true);
                    media.SetTags("landmarks", landmarksTags, true);
                    media.SetValue("description", caption);
                    media.SetValue("isAdult", isAdult);
                    media.SetValue("isRacy", isRacy);
                }
            }
        }
        public async Task <Contracts.Models.Facility> UpdateAsync(Guid facilityId, FacilityDetails details)
        {
            var facility = await dataSource.FindAsync(facilityId);

            if (facility == null)
            {
                throw new FacilityNotFoundException(facilityId);
            }

            details.Images   = ImagePathHelper.Merge(facility.Details.Images, details.Images);
            facility.Details = details;
            await dataSource.UpdateAsync(facility);

            return(facility);
        }
        public async Task <Contracts.Models.Facility> CreateAsync(FacilityDetails details)
        {
            details.Images = ImagePathHelper.CleanUp(details.Images);

            var facility = new Contracts.Models.Facility
            {
                Id       = Guid.NewGuid(),
                Details  = details,
                Location = new Location(),
                Owner    = securityContext.GetCurrentTenant()
            };

            await dataSource.CreateAsync(facility);

            return(facility);
        }
Exemplo n.º 6
0
/* ==> Stap 7 -> Detect faces and match them to Umbraco Members */
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            FaceServiceClient faceServiceClient = new FaceServiceClient(_faceApiKey, _faceApiUrl);
            IMemberService    memberService     = ApplicationContext.Current.Services.MemberService;

            foreach (IMedia media in e.SavedEntities)
            {
                string relativeImagePath = ImagePathHelper.GetImageFilePath(media);
                string fullPath          = _fs.GetFullPath(relativeImagePath);

                using (Stream imageFileStream = File.OpenRead(fullPath))
                {
                    var faces = AsyncHelpers.RunSync(() =>
                                                     faceServiceClient.DetectAsync(imageFileStream));

                    if (faces.Any())
                    {
                        Guid[]           faceIds = faces.Select(a => a.FaceId).ToArray();
                        IdentifyResult[] results = AsyncHelpers.RunSync(() =>
                                                                        faceServiceClient.IdentifyAsync(_faceApiGroup, faceIds, 5));

                        var matchedPersons = new List <IMember>();

                        foreach (IdentifyResult identifyResult in results)
                        {
                            foreach (var candidate in identifyResult.Candidates)
                            {
                                IEnumerable <IMember> searchResult = memberService.GetMembersByPropertyValue("personId", candidate.PersonId.ToString());
                                matchedPersons.AddRange(searchResult);
                            }
                        }

                        if (matchedPersons.Any())
                        {
                            media.SetValue("persons", string.Join(",", matchedPersons.Select(a => a.GetUdi().ToString())));
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public static Facility NewFacility(string owner,
                                           string name,
                                           string description,
                                           string streetAddress,
                                           double?latitude,
                                           double?longitude,
                                           string[] images)
        {
            var facility = new Facility
            {
                Owner = owner ?? throw new ArgumentNullException(nameof(owner)),
                              Name = name ?? throw new ArgumentNullException(nameof(name)),
                                           Description   = description,
                                           StreetAddress = streetAddress ?? throw new ArgumentNullException(streetAddress),
                                                                 Location = (latitude != null && longitude != null) ? new Location(latitude.Value, longitude.Value) : null,
                                                                 Images   = ImagePathHelper.CleanUp(images)
            };

            facility.AddEvent(new FacilityCreatedEvent(facility));

            return(facility);
        }
        public async Task <Accommodation> CreateAsync(Guid facilityId, AccommodationDetails details)
        {
            await facilityService.CheckFacilityAsync(facilityId);

            details.Images = ImagePathHelper.CleanUp(details.Images);
            var accommodation = new Accommodation
            {
                Id         = Guid.NewGuid(),
                FacilityId = facilityId,
                Details    = details
            };

            await accommodationDataSource.CreateAsync(accommodation);

            await facilityService.IncrementAccommodationCountAsync(facilityId);

            var facility = await facilityService.FindAsync(facilityId);

            await searchIndexer.IndexAccommodationAsync(accommodation, facility);

            return(accommodation);
        }
        public async Task <Accommodation> UpdateAsync(Guid accommodationId, AccommodationDetails details)
        {
            var accommodation = await accommodationDataSource.FindAsync(accommodationId);

            if (accommodation == null)
            {
                throw new AccommodationNotFoundException(accommodationId);
            }

            var facility = await facilityService.FindAsync(accommodation.FacilityId);

            if (facility == null)
            {
                throw new FacilityNotFoundException(accommodation.FacilityId);
            }

            details.Images        = ImagePathHelper.Merge(accommodation.Details.Images, details.Images);
            accommodation.Details = details;
            await accommodationDataSource.UpdateAsync(accommodation);

            await searchIndexer.IndexAccommodationAsync(accommodation, facility);

            return(accommodation);
        }
        private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            VisionServiceClient visionServiceClient = new VisionServiceClient(_visionApiKey, _visionApiUrl);

            foreach (IMedia media in e.SavedEntities.Where(a => a.ContentType.Alias.Equals(Constants.Conventions.MediaTypes.Image)))
            {
                string         relativeImagePath = ImagePathHelper.GetImageFilePath(media);
                AnalysisResult computervisionResult;

                // Computer Vision API
                using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                {
                    // Call the Computer Vision API
                    computervisionResult = visionServiceClient
                                           .AnalyzeImageAsync(
                        imageFileStream,
                        new[]
                    {
                        VisualFeature.Description,
                        VisualFeature.Adult,
                        VisualFeature.Tags,
                        VisualFeature.Categories
                    },
                        new[]
                    {
                        "celebrities", "landmarks"
                    }
                        ).Result;

                    // Get the result and set the values of the ContentItem
                    var celebrityTags = new List <string>();
                    var landmarksTags = new List <string>();

                    foreach (Category category in computervisionResult.Categories.Where(a => a.Detail != null))
                    {
                        var detailResult = JsonConvert.DeserializeObject <DetailsModel>(category.Detail.ToString());
                        celebrityTags.AddRange(detailResult.Celebrities.Select(a => a.Name));
                        landmarksTags.AddRange(detailResult.Landmarks.Select(a => a.Name));
                    }

                    IEnumerable <string> tags = computervisionResult.Tags.Select(a => a.Name);
                    string caption            = computervisionResult.Description.Captions.First().Text;
                    bool   isAdult            = computervisionResult.Adult.IsAdultContent;
                    bool   isRacy             = computervisionResult.Adult.IsRacyContent;

                    media.SetTags("tags", tags, true);
                    media.SetTags("celebrities", celebrityTags, true);
                    media.SetTags("landmarks", landmarksTags, true);
                    media.SetValue("description", caption);
                    media.SetValue("isAdult", isAdult);
                    media.SetValue("isRacy", isRacy);
                }

                // Computer Vision => OCR
                using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                {
                    var boundingBoxes = new List <Rectangle>();
                    var textLines     = new List <string>();


                    OcrResults result = visionServiceClient.RecognizeTextAsync(imageFileStream).Result;

                    if (result.Regions.Any())
                    {
                        boundingBoxes = result.Regions.SelectMany(a => a.Lines.Select(b => b.Rectangle)).ToList();
                        textLines.AddRange(result.Regions.SelectMany(a => a.Lines).Select(line => string.Join(" ", line.Words.Select(a => a.Text))));


                        var    totalarea          = (computervisionResult.Metadata.Height * computervisionResult.Metadata.Width);
                        var    coveredArea        = boundingBoxes.Sum(a => (a.Height * a.Width));
                        double percentageConvered = 100.0 / totalarea * coveredArea;

                        media.SetValue("hasText", true);
                        media.SetValue("textOnTheImage", string.Join("\n", textLines));
                        media.SetValue("percentageCovered", percentageConvered);
                    }
                }
            }
        }