public TrackedObject(OpenCVForUnity.CoreModule.Rect rect) { lastPositions = new PositionsVector(); numDetectedFrames = 1; numFramesNotDetected = 0; lastPositions.Add(rect.clone()); _id = GetNextId(); id = _id; }
/// <summary> /// Postprocess the specified frame, outs and net. /// </summary> /// <param name="frame">Frame.</param> /// <param name="outs">Outs.</param> /// <param name="net">Net.</param> private 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 <OpenCVForUnity.CoreModule.Rect> boxesList = new List <OpenCVForUnity.CoreModule.Rect>(); 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]); int left = (int)(data[3] * frame.cols()); int top = (int)(data[4] * frame.rows()); int right = (int)(data[5] * frame.cols()); int bottom = (int)(data[6] * frame.rows()); int width = right - left + 1; int height = bottom - top + 1; classIdsList.Add((int)(class_id) - 0); confidencesList.Add((float)confidence); boxesList.Add(new OpenCVForUnity.CoreModule.Rect(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]); int left = (int)(data[3] * frame.cols()); int top = (int)(data[4] * frame.rows()); int right = (int)(data[5] * frame.cols()); int bottom = (int)(data[6] * frame.rows()); int width = right - left + 1; int height = bottom - top + 1; classIdsList.Add((int)(class_id) - 0); confidencesList.Add((float)confidence); boxesList.Add(new OpenCVForUnity.CoreModule.Rect(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) { int centerX = (int)(positionData[0] * frame.cols()); int centerY = (int)(positionData[1] * frame.rows()); int width = (int)(positionData[2] * frame.cols()); int height = (int)(positionData[3] * frame.rows()); int left = centerX - width / 2; int top = centerY - height / 2; classIdsList.Add(maxIdx); confidencesList.Add((float)confidence); boxesList.Add(new OpenCVForUnity.CoreModule.Rect(left, top, width, height)); } } } } else { Debug.Log("Unknown output layer type: " + outLayerType); } MatOfRect boxes = new MatOfRect(); boxes.fromList(boxesList); detectionBoxes.AddRange(boxesList); MatOfFloat confidences = new MatOfFloat(); confidences.fromList(confidencesList); MatOfInt indices = new MatOfInt(); Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices); for (int i = 0; i < indices.total(); ++i) { int idx = (int)indices.get(i, 0)[0]; if (classNames != null) { modelOutput.Add(new KeyValuePair <string, float>(classNames[classIdsList[idx]], confidencesList[idx])); } if (gameObject.GetComponent <Renderer>() != null) { OpenCVForUnity.CoreModule.Rect 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(); }