Example #1
0
        public static ObjectDetectionResult[] Detect(dynamic detector, Size supportedSize, Image <Rgb24> image)
        {
            if (detector is null)
            {
                throw new ArgumentNullException(nameof(detector));
            }
            if (image is null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var input = ImageTools.YoloPreprocess(new ObjectDetectionDataset.ClrEntry {
                Image = image.Clone(),
            }, supportedSize);
            var images = input.Image[np.newaxis, np.rest_of_the_axes].AsArray();

            IDictionary <string, Tensor> prediction = detector(tf.constant(images));

            _ArrayLike Get(string name) => prediction["tf_op_layer_" + name].numpy();

            ndarray <float> boxs       = Get(nameof(SelectedBoxesOutput.Boxes)).AsArray <float>();
            ndarray <float> scores     = Get(nameof(SelectedBoxesOutput.Scores)).AsArray <float>();
            ndarray <long>  classes    = Get(nameof(SelectedBoxesOutput.Classes)).AsArray <long>();
            ndarray <int>   detections = Get(nameof(SelectedBoxesOutput.Detections)).AsArray <int>();

            return(ObjectDetectionResult.FromCombinedNonMaxSuppressionBatch(
                       boxs, scores, classes, detections[0].AsScalar()));
        }
Example #2
0
        public static ObjectDetectionResult[] FromCombinedNonMaxSuppressionBatch(
            ndarray <float> boxes, ndarray <float> scores, ndarray <long> classes,
            int detectionCount)
        {
            var result = new ObjectDetectionResult[detectionCount];

            for (int detection = 0; detection < detectionCount; detection++)
            {
                result[detection] = new ObjectDetectionResult {
                    Class = checked ((int)classes[0, detection].AsScalar()),
                    Box   = ToBox(boxes[0, detection].AsArray()),
                    Score = scores[0, detection].AsScalar(),
                };
            }
            return(result);
        }
Example #3
0
        public static ObjectDetectionResult[] DetectRaw(Model rawDetector,
                                                        Size supportedSize, int classCount,
                                                        Image <Rgb24> image,
                                                        ReadOnlySpan <int> strides, Tensor <int> anchors,
                                                        ReadOnlySpan <float> xyScale,
                                                        float scoreThreshold = 0.2f)
        {
            if (rawDetector is null)
            {
                throw new ArgumentNullException(nameof(rawDetector));
            }
            if (image is null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var input = ImageTools.YoloPreprocess(new ObjectDetectionDataset.ClrEntry {
                Image = image.Clone(),
            }, supportedSize);
            var images = input.Image[np.newaxis, np.rest_of_the_axes].AsArray();

            IList <Tensor> prediction = rawDetector.__call__(images);

            Debug.Assert(prediction.Count == 3);
            var output = new YOLOv4.Output {
                SSBox = prediction[0],
                MBBox = prediction[1],
                LBBox = prediction[2],
            };
            var suppression = SelectBoxes(output, inputSize: supportedSize.Width, classCount: classCount,
                                          strides: strides, anchors: anchors,
                                          xyScale: xyScale,
                                          scoreThreshold: scoreThreshold);

            ndarray <float> boxs       = suppression.Boxes.numpy();
            ndarray <float> scores     = suppression.Scores.numpy();
            ndarray <long>  classes    = suppression.Classes.numpy();
            ndarray <int>   detections = suppression.Detections.numpy();

            return(ObjectDetectionResult.FromCombinedNonMaxSuppressionBatch(
                       boxs, scores, classes, detections[0].AsScalar()));
        }