Skip to content

Fixed potential out of bounds access #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 49 additions & 52 deletions src/CRAFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ HeatMapRatio CraftModel::resizeAspect(cv::Mat& img)
return output;
}
std::vector<BoundingBox> CraftModel::mergeBoundingBoxes(std::vector<BoundingBox>& dets, float distanceThresh, int height, int width)
{
{
// represents how much we change the top left Y
std::sort(dets.begin(), dets.end(), boxSorter());
bool merge = NULL;
std::vector<BoundingBox> merged;
std::vector<BoundingBox> mergedBoxes;
int newTopLeft;
bool firstRun = true;
for (int i = 0; i < dets.size(); i++)
{
bool merged = false;
cv::Point newBottomRight = dets[i].bottomRight;
int minY = 0;
int maxY = 0;
if (i < dets.size())
if (i + 1 < dets.size())
{
float x = dets[i].bottomRight.x;
float xPrime = dets[i + 1].topLeft.x;
Expand All @@ -75,7 +75,6 @@ std::vector<BoundingBox> CraftModel::mergeBoundingBoxes(std::vector<BoundingBox>
if (width > 5 * height)
{
// box is a line, skip merging
merge = false;
continue;
}
//merge box, store point
Expand All @@ -94,68 +93,66 @@ std::vector<BoundingBox> CraftModel::mergeBoundingBoxes(std::vector<BoundingBox>
{
maxY = dets[i + 1].bottomRight.y;
}

}
merge = true;
merged = true;
}
else
{
newBottomRight = dets[i].bottomRight;
merge = false;
}
if (firstRun)
}
if (firstRun)
{
// store first
newTopLeft = i;
firstRun = false;
}
//cant merge anymore other box is too far, build new box
if (!merged)
{
BoundingBox newBox;
newBox.topLeft = dets[newTopLeft].topLeft;
newBox.topLeft.y -= minY;
//margin
newBox.topLeft.y *= .998;
newBox.topLeft.x *= .998;
if (newBox.topLeft.y < 0)
{
// store first
newTopLeft = i;
firstRun = false;
newBox.topLeft.y = 0;
}
//cant merge anymore other box is too far, build new box
if (!merge)
if (newBox.topLeft.x < 0)
{
BoundingBox newBox;
newBox.topLeft = dets[newTopLeft].topLeft;
newBox.topLeft.y -= minY;
//margin
newBox.topLeft.y *= .998;
newBox.topLeft.x *= .998;
if (newBox.topLeft.y < 0)
{
newBox.topLeft.y = 0;
}

if (newBox.topLeft.x < 0)
{
newBox.topLeft.x = 0;
}
newBox.topLeft.x = int(newBox.topLeft.x);
newBox.topLeft.y = int(newBox.topLeft.y);
newBox.topLeft.x = 0;
}
newBox.topLeft.x = int(newBox.topLeft.x);
newBox.topLeft.y = int(newBox.topLeft.y);

newBox.bottomRight = newBottomRight;
newBox.bottomRight.y += maxY;
// margin
newBox.bottomRight.y *= 1.003;
newBox.bottomRight.x *= 1.003;
newBox.bottomRight = newBottomRight;
newBox.bottomRight.y += maxY;
// margin
newBox.bottomRight.y *= 1.003;
newBox.bottomRight.x *= 1.003;

if (newBox.bottomRight.y > height)
{
newBox.bottomRight.y = height-1;
}
if (newBox.bottomRight.y > height)
{
newBox.bottomRight.y = height-1;
}

if (newBox.bottomRight.x > width)
{
newBox.bottomRight.x = width-5;
}
newBox.bottomRight.x = int(newBox.bottomRight.x);
newBox.bottomRight.y = int(newBox.bottomRight.y);
merged.push_back(newBox);
// move top left box to next box
newTopLeft = i + 1;
minY = 0;
maxY = 0;
if (newBox.bottomRight.x > width)
{
newBox.bottomRight.x = width-5;
}
newBox.bottomRight.x = int(newBox.bottomRight.x);
newBox.bottomRight.y = int(newBox.bottomRight.y);
mergedBoxes.push_back(newBox);
// move top left box to next box
newTopLeft = i + 1;
minY = 0;
maxY = 0;

}
}
return merged;
return mergedBoxes;
}

std::vector<BoundingBox> CraftModel::getBoundingBoxes(const torch::Tensor &input, const torch::Tensor& output, float textThresh, float linkThresh, float lowText)
Expand Down