Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(text-overlay): correct coordinate scaling for OCR bounding boxes

Louis Beaumontc24714f6f0e1
diff --git a/screenpipe-app-tauri/components/__tests__/text-overlay.test.tsx b/screenpipe-app-tauri/components/__tests__/text-overlay.test.tsx
index 2b8ae0880..2dc81517f 100644
--- a/screenpipe-app-tauri/components/__tests__/text-overlay.test.tsx
+++ b/screenpipe-app-tauri/components/__tests__/text-overlay.test.tsx
@@ -32,9 +32,10 @@ describe("TextOverlay", () => {
});
it("should render text positions with correct scaling", () => {
+ // Using normalized coordinates (0-1 range)
const positions = [
- createTextPosition("Hello", 100, 50, 80, 20),
- createTextPosition("World", 200, 50, 100, 20),
+ createTextPosition("Hello", 0.1, 0.05, 0.08, 0.02),
+ createTextPosition("World", 0.2, 0.05, 0.1, 0.02),
];
render(
@@ -51,8 +52,9 @@ describe("TextOverlay", () => {
expect(screen.getByText("World")).toBeInTheDocument();
});
- it("should scale coordinates correctly (50% scale)", () => {
- const positions = [createTextPosition("Test", 100, 200, 80, 40)];
+ it("should scale normalized coordinates correctly", () => {
+ // Normalized coordinates: left=0.1 (10%), top=0.2 (20%), width=0.08, height=0.04
+ const positions = [createTextPosition("Test", 0.1, 0.2, 0.08, 0.04)];
const { container } = render(
<TextOverlay
@@ -66,17 +68,17 @@ describe("TextOverlay", () => {
const textElement = container.querySelector("span");
expect(textElement).toHaveStyle({
- left: "50px", // 100 * 0.5
- top: "100px", // 200 * 0.5
- width: "40px", // 80 * 0.5
- height: "20px", // 40 * 0.5
+ left: "50px", // 0.1 * 500
+ top: "100px", // 0.2 * 500
+ width: "40px", // 0.08 * 500
+ height: "20px", // 0.04 * 500
});
});
it("should filter out text below minimum confidence", () => {
const positions = [
- createTextPosition("HighConf", 100, 50, 80, 20, 0.9),
- createTextPosition("LowConf", 200, 50, 80, 20, 0.3),
+ createTextPosition("HighConf", 0.1, 0.05, 0.08, 0.02, 0.9),
+ createTextPosition("LowConf", 0.2, 0.05, 0.08, 0.02, 0.3),
];
render(
@@ -94,15 +96,15 @@ describe("TextOverlay", () => {
expect(screen.queryByText("LowConf")).not.toBeInTheDocument();
});
- it("should render nothing when dimensions are invalid", () => {
- const positions = [createTextPosition("Test", 100, 50, 80, 20)];
+ it("should render nothing when displayed dimensions are invalid", () => {
+ const positions = [createTextPosition("Test", 0.1, 0.05, 0.08, 0.02)];
const { container } = render(
<TextOverlay
textPositions={positions}
- originalWidth={0}
+ originalWidth={1920}
originalHeight={1080}
- displayedWidth={960}
+ displayedWidth={0}
displayedHeight={540}
/>
);
@@ -112,9 +114,9 @@ describe("TextOverlay", () => {
it("should filter out positions with invalid scaled dimensions", () => {
const positions = [
- createTextPosition("Valid", 100, 50, 80, 20),
- createTextPosition("ZeroWidth", 200, 50, 0, 20),
- createTextPosition("ZeroHeight", 300, 50, 80, 0),
+ createTextPosition("Valid", 0.1, 0.05, 0.08, 0.02),
+ createTextPosition("ZeroWidth", 0.2, 0.05, 0, 0.02),
+ createTextPosition("ZeroHeight", 0.3, 0.05, 0.08, 0),
];
render(
@@ -134,8 +136,8 @@ describe("TextOverlay", () => {
it("should filter out positions outside the display bounds", () => {
const positions = [
- createTextPosition("Inside", 100, 50, 80, 20),
- createTextPosition("Outside", 2000, 50, 80, 20), // Beyond 1920px original width
+ createTextPosition("Inside", 0.1, 0.05, 0.08, 0.02),
+ createTextPosition("Outside", 1.5, 0.05, 0.08, 0.02), // Beyond normalized 1.0
];
render(
@@ -153,7 +155,7 @@ describe("TextOverlay", () => {
});
it("should apply debug styles when debug prop is true", () => {
- const positions = [createTextPosition("Debug", 100, 50, 80, 20)];
+ const positions = [createTextPosition("Debug", 0.1, 0.05, 0.08, 0.02)];
const { container } = render(
<TextOverlay
@@ -172,7 +174,7 @@ describe("TextOverlay", () => {
});
it("should have transparent text when debug is false", () => {
- const positions = [createTextPosition("Hidden", 100, 50, 80, 20)];
+ const positions = [createTextPosition("Hidden", 0.1, 0.05, 0.08, 0.02)];
const { container } = render(
<TextOverlay
@@ -190,7 +192,7 @@ describe("TextOverlay", () => {
});
it("should enable text selection with user-select CSS", () => {
- const positions = [createTextPosition("Selectable", 100, 50, 80, 20)];
+ const positions = [createTextPosition("Selectable", 0.1, 0.05, 0.08, 0.02)];
const { container } = render(
<TextOverlay
@@ -207,7 +209,7 @@ describe("TextOverlay", () => {
});
it("should set container dimensions to match displayed image", () => {
- const positions = [createTextPosition("Test", 100, 50, 80, 20)];
+ const positions = [createTextPosition("Test", 0.1, 0.05, 0.08, 0.02)];
const { container } = render(
<TextOverlay
@@ -227,9 +229,9 @@ describe("TextOverlay", () => {
});
it("should handle many text positions efficiently", () => {
- // Create 1000 text positions to test performance
+ // Create 1000 text positions to test performance (using normalized coordinates)
const positions = Array.from({ length: 1000 }, (_, i) =>
- createTextPosition(`Word${i}`, i * 10, Math.floor(i / 100) * 20, 50, 15)
+ createTextPosition(`Word${i}`, (i % 100) * 0.01, Math.floor(i / 100) * 0.1, 0.05, 0.015)
);
const startTime = performance.now();
@@ -257,8 +259,8 @@ describe("TextOverlay", () => {
it("should render URLs as clickable links when clickableUrls is true", () => {
const positions = [
- createTextPosition("https://example.com", 100, 50, 200, 20),
- createTextPosition("Regular text", 100, 100, 150, 20),
+ createTextPosition("https://example.com", 0.1, 0.05, 0.2, 0.02),
+ createTextPosition("Regular text", 0.1, 0.1, 0.15, 0.02),
];
const { container } = render(
@@ -282,7 +284,7 @@ describe("TextOverlay", () => {
});
it("should not render URLs as links when clickableUrls is false", () => {
- const positions = [createTextPosition("https://example.com", 100, 50, 200, 20)];
+ const positions = [createTextPosition("https://example.com", 0.1, 0.05, 0.2, 0.02)];
const { container } = render(
<TextOverlay
@@ -305,7 +307,7 @@ describe("TextOverlay", () => {
it("should open URL in new tab when clicked", () => {
const mockOpen = vi.spyOn(window, "open").mockImplementation(() => null);
- const positions = [createTextPosition("https://test.com/page", 100, 50, 200, 20)];
+ const positions = [createTextPosition("https://test.com/page", 0.1, 0.05, 0.2, 0.02)];
const { container } = render(
<TextOverlay
diff --git a/screenpipe-app-tauri/components/text-overlay.tsx b/screenpipe-app-tauri/components/text-overlay.tsx
index d89c6a466..f05afa152 100644
--- a/screenpipe-app-tauri/components/text-overlay.tsx
+++ b/screenpipe-app-tauri/components/text-overlay.tsx
@@ -94,13 +94,10 @@ export const TextOverlay = memo(function TextOverlay({
debug = false,
clickableUrls = true,
}: TextOverlayProps) {
- // Calculate scale factors between original and displayed image
- const scaleX = displayedWidth / originalWidth;
- const scaleY = displayedHeight / originalHeight;
-
// Scale and filter text positions, detect URLs
+ // Note: OCR bounds are normalized (0-1 range), so we multiply directly by displayed dimensions
const scaledPositions = useMemo<ScaledTextPositionWithUrl[]>(() => {
- if (!originalWidth || !originalHeight || !displayedWidth || !displayedHeight) {
+ if (!displayedWidth || !displayedHeight) {
return [];
}
@@ -111,10 +108,10 @@ export const TextOverlay = memo(function TextOverlay({
return {
...pos,
scaledBounds: {
- left: pos.bounds.left * scaleX,
- top: pos.bounds.top * scaleY,
- width: pos.bounds.width * scaleX,
- height: pos.bounds.height * scaleY,
+ left: pos.bounds.left * displayedWidth,
+ top: pos.bounds.top * displayedHeight,
+ width: pos.bounds.width * displayedWidth,
+ height: pos.bounds.height * displayedHeight,
},
isUrl: textIsUrl,
normalizedUrl: textIsUrl ? normalizeUrl(pos.text) : undefined,
@@ -131,12 +128,8 @@ export const TextOverlay = memo(function TextOverlay({
);
}, [
textPositions,
- originalWidth,
- originalHeight,
displayedWidth,
displayedHeight,
- scaleX,
- scaleY,
minConfidence,
]);