From b1ac94586b9933c5d28be0e75212058615072dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E5=B9=BF=E5=B9=BF?= <34500722+LuGuangguang@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:30:12 +0800 Subject: [PATCH] Update PaddleDetector.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 防止当识别到的ClassID大于模型中预设的种类时,导致Config.LabelList 索引溢出的问题 --- src/Sdcb.PaddleDetection/PaddleDetector.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Sdcb.PaddleDetection/PaddleDetector.cs b/src/Sdcb.PaddleDetection/PaddleDetector.cs index cca792a..247eb25 100644 --- a/src/Sdcb.PaddleDetection/PaddleDetector.cs +++ b/src/Sdcb.PaddleDetection/PaddleDetector.cs @@ -172,6 +172,10 @@ public DetectionResult[] Run(Mat src) if (isRbox) { int classId = (int)MathUtil.Round(outputData[0 + j * 10]); + if (classId > Config.LabelList.Length - 1) + { + continue; + } float score = outputData[1 + j * 10]; int x1 = (int)(outputData[2 + j * 10] * r.Width); int y1 = (int)(outputData[3 + j * 10] * r.Height); @@ -186,6 +190,10 @@ public DetectionResult[] Run(Mat src) else { int classId = (int)MathUtil.Round(outputData[0 + j * 6]); + if (classId > Config.LabelList.Length - 1) + { + continue; + } float score = outputData[1 + j * 6]; int xmin = (int)(outputData[2 + j * 6] * r.Width); int ymin = (int)(outputData[3 + j * 6] * r.Height); @@ -194,6 +202,7 @@ public DetectionResult[] Run(Mat src) result[j] = new DetectionResult(new[] { xmin, ymin, xmax, ymax }, classId, Config.LabelList[classId], score); } } + result = result.Where(r => r != null).ToArray(); return result; } } @@ -406,4 +415,4 @@ static IEnumerable GenerateColorMapToScalar() } } } -} \ No newline at end of file +}