コード例 #1
0
        public static void GetFilesAndSaveResized(string sourceDirectory, string destinationDirectory)
        {
            // Check if source directory exists
            if (!Directory.Exists(sourceDirectory))
            {
                throw new Exception($"SourceImageLocation {sourceDirectory} does not exist");
            }

            // Create directory if needed
            if (!Directory.Exists(destinationDirectory))
            {
                Console.WriteLine($"Creating destination directory {destinationDirectory}");
                Directory.CreateDirectory(destinationDirectory);
            }

            var filesToCopy = Directory.GetFiles(sourceDirectory, "*.jpg")
                              .Select(item => ImageFileDetails.CreateImageFileDetails(item))
                              .ToList();

            filesToCopy.RemoveAll(item => item == null);

            // Sort the files so that renaming with index is possible
            filesToCopy = filesToCopy.OrderBy(item => item.DateTimeTaken).ToList();

            Console.WriteLine($"Total files in source directory {sourceDirectory}: {filesToCopy.Count}");

            var index     = 0;
            var stopwatch = Stopwatch.StartNew();

            foreach (var fileToCopy in filesToCopy)
            {
                stopwatch.Restart();

                var  localFileName         = $"image_{index++.ToString("D4")}.jpg";
                var  destinationPath       = Path.Combine(destinationDirectory, localFileName);
                long downloadTimeInSeconds = 0;

                if (File.Exists(destinationPath))
                {
                    Console.WriteLine($"File {fileToCopy.FileName} already exists in save directory {destinationPath}");
                    continue;
                }

                // Load the image and save a resized version
                using (var image = Image.Load(fileToCopy.Path))
                {
                    downloadTimeInSeconds = stopwatch.ElapsedMilliseconds;
                    image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
                    image.Save(destinationPath);
                }

                var info = $"Finished copying and resizing file: {fileToCopy.FileName}. File {index}/{filesToCopy.Count}. Statistics {downloadTimeInSeconds} - {stopwatch.ElapsedMilliseconds}";
                Console.WriteLine(info);
            }
        }
コード例 #2
0
        public void Stub()
        {
            IGrabber  imageGrab = new ImageVideoGrabber.Grabber();
            ImageFile fileInput = new ImageFile();

            fileInput.FileName = txtFilePath.Text;
            //test1

            var result = imageGrab.ExtractTextFromImage(fileInput);
            var v      = result;

            //test2
            fileInput.FileName = txtFilePath.Text;
            List <Colors> result2 = imageGrab.GetImageColors(fileInput);
            var           v2      = result2;

            //test3
            VideoFileDetail videoFielOutput   = new VideoFileDetail();
            string          frameName         = Guid.NewGuid().ToString();
            string          outputImgFilePath = appStartPath + @"\bin\img\";
            string          filePath          = txtFilePath.Text.ToString().Trim();
            string          batchFilePath     = appStartPath + @"\ff-prompt.bat";

            videoFielOutput.ApplicationStartupPath = appStartPath;
            videoFielOutput.OutputImagePath        = outputImgFilePath;
            videoFielOutput.InputFilePath          = filePath;
            videoFielOutput.BatchFilePath          = batchFilePath;

            var result3 = imageGrab.GetVideoDetails(videoFielOutput);
            var v3      = result3;

            //test4
            ImageFileDetails imageFileDupCheck = new ImageFileDetails();
            string           targetDirPath     = @"D:\git-code\ImageProcessing\KantarImageProcessing\ImageProcessing\bin\Debug\bin\img";

            imageFileDupCheck.FilePath               = txtFilePath.Text.ToString();
            imageFileDupCheck.FileLength             = 100000;
            imageFileDupCheck.FolderPath             = targetDirPath;
            imageFileDupCheck.ApplicationStartupPath = appStartPath;
            var result4 = imageGrab.GetAllSimilarImages(imageFileDupCheck);
            var v4      = result4;

            //upload image
            imageGrab.UploadImageFile(txtFilePath.Text.ToString(), appStartPath);

            //download file
            string imagePath = txtFilePath.Text.ToString();

            imageGrab.DownloadFile(imagePath.Contains("\\") ? imagePath.Split('\\')[imagePath.Split('\\').Count() - 1] : imagePath);
        }
コード例 #3
0
        public void GetAllSimilarImagesForInValidStartupPath()
        {
            //Action
            ImageFileDetails imageObj = new ImageFileDetails();

            imageObj.ApplicationStartupPath = @"D:\invalid\git-code\ImageProcessing\KantarImageProcessing\ImageVideoProcessing\bin\Debug";
            imageObj.FileLength             = 100000;
            imageObj.FilePath   = @"D:\videos\bmp images\1111 - Copy (2).png";
            imageObj.FolderPath = @"D:\git-code\ImageProcessing\KantarImageProcessing\ImageVideoProcessing\bin\Debug";
            imageObj.Message    = "";

            //Act
            List <DuplicateImage> actualResult = _grab.GetAllSimilarImages(imageObj);

            //Assert
            Assert.IsTrue(actualResult.Count == 0, "Exception Occurred in GetAllSimilarImage with invalid application location path");
        }
コード例 #4
0
        public async Task GetEveryHourFile(string sourceDirectory)
        {
            var saveDir = "Files/EveryHour";

            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }

            var fileGroups = Directory.GetFiles(sourceDirectory, "*.jpg", SearchOption.AllDirectories)
                             .Select(item => ImageFileDetails.CreateImageFileDetails(item))
                             .Where(item => item != null)
                             .GroupBy(item => item.DateTimeTaken.Hour)
                             .ToList();

            foreach (var fileGroup in fileGroups)
            {
                var hour    = fileGroup.Key;
                var picture = fileGroup.OrderBy(item => item.DateTimeTaken).First();
            }
        }
コード例 #5
0
        public static async Task Execute(AppSettings appSettings, string workingDirectory)
        {
            var fileGroup = Directory.GetFiles(workingDirectory, "*.jpg", SearchOption.AllDirectories)
                            .Select(item => ImageFileDetails.CreateImageFileDetails(item))
                            .Where(item => item != null)
                            .GroupBy(item => item.DateTimeTaken.Date)
                            .ToList();

            foreach (var files in fileGroup)
            {
                var fileAfter = files
                                .OrderBy(item => item.DateTimeTaken)
                                .FirstOrDefault(item => item.DateTimeTaken.Hour >= 14);

                var fileBefore = files
                                 .OrderByDescending(item => item.DateTimeTaken)
                                 .FirstOrDefault(item => item.DateTimeTaken.Hour < 14);

                ImageFileDetails saveFile = null;
                if ((fileAfter?.DateTimeTaken.Hour ?? 23) - 14 <= 14 - (fileBefore?.DateTimeTaken.Hour ?? 0))
                {
                    saveFile = fileAfter;
                }

                if ((fileAfter?.DateTimeTaken.Hour ?? 23) - 14 >= 14 - (fileBefore?.DateTimeTaken.Hour ?? 0))
                {
                    saveFile = fileBefore;
                }

                if (saveFile == null && fileBefore != null)
                {
                    saveFile = fileBefore;
                }

                if (saveFile == null)
                {
                    return;
                }

                Console.WriteLine($"Hour1400 file: {saveFile.Path}");

                try
                {
                    using (var client = new Services.UplivingAPI {
                        BaseUri = new Uri(appSettings.ServiceApiLocation)
                    })
                    {
                        var fileName = $"{saveFile.DateTimeTaken.ToString("yyyy-MM-ddTHHmmss")}.jpg";

                        var uploadResponse = await client.Hour1400UploadWithHttpMessagesAsync(new Services.Models.Hour1400UploadRequest
                        {
                            Secret   = appSettings.Hour1400UploadSecret,
                            Bytes    = await File.ReadAllBytesAsync(saveFile.Path),
                            FileName = fileName
                        });
                    }
                }
                catch (Exception excep)
                {
                    Console.Error.WriteLine(excep.Message);
                }
            }
        }
コード例 #6
0
        public static void UnsortedFiles(AppSettings appSettings)
        {
            var filesToCopy = Directory.GetFiles(appSettings.UnsortedImagesDirectory, "*.jpg", SearchOption.AllDirectories)
                              .Select(item => ImageFileDetails.CreateImageFromEpochFile(item))
                              .ToList();

            filesToCopy.RemoveAll(item => item == null);
            filesToCopy = filesToCopy.Distinct().ToList();

            // Sort the files so that renaming with index is possible
            filesToCopy = filesToCopy.OrderBy(item => item.DateTimeTaken).ToList();

            var finishedDates = Directory.GetDirectories(appSettings.LocalImageLocation)
                                .Select(item => {
                DateTime.TryParseExact(
                    Path.GetFileName(item),
                    "yyyy-MM-dd",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.None, out var parsedDate);
                return(parsedDate);
            })
                                .ToList();

            filesToCopy.RemoveAll(item => finishedDates.Contains(item.DateTimeTaken.Date));

            Console.WriteLine($"Total files in directory {appSettings.UnsortedImagesDirectory}: {filesToCopy.Count}");

            var index        = 0;
            var overallIndex = 0;
            var stopwatch    = Stopwatch.StartNew();

            foreach (var fileToCopy in filesToCopy)
            {
                stopwatch.Restart();

                // Create destinationDirectory if needed and reset index (files are sorted)
                var destinationDirectory = Path.Combine(appSettings.LocalImageLocation, fileToCopy.DateTimeTaken.ToString("yyyy-MM-dd"));
                if (!Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);
                    index = 0;
                }

                var  localFileName         = $"image_{index++.ToString("D4")}.jpg";
                var  destinationPath       = Path.Combine(destinationDirectory, localFileName);
                long downloadTimeInSeconds = 0;
                try
                {
                    // Load the image and save a resized version
                    using (var image = Image.Load(fileToCopy.Path))
                    {
                        downloadTimeInSeconds = stopwatch.ElapsedMilliseconds;
                        image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
                        image.Save(destinationPath);
                    }

                    var info = $"Finished copying and resizing file: {fileToCopy.FileName} date {fileToCopy.DateTimeTaken.ToString("yyyy-MM-dd HH:mm:ss")}. " +
                               $"File {overallIndex++}/{filesToCopy.Count}. " +
                               $"Statistics {downloadTimeInSeconds} - {stopwatch.ElapsedMilliseconds}";
                    Console.WriteLine(info);
                }
                catch (Exception excep)
                {
                    index--;
                    Console.WriteLine(excep.Message);
                    // Oops, 0 kb file?
                }
            }
        }
コード例 #7
0
        public async Task Get1400HourFile(string sourceDirectory)
        {
            var saveDir = "Files/Hour1400";

            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }

            var fileGroup = Directory.GetFiles(sourceDirectory, "*.jpg", SearchOption.AllDirectories)
                            .Select(item => ImageFileDetails.CreateImageFileDetails(item))
                            .Where(item => item != null)
                            .GroupBy(item => item.DateTimeTaken.Date)
                            .ToList();

            foreach (var files in fileGroup)
            {
                var fileAfter = files
                                .OrderBy(item => item.DateTimeTaken)
                                .FirstOrDefault(item => item.DateTimeTaken.Hour >= 14);

                var fileBefore = files
                                 .OrderByDescending(item => item.DateTimeTaken)
                                 .FirstOrDefault(item => item.DateTimeTaken.Hour < 14);

                ImageFileDetails saveFile = null;
                if ((fileAfter?.DateTimeTaken.Hour ?? 23) - 14 <= 14 - (fileBefore?.DateTimeTaken.Hour ?? 0))
                {
                    saveFile = fileAfter;
                }

                if ((fileAfter?.DateTimeTaken.Hour ?? 23) - 14 >= 14 - (fileBefore?.DateTimeTaken.Hour ?? 0))
                {
                    saveFile = fileBefore;
                }

                if (saveFile == null && fileBefore != null)
                {
                    saveFile = fileBefore;
                }

                if (saveFile == null)
                {
                    continue;
                }

                Console.WriteLine($"Hour1400 file: {saveFile.Path}");

                try
                {
                    using (var client = new TimelapseMP4Webpage
                    {
                        BaseUri = new Uri(_appSettings.ServiceApiLocation)
                    })
                    {
                        // Load the image and save a resized version
                        using (var image = Image.Load(saveFile.Path))
                        {
                            var fileName = $"{saveFile.DateTimeTaken.ToString("yyyy-MM-ddTHHmmss")}.jpg";
                            image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
                            image.Save($"{saveDir}/{fileName}");

                            var uploadResponse = await client.ApiHour1400UploadPostAsync(new Services.Models.Hour1400UploadRequest
                            {
                                FileName = fileName,
                                Secret   = _appSettings.Hour1400UploadSecret,
                                Bytes    = File.ReadAllBytes($"{saveDir}/{fileName}")
                            });
                        }

                        // Load the image and save a thumbnail
                        using (var image = Image.Load(saveFile.Path))
                        {
                            var     height     = 200;
                            decimal widthRatio = image.Height / height;
                            int     width      = (int)Math.Round(image.Width / widthRatio, 0);
                            var     fileName   = $"{saveFile.DateTimeTaken.ToString("yyyy-MM-ddTHHmmss")}_thumb.jpg";

                            image.Mutate(x => x.Resize(width, height));
                            image.Save($"{saveDir}/{fileName}");

                            var uploadResponse = await client.ApiHour1400UploadPostAsync(new Services.Models.Hour1400UploadRequest
                            {
                                FileName = fileName,
                                Secret   = _appSettings.Hour1400UploadSecret,
                                Bytes    = File.ReadAllBytes($"{saveDir}/{fileName}")
                            });
                        }
                    }
                }
                catch (Exception excep)
                {
                    Console.WriteLine(excep.Message);
                }
            }
        }