forked from robertknight/ocrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.ts
More file actions
466 lines (413 loc) · 14.3 KB
/
background.ts
File metadata and controls
466 lines (413 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import {
DetectedLine,
OcrEngine,
OcrEngineInit,
TextLine,
default as initOcrLib,
} from "../build/ocrs.js";
import type { LineRecResult, RotatedRect, WordRecResult } from "./types";
import type * as contentModule from "./content";
import type { TextOverlay, TextOverlayOptions } from "./content";
type OCRResources = {
detectionModel: Uint8Array;
recognitionModel: Uint8Array;
};
let ocrResources: Promise<OCRResources> | undefined;
/**
* Create an OCR engine and configure its models.
*/
async function createOCREngine(): Promise<OcrEngine> {
if (!ocrResources) {
// Initialize OCR library and fetch models on first use.
const init = async () => {
const [ocrBin, detectionModel, recognitionModel] = await Promise.all([
fetch("../build/ocrs_bg.wasm").then((r) => r.arrayBuffer()),
fetch("../build/text-detection.rten").then((r) => r.arrayBuffer()),
fetch("../build/text-recognition.rten").then((r) => r.arrayBuffer()),
]);
await initOcrLib(ocrBin);
return {
detectionModel: new Uint8Array(detectionModel),
recognitionModel: new Uint8Array(recognitionModel),
};
};
ocrResources = init();
}
const { detectionModel, recognitionModel } = await ocrResources;
const ocrInit = new OcrEngineInit();
ocrInit.setDetectionModel(detectionModel);
ocrInit.setRecognitionModel(recognitionModel);
return new OcrEngine(ocrInit);
}
/**
* Capture the visible area of the current tab.
*/
async function captureTabImage(): Promise<ImageData> {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const activeTab = tabs[0];
const bitmap = await chrome.tabs
.captureVisibleTab()
.then((dataURL) => fetch(dataURL))
.then((response) => response.blob())
.then((blob) =>
createImageBitmap(blob, {
// `captureVisibleTab` may return a HiDPI image if
// `window.devicePixelRatio > 1`. Scaling the image down as soon as
// possible makes subsequent operations cheaper.
resizeWidth: activeTab.width,
resizeHeight: activeTab.height,
}),
);
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const context = canvas.getContext("2d")!;
context.drawImage(bitmap, 0, 0);
return context.getImageData(0, 0, bitmap.width, bitmap.height);
}
function* chunks<T>(items: T[], chunkSize: number) {
for (let i = 0; i < items.length; i += chunkSize) {
yield items.slice(i, i + chunkSize);
}
}
/** Return an array of numbers in the range `[start, end)` */
function range(start: number, end: number) {
return Array(end - start)
.fill(start)
.map((x, i) => x + i);
}
function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Content script function that tests if the tab is displaying Chrome's native
* PDF viewer.
*/
function tabIsPDFViewer() {
return document.querySelector('embed[type="application/pdf"]') !== null;
}
/**
* Content script function that removes the overlay in the current tab.
*/
async function dismissTextOverlay() {
const contentSrc = chrome.runtime.getURL("build-extension/content.js");
const content: typeof contentModule = await import(contentSrc);
content.dismissTextOverlay();
}
/**
* Content script function that creates an overlay in the current tab.
*/
async function createTextOverlay(lineCoords: RotatedRect[]) {
const contentSrc = chrome.runtime.getURL("build-extension/content.js");
const content: typeof contentModule = await import(contentSrc);
const overlayOptions: TextOverlayOptions = {
lineCoords,
// Perform on-demand recognition of a text line.
recognizeText(lineIndex: number): Promise<LineRecResult | null> {
return chrome.runtime.sendMessage({
method: "recognizeText",
args: { lineIndex },
time: Date.now(),
});
},
};
const overlay = content.createTextOverlay(overlayOptions);
// Receive eagerly recognized text from the OCR engine.
const onMessage = (message: any, sender: any) => {
if (message.type === "textRecognized") {
overlay.setLineText(message.lineIndex, message.recResult);
}
};
chrome.runtime.onMessage.addListener(onMessage);
// Handle removal of overlay.
overlay.dismissed.addEventListener("abort", () => {
chrome.runtime.onMessage.removeListener(onMessage);
chrome.runtime.sendMessage({
method: "cancelRecognition",
time: Date.now(),
});
});
}
/**
* Map coordinates from a tab screenshot to the coordinate system of the
* document's viewport, as seen by code running in the tab.
*
* This assumes that the screenshot was captured at regular DPI (ie. as if
* `window.devicePixelRatio` was 1), and so we don't need to compensate for
* that here.
*/
function tabImageToDocumentCoords(coords: number[], zoom: number) {
return coords.map((c) => c / zoom);
}
/**
* Convert a `TextLineList` from the OCR engine to a `LineRecResult` array
* that can be serialized and sent to the tab.
*
* @param zoom - The tab's zoom level, as returned by `chrome.tabs.getZoom`
* @param coords - Coordinates of the text lines from text detection
*/
function textLineToLineRecResult(
lines: TextLine[],
zoom: number,
coords: RotatedRect[],
): Array<LineRecResult | null> {
const result: Array<LineRecResult | null> = [];
for (let i = 0; i < lines.length; i++) {
const recLine = lines[i];
const words = recLine.words();
result.push({
words: words.map((word) => ({
text: word.text(),
coords: tabImageToDocumentCoords(
Array.from(word.rotatedRect().corners()),
zoom,
),
})),
coords: tabImageToDocumentCoords(coords[i], zoom),
});
}
return result;
}
/**
* Map of tab ID to controller for canceling background OCR, in tabs where this
* is currently active.
*/
const cancelControllers = new Map<number, AbortController>();
/**
* Callback for recognizing lines on-demand in tabs where extension has most
* recently been activated.
*
* TODO - There should be one of these per tab.
*/
let recognizeText:
| ((lineIndexes: number[]) => Array<LineRecResult | null>)
| undefined;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (
request.method === "recognizeText" &&
typeof recognizeText === "function"
) {
const [result] = recognizeText([request.args.lineIndex]);
sendResponse(result);
return true;
}
if (
request.method === "cancelRecognition" &&
typeof sender.tab?.id === "number"
) {
const tabId = sender.tab.id;
const cancelCtrl = cancelControllers.get(tabId);
if (cancelCtrl) {
cancelCtrl.abort();
cancelControllers.delete(tabId);
}
return true;
}
return false;
});
chrome.action.onClicked.addListener(async (tab) => {
if (!tab.id) {
return;
}
// True if the tab is using Chrome's native PDF viewer.
let isPDF = false;
// The tab in which the screenshot and text overlay are displayed, if we can't
// show the overlay directly in the tab where the extension was activated.
let screenshotTab: chrome.tabs.Tab | null = null;
// The tab screenshot, if captured as part of creating `screenshotTab`.
let tabImage: ImageData | null = null;
// Determine whether this tab has content that needs special handling.
try {
const [isPDFResult] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: tabIsPDFViewer,
});
isPDF = isPDFResult.result;
} catch (err) {
// If the script failed to run, this may be because:
//
// 1. It is a tab which does not allow content scripts, even with `activeTab`
// permissions (eg. `chrome://` URLs).
// 2. The page is using a sandbox origin (see
// https://bugs.chromium.org/p/chromium/issues/detail?id=1407986)
//
// Try to capture a screenshot instead and open that in a new tab with an
// overlay.
chrome.action.setBadgeText({ text: "..." });
try {
// TODO - Capture a HiDPI image here since we're going to show this
// directly to the user.
tabImage = await captureTabImage();
const screenshotTabURL = new URL("/screenshot.html", location.href);
screenshotTabURL.searchParams.set("url", tab.url ?? "");
screenshotTab = await chrome.tabs.create({
openerTabId: tab.id,
url: screenshotTabURL.href,
});
chrome.tabs.sendMessage(screenshotTab.id!, {
type: "imageLoaded",
image: {
width: tabImage.width,
height: tabImage.height,
data: Array.from(tabImage.data),
},
});
} catch (err) {
// We can't even capture a screenshot :( - Just show an error.
const errorTabURL = new URL("/error.html", location.href);
errorTabURL.searchParams.set("url", tab.url ?? "");
chrome.tabs.create({
openerTabId: tab.id,
url: errorTabURL.href,
});
console.warn("Unable to capture tab image:", err);
return;
} finally {
chrome.action.setBadgeText({ text: "" });
}
}
// Remove existing overlay, if extension was already activated in current tab.
if (!screenshotTab) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: dismissTextOverlay,
});
}
// Cancel background text recognition for existing overlay.
let cancelCtrl = cancelControllers.get(tab.id);
if (cancelCtrl) {
cancelCtrl.abort();
cancelControllers.delete(tab.id);
}
cancelCtrl = new AbortController();
cancelControllers.set(tab.id, cancelCtrl);
cancelCtrl.signal.addEventListener("abort", () => {
chrome.action.setBadgeText({ text: "" });
});
chrome.action.setBadgeText({ text: "..." });
// Get the zoom level of the tab. Tab image coordinates need to be scaled by
// 1/zoom to map them to document coordinates. Chrome's PDF viewer is a
// special case because the zoom level applies to the embedded native viewer,
// but not the HTML document which contains it.
const zoom = isPDF || screenshotTab ? 1 : await chrome.tabs.getZoom(tab.id);
// Cache of line number to recognition result for the current image.
const recognizedLines = new Map<number, LineRecResult | null>();
// List of all text lines detected in the current image.
let lines: DetectedLine[];
try {
// Init OCR engine concurrently with capturing tab image.
const ocrEnginePromise = createOCREngine();
const captureStart = performance.now();
const image = tabImage ?? (await captureTabImage());
const captureEnd = performance.now();
const ocrEngine = await ocrEnginePromise;
const ocrInput = ocrEngine.loadImage(
image.width,
image.height,
// Cast from `Uint8ClampedArray` to `Uint8Array`.
image.data as unknown as Uint8Array,
);
const detStart = performance.now();
lines = ocrEngine.detectText(ocrInput);
const detEnd = performance.now();
console.log(
`Detected ${lines.length} lines. Capture ${
captureEnd - captureStart
} ms, detection ${detEnd - detStart}ms.`,
);
/**
* Perform recognition on a batch of lines and cache the results.
*/
const recognizeLineBatch = (lineIndexes: number[]) => {
const recInput: DetectedLine[] = [];
const lineCoords: RotatedRect[] = [];
for (const lineIndex of lineIndexes) {
const line = lines[lineIndex];
if (!line) {
throw new Error("Invalid line number");
}
lineCoords.push(Array.from(line.rotatedRect().corners()));
recInput.push(line);
}
const textLines = ocrEngine.recognizeText(ocrInput, recInput);
for (const [i, recLine] of textLineToLineRecResult(
textLines,
zoom,
lineCoords,
).entries()) {
recognizedLines.set(lineIndexes[i], recLine);
}
};
// Set up callback that performs text recognition.
//
// This takes a list of line indexes as input. The recognition time per
// line can be up to ~45% lower when recognizing a batch of similar-length
// lines.
recognizeText = (lineIndexes: number[]) => {
const unrecognizedLines = lineIndexes.filter(
(idx) => !recognizedLines.has(idx),
);
recognizeLineBatch(unrecognizedLines);
return lineIndexes.map((li) => recognizedLines.get(li)!);
};
// Create the text layer in the current tab.
const lineCoords = lines.map((line) =>
tabImageToDocumentCoords(Array.from(line.rotatedRect().corners()), zoom),
);
if (screenshotTab) {
chrome.tabs.sendMessage(screenshotTab.id!, {
type: "createTextOverlay",
lineCoords,
});
} else {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: createTextOverlay,
args: [lineCoords],
});
}
} finally {
if (!cancelCtrl.signal.aborted) {
chrome.action.setBadgeText({ text: "" });
}
}
// Eagerly recognize lines, before the text layer has requested them.
if (lines && !cancelCtrl.signal.aborted) {
chrome.action.setBadgeText({ text: "~" });
// Sort lines by width, so lines in each batch are likely to have similar
// widths. This optimizes the benefit of performing recognition on a batch
// of lines at once.
const lineWidth = (dl: DetectedLine) => {
const [left, top, right, bottom] = dl.rotatedRect().boundingRect();
return right - left;
};
const sortedLines = [...lines];
sortedLines.sort((a, b) => lineWidth(a) - lineWidth(b));
// Recognize lines in batches.
const chunkSize = 4;
for (let indices of chunks(range(0, sortedLines.length), chunkSize)) {
if (cancelCtrl.signal.aborted) {
break;
}
// Skip any lines that were already recognized in response to a request
// from the tab.
indices = indices.filter((idx) => !recognizedLines.has(idx));
const recResults = await recognizeText(indices);
if (cancelCtrl.signal.aborted) {
break;
}
for (let i = 0; i < indices.length; i++) {
const targetTab = screenshotTab?.id ?? tab.id;
chrome.tabs.sendMessage(targetTab, {
type: "textRecognized",
lineIndex: indices[i],
recResult: recResults[i],
});
}
// Pause between batches to allow any messages sent from content scripts
// in the interim to be processed first.
await delay(0);
}
if (!cancelCtrl.signal.aborted) {
chrome.action.setBadgeText({ text: "" });
}
}
});