예제 #1
0
        public System.Tuple <int, int> GetDimensions()
        {
            throwNotAnImageException();

            var fs         = _fs.OpenFile(_path);
            var image      = Image.FromStream(fs);
            var fileWidth  = image.Width;
            var fileHeight = image.Height;

            fs.Close();
            image.Dispose();

            return(new System.Tuple <int, int>(fileWidth, fileHeight));
        }
예제 #2
0
        public ImageInfo(string relativePath)
        {
            _fs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();

            try
            {
                RelativePath = relativePath;

                //This get's the IFileSystem's path based on the URL (i.e. /media/blah/blah.jpg )
                Path = _fs.GetRelativePath(relativePath);

                using (var stream = _fs.OpenFile(Path))
                    using (image = Image.FromStream(stream))
                    {
                        var fileName = _fs.GetFileName(Path);
                        Name = fileName.Substring(0, fileName.LastIndexOf('.'));

                        DateStamp = _fs.GetLastModified(Path).Date;
                        Width     = image.Width;
                        Height    = image.Height;
                        Aspect    = (float)Width / Height;
                    }
            }
            catch (Exception)
            {
                Width  = 0;
                Height = 0;
                Aspect = 0;
            }
        }
예제 #3
0
        private static string DoResize(MediaFileSystem fileSystem, string path, string extension, int width, int height, int maxWidthHeight, string fileNameAddition)
        {
            var fs    = fileSystem.OpenFile(path);
            var image = Image.FromStream(fs);

            fs.Close();

            string fileNameThumb = String.IsNullOrEmpty(fileNameAddition) ?
                                   string.Format("{0}_UMBRACOSYSTHUMBNAIL.jpg", path.Substring(0, path.LastIndexOf("."))) :
                                   string.Format("{0}_{1}.jpg", path.Substring(0, path.LastIndexOf(".")), fileNameAddition);

            var thumb = GenerateThumbnail(fileSystem,
                                          image,
                                          maxWidthHeight,
                                          width,
                                          height,
                                          path,
                                          extension,
                                          fileNameThumb,
                                          maxWidthHeight == 0);

            fileNameThumb = thumb.Item3;

            image.Dispose();

            return(fileNameThumb);
        }
예제 #4
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);
                    }
                }
            }
        }
        public static void Execute(string sourceFile, string name, int cropX, int cropY, int cropWidth, int cropHeight, int sizeWidth, int sizeHeight, long quality)
        {

            if (!_fs.FileExists(sourceFile)) return;


            string path = string.Empty;

            //http or local filesystem
            if (sourceFile.Contains("/"))
                path = sourceFile.Substring(0, sourceFile.LastIndexOf('/'));
            else
                path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\'));

            // TODO: Make configurable and move to imageInfo
            //if(File.Exists(String.Format(@"{0}\{1}.jpg", path, name))) return;

            //Do we need this check as we are always working with images that are already in a folder??
            //DirectoryInfo di = new DirectoryInfo(path);
            //if (!di.Exists) di.Create();

            using (var stream = _fs.OpenFile(sourceFile))
            using (var image = Image.FromStream(stream))
            using (var croppedImage = CropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight)))
            using (var resizedImage = ResizeImage(croppedImage, new Size(sizeWidth, sizeHeight)))
            using (var b = new Bitmap(resizedImage))
            {
                SaveJpeg(String.Format("{0}/{1}.jpg", path, name), b, quality);
            }
           
        }
예제 #6
0
        public string GetTextFromAllPages(string pdfPath, MediaFileSystem mediaFileSystem, Action <Exception> onError)
        {
            var output = new StringWriter();

            try
            {
                using (var stream = mediaFileSystem.OpenFile(pdfPath))
                    using (var reader = new PdfReader(stream))
                    {
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            var result =
                                ExceptChars(
                                    PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()),
                                    UnsupportedRange.Value,
                                    ReplaceWithSpace);
                            output.Write(result + " ");
                        }
                    }
            }
            catch (Exception ex)
            {
                onError(ex);
            }

            return(output.ToString());
        }
예제 #7
0
        public void CopyFileStream(IMedia mediaToCopy, IMedia copiedMedia)
        {
            string path = mediaToCopy?.GetUrl();

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            string fullPath = _mediaFileSystem?.GetFullPath(path);

            if (string.IsNullOrEmpty(fullPath))
            {
                return;
            }

            try
            {
                using (var inStream = _mediaFileSystem.OpenFile(WebUtility.UrlDecode(fullPath)))
                {
                    inStream.Position = 0;
                    copiedMedia.Properties[UmbracoFileAlias].Value = null;
                    copiedMedia.SetValue(UmbracoFileAlias, Path.GetFileName(path), inStream);
                }
            }
            catch { return; }
        }
예제 #8
0
        private static Tuple <int, int> GetDimensions(MediaFileSystem fileSystem, string path)
        {
            var fs         = fileSystem.OpenFile(path);
            var image      = Image.FromStream(fs);
            var fileWidth  = image.Width;
            var fileHeight = image.Height;

            fs.Close();
            image.Dispose();

            return(new Tuple <int, int>(fileWidth, fileHeight));
        }
예제 #9
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.Where(a => a.ContentType.Alias.Equals(Constants.Conventions.MediaTypes.Image)))
            {
                string relativeImagePath = ImagePathHelper.GetImageFilePath(media);
                // string fullPath = _fs.GetFullPath(relativeImagePath);

                using (Stream imageFileStream = _fs.OpenFile(relativeImagePath))
                {
                    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())));
                        }
                    }
                }
            }
        }
예제 #10
0
        /// <summary>
        /// Populates the auto-fill properties of a content item, for a specified auto-fill configuration.
        /// </summary>
        /// <param name="content">The content item.</param>
        /// <param name="autoFillConfig">The auto-fill configuration.</param>
        /// <param name="filepath">The filesystem path to the uploaded file.</param>
        /// <remarks>The <paramref name="filepath"/> parameter is the path relative to the filesystem.</remarks>
        public void Populate(IContentBase content, IImagingAutoFillUploadField autoFillConfig, string filepath)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException("autoFillConfig");
            }

            // no file = reset, file = auto-fill
            if (filepath.IsNullOrWhiteSpace())
            {
                ResetProperties(content, autoFillConfig);
            }
            else
            {
                // if anything goes wrong, just reset the properties
                try
                {
                    using (var filestream = _mediaFileSystem.OpenFile(filepath))
                    {
                        var extension = (Path.GetExtension(filepath) ?? "").TrimStart('.');
                        var size      = _mediaFileSystem.IsImageFile(extension) ? (Size?)_mediaFileSystem.GetDimensions(filestream) : null;
                        SetProperties(content, autoFillConfig, size, filestream.Length, extension);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(typeof(UploadAutoFillProperties), "Could not populate upload auto-fill properties for file \""
                                  + filepath + "\".", ex);
                    ResetProperties(content, autoFillConfig);
                }
            }
        }
예제 #11
0
        private void MemberService_Saving(IMemberService sender, SaveEventArgs <IMember> e)
        {
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

            var faceServiceClient = new FaceServiceClient(_faceApiKey, _faceApiUrl);

            foreach (IMember member in e.SavedEntities)
            {
                var profileImage = member.GetValue <string>("profilePicture");

                if (!string.IsNullOrWhiteSpace(profileImage))
                {
                    var profileImageUdi   = Udi.Parse(profileImage);
                    var profileImageMedia = umbracoHelper.TypedMedia(profileImageUdi);

                    string fullPath = _fs.GetFullPath(profileImageMedia.Url);

                    /* Stap 2  -> Face API: Delete the person if exists */
                    if (!string.IsNullOrWhiteSpace(member.GetValue <string>("personId")))
                    {
                        try
                        {
                            var personId = Guid.Parse(member.GetValue <string>("personId"));
                            AsyncHelpers.RunSync(() => faceServiceClient.DeletePersonAsync(_faceApiGroup, personId));
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    /* Stap 3 -> Face API: Detect face and attributes */
                    using (Stream imageFileStream = _fs.OpenFile(fullPath))
                    {
                        Face[] detectface = AsyncHelpers.RunSync(
                            () => faceServiceClient.DetectAsync(imageFileStream,
                                                                false, false, new[]
                        {
                            FaceAttributeType.Age,
                            FaceAttributeType.Gender,
                            FaceAttributeType.Glasses,
                            FaceAttributeType.Makeup,
                            FaceAttributeType.Hair,
                        }));

                        // Getting values and setting the properties on the member
                        string age       = detectface.First().FaceAttributes.Age.ToString();
                        string gender    = detectface.First().FaceAttributes.Gender;
                        string glasses   = detectface.First().FaceAttributes.Glasses.ToString();
                        bool   eyeMakeup = detectface.First().FaceAttributes.Makeup.EyeMakeup;
                        bool   lipMakeup = detectface.First().FaceAttributes.Makeup.LipMakeup;

                        member.SetValue("Age", age);
                        member.SetValue("Gender", gender);
                        member.SetValue("glasses", glasses);
                        member.SetValue("eyeMakeup", eyeMakeup);
                        member.SetValue("lipMakeup", lipMakeup);
                    }

                    // ==> Stap 4 -> Create a person in the persongroup
                    CreatePersonResult person = AsyncHelpers.RunSync(() => faceServiceClient.CreatePersonAsync(_faceApiGroup, member.Name));

                    member.SetValue("personId", person.PersonId.ToString());

                    // ==> Stap 5 -> Add face to person and make persistent
                    using (Stream imageFileStream = _fs.OpenFile(fullPath))
                    {
                        AddPersistedFaceResult result = AsyncHelpers.RunSync(() => faceServiceClient.AddPersonFaceAsync(_faceApiGroup, person.PersonId, imageFileStream));
                        member.SetValue("faceId", result.PersistedFaceId.ToString());
                    }
                }
            }

            // ==> Stap 6 -> Train the facegroup
            AsyncHelpers.RunSync(() => faceServiceClient.TrainPersonGroupAsync(_faceApiGroup));
        }
예제 #12
0
        protected UrlData newMediaObjectLogic(
            string blogid,
            string username,
            string password,
            FileData file)
        {
            if (ValidateUser(username, password))
            {
                User    u           = new User(username);
                Channel userChannel = new Channel(username);
                UrlData fileUrl     = new UrlData();
                if (userChannel.ImageSupport)
                {
                    Media rootNode;
                    if (userChannel.MediaFolder > 0)
                    {
                        rootNode = new Media(userChannel.MediaFolder);
                    }
                    else
                    {
                        rootNode = new Media(u.StartMediaId);
                    }

                    // Create new media
                    Media m = Media.MakeNew(file.name, MediaType.GetByAlias(userChannel.MediaTypeAlias), u, rootNode.Id);

                    Property fileObject = m.getProperty(userChannel.MediaTypeFileProperty);

                    var filename         = file.name.Replace("/", "_");
                    var relativeFilePath = UmbracoMediaFactory.GetRelativePath(fileObject.Id, filename);

                    fileObject.Value = _fs.GetUrl(relativeFilePath);
                    fileUrl.url      = fileObject.Value.ToString();

                    if (!fileUrl.url.StartsWith("http"))
                    {
                        var protocol = GlobalSettings.UseSSL ? "https" : "http";
                        fileUrl.url = protocol + "://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + fileUrl.url;
                    }

                    _fs.AddFile(relativeFilePath, new MemoryStream(file.bits));

                    // Try updating standard file values
                    try
                    {
                        string orgExt = "";
                        // Size
                        if (m.getProperty(Constants.Conventions.Media.Bytes) != null)
                        {
                            m.getProperty(Constants.Conventions.Media.Bytes).Value = file.bits.Length;
                        }
                        // Extension
                        if (m.getProperty(Constants.Conventions.Media.Extension) != null)
                        {
                            orgExt =
                                ((string)
                                 file.name.Substring(file.name.LastIndexOf(".") + 1,
                                                     file.name.Length - file.name.LastIndexOf(".") - 1));
                            m.getProperty(Constants.Conventions.Media.Extension).Value = orgExt.ToLower();
                        }
                        // Width and Height
                        // Check if image and then get sizes, make thumb and update database
                        if (m.getProperty(Constants.Conventions.Media.Width) != null && m.getProperty(Constants.Conventions.Media.Height) != null &&
                            ",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + orgExt.ToLower() + ",") > 0)
                        {
                            int fileWidth;
                            int fileHeight;

                            using (var stream = _fs.OpenFile(relativeFilePath))
                            {
                                Image image = Image.FromStream(stream);
                                fileWidth  = image.Width;
                                fileHeight = image.Height;
                                stream.Close();
                                try
                                {
                                    m.getProperty(Constants.Conventions.Media.Width).Value  = fileWidth.ToString();
                                    m.getProperty(Constants.Conventions.Media.Height).Value = fileHeight.ToString();
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.Error <UmbracoMetaWeblogAPI>("An error occurred reading the media stream", ex);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error <UmbracoMetaWeblogAPI>("An error occurred in newMediaObjectLogic", ex);
                    }

                    return(fileUrl);
                }
                else
                {
                    throw new ArgumentException(
                              "Image Support is turned off in this channel. Modify channel settings in umbraco to enable image support.");
                }
            }
            return(new UrlData());
        }
        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);
                    }
                }
            }
        }