Exemplo n.º 1
0
        partial void stylizeClicked(NSObject sender)
        {
            messageLabel.StringValue = "stylize Clicked";
            //mainImageView.Image = null;

            StylizeGraph stylizeGraph = new StylizeGraph();

            Tensor image         = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile("surfers.jpg");
            Tensor stylizedImage = stylizeGraph.Stylize(image, 0);
            var    dim           = stylizedImage.Dim;

            System.Drawing.Size sz = new System.Drawing.Size(dim[2], dim[1]);

            byte[] rawPixels = Emgu.TF.Models.ImageIO.TensorToPixel(stylizedImage, 255, 4);

            CGColorSpace    cspace  = CGColorSpace.CreateDeviceRGB();
            CGBitmapContext context = new CGBitmapContext(
                rawPixels,
                sz.Width, sz.Height,
                8,
                sz.Width * 4,
                cspace,
                CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
            CGImage cgImage = context.ToImage();

            NSImage newImg = new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height));

            mainImageView.Image = newImg;

            NSData           dta    = newImg.AsTiff();
            NSBitmapImageRep imgRep = new NSBitmapImageRep(dta);
            //var datra = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.NSBitmapImageFileType.Jpeg);
        }
Exemplo n.º 2
0
        public StylizePage()
            : base()
        {
            _stylizeGraph = new StylizeGraph();
            _stylizeGraph.OnDownloadProgressChanged += onDownloadProgressChanged;
            _stylizeGraph.OnDownloadCompleted       += onDownloadCompleted;

            OnImagesLoaded += (sender, image) =>
            {
                try
                {
                    SetMessage("Please wait...");
                    SetImage();
                    Stopwatch watch = Stopwatch.StartNew();
                    byte[]    jpeg  = _stylizeGraph.StylizeToJpeg(image[0], 1);
                    watch.Stop();
                    SetImage(jpeg);
#if __MACOS__
                    NSImage img          = new NSImage(image[0]);
                    var     displayImage = this.GetImage();
                    displayImage.WidthRequest  = img.Size.Width;
                    displayImage.HeightRequest = img.Size.Height;
#endif
                    SetMessage(String.Format("Stylized in {0} milliseconds.", watch.ElapsedMilliseconds));
                }
                catch (Exception excpt)
                {
                    String msg = excpt.Message.Replace(System.Environment.NewLine, " ");
                    SetMessage(msg);
                }
            };
        }
Exemplo n.º 3
0
        public async Task TestStylize()
        {
            StylizeGraph stylizeGraph = new StylizeGraph();
            await stylizeGraph.Init();

            Tensor image         = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg");
            Tensor stylizedImage = stylizeGraph.Stylize(image, 0);
        }
Exemplo n.º 4
0
        partial void stylizeClicked(NSObject sender)
        {
            messageLabel.StringValue = "stylize Clicked";
            mainImageView.Image      = null;

            StylizeGraph stylizeGraph = new StylizeGraph();

            Tensor image         = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile("surfers.jpg");
            Tensor stylizedImage = stylizeGraph.Stylize(image, 0);
        }
Exemplo n.º 5
0
        public StylizePage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Stylizing image";
            button.Clicked += OnButtonClicked;

            OnImagesLoaded += async(sender, image) =>
            {
                SetMessage("Please wait...");
                SetImage();

                Task <Tuple <Tensor, string, long> > t = new Task <Tuple <Tensor, string, long> >(
                    () =>
                {
                    try
                    {
                        SetMessage("Please wait while we download the Stylize Model from internet.");
                        StylizeGraph stylizeGraph = new StylizeGraph();
                        SetMessage("Please wait...");

                        Tensor imageTensor   = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], -1, -1, 0f, 1.0f / 255f);
                        Tensor stylizedImage = stylizeGraph.Stylize(imageTensor, 0);
                        return(new Tuple <Tensor, string, long>(stylizedImage, null, 0));
                    }
                    catch (Exception e)
                    {
                        String msg = e.Message.Replace(System.Environment.NewLine, " ");
                        SetMessage(msg);
                        return(new Tuple <Tensor, string, long>(null, msg, 0));
                    }
                });
                t.Start();

                var result = await t;
                SetImage(Emgu.TF.Models.ImageIO.TensorToJpeg(t.Result.Item1, 255.0f));
                GetLabel().Text = t.Result.Item2;

                /*
                 #if __ANDROID__
                 * var result = await t;
                 * SetImage(t.Result.Item1);
                 * GetLabel().Text = t.Result.Item2;
                 #elif __IOS__
                 * var result = await t;
                 * SetImage(t.Result.Item1);
                 * GetLabel().Text = t.Result.Item2;
                 #elif !(__UNIFIED__)
                 * var result = await t;
                 * //SetImage(t.Result.Item1);
                 * GetLabel().Text = t.Result.Item2;
                 #endif*/
            };
        }
Exemplo n.º 6
0
        partial void stylizeClicked(NSObject sender)
        {
            SetMessage("Please wait while we download Stylize model from internet...");
            SetImage(null);

            if (_stylizeGraph == null)
            {
                _stylizeGraph = new StylizeGraph();
                _stylizeGraph.OnDownloadProgressChanged += OnDownloadProgressChanged;
                _stylizeGraph.OnDownloadCompleted       += stylizeGraph_OnDownloadCompleted;
            }
            _stylizeGraph.Init();
        }
Exemplo n.º 7
0
        public StylizePage()
            : base()
        {
            Title = "Stylize";

            if (_stylizeGraph == null)
            {
                SessionOptions so = new SessionOptions();
                if (TfInvoke.IsGoogleCudaEnabled)
                {
                    Tensorflow.ConfigProto config = new Tensorflow.ConfigProto();
                    config.GpuOptions             = new Tensorflow.GPUOptions();
                    config.GpuOptions.AllowGrowth = true;
                    so.SetConfig(config.ToProtobuf());
                }
                _stylizeGraph = new StylizeGraph(null, so);
                _stylizeGraph.OnDownloadProgressChanged += onDownloadProgressChanged;
                _stylizeGraph.OnDownloadCompleted       += onDownloadCompleted;
                _stylizeGraph.OnDownloadCompleted       += (sender, e) =>
                {
                    OnButtonClicked(sender, e);
                };
            }

            OnImagesLoaded += (sender, image) =>
            {
                try
                {
                    SetMessage("Please wait...");
                    SetImage();
                    Stopwatch watch = Stopwatch.StartNew();
                    byte[]    jpeg  = _stylizeGraph.StylizeToJpeg(image[0], 1);
                    watch.Stop();
                    SetImage(jpeg);
#if __MACOS__
                    NSImage img          = new NSImage(image[0]);
                    var     displayImage = this.GetImage();
                    displayImage.WidthRequest  = img.Size.Width;
                    displayImage.HeightRequest = img.Size.Height;
#endif
                    SetMessage(String.Format("Stylized in {0} milliseconds.", watch.ElapsedMilliseconds));
                }
                catch (Exception excpt)
                {
                    String msg = excpt.Message.Replace(System.Environment.NewLine, " ");
                    SetMessage(msg);
                }
            };
        }
Exemplo n.º 8
0
        async void stylizeClicked(NSObject sender)
        {
            SetMessage("Please wait while we download Stylize model from internet...");
            SetImage(null);

            if (_stylizeGraph == null)
            {
                _stylizeGraph = new StylizeGraph();
                _stylizeGraph.OnDownloadProgressChanged += OnDownloadProgressChanged;
                //_stylizeGraph.OnDownloadCompleted += stylizeGraph_OnDownloadCompleted;
            }
            await _stylizeGraph.Init();

            SetMessage("");

            byte[] jpeg = _stylizeGraph.StylizeToJpeg("surfers.jpg", 0);
            using (System.IO.Stream s = new System.IO.MemoryStream(jpeg))
            {
                NSImage newImg = NSImage.FromStream(s);
                SetImage(newImg);
            }
        }
Exemplo n.º 9
0
 public void TestStylize()
 {
     StylizeGraph stylizeGraph  = new StylizeGraph();
     Tensor       image         = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg");
     Tensor       stylizedImage = stylizeGraph.Stylize(image, 0);
 }
Exemplo n.º 10
0
        public StylizePage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Stylizing image";
            button.Clicked += OnButtonClicked;

            OnImagesLoaded += async(sender, image) =>
            {
                SetMessage("Please wait...");
                SetImage();

                Task <Tuple <byte[], string, long> > t = new Task <Tuple <byte[], string, long> >(
                    () =>
                {
                    try
                    {
                        SetMessage("Please wait while we download the Stylize Model from internet.");
                        StylizeGraph stylizeGraph = new StylizeGraph();
                        SetMessage("Please wait...");

                        Tensor imageTensor   = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], -1, -1, 0f, 1.0f / 255f);
                        Tensor stylizedImage = stylizeGraph.Stylize(imageTensor, 0);



#if __ANDROID__
                        byte[] rawPixel = Emgu.TF.Models.ImageIO.GetRawImage(stylizedImage, 255.0f, 4);
                        int[] dim       = stylizedImage.Dim;
                        using (Bitmap bitmap = Bitmap.CreateBitmap(dim[2], dim[1], Bitmap.Config.Argb8888))
                            using (MemoryStream ms = new MemoryStream())
                            {
                                IntPtr ptr = bitmap.LockPixels();
                                //GCHandle handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                                Marshal.Copy(rawPixel, 0, ptr, rawPixel.Length);

                                bitmap.UnlockPixels();

                                bitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                                return(new Tuple <byte[], string, long>(ms.ToArray(), null, 0));
                            }
#elif __IOS__
                        byte[] rawPixel        = Emgu.TF.Models.ImageIO.GetRawImage(stylizedImage, 255.0f);
                        int[] dim              = stylizedImage.Dim;
                        System.Drawing.Size sz = new System.Drawing.Size(dim[2], dim[1]);
                        GCHandle handle        = GCHandle.Alloc(rawPixel, GCHandleType.Pinned);
                        using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                            using (CGBitmapContext context = new CGBitmapContext(
                                       handle.AddrOfPinnedObject(),
                                       sz.Width, sz.Height,
                                       8,
                                       sz.Width * 3,
                                       cspace,
                                       CGImageAlphaInfo.PremultipliedLast))
                                using (CGImage cgImage = context.ToImage())
                                    using (UIImage newImg = new UIImage(cgImage))
                                    {
                                        handle.Free();
                                        var jpegData = newImg.AsJPEG();
                                        byte[] raw   = new byte[jpegData.Length];
                                        System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, raw, 0,
                                                                                    (int)jpegData.Length);
                                        return(new Tuple <byte[], string, long>(raw, String.Empty, 0));
                                    }
#else
                        return(new Tuple <byte[], string, long>(null, null, 0));
#endif

                        //SetImage(t.Result.Item1);
                        //GetLabel().Text = String.Format("Detected {0} in {1} milliseconds.", t.Result.Item2, t.Result.Item3);
                    }
                    catch (Exception e)
                    {
                        String msg = e.Message.Replace(System.Environment.NewLine, " ");
                        SetMessage(msg);
                        return(new Tuple <byte[], string, long>(null, msg, 0));
                    }
                });
                t.Start();

#if __ANDROID__
                var result = await t;
                SetImage(t.Result.Item1);
                GetLabel().Text = t.Result.Item2;
#elif __IOS__
                var result = await t;
                SetImage(t.Result.Item1);
                GetLabel().Text = t.Result.Item2;
#elif !(__UNIFIED__)
                var result = await t;
                //SetImage(t.Result.Item1);
                GetLabel().Text = t.Result.Item2;
#endif
            };
        }