// Uses the bounding box dimensions and maps them onto its respective cell within the image. private CellDimensions MapBoundingBoxToCell(int x, int y, int box, BoundingBoxDimensions boxDimensions) { return(new CellDimensions { X = ((float)x + Sigmoid(boxDimensions.X)) * CELL_WIDTH, Y = ((float)y + Sigmoid(boxDimensions.Y)) * CELL_HEIGHT, Width = (float)Math.Exp(boxDimensions.Width) * CELL_WIDTH * anchors[box * 2], Height = (float)Math.Exp(boxDimensions.Height) * CELL_HEIGHT * anchors[box * 2 + 1], }); }
public IList <YoloBoundingBox> ParseOutputs(float[] yoloModelOutputs, float threshold = .3F) { var boxes = new List <YoloBoundingBox>(); for (int row = 0; row < ROW_COUNT; row++) { for (int column = 0; column < COL_COUNT; column++) { for (int box = 0; box < BOXES_PER_CELL; box++) { var channel = (box * (CLASS_COUNT + BOX_INFO_FEATURE_COUNT)); BoundingBoxDimensions boundingBoxDimensions = ExtractBoundingBoxDimensions(yoloModelOutputs, row, column, channel); float confidence = GetConfidence(yoloModelOutputs, row, column, channel); CellDimensions mappedBoundingBox = MapBoundingBoxToCell(row, column, box, boundingBoxDimensions); if (confidence < threshold) { continue; } float[] predictedClasses = ExtractClasses(yoloModelOutputs, row, column, channel); var(topResultIndex, topResultScore) = GetTopResult(predictedClasses); var topScore = topResultScore * confidence; if (topScore < threshold) { continue; } boxes.Add(new YoloBoundingBox() { Dimensions = new BoundingBoxDimensions { X = (mappedBoundingBox.X - mappedBoundingBox.Width / 2), Y = (mappedBoundingBox.Y - mappedBoundingBox.Height / 2), Width = mappedBoundingBox.Width, Height = mappedBoundingBox.Height, }, Confidence = topScore, Label = labels[topResultIndex], BoxColor = classColors[topResultIndex] }); } } } return(boxes); }