Beispiel #1
0
        //javadoc: NMSBoxes(bboxes, scores, score_threshold, nms_threshold, indices)
        public static void NMSBoxes(MatOfRect2d bboxes, MatOfFloat scores, float score_threshold, float nms_threshold, MatOfInt indices)
        {
            if (bboxes != null)
            {
                bboxes.ThrowIfDisposed();
            }
            if (scores != null)
            {
                scores.ThrowIfDisposed();
            }
            if (indices != null)
            {
                indices.ThrowIfDisposed();
            }
#if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
            Mat bboxes_mat  = bboxes;
            Mat scores_mat  = scores;
            Mat indices_mat = indices;
            dnn_Dnn_NMSBoxes_15(bboxes_mat.nativeObj, scores_mat.nativeObj, score_threshold, nms_threshold, indices_mat.nativeObj);

            return;
#else
            return;
#endif
        }
        private void Init()
        {
            rgbMat = new Mat();

            if (capture.isOpened())
            {
                Debug.Log("capture.isOpened() true");
            }
            else
            {
                Debug.Log("capture.isOpened() false");
            }


            Debug.Log("CAP_PROP_FORMAT: " + capture.get(Videoio.CAP_PROP_FORMAT));
            Debug.Log("CV_CAP_PROP_PREVIEW_FORMAT: " + capture.get(Videoio.CV_CAP_PROP_PREVIEW_FORMAT));
            Debug.Log("CAP_PROP_POS_MSEC: " + capture.get(Videoio.CAP_PROP_POS_MSEC));
            Debug.Log("CAP_PROP_POS_FRAMES: " + capture.get(Videoio.CAP_PROP_POS_FRAMES));
            Debug.Log("CAP_PROP_POS_AVI_RATIO: " + capture.get(Videoio.CAP_PROP_POS_AVI_RATIO));
            Debug.Log("CAP_PROP_FRAME_COUNT: " + capture.get(Videoio.CAP_PROP_FRAME_COUNT));
            Debug.Log("CAP_PROP_FPS: " + capture.get(Videoio.CAP_PROP_FPS));
            Debug.Log("CAP_PROP_FRAME_WIDTH: " + capture.get(Videoio.CAP_PROP_FRAME_WIDTH));
            Debug.Log("CAP_PROP_FRAME_HEIGHT: " + capture.get(Videoio.CAP_PROP_FRAME_HEIGHT));

            capture.grab();
            capture.retrieve(rgbMat, 0);
            int frameWidth  = rgbMat.cols();
            int frameHeight = rgbMat.rows();

            colors  = new Color32[frameWidth * frameHeight];
            texture = new Texture2D(frameWidth, frameHeight, TextureFormat.RGBA32, false);
            gameObject.transform.localScale = new Vector3((float)frameWidth, (float)frameHeight, 1);
            float widthScale  = (float)Screen.width / (float)frameWidth;
            float heightScale = (float)Screen.height / (float)frameHeight;

            if (widthScale < heightScale)
            {
                Camera.main.orthographicSize = ((float)frameWidth * (float)Screen.height / (float)Screen.width) / 2;
            }
            else
            {
                Camera.main.orthographicSize = (float)frameHeight / 2;
            }
            capture.set(Videoio.CAP_PROP_POS_FRAMES, 0);


            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;


            trackers = new MultiTracker("KCF");
            objects  = new MatOfRect2d();

            trackingColorList = new List <Scalar> ();
            selectedPointList = new List <Point> ();
        }
Beispiel #3
0
        //
        // C++:  bool cv::MultiTracker::update(Mat image, vector_Rect2d& boundingBox)
        //

        /**
         * Update the current tracking status.
         * param image input image
         * param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
         * return automatically generated
         */
        public bool update(Mat image, MatOfRect2d boundingBox)
        {
            ThrowIfDisposed();
            if (image != null)
            {
                image.ThrowIfDisposed();
            }
            if (boundingBox != null)
            {
                boundingBox.ThrowIfDisposed();
            }
            Mat boundingBox_mat = boundingBox;

            return(tracking_MultiTracker_update_10(nativeObj, image.nativeObj, boundingBox_mat.nativeObj));
        }
        public void OnResetTrackerButton()
        {
            if (trackers != null)
            {
                trackers.Dispose();
                trackers = null;
            }
            if (objects != null)
            {
                objects.Dispose();
                objects = null;
            }

            trackers = new MultiTracker("KCF");
            objects  = new MatOfRect2d();
            trackingColorList.Clear();
            selectedPointList.Clear();
        }
        public override void OnWebCamTextureToMatHelperDisposed()
        {
            base.OnWebCamTextureToMatHelperDisposed();

            if (pb != null)
            {
                pb.dispose();
                pb = null;
            }

            if (boxes_m_c1 != null)
            {
                boxes_m_c1.Dispose();
            }
            if (boxes_m_c4 != null)
            {
                boxes_m_c4.Dispose();
            }
            if (confidences_m != null)
            {
                confidences_m.Dispose();
            }
            if (boxes != null)
            {
                boxes.Dispose();
            }
            if (confidences != null)
            {
                confidences.Dispose();
            }
            if (indices != null)
            {
                indices.Dispose();
            }

            boxes_m_c1    = null;
            boxes_m_c4    = null;
            confidences_m = null;
            boxes         = null;
            confidences   = null;
            indices       = null;
        }
        /// <summary>
        /// Raises the video capture to mat helper initialized event.
        /// </summary>
        public void OnVideoCaptureToMatHelperInitialized()
        {
            Debug.Log("OnVideoCaptureToMatHelperInitialized");

            Mat rgbMat = sourceToMatHelper.GetMat();

            texture = new Texture2D(rgbMat.cols(), rgbMat.rows(), TextureFormat.RGB24, false);
            Utils.fastMatToTexture2D(rgbMat, texture);

            gameObject.GetComponent <Renderer>().material.mainTexture = texture;

            gameObject.transform.localScale = new Vector3(rgbMat.cols(), rgbMat.rows(), 1);
            Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);


            float width  = rgbMat.width();
            float height = rgbMat.height();

            float widthScale  = (float)Screen.width / width;
            float heightScale = (float)Screen.height / height;

            if (widthScale < heightScale)
            {
                Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
            }
            else
            {
                Camera.main.orthographicSize = height / 2;
            }


            trackers = MultiTracker.create();
            objects  = new MatOfRect2d();

            trackingColorList = new List <Scalar>();
            selectedPointList = new List <Point>();
        }
Beispiel #7
0
        /// <summary>
        /// Postprocess the specified frame, outs and net.
        /// </summary>
        /// <param name="frame">Frame.</param>
        /// <param name="outs">Outs.</param>
        /// <param name="net">Net.</param>
        /// <param name="backend">Backend.</param>
        protected virtual void postprocess(Mat frame, List <Mat> outs, Net net, int backend = Dnn.DNN_BACKEND_OPENCV)
        {
            MatOfInt outLayers    = net.getUnconnectedOutLayers();
            string   outLayerType = outBlobTypes[0];

            List <int>    classIdsList    = new List <int>();
            List <float>  confidencesList = new List <float>();
            List <Rect2d> boxesList       = new List <Rect2d>();

            if (net.getLayer(new DictValue(0)).outputNameToIndex("im_info") != -1)
            {
                // Faster-RCNN or R-FCN
                // Network produces output blob with a shape 1x1xNx7 where N is a number of
                // detections and an every detection is a vector of values
                // [batchId, classId, confidence, left, top, right, bottom]

                if (outs.Count == 1)
                {
                    outs[0] = outs[0].reshape(1, (int)outs[0].total() / 7);

                    //Debug.Log ("outs[i].ToString() " + outs [0].ToString ());

                    float[] data = new float[7];

                    for (int i = 0; i < outs[0].rows(); i++)
                    {
                        outs[0].get(i, 0, data);

                        float confidence = data[2];
                        if (confidence > confThreshold)
                        {
                            int class_id = (int)(data[1]);

                            float left   = data[3] * frame.cols();
                            float top    = data[4] * frame.rows();
                            float right  = data[5] * frame.cols();
                            float bottom = data[6] * frame.rows();
                            float width  = right - left + 1f;
                            float height = bottom - top + 1f;

                            classIdsList.Add((int)(class_id) - 1); // Skip 0th background class id.
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else if (outLayerType == "DetectionOutput")
            {
                // Network produces output blob with a shape 1x1xNx7 where N is a number of
                // detections and an every detection is a vector of values
                // [batchId, classId, confidence, left, top, right, bottom]

                if (outs.Count == 1)
                {
                    outs[0] = outs[0].reshape(1, (int)outs[0].total() / 7);

                    //Debug.Log ("outs[i].ToString() " + outs [0].ToString ());

                    float[] data = new float[7];
                    for (int i = 0; i < outs[0].rows(); i++)
                    {
                        outs[0].get(i, 0, data);

                        float confidence = data[2];
                        if (confidence > confThreshold)
                        {
                            int class_id = (int)(data[1]);

                            float left   = data[3] * frame.cols();
                            float top    = data[4] * frame.rows();
                            float right  = data[5] * frame.cols();
                            float bottom = data[6] * frame.rows();
                            float width  = right - left + 1f;
                            float height = bottom - top + 1f;

                            classIdsList.Add((int)(class_id) - 1); // Skip 0th background class id.
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else if (outLayerType == "Region")
            {
                for (int i = 0; i < outs.Count; ++i)
                {
                    // Network produces output blob with a shape NxC where N is a number of
                    // detected objects and C is a number of classes + 4 where the first 4
                    // numbers are [center_x, center_y, width, height]

                    //Debug.Log ("outs[i].ToString() "+outs[i].ToString());

                    float[] positionData   = new float[5];
                    float[] confidenceData = new float[outs[i].cols() - 5];
                    for (int p = 0; p < outs[i].rows(); p++)
                    {
                        outs[i].get(p, 0, positionData);
                        outs[i].get(p, 5, confidenceData);

                        int   maxIdx     = confidenceData.Select((val, idx) => new { V = val, I = idx }).Aggregate((max, working) => (max.V > working.V) ? max : working).I;
                        float confidence = confidenceData[maxIdx];
                        if (confidence > confThreshold)
                        {
                            float centerX = positionData[0] * frame.cols();
                            float centerY = positionData[1] * frame.rows();
                            float width   = positionData[2] * frame.cols();
                            float height  = positionData[3] * frame.rows();
                            float left    = centerX - width / 2;
                            float top     = centerY - height / 2;

                            classIdsList.Add(maxIdx);
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else
            {
                Debug.Log("Unknown output layer type: " + outLayerType);
            }

            // NMS is used inside Region layer only on DNN_BACKEND_OPENCV for another backends we need NMS in sample
            // or NMS is required if number of outputs > 1
            if (outLayers.total() > 1 || (outLayerType == "Region" && backend != Dnn.DNN_BACKEND_OPENCV))
            {
                Dictionary <int, List <int> > class2indices = new Dictionary <int, List <int> >();
                for (int i = 0; i < classIdsList.Count; i++)
                {
                    if (confidencesList[i] >= confThreshold)
                    {
                        if (!class2indices.ContainsKey(classIdsList[i]))
                        {
                            class2indices.Add(classIdsList[i], new List <int>());
                        }

                        class2indices[classIdsList[i]].Add(i);
                    }
                }

                List <Rect2d> nmsBoxesList       = new List <Rect2d>();
                List <float>  nmsConfidencesList = new List <float>();
                List <int>    nmsClassIdsList    = new List <int>();
                foreach (int key in class2indices.Keys)
                {
                    List <Rect2d> localBoxesList       = new List <Rect2d>();
                    List <float>  localConfidencesList = new List <float>();
                    List <int>    classIndicesList     = class2indices[key];
                    for (int i = 0; i < classIndicesList.Count; i++)
                    {
                        localBoxesList.Add(boxesList[classIndicesList[i]]);
                        localConfidencesList.Add(confidencesList[classIndicesList[i]]);
                    }

                    using (MatOfRect2d localBoxes = new MatOfRect2d(localBoxesList.ToArray()))
                        using (MatOfFloat localConfidences = new MatOfFloat(localConfidencesList.ToArray()))
                            using (MatOfInt nmsIndices = new MatOfInt())
                            {
                                Dnn.NMSBoxes(localBoxes, localConfidences, confThreshold, nmsThreshold, nmsIndices);
                                for (int i = 0; i < nmsIndices.total(); i++)
                                {
                                    int idx = (int)nmsIndices.get(i, 0)[0];
                                    nmsBoxesList.Add(localBoxesList[idx]);
                                    nmsConfidencesList.Add(localConfidencesList[idx]);
                                    nmsClassIdsList.Add(key);
                                }
                            }
                }

                boxesList       = nmsBoxesList;
                classIdsList    = nmsClassIdsList;
                confidencesList = nmsConfidencesList;
            }

            for (int idx = 0; idx < boxesList.Count; ++idx)
            {
                Rect2d box = boxesList[idx];
                drawPred(classIdsList[idx], confidencesList[idx], box.x, box.y,
                         box.x + box.width, box.y + box.height, frame);
            }
        }
        protected override void postprocess(Mat frame, List <Mat> outs, Net net, int backend = Dnn.DNN_BACKEND_OPENCV)
        {
            List <int>     classIdsList    = new List <int>();
            List <float>   confidencesList = new List <float>();
            List <Rect2d>  boxesList       = new List <Rect2d>();
            List <Point[]> pointsList      = new List <Point[]>();

            if (outs.Count == 2)
            {
                // reshape mat : outs[0]:[1, x, 4] to [x, 4], outs[1]:[1, x, 2] to [x, 2]
                Mat boxes_m  = outs[0].reshape(1, new int[] { outs[0].size(1), outs[0].size(2) });
                Mat scores_m = outs[1].reshape(1, new int[] { outs[1].size(1), outs[1].size(2) });

                //Debug.Log("boxes_m: " + boxes_m);
                //Debug.Log("scores_m: " + scores_m);
                //Debug.Log("priors: " + priors);

                convertLocationsToBoxes(boxes_m, priors, 0.1f, 0.2f);
                centerFormToCornerForm(boxes_m);

                Mat     boxes_0_4 = new Mat(boxes_m, new Range(0, boxes_m.rows()), new Range(0, 4));
                float[] boxes_arr = new float[boxes_0_4.rows() * boxes_0_4.cols()];
                MatUtils.copyFromMat(boxes_0_4, boxes_arr);

                Mat     scores_1_2      = new Mat(scores_m, new Range(0, scores_m.rows()), new Range(1, 2));
                float[] confidences_arr = new float[scores_1_2.rows()];
                MatUtils.copyFromMat(scores_1_2, confidences_arr);

                for (int i = 0; i < boxes_m.rows(); i++)
                {
                    float confidence = confidences_arr[i];

                    if (confidence > confThreshold)
                    {
                        int boxes_index = i * 4;

                        float left   = boxes_arr[boxes_index] * frame.cols();
                        float top    = boxes_arr[boxes_index + 1] * frame.rows();
                        float right  = boxes_arr[boxes_index + 2] * frame.cols();
                        float bottom = boxes_arr[boxes_index + 3] * frame.rows();
                        float width  = right - left + 1f;
                        float height = bottom - top + 1f;

                        classIdsList.Add(0);
                        confidencesList.Add(confidence);
                        boxesList.Add(new Rect2d(left, top, width, height));
                    }
                }

                if (boxes_m.cols() > 4 && boxes_m.cols() % 2 == 0)
                {
                    Mat     points     = new Mat(boxes_m, new Range(0, boxes_m.rows()), new Range(4, boxes_m.cols()));
                    float[] points_arr = new float[points.rows() * points.cols()];
                    MatUtils.copyFromMat(points, points_arr);

                    for (int i = 0; i < boxes_m.rows(); i++)
                    {
                        float confidence = confidences_arr[i];

                        if (confidence > confThreshold)
                        {
                            int points_index = i * points.cols();

                            Point[] p_arr = new Point[points.cols() / 2];
                            for (int index = 0; index < points.cols() / 2; index++)
                            {
                                float x = points_arr[points_index + index * 2] * frame.cols();
                                float y = points_arr[points_index + index * 2 + 1] * frame.rows();
                                p_arr[index] = new Point(x, y);
                            }
                            pointsList.Add(p_arr);
                        }
                    }
                }
            }

            MatOfRect2d boxes = new MatOfRect2d();

            boxes.fromList(boxesList);

            MatOfFloat confidences = new MatOfFloat();

            confidences.fromList(confidencesList);

            MatOfInt indices = new MatOfInt();

            Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);

            //Debug.Log("indices.dump () " + indices.dump());
            //Debug.Log ("indices.ToString () "+indices.ToString());

            for (int i = 0; i < indices.total(); ++i)
            {
                int    idx = (int)indices.get(i, 0)[0];
                Rect2d box = boxesList[idx];
                drawPred(classIdsList[idx], confidencesList[idx], box.x, box.y,
                         box.x + box.width, box.y + box.height, frame);

                if (pointsList.Count > 0)
                {
                    drawPredPoints(pointsList[idx], frame);
                }
            }

            indices.Dispose();
            boxes.Dispose();
            confidences.Dispose();
        }
        /// <summary>
        /// Postprocess the specified frame, outs and net.
        /// </summary>
        /// <param name="frame">Frame.</param>
        /// <param name="outs">Outs.</param>
        /// <param name="net">Net.</param>
        protected virtual void postprocess(Mat frame, List <Mat> outs, Net net)
        {
            string outLayerType = outBlobTypes[0];


            List <int>    classIdsList    = new List <int>();
            List <float>  confidencesList = new List <float>();
            List <Rect2d> boxesList       = new List <Rect2d>();

            if (net.getLayer(new DictValue(0)).outputNameToIndex("im_info") != -1)
            {
                // Faster-RCNN or R-FCN
                // Network produces output blob with a shape 1x1xNx7 where N is a number of
                // detections and an every detection is a vector of values
                // [batchId, classId, confidence, left, top, right, bottom]

                if (outs.Count == 1)
                {
                    outs[0] = outs[0].reshape(1, (int)outs[0].total() / 7);

                    //Debug.Log ("outs[i].ToString() " + outs [0].ToString ());

                    float[] data = new float[7];

                    for (int i = 0; i < outs[0].rows(); i++)
                    {
                        outs[0].get(i, 0, data);

                        float confidence = data[2];

                        if (confidence > confThreshold)
                        {
                            int class_id = (int)(data[1]);

                            float left   = data[3] * frame.cols();
                            float top    = data[4] * frame.rows();
                            float right  = data[5] * frame.cols();
                            float bottom = data[6] * frame.rows();
                            float width  = right - left + 1f;
                            float height = bottom - top + 1f;

                            classIdsList.Add((int)(class_id) - 0);
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else if (outLayerType == "DetectionOutput")
            {
                // Network produces output blob with a shape 1x1xNx7 where N is a number of
                // detections and an every detection is a vector of values
                // [batchId, classId, confidence, left, top, right, bottom]

                if (outs.Count == 1)
                {
                    outs[0] = outs[0].reshape(1, (int)outs[0].total() / 7);

                    //Debug.Log ("outs[i].ToString() " + outs [0].ToString ());

                    float[] data = new float[7];

                    for (int i = 0; i < outs[0].rows(); i++)
                    {
                        outs[0].get(i, 0, data);

                        float confidence = data[2];

                        if (confidence > confThreshold)
                        {
                            int class_id = (int)(data[1]);

                            float left   = data[3] * frame.cols();
                            float top    = data[4] * frame.rows();
                            float right  = data[5] * frame.cols();
                            float bottom = data[6] * frame.rows();
                            float width  = right - left + 1f;
                            float height = bottom - top + 1f;

                            classIdsList.Add((int)(class_id) - 0);
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else if (outLayerType == "Region")
            {
                for (int i = 0; i < outs.Count; ++i)
                {
                    // Network produces output blob with a shape NxC where N is a number of
                    // detected objects and C is a number of classes + 4 where the first 4
                    // numbers are [center_x, center_y, width, height]

                    //Debug.Log ("outs[i].ToString() "+outs[i].ToString());

                    float[] positionData   = new float[5];
                    float[] confidenceData = new float[outs[i].cols() - 5];

                    for (int p = 0; p < outs[i].rows(); p++)
                    {
                        outs[i].get(p, 0, positionData);

                        outs[i].get(p, 5, confidenceData);

                        int   maxIdx     = confidenceData.Select((val, idx) => new { V = val, I = idx }).Aggregate((max, working) => (max.V > working.V) ? max : working).I;
                        float confidence = confidenceData[maxIdx];

                        if (confidence > confThreshold)
                        {
                            float centerX = positionData[0] * frame.cols();
                            float centerY = positionData[1] * frame.rows();
                            float width   = positionData[2] * frame.cols();
                            float height  = positionData[3] * frame.rows();
                            float left    = centerX - width / 2;
                            float top     = centerY - height / 2;

                            classIdsList.Add(maxIdx);
                            confidencesList.Add((float)confidence);
                            boxesList.Add(new Rect2d(left, top, width, height));
                        }
                    }
                }
            }
            else
            {
                Debug.Log("Unknown output layer type: " + outLayerType);
            }


            MatOfRect2d boxes = new MatOfRect2d();

            boxes.fromList(boxesList);

            MatOfFloat confidences = new MatOfFloat();

            confidences.fromList(confidencesList);


            MatOfInt indices = new MatOfInt();

            Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);

            //Debug.Log ("indices.dump () "+indices.dump ());
            //Debug.Log ("indices.ToString () "+indices.ToString());

            for (int i = 0; i < indices.total(); ++i)
            {
                int    idx = (int)indices.get(i, 0)[0];
                Rect2d box = boxesList[idx];
                drawPred(classIdsList[idx], confidencesList[idx], box.x, box.y,
                         box.x + box.width, box.y + box.height, frame);
            }

            indices.Dispose();
            boxes.Dispose();
            confidences.Dispose();
        }
        protected override void postprocess(Mat frame, List <Mat> outs, Net net, int backend = Dnn.DNN_BACKEND_OPENCV)
        {
            // # Decode bboxes and landmarks
            Mat dets = pb.decode(outs[0], outs[1], outs[2]);


            // # Ignore low scores + NMS
            int num = dets.rows();

            if (boxes_m_c1 == null)
            {
                boxes_m_c1 = new Mat(num, 4, CvType.CV_64FC1);
            }
            if (boxes_m_c4 == null)
            {
                boxes_m_c4 = new Mat(num, 1, CvType.CV_64FC4);
            }
            if (confidences_m == null)
            {
                confidences_m = new Mat(num, 1, CvType.CV_32FC1);
            }

            if (boxes == null)
            {
                boxes = new MatOfRect2d(boxes_m_c4);
            }
            if (confidences == null)
            {
                confidences = new MatOfFloat(confidences_m);
            }
            if (indices == null)
            {
                indices = new MatOfInt();
            }

            Mat bboxes = dets.colRange(0, 4);

            bboxes.convertTo(boxes_m_c1, CvType.CV_64FC1);

            // x1,y1,x2,y2 => x,y,w,h
            Mat boxes_m_0_2 = boxes_m_c1.colRange(0, 2);
            Mat boxes_m_2_4 = boxes_m_c1.colRange(2, 4);

            Core.subtract(boxes_m_2_4, boxes_m_0_2, boxes_m_2_4);

            MatUtils.copyToMat(new IntPtr(boxes_m_c1.dataAddr()), boxes_m_c4);

            Mat scores = dets.colRange(14, 15);

            scores.copyTo(confidences_m);

            Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);


            // # Draw boudning boxes and landmarks on the original image
            for (int i = 0; i < indices.total(); ++i)
            {
                int idx = (int)indices.get(i, 0)[0];

                float[] bbox_arr = new float[4];
                bboxes.get(idx, 0, bbox_arr);
                float[] confidence_arr = new float[1];
                confidences.get(idx, 0, confidence_arr);
                drawPred(0, confidence_arr[0], bbox_arr[0], bbox_arr[1], bbox_arr[2], bbox_arr[3], frame);

                Mat     landmarks     = dets.colRange(4, 14);
                float[] landmarks_arr = new float[10];
                landmarks.get(idx, 0, landmarks_arr);
                Point[] points = new Point[] { new Point(landmarks_arr[0], landmarks_arr[1]), new Point(landmarks_arr[2], landmarks_arr[3]),
                                               new Point(landmarks_arr[4], landmarks_arr[5]), new Point(landmarks_arr[6], landmarks_arr[7]), new Point(landmarks_arr[8], landmarks_arr[9]) };
                drawPredPoints(points, frame);
            }
        }
Beispiel #11
0
        //
        // C++:  vector_Rect2d cv::MultiTracker::getObjects()
        //

        /**
         * Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algorithm
         * return automatically generated
         */
        public MatOfRect2d getObjects()
        {
            ThrowIfDisposed();

            return(MatOfRect2d.fromNativeAddr(tracking_MultiTracker_getObjects_10(nativeObj)));
        }
Beispiel #12
0
        /// <summary>
        /// Get result form all output
        /// </summary>
        /// <param name="output"></param>
        /// <param name="image"></param>
        /// <param name="threshold"></param>
        /// <param name="nmsThreshold">threshold for nms</param>
        /// <param name="nms">Enable Non-maximum suppression or not</param>
        private static void GetResult(IEnumerable <Mat> output, Mat image, float threshold, float nmsThreshold, bool nms = true)
        {
            //for nms
            List <int>    classIds      = new List <int>();
            List <float>  confidences   = new List <float>();
            List <float>  probabilities = new List <float>();
            List <Rect2d> boxes         = new List <Rect2d>();

            var w = image.width();
            var h = image.height();

            /*
             * YOLO3 COCO trainval output
             * 0 1 : center                    2 3 : w/h
             * 4 : confidence                  5 ~ 84 : class probability
             */
            const int prefix = 5;   //skip 0~4

            foreach (Mat prob in output)
            {
                for (int i = 0; i < prob.rows(); i++)
                {
                    var confidence = (float)prob.get(i, 4)[0];
                    if (confidence > threshold)
                    {
                        //get classes probability
                        Core.MinMaxLocResult minAndMax = Core.minMaxLoc(prob.row(i).colRange(prefix, prob.cols()));
                        int classes     = (int)minAndMax.maxLoc.x;
                        var probability = (float)prob.get(i, classes + prefix)[0];

                        if (probability > threshold) //more accuracy, you can cancel it
                        {
                            //get center and width/height
                            float centerX = (float)prob.get(i, 0)[0] * w;
                            float centerY = (float)prob.get(i, 1)[0] * h;
                            float width   = (float)prob.get(i, 2)[0] * w;
                            float height  = (float)prob.get(i, 3)[0] * h;

                            if (!nms)
                            {
                                // draw result (if don't use NMSBoxes)
                                Draw(image, classes, confidence, probability, centerX, centerY, width, height);
                                continue;
                            }

                            //put data to list for NMSBoxes
                            classIds.Add(classes);
                            confidences.Add(confidence);
                            probabilities.Add(probability);
                            boxes.Add(new Rect2d(centerX, centerY, width, height));
                        }
                    }
                }
            }

            if (!nms)
            {
                return;
            }

            //using non-maximum suppression to reduce overlapping low confidence box
            MatOfRect2d bboxes  = new MatOfRect2d();
            MatOfFloat  scores  = new MatOfFloat();
            MatOfInt    indices = new MatOfInt();

            bboxes.fromList(boxes);
            scores.fromList(probabilities);


            Dnn.NMSBoxes(bboxes, scores, threshold, nmsThreshold, indices);

            int[] indicesA = indices.toArray();

            foreach (var i in indicesA)
            {
                var box = boxes[i];
                Draw(image, classIds[i], confidences[i], probabilities[i], box.x, box.y, box.width, box.height);
            }
        }