Beispiel #1
0
        public byte[] DrawResultsToJpeg(String fileName, MultiboxGraph.Result detectResult, float scoreThreshold = 0.2f)
        {
#if __ANDROID__
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InMutable = true;
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options);
            MultiboxGraph.DrawResults(bmp, detectResult, scoreThreshold);
            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                return(ms.ToArray());
            }
#elif __MACOS__
            NSImage img = NSImage.ImageNamed(fileName);
            MultiboxGraph.DrawResults(img, detectResult, scoreThreshold);
            var    imageData = img.AsTiff();
            var    imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var    jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);
            byte[] jpeg      = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            return(jpeg);
#elif __IOS__
            UIImage uiimage = new UIImage(fileName);

            UIImage newImg   = MultiboxGraph.DrawResults(uiimage, detectResult, scoreThreshold);
            var     jpegData = newImg.AsJPEG();
            byte[]  jpeg     = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            return(jpeg);
#else
            throw new Exception("Not implemented");
#endif
        }
        private void ShowImageInfo(string filename)
        {
            var sb = new System.Text.StringBuilder();

            sb.AppendLine($"Filename: {filename}");

            var nsImage = new NSImage(filename);

            // 画像サイズ
            sb.AppendLine($"NSImage.Size: {nsImage.Size.Width} {nsImage.Size.Height}");

            //var nsImageRep = NSImageRep.ImageRepFromFile(filename);
            var nsImageRep = NSBitmapImageRep.ImageRepsWithData(nsImage.AsTiff())[0];

            // 画像サイズ
            sb.AppendLine($"NSImageRep.Size: {nsImageRep.Size.Width} {nsImageRep.Size.Height}");
            // ピクセル数
            sb.AppendLine($"NSImageRep.Pixels: {nsImageRep.PixelsWide} {nsImageRep.PixelsHigh}");

            // DPI
            var dpi = Math.Ceiling((72.0 * nsImageRep.PixelsWide) / nsImageRep.Size.Width);

            sb.AppendLine($"DPI = {dpi:F4}");

            Label.StringValue = sb.ToString();
        }
Beispiel #3
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
            NSImage img;

            if (File.Exists(fileName))
            {
                img = new NSImage(fileName); //full path
            }
            else
            {
                img = NSImage.ImageNamed(fileName); //image included in the package
            }
            if (annotations != null && annotations.Length > 0)
            {
                DrawAnnotations(img, annotations);
            }

            var imageData = img.AsTiff();
            var imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);

            byte[] jpeg = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);

            JpegData result = new JpegData();

            result.Raw    = jpeg;
            result.Width  = (int)img.Size.Width;
            result.Height = (int)img.Size.Height;

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and lables</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static byte[] ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
#if __ANDROID__
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InMutable = true;
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options);

            Android.Graphics.Paint p = new Android.Graphics.Paint();
            p.SetStyle(Paint.Style.Stroke);
            p.AntiAlias = true;
            p.Color     = Android.Graphics.Color.Red;
            Canvas c = new Canvas(bmp);

            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects           = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height);
                Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2], (int)rects[3]);
                c.DrawRect(r, p);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                return(ms.ToArray());
            }
#elif __MACOS__
            NSImage img = NSImage.ImageNamed(fileName);

            DrawAnnotations(img, annotations);

            /*
             * img.LockFocus();
             *
             * NSColor redColor = NSColor.Red;
             * redColor.Set();
             * var context = NSGraphicsContext.CurrentContext;
             * var cgcontext = context.CGContext;
             * cgcontext.ScaleCTM(1, -1);
             * cgcontext.TranslateCTM(0, -img.Size.Height);
             * //context.IsFlipped = !context.IsFlipped;
             * for (int i = 0; i < annotations.Length; i++)
             * {
             *  float[] rects = ScaleLocation(annotations[i].Rectangle, (int)img.Size.Width, (int) img.Size.Height);
             *  CGRect cgRect = new CGRect(
             *      rects[0],
             *      rects[1],
             *      rects[2] - rects[0],
             *      rects[3] - rects[1]);
             *  NSBezierPath.StrokeRect(cgRect);
             * }
             * img.UnlockFocus();
             */

            var    imageData = img.AsTiff();
            var    imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var    jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);
            byte[] jpeg      = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            return(jpeg);
#elif __IOS__
            UIImage uiimage = new UIImage(fileName);

            UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0);
            var context = UIGraphics.GetCurrentContext();

            uiimage.Draw(new CGPoint());
            context.SetStrokeColor(UIColor.Red.CGColor);
            context.SetLineWidth(2);
            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects = ScaleLocation(
                    annotations[i].Rectangle,
                    (int)uiimage.Size.Width,
                    (int)uiimage.Size.Height);
                CGRect cgRect = new CGRect(
                    (nfloat)rects[0],
                    (nfloat)rects[1],
                    (nfloat)(rects[2] - rects[0]),
                    (nfloat)(rects[3] - rects[1]));
                context.AddRect(cgRect);
                context.DrawPath(CGPathDrawingMode.Stroke);
            }
            UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            var    jpegData = imgWithRect.AsJPEG();
            byte[] jpeg     = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            return(jpeg);
#else
            if (Emgu.TF.Util.Platform.OperationSystem == OS.Windows)
            {
                Bitmap img = new Bitmap(fileName);

                if (annotations != null)
                {
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        for (int i = 0; i < annotations.Length; i++)
                        {
                            if (annotations[i].Rectangle != null)
                            {
                                float[]    rects  = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height);
                                PointF     origin = new PointF(rects[0], rects[1]);
                                RectangleF rect   = new RectangleF(rects[0], rects[1], rects[2] - rects[0], rects[3] - rects[1]);
                                Pen        redPen = new Pen(Color.Red, 3);
                                g.DrawRectangle(redPen, Rectangle.Round(rect));

                                String label = annotations[i].Label;
                                if (label != null)
                                {
                                    g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin);
                                }
                            }
                        }
                        g.Save();
                    }
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return(ms.ToArray());
                }
            }
            else
            {
                throw new Exception("DrawResultsToJpeg Not implemented for this platform");
            }
#endif
        }
Beispiel #5
0
        public MultiboxDetectionPage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Perform People Detection";
            button.Clicked += OnButtonClicked;

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

                Task <Tuple <byte[], long> > t = new Task <Tuple <byte[], long> >(
                    () =>
                {
                    //MultiboxGrapho.Download();
                    MultiboxGraph graph = new MultiboxGraph();
                    Tensor imageTensor  = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], 224, 224, 128.0f, 1.0f / 128.0f);
                    MultiboxGraph.Result detectResult = graph.Detect(imageTensor);
#if __ANDROID__
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.InMutable             = true;
                    Android.Graphics.Bitmap bmp   = BitmapFactory.DecodeFile(image[0], options);
                    MultiboxGraph.DrawResults(bmp, detectResult, 0.2f);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                        return(new Tuple <byte[], long>(ms.ToArray(), 0));
                    }
#elif __UNIFIED__ && !__IOS__
                    NSImage img = NSImage.ImageNamed(image[0]);

                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        MultiboxGraph.DrawResults(img, detectResult, 0.1f);
                        var imageData = img.AsTiff();
                        var imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
                        var jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);
                        byte[] raw    = new byte[jpegData.Length];
                        System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, raw, 0, (int)jpegData.Length);
                        SetImage(raw);
                        GetLabel().Text = String.Format("Detected with in {0} milliseconds.", 0);
                    });



                    return(new Tuple <byte[], long>(null, 0));
#elif __IOS__
                    UIImage uiimage = new UIImage(image[0]);

                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        UIImage newImg = MultiboxGraph.DrawResults(uiimage, detectResult, 0.1f);
                        var jpegData   = newImg.AsJPEG();
                        byte[] raw     = new byte[jpegData.Length];
                        System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, raw, 0, (int)jpegData.Length);
                        SetImage(raw);
                        GetLabel().Text = String.Format("Detected with in {0} milliseconds.", 0);
                    });

                    return(new Tuple <byte[], long>(null, 0));
#else
                    return(new Tuple <byte[], long>(new byte[10], 0));
#endif
                });
                t.Start();

#if !(__UNIFIED__)
                var result = await t;
                SetImage(t.Result.Item1);
                GetLabel().Text = String.Format("Detected with in {0} milliseconds.", t.Result.Item2);
#endif
            };
        }
Beispiel #6
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and lables</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
#if __ANDROID__
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InMutable = true;
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options);

            Android.Graphics.Paint p = new Android.Graphics.Paint();
            p.SetStyle(Paint.Style.Stroke);
            p.AntiAlias = true;
            p.Color     = Android.Graphics.Color.Red;
            Canvas c = new Canvas(bmp);

            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects           = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height);
                Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2], (int)rects[3]);
                c.DrawRect(r, p);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                JpegData result = new JpegData();
                result.Raw    = ms.ToArray();
                result.Width  = bmp.Width;
                result.Height = bmp.Height;
                return(result);
            }
#elif __MACOS__
            NSImage img = NSImage.ImageNamed(fileName);

            if (annotations != null && annotations.Length > 0)
            {
                DrawAnnotations(img, annotations);
            }

            var    imageData = img.AsTiff();
            var    imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var    jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);
            byte[] jpeg      = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);

            JpegData result = new JpegData();
            result.Raw    = jpeg;
            result.Width  = (int)img.Size.Width;
            result.Height = (int)img.Size.Height;

            return(result);
#elif __IOS__
            UIImage uiimage = new UIImage(fileName);

            UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0);
            var context = UIGraphics.GetCurrentContext();

            uiimage.Draw(new CGPoint());
            context.SetStrokeColor(UIColor.Red.CGColor);
            context.SetLineWidth(2);

            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects = ScaleLocation(
                    annotations[i].Rectangle,
                    (int)uiimage.Size.Width,
                    (int)uiimage.Size.Height);
                CGRect cgRect = new CGRect(
                    (nfloat)rects[0],
                    (nfloat)rects[1],
                    (nfloat)(rects[2] - rects[0]),
                    (nfloat)(rects[3] - rects[1]));
                context.AddRect(cgRect);
                context.DrawPath(CGPathDrawingMode.Stroke);
            }
            context.ScaleCTM(1, -1);
            context.TranslateCTM(0, -uiimage.Size.Height);
            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects = ScaleLocation(
                    annotations[i].Rectangle,
                    (int)uiimage.Size.Width,
                    (int)uiimage.Size.Height);
                context.SelectFont("Helvetica", 18, CGTextEncoding.MacRoman);
                context.SetFillColor((nfloat)1.0, (nfloat)0.0, (nfloat)0.0, (nfloat)1.0);
                context.SetTextDrawingMode(CGTextDrawingMode.Fill);
                context.ShowTextAtPoint(rects[0], uiimage.Size.Height - rects[1], annotations[i].Label);
            }
            UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            var    jpegData = imgWithRect.AsJPEG();
            byte[] jpeg     = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            JpegData result = new JpegData();
            result.Raw    = jpeg;
            result.Width  = (int)uiimage.Size.Width;
            result.Height = (int)uiimage.Size.Height;
            return(result);
#else
            Bitmap img = new Bitmap(fileName);

            if (annotations != null)
            {
                using (Graphics g = Graphics.FromImage(img))
                {
                    for (int i = 0; i < annotations.Length; i++)
                    {
                        if (annotations[i].Rectangle != null)
                        {
                            float[]    rects  = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height);
                            PointF     origin = new PointF(rects[0], rects[1]);
                            RectangleF rect   = new RectangleF(origin, new SizeF(rects[2] - rects[0], rects[3] - rects[1]));
                            Pen        redPen = new Pen(Color.Red, 3);
                            g.DrawRectangle(redPen, Rectangle.Round(rect));

                            String label = annotations[i].Label;
                            if (label != null)
                            {
                                g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin);
                            }
                        }
                    }
                    g.Save();
                }
            }

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                JpegData result = new JpegData();
                result.Raw    = ms.ToArray();
                result.Width  = img.Size.Width;
                result.Height = img.Size.Height;
                return(result);
            }
#endif
        }