public Datum ProcessFrame(Mat image)                                              // gets result keypoints from OpenPoseDotNet.Mat
 {
     datumProcessed = opWrapper.EmplaceAndPop(image);                              // method detects on OpenPoseDotNet.Mat
     if (datumProcessed != null && datumProcessed.TryGet(out data) && !data.Empty) // if datumProcessed exists && we can get the data sucessfully && retrieved data exists
     {
         Datum result = data.ToArray()[0].Get();                                   // retrieve datum object which contains the keypoint data
         opWrapper.Dispose();                                                      // dispose of wrapper after detection
         return(result);
     }
     else
     {
         OpenPose.Log("Nullptr or empty datumsPtr found.", Priority.High);
         return(null);
     }
 }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(j => new StdVector <MModRect>(Enumerable.Range(0, size).Select(i => new MModRect {
                Ignore = true, DetectionConfidence = i
            })));
            var vector = new StdVector <StdVector <MModRect> >(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var j = 0; j < size; j++)
            {
                var tmp = ret[j].ToArray();
                for (var i = 0; i < size; i++)
                {
                    Assert.AreEqual(tmp[i].DetectionConfidence, i);
                    Assert.AreEqual(tmp[i].Ignore, true);
                }
            }
            this.DisposeAndCheckDisposedState(vector);
            foreach (var s in source)
            {
                s.Dispose();
            }
        }
Esempio n. 3
0
        // starts detector
        public override void Run(IProgress <DetectionResults> progress)
        {
            // detection loop - detects on each frame
            while (!userWantsToExit) // setting userWantsToExit to true breaks this loop
            {
                // get latest detected frame from input, which we set during the config
                if (opWrapper.WaitAndPop(out datumProcessed))                                     // detection data gets put into datumProcessed
                {
                    if (datumProcessed != null && datumProcessed.TryGet(out data) && !data.Empty) // if datumProcessed exists && we can get the data sucessfully && retrieved data exists
                    {
                        Datum d = data.ToArray()[0].Get();                                        // retrieve datum object which contains the keypoint data

                        progress.Report(new DetectionResults()                                    // report calculated keypoint data with DetectionResults object
                        {
                            data       = d,                                                       // keypoint data
                            isFinished = false                                                    // are we finished with the detection
                        });
                    }
                }
                else // can't get next frame, so we end the detection loop
                {
                    OpenPose.Log("Processed datum could not be emplaced.", Priority.High);
                    userWantsToExit = true;
                    break;
                }
            }

            progress.Report(new DetectionResults() // when finished, we report with isFinished set to true
            {
                data       = null,
                isFinished = true
            });

            opWrapper.Dispose(); // dispose of openpose wrapper
        }
Esempio n. 4
0
File: Box.cs Progetto: TrojanOlx/AI
            public IEnumerator <KeyValuePair <string, Point> > GetEnumerator()
            {
                this._Parent.ThrowIfDisposed();

                using (var strings = new StdVector <StdString>())
                    using (var points = new StdVector <Point>())
                    {
                        NativeMethods.image_dataset_metadata_box_get_parts_get_all(this._Parent.NativePtr, strings.NativePtr, points.NativePtr);

                        var stdStrings  = strings.ToArray();
                        var stringArray = stdStrings.Select(stdstr => StringHelper.FromStdString(stdstr.NativePtr)).ToArray();
                        var pointArray  = points.ToArray();

                        foreach (var stdString in stdStrings)
                        {
                            stdString.Dispose();
                        }

                        for (var index = 0; index < stringArray.Length; index++)
                        {
                            var s = stringArray[index];
                            var p = pointArray[index];
                            yield return(new KeyValuePair <string, Point>(s, p));
                        }
                    }
            }
Esempio n. 5
0
        public FullObjectDetection[] FaceLandmarks(Array2DBase image, Rectangle[] rects)
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var rectangles = new StdVector <Rectangle>(rects))
            {
                using (var dets = new StdVector <FullObjectDetection>())
                {
                    var ret = Native.face_recognition_face_landmarks(dets.NativePtr, this.NativePtr, inType, image.NativePtr, rectangles.NativePtr);
                    if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                    {
                        throw new ArgumentException($"Input {inType} is not supported.");
                    }

                    return(dets.ToArray());
                }
            }
        }
Esempio n. 6
0
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(j => new StdVector <Rectangle>(Enumerable.Range(0, size).Select(i => new Rectangle(i, i, i, i))));
            var       vector = new StdVector <StdVector <Rectangle> >(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var j = 0; j < size; j++)
            {
                var tmp = ret[j].ToArray();
                for (var i = 0; i < size; i++)
                {
                    Assert.AreEqual(tmp[i].Left, i);
                    Assert.AreEqual(tmp[i].Top, i);
                    Assert.AreEqual(tmp[i].Right, i);
                    Assert.AreEqual(tmp[i].Bottom, i);
                }
            }

            this.DisposeAndCheckDisposedState(vector);
            foreach (var s in source)
            {
                s.Dispose();
            }
        }
Esempio n. 7
0
        public Matrix <double>[] FaceEncodings(Array2DBase image, FullObjectDetection[] landmarks, double padding = 0.2d)
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (landmarks == null)
            {
                throw new ArgumentNullException(nameof(landmarks));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var rectangles = new StdVector <FullObjectDetection>((IEnumerable <FullObjectDetection>)landmarks))
            {
                using (var dets = new StdVector <Matrix <double> >())
                {
                    var ret = Native.face_recognition_face_encodings2(dets.NativePtr, this.NativePtr, inType, image.NativePtr, rectangles.NativePtr, padding);
                    if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                    {
                        throw new ArgumentException($"Input {inType} is not supported.");
                    }

                    return(dets.ToArray());
                }
            }
        }
        public StdSharedPtr <StdVector <UserDatum> > CreateDatum()
        {
            // Close program when empty frame
            if (this._Closed || this._ImageFiles.Length <= this._Counter)
            {
                OpenPose.Log("Last frame read and added to queue. Closing program after it is processed.", Priority.High);

                // This funtion stops this worker, which will eventually stop the whole thread system once all the
                // frames have been processed
                this._Closed = true;
                return(null);
            }
            else
            {
                // Create new datum
                var tmp = new StdVector <UserDatum>();
                tmp.EmplaceBack();
                var datumsPtr = new StdSharedPtr <StdVector <UserDatum> >(tmp);
                var datum     = tmp.ToArray()[0];

                // Fill datum
                using (var mat = Cv.ImRead(this._ImageFiles[this._Counter++]))
                    datum.CvInputData = mat;

                // If empty frame -> return nullptr
                if (datum.CvInputData.Empty)
                {
                    OpenPose.Log($"Empty frame detected on path: {this._ImageFiles[this._Counter - 1]}. Closing program.", Priority.High);
                    this._Closed = true;
                    datumsPtr    = null;
                }

                return(datumsPtr);
            }
        }
Esempio n. 9
0
        public IEnumerable <float[]> Probability <T>(IEnumerable <Matrix <T> > images, ulong batchSize = 128)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (!images.Any())
            {
                throw new ArgumentException();
            }
            if (images.Any(matrix => matrix == null))
            {
                throw new ArgumentException();
            }

            images.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var imageType);

            var templateRows    = images.First().TemplateRows;
            var templateColumns = images.First().TemplateColumns;

            var array = images.Select(matrix => matrix.NativePtr).ToArray();
            var ret   = NativeMethods.LossMulticlassLog_probability(this.NetworkType,
                                                                    this.NativePtr,
                                                                    imageType.ToNativeMatrixElementType(),
                                                                    array,
                                                                    array.Length,
                                                                    templateRows,
                                                                    templateColumns,
                                                                    (uint)batchSize,
                                                                    out var vecOut);

            using (var vector = new StdVector <float>(vecOut))
            {
                Cuda.ThrowCudaException(ret);
                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{imageType} is not supported.");
                }

                var probabilityArray = vector.ToArray();
                var batches          = (int)batchSize;
                var classes          = probabilityArray.Length / batches;

                var probability = new List <float[]>(Enumerable.Range(0, batches).Select <int, float[]>(i => null));
                for (var index = 0; index < batches; index++)
                {
                    probability[index] = probabilityArray.Skip(index * classes).Take(classes).ToArray();
                }

                return(probability);
            }
        }
Esempio n. 10
0
        public void Operator <T>(
            uint numCrops,
            IEnumerable <Matrix <T> > images,
            IEnumerable <IEnumerable <MModRect> > rects,
            out IEnumerable <Matrix <T> > crops,
            out IEnumerable <IEnumerable <MModRect> > cropRects)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            var count   = images.Count();
            var numRect = rects.Count();

            if (count != numRect)
            {
                throw new ArgumentException();
            }

            images.ThrowIfDisposed();
            rects.ThrowIfDisposed();

            using (var matrix = new Matrix <T>())
                using (var inImages = new StdVector <Matrix <T> >(images))
                    using (var disposer = new EnumerableDisposer <StdVector <MModRect> >(rects.Select(r => new StdVector <MModRect>(r))))
                        using (var inRects = new StdVector <StdVector <MModRect> >(disposer.Collection))
                            using (new EnumerableDisposer <StdVector <MModRect> >(inRects))
                                using (var outCrops = new StdVector <Matrix <T> >())
                                    using (var outCropRects = new StdVector <StdVector <MModRect> >())
                                        using (new EnumerableDisposer <StdVector <MModRect> >(outCropRects))
                                        {
                                            var type = matrix.MatrixElementType.ToNativeMatrixElementType();
                                            var ret  = NativeMethods.random_cropper_operator(this.NativePtr,
                                                                                             numCrops,
                                                                                             type,
                                                                                             inImages.NativePtr,
                                                                                             inRects.NativePtr,
                                                                                             outCrops.NativePtr,
                                                                                             outCropRects.NativePtr);
                                            if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport)
                                            {
                                                throw new ArgumentException($"{type} is not supported.");
                                            }

                                            crops     = outCrops.ToArray();
                                            cropRects = outCropRects.ToArray().Select(box => box.ToArray()).ToList();
                                        }
        }
Esempio n. 11
0
        public Spins GenSpin(StdMt19937 randomNumderEngine)
        {
            if (randomNumderEngine == null)
            {
                throw new ArgumentNullException(nameof(randomNumderEngine));
            }

            randomNumderEngine.ThrowIfDisposed();
            this.ThrowIfDisposed();

            var spins = NativeMethods.graph_Graph_gen_spin_mt19937(this.NativePtr, randomNumderEngine.NativePtr);

            using (var vector = new StdVector <int>(spins))
                return(new Spins(vector.ToArray().Select(v => new Spin(v))));
        }
Esempio n. 12
0
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(i => new StdString(i.ToString()));
            var       vector = new StdVector <StdString>(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.AreEqual(ret[i].ToString(), i.ToString());
            }
            this.DisposeAndCheckDisposedState(vector);
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).ToArray();
            var       vector = new StdVector <int>(source);

            Assert.Equal(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.Equal(ret[i], i);
            }
            this.DisposeAndCheckDisposedState(vector);
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(i => new Matrix <ushort>(i, i));
            var       vector = new StdVector <Matrix <ushort> >(source);

            Assert.Equal(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.Equal(ret[i].Rows, i);
                Assert.Equal(ret[i].Columns, i);
            }
            this.DisposeAndCheckDisposedState(vector);
        }
Esempio n. 15
0
        public static Spins GetSolution(Ising system)
        {
            if (system == null)
            {
                throw new ArgumentNullException(nameof(system));
            }

            system.ThrowIfDisposed();

            var ret = NativeMethods.result_get_solution(system.NativePtr,
                                                        system.IsingType,
                                                        system.GraphType,
                                                        system.FloatType,
                                                        out var spins);

            using (var vector = new StdVector <int>(spins))
                return(new Spins(vector.ToArray().Select(v => new Spin(v))));
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(i => new ChipDetails(new DRectangle(i, i, i, i), (uint)i));
            var       vector = new StdVector <ChipDetails>(source);

            Assert.Equal(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.Equal(ret[i].Rect.Left, i);
                Assert.Equal(ret[i].Rect.Top, i);
                Assert.Equal(ret[i].Rect.Right, i);
                Assert.Equal(ret[i].Rect.Bottom, i);
            }
            this.DisposeAndCheckDisposedState(vector);
        }
Esempio n. 17
0
        protected override StdSharedPtr <StdVector <UserDatum> > WorkProducer()
        {
            try
            {
                // Close program when empty frame
                if (this._ImageFiles.Length <= this._Counter)
                {
                    OpenPose.Log("Last frame read and added to queue. Closing program after it is processed.", Priority.High);
                    // This funtion stops this worker, which will eventually stop the whole thread system once all the
                    // frames have been processed
                    this.Stop();
                    return(null);
                }
                else
                {
                    // Create new datum
                    var tmp = new StdVector <UserDatum>();
                    tmp.EmplaceBack();
                    var datumsPtr = new StdSharedPtr <StdVector <UserDatum> >(tmp);
                    var datum     = tmp.ToArray()[0];

                    // Fill datum
                    using (var mat = Cv.ImRead(this._ImageFiles[this._Counter++]))
                        datum.CvInputData = mat;

                    // If empty frame -> return nullptr
                    if (datum.CvInputData.Empty)
                    {
                        OpenPose.Log($"Empty frame detected on path: {this._ImageFiles[this._Counter - 1]}. Closing program.", Priority.High);
                        this.Stop();
                        datumsPtr = null;
                    }

                    return(datumsPtr);
                }
            }
            catch (Exception e)
            {
                OpenPose.Log("Some kind of unexpected error happened.");
                this.Stop();
                OpenPose.Error(e.Message, -1, nameof(this.WorkProducer));
                return(null);
            }
        }
Esempio n. 18
0
        public string[] GetLabels()
        {
            this.ThrowIfDisposed();

            NativeMethods.LossMulticlassLog_get_label(this.NetworkType, this.NativePtr, out var label);
            if (label == IntPtr.Zero)
            {
                return(null);
            }

            using (var vector = new StdVector <StdString>(label))
            {
                var tmp = vector.ToArray();
                var ret = tmp.Select(s => s.ToString()).ToArray();
                foreach (var s in tmp)
                {
                    s.Dispose();
                }
                return(ret);
            }
        }
Esempio n. 19
0
        public void Operator <T>(Matrix <T> image,
                                 IEnumerable <MModRect> rects,
                                 out Matrix <T> crop,
                                 out IEnumerable <MModRect> cropRects)
            where T : struct
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            image.ThrowIfDisposed();
            rects.ThrowIfDisposed();

            using (var matrix = new Matrix <T>())
                using (var inRects = new StdVector <MModRect>(rects))
                    using (var outCropRects = new StdVector <MModRect>())
                    {
                        var type = matrix.MatrixElementType.ToNativeMatrixElementType();
                        var ret  = NativeMethods.random_cropper_operator2(this.NativePtr,
                                                                          type,
                                                                          image.NativePtr,
                                                                          inRects.NativePtr,
                                                                          out var outCrop,
                                                                          outCropRects.NativePtr);
                        if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport)
                        {
                            throw new ArgumentException($"{type} is not supported.");
                        }

                        crop      = new Matrix <T>(outCrop, image.TemplateRows, image.TemplateColumns);
                        cropRects = outCropRects.ToArray();
                    }
        }
Esempio n. 20
0
        public Rectangle[] FaceLocations(Array2DBase image, uint numberOfTimesToUpsample = 0, string model = "hog")
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var dets = new StdVector <Rectangle>())
            {
                var ret = Native.face_recognition_face_locations(dets.NativePtr, this.NativePtr, inType, image.NativePtr, numberOfTimesToUpsample, Encoding.UTF8.GetBytes(model));
                if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                {
                    throw new ArgumentException($"Input {inType} is not supported.");
                }

                return(dets.ToArray());
            }
        }
Esempio n. 21
0
        public void Operator <T>(
            uint numCrops,
            IEnumerable <Matrix <T> > images,
            IEnumerable <IEnumerable <MModRect> > rects,
            out IEnumerable <Matrix <T> > crops,
            out IEnumerable <IEnumerable <MModRect> > cropRects)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            var count   = images.Count();
            var numRect = rects.Count();

            if (count != numRect)
            {
                throw new ArgumentException();
            }

            List <StdVector <MModRect> > listOfVectorOfMModRect = null;

            try
            {
                listOfVectorOfMModRect = rects.Select(r => new StdVector <MModRect>(r)).ToList();

                using (var matrix = new Matrix <T>())
                    using (var inImages = new StdVector <Matrix <T> >(images))
                        using (var inRects = new StdVector <StdVector <MModRect> >(listOfVectorOfMModRect))
                            using (var outCrops = new StdVector <Matrix <T> >())
                                using (var outCropRects = new StdVector <StdVector <MModRect> >())
                                {
                                    var type = matrix.MatrixElementType.ToNativeMatrixElementType();
                                    var ret  = Native.random_cropper_operator(
                                        this.NativePtr,
                                        numCrops,
                                        type,
                                        inImages.NativePtr,
                                        inRects.NativePtr,
                                        outCrops.NativePtr,
                                        outCropRects.NativePtr);
                                    if (ret == Dlib.Native.ErrorType.ElementTypeNotSupport)
                                    {
                                        throw new ArgumentException($"{type} is not supported.");
                                    }

                                    crops     = outCrops.ToArray();
                                    cropRects = outCropRects.ToArray().Select(box => box.ToArray()).ToList();
                                }
            }
            finally
            {
                if (listOfVectorOfMModRect != null)
                {
                    foreach (var stdVector in listOfVectorOfMModRect)
                    {
                        stdVector?.Dispose();
                    }
                }
            }
        }