Skip to content

Commit e7c453e

Browse files
authored
Fix text placement for large bounding boxes (#15)
* Improve text positioning * Add a minimum Y value for text positioning
1 parent 285b4ac commit e7c453e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

static/draw.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,27 @@ function snapshotBoundingBoxes(img) {
7575

7676
ctx.font = FONT;
7777
ctx.fillStyle = color;
78-
const TEXT_OFFSET = -10;
79-
ctx.fillText(p.class, p.bbox[0], p.bbox[1] + TEXT_OFFSET);
78+
79+
var textPos = bbTextPosition(p.bbox[0], p.bbox[1], p.bbox[3]);
80+
ctx.fillText(p.class, textPos.x, textPos.y);
8081
});
8182
});
8283
});
8384
}
8485

86+
function bbTextPosition(x, y, height) {
87+
const TEXT_OFFSET_X = 3;
88+
const TEXT_OFFSET_Y = -10;
89+
const TEXT_MIN_Y = 13;
90+
var shiftX = x + TEXT_OFFSET_X;
91+
var shiftY = y + TEXT_OFFSET_Y;
92+
if (shiftY < TEXT_MIN_Y) {
93+
return { x: shiftX, y: shiftY + height};
94+
} else {
95+
return { x: shiftX, y: shiftY };
96+
}
97+
}
98+
8599
function swapToVideo() {
86100
var constraints = { audio: false, video: { facingMode: { ideal: "environment"} } };
87101

0 commit comments

Comments
 (0)