public static KeypointsDetectionInfo[] Decode(Nnet net, int w, int h, double threshold, string[] allowedClasses = null) { List <KeypointsDetectionInfo> ret = new List <KeypointsDetectionInfo>(); var inp = net.Nodes.First(z => z.IsInput); var f1 = net.Nodes.FirstOrDefault(z => z.Dims.Last() == 3); var snd = net.Nodes.FirstOrDefault(z => z.Dims.Length == 1 && z.ElementType == typeof(float)); if (f1 == null) { return(null); } var rets1 = net.OutputDatas[f1.Name] as float[]; var scores = net.OutputDatas[snd.Name] as float[]; InternalArray ar = new InternalArray(f1.Dims); ar.Data = new double[rets1.Length]; for (int i = 0; i < rets1.Length; i++) { ar.Data[i] = rets1[i]; } int cnt = scores.Length; for (int i = 0; i < cnt; i++) { if (scores[i] < 0.9) { continue; } var kp = new KeypointsDetectionInfo(); ret.Add(kp); var sub = ar.Get2DImageFrom3DArray(i); List <Point2f> pp = new List <Point2f>(); for (int j = 0; j < sub.Shape[0]; j++) { pp.Add(new Point2f((float)(sub.Get2D(j, 0) / inp.Dims[3] * w), (float)(sub.Get2D(j, 1) / inp.Dims[2] * h))); } kp.Points = pp.ToArray(); } return(ret.ToArray()); }
public static ObjectDetectionInfo[] yoloBoxesDecode(Nnet net, int w, int h, float nms_tresh, double threshold, string[] allowedClasses = null) { List <ObjectDetectionInfo> ret = new List <ObjectDetectionInfo>(); var f1 = net.Nodes.FirstOrDefault(z => z.Dims.Last() == 4); var f2 = net.Nodes.FirstOrDefault(z => z.Dims.Last() > 4); if (f1 == null || f2 == null) { return(null); } var nms = Helpers.ReadResource("coco.names").Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); var rets1 = net.OutputDatas[f2.Name] as float[]; var rets3 = net.OutputDatas[f1.Name] as float[]; var dims = net.Nodes.First(z => z.IsInput).Dims; List <int> indexes = new List <int>(); List <double> confs = new List <double>(); List <int> classes = new List <int>(); Dictionary <int, List <int> > perClassIndexes = new Dictionary <int, List <int> >(); Dictionary <int, List <float> > perClassConfs = new Dictionary <int, List <float> >(); int pos = 0; var cnt = f2.Dims.Last(); for (int i = 0; i < rets1.Length; i += cnt, pos++) { int maxind = -1; double maxv = double.NaN; for (int j = 0; j < cnt; j++) { if (allowedClasses != null && !allowedClasses.Contains(nms[j])) { continue; } if (maxind == -1 || rets1[i + j] > maxv) { maxv = rets1[i + j]; maxind = j; } } if (maxind != -1 && maxv > threshold) { confs.Add(maxv); classes.Add(maxind); indexes.Add(pos); if (!perClassIndexes.ContainsKey(maxind)) { perClassIndexes.Add(maxind, new List <int>()); perClassConfs.Add(maxind, new List <float>()); } perClassIndexes[maxind].Add(pos); perClassConfs[maxind].Add((float)maxv); } } List <int> res = new List <int>(); foreach (var item in perClassIndexes) { List <float[]> boxes = new List <float[]>(); for (int i = 0; i < item.Value.Count; i++) { var box = rets3.Skip(item.Value[i] * 4).Take(4).ToArray(); boxes.Add(new float[] { box[0], box[1], box[2], box[3], (float)perClassConfs[item.Key][i] }); } var res2 = Decoders.nms(boxes, nms_tresh); res.AddRange(res2.Select(z => item.Value[z])); } for (int i = 0; i < indexes.Count; i++) { if (!res.Contains(indexes[i])) { continue; } int offset = indexes[i] * 4; //var box = rets3.Skip(indexes[i] * 4).Take(4).ToArray(); ret.Add(new ObjectDetectionInfo() { Class = classes[i], Conf = (float)confs[i], Rect = new Rect((int)(rets3[0 + offset] * w), (int)(rets3[1 + offset] * h), (int)((rets3[2 + offset] - rets3[0 + offset]) * w), (int)((rets3[3 + offset] - rets3[1 + offset]) * h)), Label = nms[classes[i]] }); } return(ret.ToArray()); }
public SegmentationDetectionInfo[] Decode(Nnet net, int w, int h, string[] allowedClasses = null) { List <SegmentationDetectionInfo> ret = new List <SegmentationDetectionInfo>(); var inp = net.Nodes.First(z => z.IsInput); var f1 = net.Nodes.FirstOrDefault(z => z.Dims.Last() == inp.Dims.Last()); var snd = net.Nodes.FirstOrDefault(z => z.Dims.Length == 1 && z.ElementType == typeof(float)); var labels = net.Nodes.FirstOrDefault(z => z.Dims.Length == 1 && z.ElementType != typeof(float)); var boxes = net.Nodes.FirstOrDefault(z => z.Dims.Last() == 4); if (f1 == null) { return(null); } var nms = Helpers.ReadResource("coco.2.names").Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); var rets1 = net.OutputDatas[f1.Name] as float[]; var labls = net.OutputDatas[labels.Name] as Int64[]; var bxs = net.OutputDatas[boxes.Name] as float[]; var scores = net.OutputDatas[snd.Name] as float[]; InternalArray ar = new InternalArray(f1.Dims); ar.Data = new double[rets1.Length]; for (int i = 0; i < rets1.Length; i++) { ar.Data[i] = rets1[i]; if (rets1[i] > MaskThreshold) { ar.Data[i] = 1; } else { ar.Data[i] = 0; } } int cnt = scores.Length; for (int i = 0; i < cnt; i++) { if (scores[i] < Threshold) { continue; } var kp = new SegmentationDetectionInfo(); kp.Class = (int)(labls[i]); kp.Conf = scores[i]; if (labls[i] < nms.Length) { kp.Label = nms[labls[i]]; } else { kp.Label = "(unknown)"; } if (AllowedClasses.Count != 0 && !AllowedClasses.Contains(kp.Label)) { continue; } ret.Add(kp); double fx = 1f / 900 * w; double fy = 1f / 600 * h; Rect rect = new Rect((int)(bxs[i * 4] * fx), (int)(bxs[i * 4 + 1] * fy), (int)((bxs[i * 4 + 2] - bxs[i * 4]) * fx), (int)((bxs[i * 4 + 3] - bxs[i * 4 + 1]) * fy)); kp.Rect = rect; var sub = ar.Get3DImageFrom4DArray(i); byte[] arr = new byte[sub.Data.Length]; for (int j = 0; j < sub.Data.Length; j++) { arr[j] = (byte)sub.Data[j]; } var cnt2 = arr.Count(z => z == 1); if (cnt2 > 0) { } Mat mat = new Mat(600, 900, MatType.CV_8UC1, arr); kp.Mask = mat; //mat.SaveImage("test1.jpg"); } return(ret.ToArray()); }