Esempio n. 1
0
        private static void DrawPose(NcnnDotNet.OpenCV.Mat bgr, List <KeyPoint> keyPoints)
        {
            using var image = bgr.Clone();

            // draw bone
            var jointParts = new[]
            {
                new[] { 0, 1 },
                new[] { 1, 3 },
                new[] { 0, 2 },
                new[] { 2, 4 },
                new[] { 5, 6 },
                new[] { 5, 7 },
                new[] { 7, 9 },
                new[] { 6, 8 },
                new[] { 8, 10 },
                new[] { 5, 11 },
                new[] { 6, 12 },
                new[] { 11, 12 },
                new[] { 11, 13 },
                new[] { 12, 14 },
                new[] { 13, 15 },
                new[] { 14, 16 }
            };

            for (var i = 0; i < 16; i++)
            {
                var p1 = keyPoints[jointParts[i][0]];
                var p2 = keyPoints[jointParts[i][1]];

                if (p1.Prob < 0.2f || p2.Prob < 0.2f)
                {
                    continue;
                }

                Cv2.Line(image, p1.P, p2.P, new Scalar <double>(255, 0, 0), 2);
            }

            // draw joint
            for (var i = 0; i < keyPoints.Count; i++)
            {
                var keyPoint = keyPoints[i];

                Console.WriteLine($"{keyPoint.P.X:f2} {keyPoint.P.Y:f2} = {keyPoint.Prob:f5}");

                if (keyPoint.Prob < 0.2f)
                {
                    continue;
                }

                Cv2.Circle(image, keyPoint.P, 3, new Scalar <double>(0, 255, 0), -1);
            }

            Cv2.ImShow("image", image);
            Cv2.WaitKey(0);
        }
Esempio n. 2
0
        private static void DrawFaceObject(NcnnDotNet.OpenCV.Mat bgr, List <FaceObject> faceObjects)
        {
            using var image = bgr.Clone();

            for (var i = 0; i < faceObjects.Count; i++)
            {
                var obj = faceObjects[i];

                Console.WriteLine($"{obj.Prob:f5} at {obj.Rect.X:f2} {obj.Rect.Y:f2} {obj.Rect.Width:f2} x {obj.Rect.Height:f2}");

                Cv2.Rectangle(image, obj.Rect, new Scalar <double>(0, 255, 0));

                Cv2.Circle(image, obj.Landmark[0], 2, new Scalar <double>(0, 255, 255), -1);
                Cv2.Circle(image, obj.Landmark[1], 2, new Scalar <double>(0, 255, 255), -1);
                Cv2.Circle(image, obj.Landmark[2], 2, new Scalar <double>(0, 255, 255), -1);
                Cv2.Circle(image, obj.Landmark[3], 2, new Scalar <double>(0, 255, 255), -1);
                Cv2.Circle(image, obj.Landmark[4], 2, new Scalar <double>(0, 255, 255), -1);

                var text = $"{obj.Prob * 100:f1}%";

                var baseLine  = 0;
                var labelSize = Cv2.GetTextSize(text, CvHersheyFonts.HersheySimplex, 0.5, 1, ref baseLine);


                var x = (int)obj.Rect.X;
                var y = (int)(obj.Rect.Y - labelSize.Height - baseLine);
                if (y < 0)
                {
                    y = 0;
                }
                if (x + labelSize.Width > image.Cols)
                {
                    x = image.Cols - labelSize.Width;
                }

                Cv2.Rectangle(image, new Rect <int>(new Point <int>(x, y),
                                                    new Size <int>(labelSize.Width, labelSize.Height + baseLine)),
                              new Scalar <double>(255, 255, 255), -1);

                Cv2.PutText(image, text, new Point <int>(x, y + labelSize.Height),
                            CvHersheyFonts.HersheySimplex, 0.5, new Scalar <double>(0, 0, 0));
            }

            Cv2.ImShow("image", image);
            Cv2.WaitKey(0);
        }
Esempio n. 3
0
        private static void DrawObjects(NcnnDotNet.OpenCV.Mat bgr, List <Object> objects)
        {
            string[] classNames =
            {
                "background",
                "aeroplane",
                "bicycle",
                "bird",
                "boat",
                "bottle",
                "bus",
                "car",
                "cat",
                "chair",
                "cow",
                "diningtable",
                "dog",
                "horse",
                "motorbike",
                "person",
                "pottedplant",
                "sheep",
                "sofa",
                "train",
                "tvmonitor"
            };

            using var image = bgr.Clone();
            for (var i = 0; i < objects.Count; i++)
            {
                var obj = objects[i];

                Console.WriteLine($"{obj.Label} = {obj.Prob:f5} at {obj.Rect.X:f2} {obj.Rect.Y:f2} {obj.Rect.Width:f2} {obj.Rect.Height:f2}");

                Cv2.Rectangle(image, obj.Rect, new Scalar <double>(255, 0, 0));

                var text = $"{classNames[obj.Label]} {(obj.Prob * 100):f1}";

                var baseLine  = 0;
                var labelSize = Cv2.GetTextSize(text, CvHersheyFonts.HersheySimplex, 0.5, 1, ref baseLine);

                var x = (int)obj.Rect.X;
                var y = (int)(obj.Rect.Y - labelSize.Height - baseLine);
                if (y < 0)
                {
                    y = 0;
                }
                if (x + labelSize.Width > image.Cols)
                {
                    x = image.Cols - labelSize.Width;
                }

                Cv2.Rectangle(image, new Rect <int>(new Point <int>(x, y),
                                                    new Size <int>(labelSize.Width, labelSize.Height + baseLine)),
                              new Scalar <double>(255, 255, 255), -1);

                Cv2.PutText(image, text, new Point <int>(x, y + labelSize.Height),
                            CvHersheyFonts.HersheySimplex, 0.5, new Scalar <double>(0, 0, 0));
            }

            Cv2.ImShow("image", image);
            Cv2.WaitKey(0);
        }
Esempio n. 4
0
        private static void DrawObjects(NcnnDotNet.OpenCV.Mat bgr, List <Object> objects, NcnnDotNet.Mat map)
        {
            string[] classNames =
            {
                "background",
                "person",
                "rider",
                "car",
                "bus",
                "truck",
                "bike",
                "motor",
                "traffic light",
                "traffic sign",
                "train"
            };

            using var image = bgr.Clone();
            var color      = new[] { 128, 255, 128, 244, 35, 232 };
            var colorCount = color.Length;

            for (var i = 0; i < objects.Count; i++)
            {
                var obj = objects[i];

                Console.WriteLine($"{obj.Label} = {obj.Prob:f5} at {obj.Rect.X:f2} {obj.Rect.Y:f2} {obj.Rect.Width:f2} {obj.Rect.Height:f2}");

                Cv2.Rectangle(image, obj.Rect, new Scalar <double>(255, 0, 0));

                var text = $"{classNames[obj.Label]} {(obj.Prob * 100):f1}";

                var baseLine  = 0;
                var labelSize = Cv2.GetTextSize(text, CvHersheyFonts.HersheySimplex, 0.5, 1, ref baseLine);

                var x = (int)obj.Rect.X;
                var y = (int)(obj.Rect.Y - labelSize.Height - baseLine);
                if (y < 0)
                {
                    y = 0;
                }
                if (x + labelSize.Width > image.Cols)
                {
                    x = image.Cols - labelSize.Width;
                }

                Cv2.Rectangle(image, new Rect <int>(new Point <int>(x, y),
                                                    new Size <int>(labelSize.Width, labelSize.Height + baseLine)),
                              new Scalar <double>(255, 255, 255), -1);

                Cv2.PutText(image, text, new Point <int>(x, y + labelSize.Height),
                            CvHersheyFonts.HersheySimplex, 0.5, new Scalar <double>(0, 0, 0));
            }

            unsafe
            {
                var width     = map.W;
                var height    = map.H;
                var size      = map.C;
                var imgIndex2 = 0;
                var threshold = 0.45f;
                var ptr2      = (float *)map.Data;
                for (var i = 0; i < height; i++)
                {
                    var ptr1      = (byte *)image.Ptr(i);
                    var imgIndex1 = 0;
                    for (var j = 0; j < width; j++)
                    {
                        var maxImage = threshold;
                        var index    = -1;
                        for (var c = 0; c < size; c++)
                        {
                            //const float* ptr3 = map.channel(c);
                            var ptr3 = ptr2 + c * width * height;
                            if (ptr3[imgIndex2] > maxImage)
                            {
                                maxImage = ptr3[imgIndex2];
                                index    = c;
                            }
                        }

                        if (index > -1)
                        {
                            var colorIndex = index * 3;
                            if (colorIndex < colorCount)
                            {
                                var b = color[colorIndex];
                                var g = color[colorIndex + 1];
                                var r = color[colorIndex + 2];
                                ptr1[imgIndex1]     = (byte)(b / 2 + ptr1[imgIndex1] / 2);
                                ptr1[imgIndex1 + 1] = (byte)(g / 2 + ptr1[imgIndex1 + 1] / 2);
                                ptr1[imgIndex1 + 2] = (byte)(r / 2 + ptr1[imgIndex1 + 2] / 2);
                            }
                        }

                        imgIndex1 += 3;
                        imgIndex2++;
                    }
                }
            }

            Cv2.ImShow("image", image);
            Cv2.WaitKey(0);
        }