Beispiel #1
0
        private void GenerateAndPrintCaption(ImageFile _imageFile)
        {
            try
            {
                SelectedImage.Source = _imageFile.ImageBmp;
                CaptionText.Text     = ImageCaptioner.CaptionImage(_imageFile);

                Run    r1   = new Run("File name: " + _imageFile.Name);
                string tags = "Tags: ";
                if (_imageFile.Tags != null)
                {
                    foreach (string tag in _imageFile.Tags)
                    {
                        tags += tag + ", ";
                    }
                    tags = tags.Remove(tags.Length - 2);
                }
                Run r2 = new Run(tags);
                Run r3 = new Run("Location: " + _imageFile.FullPath);

                FileInfo.Inlines.Clear();
                FileInfo.Inlines.Add(r1);
                FileInfo.Inlines.Add(new LineBreak());
                FileInfo.Inlines.Add(r2);
                FileInfo.Inlines.Add(new LineBreak());
                FileInfo.Inlines.Add(r3);

                MDCardCaption.Visibility  = Visibility.Visible;
                MDCardFileInfo.Visibility = Visibility.Visible;
                MDCardImage.Visibility    = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static string CaptionImageOnnx(ImageFile imageFile)
        {
            var image = Image.Load <Rgb24>(imageFile.FullPath, out IImageFormat format);

            bool modifyImageFile = false;

            if (!modifyImageFile)
            {
                image.Mutate(x =>
                {
                    x.Resize(new ResizeOptions
                    {
                        Size = new Size(299, 299),
                        Mode = ResizeMode.Crop
                    });
                });
            }
            else
            {
                using (Stream imageStream = new MemoryStream())
                {
                    image.Mutate(x =>
                    {
                        x.Resize(new ResizeOptions
                        {
                            Size = new Size(299, 299),
                            Mode = ResizeMode.Crop
                        });
                    });
                    image.Save(imageStream, format);
                };
            }

            Tensor <float> input  = new DenseTensor <float>(new[] { 1, 3, 299, 299 });
            var            mean   = new[] { 0.485f, 0.456f, 0.406f };
            var            stddev = new[] { 0.229f, 0.224f, 0.225f };

            for (int y = 0; y < image.Height; y++)
            {
                Span <Rgb24> pixelSpan = image.GetPixelRowSpan(y);
                for (int x = 0; x < image.Width; x++)
                {
                    input[0, 0, y, x] = ((pixelSpan[x].R / 255f) - mean[0]) / stddev[0];
                    input[0, 1, y, x] = ((pixelSpan[x].G / 255f) - mean[1]) / stddev[1];
                    input[0, 2, y, x] = ((pixelSpan[x].B / 255f) - mean[2]) / stddev[2];
                }
            }

            var inputs1 = new List <NamedOnnxValue>()
            {
                NamedOnnxValue.CreateFromTensor <float>("image", input)
            };

            var imgFtEx_session = new InferenceSession("C:\\Users\\jsell\\source\\repos\\FileSystemHelper\\ImageCaptionerPlugin\\Resources\\model\\ifem.onnx");
            var encoder_session = new InferenceSession("C:\\Users\\jsell\\source\\repos\\FileSystemHelper\\ImageCaptionerPlugin\\Resources\\model\\encoder.onnx");
            var decoder_session = new InferenceSession("C:\\Users\\jsell\\source\\repos\\FileSystemHelper\\ImageCaptionerPlugin\\Resources\\model\\decoder.onnx");

            // image feature extraction layer
            using (var outputs1 = imgFtEx_session.Run(inputs1))
            {
                var input2 = outputs1.First();
                input2.Name = "imageFeatures";
                var inputs2 = new List <NamedOnnxValue>()
                {
                    input2
                };
                Console.WriteLine(outputs1.Count);

                // encoder
                using (var outputs2 = encoder_session.Run(inputs2))
                {
                    // TODO
                }
            }

            return("Caption");
        }
Beispiel #3
0
        private void ProcessSelectedFiles(string[] fileNames)
        {
            /*
             * Handle improper file types
             */
            Form wf = new Form()
            {
                Size = new System.Drawing.Size(0, 0)
            };

            Task.Delay(TimeSpan.FromSeconds(7))
            .ContinueWith((t) => wf.Close(), TaskScheduler.FromCurrentSynchronizationContext());

            if (fileNames.Length == 1)
            {
                string fileExt = System.IO.Path.GetExtension(fileNames[0]);

                if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".bmp")
                {
                    AddProperties_Button.Visibility = Visibility.Visible;
                }
                else
                {
                    string message = "Unsupported file type selected: " + fileExt +
                                     "\nType must be .jpg, .bmp, or .png";
                    string caption = "Unsupported file type...";
                    System.Windows.Forms.MessageBox.Show(wf, message, caption);
                    return;
                }

                _activeImageFile = new ImageFile(fileNames[0]);
                GenerateAndPrintCaption(_activeImageFile);

                DragAndDrop.Visibility  = Visibility.Hidden;
                Copy_Button.Visibility  = Visibility.Visible;
                Clear_Button.Visibility = Visibility.Visible;
            }
            else if (fileNames.Length > 1)
            {
                string overallCaption = "";
                string overallPath    = System.IO.Path.GetDirectoryName(fileNames[0]);
                foreach (string filePath in fileNames)
                {
                    string fileExt = "";
                    try
                    {
                        fileExt = System.IO.Path.GetExtension(filePath);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    // Handle invalid file types
                    if (!"jpg|png|bmp".Contains(fileExt))
                    {
                        continue;
                    }

                    _activeImageFile = new ImageFile(filePath);
                    overallCaption  += ImageCaptioner.CaptionImage(_activeImageFile);
                }

                if (overallCaption.Length == 0)
                {
                    string message = "No supported file types were selected. " +
                                     "\nType must be .docx, .pdf, .odt, or .txt";
                    string caption = "Unsupported file types...";
                    System.Windows.Forms.MessageBox.Show(wf, message, caption);
                    return;
                }

                _activeImageFile = new ImageFile
                {
                    Caption   = overallCaption,
                    Name      = overallPath,
                    FullPath  = overallPath,
                    Extension = null
                };

                GenerateAndPrintCaption(_activeImageFile);
            }
        }