Beispiel #1
0
        public DnnDetectedObject[] ClassifyObjects(Mat image, Rect boxToAnalyze)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var blob = CvDnn.BlobFromImage(image, 1.0 / 255, new Size(320, 320), new Scalar(), crop: false);

            nnet.SetInput(blob);

            //create mats for output layer
            Mat[] outs = Enumerable.Repeat(false, _outNames.Length).Select(_ => new Mat()).ToArray();

            //forward model
            nnet.Forward(outs, _outNames);

            const float threshold    = 0.5f;    //for confidence
            const float nmsThreshold = 0.3f;    //threshold for nms

            var detections = ExtractYolo2Results(outs, image, threshold, nmsThreshold);

            blob.Dispose();

            foreach (var output in outs)
            {
                output.Dispose();
            }

            return(detections);
        }
        public DnnDetectedObject[][] ClassifyObjects(IEnumerable <Mat> images)
        {
            if (images is null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            var imageList = new List <Mat>(images);

            foreach (var image in imageList)
            {
                if (image?.Empty() == true)
                {
                    throw new ArgumentNullException(nameof(images), "One of the images is not initialized");
                }
            }


            using var blob = CvDnn.BlobFromImages(imageList, 1.0 / 255, new Size(320, 320), crop: false);
            nnet.SetInput(blob);

            //forward model
            nnet.Forward(outs, _outNames);

            if (imageList.Count == 1)
            {
                return(ExtractYolo3SingleResults(outs, imageList[0], threshold, nmsThreshold));
            }
            else
            {
                return(ExtractYolo3BatchedResults(outs, images, threshold, nmsThreshold));
            }
        }
        private IList <DnnDetectedObject[]> InternalClassifyObjects(IList <Mat> images, float detectionThreshold)
        {
            using var blob = CvDnn.BlobFromImages(images, scaleFactor, scaleSize, crop: false);
            nnet.SetInput(blob);

            //forward model
            nnet.Forward(outs, _outNames);

            if (images.Count == 1)
            {
                return(ExtractYoloSingleResults(outs, images[0], detectionThreshold, nmsThreshold));
            }
            else
            {
                return(ExtractYoloBatchedResults(outs, images, detectionThreshold, nmsThreshold));
            }
        }