mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-10 18:25:37 +02:00
Merge pull request #18380 from calixteman/avoid_dbl_ml_queries
[Editor] Avoid to query ML engine several times for the same image
This commit is contained in:
commit
e777ae2258
1 changed files with 40 additions and 31 deletions
|
@ -36,6 +36,8 @@ class StampEditor extends AnnotationEditor {
|
||||||
|
|
||||||
#canvas = null;
|
#canvas = null;
|
||||||
|
|
||||||
|
#hasMLBeenQueried = false;
|
||||||
|
|
||||||
#observer = null;
|
#observer = null;
|
||||||
|
|
||||||
#resizeTimeoutId = null;
|
#resizeTimeoutId = null;
|
||||||
|
@ -423,6 +425,42 @@ class StampEditor extends AnnotationEditor {
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #mlGuessAltText(bitmap, width, height) {
|
||||||
|
if (this.#hasMLBeenQueried) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#hasMLBeenQueried = true;
|
||||||
|
if (!this._uiManager.hasMLManager || this.hasAltText()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const offscreen = new OffscreenCanvas(width, height);
|
||||||
|
const ctx = offscreen.getContext("2d", { willReadFrequently: true });
|
||||||
|
ctx.drawImage(
|
||||||
|
bitmap,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
bitmap.width,
|
||||||
|
bitmap.height,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
);
|
||||||
|
const response = await this._uiManager.mlGuess({
|
||||||
|
service: "image-to-text",
|
||||||
|
request: {
|
||||||
|
data: ctx.getImageData(0, 0, width, height).data,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
channels: 4,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const altText = response?.output || "";
|
||||||
|
if (this.parent && altText && !this.hasAltText()) {
|
||||||
|
this.altTextData = { altText, decorative: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#drawBitmap(width, height) {
|
#drawBitmap(width, height) {
|
||||||
width = Math.ceil(width);
|
width = Math.ceil(width);
|
||||||
height = Math.ceil(height);
|
height = Math.ceil(height);
|
||||||
|
@ -436,37 +474,8 @@ class StampEditor extends AnnotationEditor {
|
||||||
? this.#bitmap
|
? this.#bitmap
|
||||||
: this.#scaleBitmap(width, height);
|
: this.#scaleBitmap(width, height);
|
||||||
|
|
||||||
if (this._uiManager.hasMLManager && !this.hasAltText()) {
|
this.#mlGuessAltText(bitmap, width, height);
|
||||||
const offscreen = new OffscreenCanvas(width, height);
|
|
||||||
const ctx = offscreen.getContext("2d");
|
|
||||||
ctx.drawImage(
|
|
||||||
bitmap,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
bitmap.width,
|
|
||||||
bitmap.height,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
);
|
|
||||||
this._uiManager
|
|
||||||
.mlGuess({
|
|
||||||
service: "image-to-text",
|
|
||||||
request: {
|
|
||||||
data: ctx.getImageData(0, 0, width, height).data,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
channels: 4,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
const altText = response?.output || "";
|
|
||||||
if (this.parent && altText && !this.hasAltText()) {
|
|
||||||
this.altTextData = { altText, decorative: false };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
ctx.filter = this._uiManager.hcmFilter;
|
ctx.filter = this._uiManager.hcmFilter;
|
||||||
ctx.drawImage(
|
ctx.drawImage(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue