Esempio n. 1
0
 public YoloWrapper(string configurationFilename, string weightsFilename,
                    string namesFilename, int gpu = 0)
 {
     objectTypeResolver = new YoloObjectTypeResolver(namesFilename);
     Initialize(configurationFilename, weightsFilename, gpu);
     container = new BboxContainer();
 }
Esempio n. 2
0
        private IEnumerable <YoloItem> Convert(BboxContainer container)
        {
            var yoloItems = new List <YoloItem>();

            foreach (var item in container.candidates.Where(o => o.h > 0 || o.w > 0))
            {
                if (!this._objectType.TryGetValue((int)item.obj_id, out var objectType))
                {
                    objectType = "unknown key";
                }

                var yoloItem = new YoloItem
                {
                    X          = (int)item.x,
                    Y          = (int)item.y,
                    Height     = (int)item.h,
                    Width      = (int)item.w,
                    Confidence = item.prob,
                    Type       = objectType
                };

                yoloItems.Add(yoloItem);
            }

            return(yoloItems);
        }
Esempio n. 3
0
        public IEnumerable <YoloItem> Track(IntPtr floatArrayPointer, int width, int height, int channel = 3)
        {
            var container = new BboxContainer();

            TrackObjectsGpu(floatArrayPointer, width, height, channel, ref container);
            return(Convert(container, objectTypeResolver));
        }
Esempio n. 4
0
        public IEnumerable <YoloItem> Detect(byte[] imageData)
        {
            var container = new BboxContainer();

            var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
            var pnt  = Marshal.AllocHGlobal(size);

            try
            {
                // Copy the array to unmanaged memory.
                Marshal.Copy(imageData, 0, pnt, imageData.Length);
                var count = 0;
                switch (this.DetectionSystem)
                {
                case DetectionSystem.CPU:
                    count = DetectImageCpu(pnt, imageData.Length, ref container);
                    break;

                case DetectionSystem.GPU:
                    count = DetectImageGpu(pnt, imageData.Length, ref container);
                    break;
                }
            }
            catch (Exception exception)
            {
                return(null);
            }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }

            return(this.Convert(container));
        }
Esempio n. 5
0
        public bbox_t[] Detect(string filename)
        {
            var container = new BboxContainer();
            var count     = DetectImage(filename, ref container);

            return(container.candidates);
        }
Esempio n. 6
0
        public bbox_t[] Detect(byte[] imageData)
        {
            var container = new BboxContainer();

            var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
            var pnt  = Marshal.AllocHGlobal(size);

            try
            {
                // Copy the array to unmanaged memory.
                Marshal.Copy(imageData, 0, pnt, imageData.Length);
                DetectImage(pnt, imageData.Length, ref container);
            }
            catch (Exception exception)
            {
                return(null);
            }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }

            return(container.candidates);
        }
Esempio n. 7
0
        public bbox_t[] Detect(byte[] imageData)
        {
            var container = new BboxContainer();

            var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
            var pnt  = Marshal.AllocHGlobal(size);

            try
            {
                // Copy the array to unmanaged memory.
                Marshal.Copy(imageData, 0, pnt, imageData.Length);
                var count = DetectImage(pnt, imageData.Length, ref container);
                if (count == -1)
                {
                    throw new NotSupportedException($"{YoloLibraryName} has no OpenCV support");
                }
            }
            catch (Exception exception)
            {
                return(null);
            }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }

            return(container.candidates);
        }
Esempio n. 8
0
        private IEnumerable <YoloItem> Convert(BboxContainer container)
        {
            var items = container.candidates.Where(o => o.h > 0 || o.w > 0).Select(o =>

                                                                                   new YoloItem
            {
                X          = (int)o.x,
                Y          = (int)o.y,
                Height     = (int)o.h,
                Width      = (int)o.w,
                Confidence = o.prob,
                Type       = this._objectTypeResolver.Resolve((int)o.obj_id)
            }
                                                                                   ).OrderBy(o => o.Y).ToList();;


            for (var i = 0; i < items.Count; i++)
            {
                if (i + 1 < items.Count)
                {
                    items[i].StandardDeviation = CalculateStandardDeviationTwoPoints(new int[] { items[i].Y, items[i + 1].Y });
                }
                else
                {
                    items[i].StandardDeviation = 0.0;
                }
            }
            return(items.OrderBy(o => o.Y));
        }
Esempio n. 9
0
        /// <summary>
        /// Detect objects on an image
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        /// <exception cref="FileNotFoundException">Thrown when the filepath is wrong</exception>
        public IEnumerable <YoloItem> Detect(string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new FileNotFoundException("Cannot find the file", filepath);
            }

            var container = new BboxContainer();
            var count     = 0;

            switch (this.DetectionSystem)
            {
            case DetectionSystem.CPU:
                count = DetectImageCpu(filepath, ref container);
                break;

            case DetectionSystem.GPU:
                count = DetectImageGpu(filepath, ref container);
                break;
            }

            if (count == -1)
            {
                throw new NotImplementedException("C++ dll compiled incorrectly");
            }

            return(this.Convert(container));
        }
Esempio n. 10
0
        /// <summary>
        /// Detect objects on an image
        /// </summary>
        /// <param name="imagePtr"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException">Thrown when the yolo_cpp dll is wrong compiled</exception>
        public IEnumerable <YoloItem> Detect(IntPtr imagePtr, int size)
        {
            var container = new BboxContainer();

            var count = 0;

            try
            {
                switch (this.DetectionSystem)
                {
                case DetectionSystem.CPU:
                    count = DetectImageCpu(imagePtr, size, ref container);
                    break;

                case DetectionSystem.GPU:
                    count = DetectImageGpu(imagePtr, size, ref container);
                    break;
                }
            }
            catch (Exception)
            {
                return(null);
            }

            if (count == -1)
            {
                throw new NotImplementedException("C++ dll compiled incorrectly");
            }

            return(this.Convert(container));
        }
Esempio n. 11
0
        public IEnumerable <YoloItem> Track(byte[] imageData)
        {
            if (!this._imageAnalyzer.IsValidImageFormat(imageData))
            {
                throw new Exception("Invalid image data, wrong image format");
            }

            var container = new BboxContainer();
            var size      = Marshal.SizeOf(imageData[0]) * imageData.Length;
            var pnt       = Marshal.AllocHGlobal(size);

            try
            {
                // Copy the array to unmanaged memory.
                Marshal.Copy(imageData, 0, pnt, imageData.Length);
                var count = 0;
                switch (this.DetectionSystem)
                {
                case DetectionSystem.CPU:
                    count = TrackImageCpu(pnt, imageData.Length, (float)0.3, (float)0.2, ref container);
                    break;

                case DetectionSystem.GPU:
                    switch (this.DnnMode)
                    {
                    case DNNMode.Frame:
                    case DNNMode.LT:
                        count = TrackImageGpuLt(pnt, imageData.Length, (float)0.3, (float)0.2, ref container);
                        break;

                    case DNNMode.CC:
                        count = TrackImageGpuCc(pnt, imageData.Length, (float)0.3, (float)0.2, ref container);
                        break;
                    }
                    break;
                }

                if (count == -1)
                {
                    throw new NotImplementedException("c++ dll compiled incorrectly");
                }
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }

            return(this.Convert(container));
        }
Esempio n. 12
0
        public bbox_t[] Track(bbox_t[] candiates)
        {
            var container = new BboxContainer()
            {
                candidates = candiates
            };
            var newContainer = new BboxContainer();
            var count        = TrackId(ref container, ref newContainer);

            return(newContainer.candidates);
        }
Esempio n. 13
0
        public IEnumerable <YoloItem> Detect(string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new FileNotFoundException("Cannot find the file", filepath);
            }
            var container = new BboxContainer();

            DetectImageGpu(filepath, ref container);
            return(Convert(container, objectTypeResolver));
        }
Esempio n. 14
0
 public static IEnumerable <YoloItem> Convert(BboxContainer container, YoloObjectTypeResolver objectTypeResolver) => container.candidates.Where(o => o.h > 0 || o.w > 0)
 .Select(item => new YoloItem
 {
     X          = (int)item.x,
     Y          = (int)item.y,
     Height     = (int)item.h,
     Width      = (int)item.w,
     Confidence = item.prob,
     FrameId    = (int)item.frames_counter,
     TrackId    = (int)item.track_id,
     Shape      = (YoloItem.ShapeType)item.shape,
     Type       = objectTypeResolver.Resolve((int)item.obj_id)
 }).ToList();
Esempio n. 15
0
        public bbox_t[] Detect(image_t data, float threshold)
        {
            var container = new BboxContainer();

            fixed(float *pData = data.data)
            {
                var internalData = new internal_image_t {
                    w = data.w, h = data.h, c = data.c, data = pData
                };
                var count = DetectTensor(internalData, threshold, ref container);

                return(container.candidates.Take(count).ToArray());
            }
        }
Esempio n. 16
0
        private IEnumerable <YoloItem> Convert(BboxContainer container)
        {
            return(container.candidates.Where(o => o.h > 0 || o.w > 0).Select(o =>

                                                                              new YoloItem
            {
                X = (int)o.x,
                Y = (int)o.y,
                Height = (int)o.h,
                Width = (int)o.w,
                Confidence = o.prob,
                Type = this._objectTypeResolver.Resolve((int)o.obj_id)
            }
                                                                              ));
        }
Esempio n. 17
0
        private IEnumerable <YoloItem> Convert(BboxContainer container)
        {
            var yoloItems = new List <YoloItem>();

            foreach (var item in container.candidates.Where(o => o.h > 0 || o.w > 0))
            {
                var objectType = this._objectType[(int)item.obj_id];
                var yoloItem   = new YoloItem()
                {
                    X = (int)item.x, Y = (int)item.y, Height = (int)item.h, Width = (int)item.w, Confidence = item.prob, Type = objectType
                };
                yoloItems.Add(yoloItem);
            }

            return(yoloItems);
        }
Esempio n. 18
0
        /// <summary>
        /// Detect objects on an image
        /// </summary>
        /// <param name="imageData"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException">Thrown when the yolo_cpp dll is wrong compiled</exception>
        public IEnumerable <YoloItem> Detect(IntPtr imageData, int size)
        {
            int  minHeaderSize = this._imageAnalyzer.MinHeaderSize;
            bool ok            = size >= minHeaderSize;

            if (ok)
            {
                Marshal.Copy(imageData, _headerBuffer, 0, minHeaderSize);
                ok = this._imageAnalyzer.IsValidImageFormat(_headerBuffer);
            }

            if (!ok)
            {
                throw new Exception("Invalid image data, wrong image format");
            }

            var container = new BboxContainer();

            var count = 0;

            try
            {
                switch (this.DetectionSystem)
                {
                case DetectionSystem.CPU:
                    count = DetectImageCpu(imageData, size, ref container);
                    break;

                case DetectionSystem.GPU:
                    count = DetectImageGpu(imageData, size, ref container);
                    break;
                }
            }
            catch (Exception)
            {
                return(null);
            }

            if (count == -1)
            {
                throw new NotImplementedException("C++ dll compiled incorrectly");
            }

            return(this.Convert(container));
        }
Esempio n. 19
0
        public IEnumerable <YoloItem> Detect(string filename)
        {
            var container = new BboxContainer();
            var count     = 0;

            switch (this.DetectionSystem)
            {
            case DetectionSystem.CPU:
                count = DetectImageCpu(filename, ref container);
                break;

            case DetectionSystem.GPU:
                count = DetectImageGpu(filename, ref container);
                break;
            }

            return(this.Convert(container));
        }
Esempio n. 20
0
        public bbox_t[] Detect()
        {
            var container = new BboxContainer();

            int CV_8UC3 = 16;  // OpenCV constant

            // using Emgu.CV;   // www.emgucv.com - cross platform .Net wrapper to the OpenCV image processing library.

            // Image<Bgr, byte> cvBitmap = new Image<Bgr, byte>(640, 480);
            // Image<Bgr, byte> cvBitmap = new Image<Bgr, byte>(System.Drawing.Bitmap bmp);
            // Image<Bgr, byte> cvBitmap = new Image<Bgr, byte>(string fileName);
            // etc..
            //
            //
            // Mat mat = cvBitmap.Mat;

            // int count = DetectImage(mat.Rows, mat.Cols, CV_8UC3, mat.DataPointer, ref container);

            return(container.candidates);
        }
Esempio n. 21
0
        /// <summary>
        /// Detect objects on an image
        /// </summary>
        /// <param name="imageData"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException">Thrown when the yolo_cpp dll is wrong compiled</exception>
        /// <exception cref="Exception">Thrown when the byte array is not a valid image</exception>
        public unsafe IEnumerable <YoloItem> Detect(byte[] imageData)
        {
            if (!_imageAnalyzer.IsValidImageFormat(imageData))
            {
                throw new Exception("Invalid image data, wrong image format");
            }

            var container = new BboxContainer();
            var count     = 0;

            try
            {
                fixed(byte *pnt = imageData)
                {
                    switch (DetectionSystem)
                    {
                    case DetectionSystem.CPU:
                        count = DetectImageCpu((IntPtr)pnt, imageData.Length, ref container);
                        break;

                    case DetectionSystem.GPU:
                        count = DetectImageGpu((IntPtr)pnt, imageData.Length, ref container);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return(null);
            }

            if (count == -1)
            {
                throw new NotImplementedException("C++ dll compiled incorrectly");
            }

            return(Convert(container));
        }
Esempio n. 22
0
        public IEnumerable <YoloItem> Track(string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new FileNotFoundException("Cannot find the file", filepath);
            }

            var container = new BboxContainer();
            var count     = 0;

            switch (this.DetectionSystem)
            {
            case DetectionSystem.CPU:
                count = TrackImageCpu(filepath, (float)0.3, (float)0.2, ref container);
                break;

            case DetectionSystem.GPU:
                switch (this.DnnMode)
                {
                case DNNMode.Frame:
                case DNNMode.LT:
                    count = TrackImageGpuLt(filepath, (float)0.3, (float)0.2, ref container);
                    break;

                case DNNMode.CC:
                    count = TrackImageGpuCc(filepath, (float)0.3, (float)0.2, ref container);
                    break;
                }
                break;
            }

            if (count == -1)
            {
                throw new NotImplementedException("c++ dll compiled incorrectly");
            }

            return(this.Convert(container));
        }
Esempio n. 23
0
    Convert(BboxContainer container, YoloObjectTypeResolver objectTypeResolver)
    {
        var yoloItems = new List <YoloItem>();

        foreach (var item in container.candidates)
        {
            if (item.w > 0 || item.h > 0)
            {
                yoloItems.Add(new YoloItem
                {
                    X          = (int)item.x,
                    Y          = (int)item.y,
                    Height     = (int)item.h,
                    Width      = (int)item.w,
                    Confidence = item.prob,
                    FrameId    = (int)item.frames_counter,
                    TrackId    = (int)item.track_id,
                    Shape      = (YoloItem.ShapeType)item.shape,
                    Type       = objectTypeResolver.Resolve((int)item.obj_id)
                });
            }
        }
        return(yoloItems);
    }
Esempio n. 24
0
 internal static extern int DetectImageGpu(IntPtr pArray, int nSize, ref BboxContainer container);
Esempio n. 25
0
 internal static extern int DetectImageGpu(string filename, ref BboxContainer container);
Esempio n. 26
0
 private static extern int DetectTensor(internal_image_t data, float threshold, ref BboxContainer container);
Esempio n. 27
0
 private static extern int TrackId(ref BboxContainer container, ref BboxContainer newContainer);
Esempio n. 28
0
 private static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container);
Esempio n. 29
0
 private static extern int DetectImage(string filename, ref BboxContainer container);
Esempio n. 30
0
 private static extern int TrackObjectsGpu(IntPtr pArray, int width, int height, int channel,
                                           ref BboxContainer container);