Skip to content

Commit

Permalink
update tesseract js
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Dec 22, 2023
1 parent b3a3092 commit e438cd9
Show file tree
Hide file tree
Showing 16 changed files with 546 additions and 465 deletions.
3 changes: 3 additions & 0 deletions doc/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Mouseover Translate Any Language At Once
English, Russian, Japanese, Chinese and so on

# Change Log
- 0.1.103
- update tesseract js v5.0.3
- fix pdf shortcut key (request by woong park)
- 0.1.102
- fix touch recognition
- 0.1.101
Expand Down
53 changes: 32 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"match-url-wildcard": "^0.0.5",
"source-map-support": "^0.5.21",
"stream-browserify": "^3.0.0",
"tesseract.js": "^4.1.1",
"tesseract.js": "^5.0.3",
"textfit": "^2.4.0",
"throttle-debounce": "^5.0.0",
"tippy.js": "^6.3.7",
"typeface-roboto": "^1.1.13",
Expand Down
4 changes: 2 additions & 2 deletions public/tesseract/README
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://unpkg.com/tesseract.js-core@4.0.4/tesseract-core.wasm.js
https://unpkg.com/tesseract.js@4.1.1/dist/worker.min.js
https://unpkg.com/tesseract.js-core@5.0.0/tesseract-core-lstm.wasm.js
https://unpkg.com/tesseract.js@5.0.3/dist/worker.min.js
281 changes: 281 additions & 0 deletions public/tesseract/tesseract-core-lstm.wasm.js

Large diffs are not rendered by default.

285 changes: 0 additions & 285 deletions public/tesseract/tesseract-core.wasm.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/tesseract/worker.min.js

Large diffs are not rendered by default.

Binary file removed public/traindata/jpn_vert.traineddata
Binary file not shown.
6 changes: 0 additions & 6 deletions public/traindata/readme

This file was deleted.

59 changes: 29 additions & 30 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import $ from "jquery";
import tippy, { followCursor, hideAll } from "tippy.js";
import { encode } from "he";
import matchUrl from "match-url-wildcard";
import delay from "delay";
import { waitUntil } from "async-wait-until";
import { debounce } from "throttle-debounce";
// import * as GoogleDocsUtils from "google-docs-utils";

import { enableSelectionEndEvent } from "/src/event/selection";
import { enableMouseoverTextEvent } from "/src/event/mouseover";
Expand All @@ -33,15 +30,15 @@ var destructionEvent = "destructmyextension_MouseTooltipTranslator"; // + chrome
const controller = new AbortController();
const { signal } = controller;
var mouseoverInterval;
var writingField =
'input[type="text"], input[type="search"], input:not([type]), textarea, [contenteditable="true"], [role=textbox], [spellcheck]';
var isYoutubeOn = false;
var delayTime = 700;
var isBlobPdf = false;
var prevWordParam = [];
var isGoogleDoc = false;
var mouseKeyMap = ["ClickLeft", "ClickMiddle", "ClickRight"];
var prevTooltipText = "";
var isYoutubeOn = false;
var isYoutubeScriptOn = false;
var isYoutubeListenerOn = false;

//tooltip core======================================================================
$(async function initMouseTooltipTranslator() {
Expand Down Expand Up @@ -229,7 +226,7 @@ function getDetectType() {
function checkMouseTargetIsSpecialWebBlock() {
// if mouse targeted web element contain particular class name, return true
//mousetooltip ocr block
return ["ocr_text_div"].some((className) =>
return ["ocr_text_div", "textFitted"].some((className) =>
mouseTarget?.classList?.contains(className)
);
}
Expand Down Expand Up @@ -299,7 +296,7 @@ async function translateWriting() {
//check current focus is write box
if (
!keyDownList[setting["keyDownTranslateWriting"]] ||
!getFocusedWritingBox()
!util.getFocusedWritingBox()
) {
return;
}
Expand Down Expand Up @@ -333,11 +330,6 @@ function insertText(inputText) {
// document.execCommand("insertText", false, inputText);
}

function getFocusedWritingBox() {
var writingBox = $(":focus");
return writingBox.is(writingField) ? writingBox : null;
}

function getWritingText() {
// get current selected text, if no select, get all
if (window.getSelection().type == "Caret") {
Expand Down Expand Up @@ -463,7 +455,7 @@ function setMouseStatus(e) {
const checkWritingBox = debounce(delayTime, () => {
// if mouse target is not writing box or already bound, return
// make key bind for preventDefault
var $writingField = $(writingField);
var $writingField = $(util.writingField);
if (!$writingField.is(mouseTarget) || $writingField.data("mttBound")) {
return;
}
Expand Down Expand Up @@ -545,7 +537,7 @@ async function getSetting() {
applyStyleSetting();
selectedText = "";
ocrView.removeAllOcrEnv();
initYoutubePlayer();
checkYoutube();
});
}

Expand Down Expand Up @@ -596,10 +588,8 @@ function applyStyleSetting() {
.ocr_text_div{
position: absolute;
opacity: 0.7;
font-size: calc(100% + 1cqw);
overflow: hidden;
border: 2px solid CornflowerBlue;
color: transparent !important;
border: 2px solid CornflowerBlue;
background: none !important;
}`;

Expand Down Expand Up @@ -715,14 +705,33 @@ async function checkYoutube() {
!matchUrl(document.location.href, "www.youtube.com") ||
setting["enableYoutube"] == "null"
) {
isYoutubeOn = false;
return;
}

isYoutubeOn = true;
await util.injectScript("youtube.js");
initYoutubePlayer();
await injectYoutubeScript();
util.postFrame({
type: "initYoutubePlayer",
targetLang: setting["translateTarget"],
subSetting: setting["enableYoutube"],
captionOnStatusByUser: setting["captionOnStatusByUser"],
});
addCaptionOnOffListener();
}
async function injectYoutubeScript() {
if (isYoutubeScriptOn) {
return;
}
isYoutubeScriptOn = true;
await util.injectScript("youtube.js");
}

function addCaptionOnOffListener() {
if (isYoutubeListenerOn) {
return;
}
isYoutubeListenerOn = true;
util.addFrameListener("youtubeCaptionOnOff", ({ captionOnStatusByUser }) => {
setting["captionOnStatusByUser"] = captionOnStatusByUser;
setting.save();
Expand All @@ -735,16 +744,6 @@ function pausePlayer() {
function playPlayer() {
util.postFrame({ type: "playPlayer" });
}
function initYoutubePlayer() {
if (isYoutubeOn) {
util.postFrame({
type: "initYoutubePlayer",
targetLang: setting["translateTarget"],
subSetting: setting["enableYoutube"],
captionOnStatusByUser: setting["captionOnStatusByUser"],
});
}
}

function checkMouseTargetIsYoutubeSubtitle() {
if (!isYoutubeOn || !$(mouseTarget).is(".ytp-caption-segment")) {
Expand Down
47 changes: 24 additions & 23 deletions src/ocr/ocrIframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Tesseract from "tesseract.js";
import { waitUntil, WAIT_FOREVER } from "async-wait-until";
import * as util from "/src/util";

var schedulerList = {};
var loadingList = {};

//ocr process, listen image from contents js and respond text
//1. listen to get image from iframe host
//2. use tesseract to process ocr to image to get text
Expand All @@ -20,7 +23,6 @@ window.addEventListener(
);

// ocr ===========================================================
var schedulerList = {};

async function doOcr(request) {
var type = "ocrSuccess";
Expand Down Expand Up @@ -72,7 +74,6 @@ function useTesseract(image, lang, rectangles, mode) {
try {
var data = [];
var scheduler = await getScheduler(lang, mode);

// //ocr on plain image
if (mode == "auto") {
var d = await scheduler.addJob("recognize", image);
Expand All @@ -97,41 +98,43 @@ function useTesseract(image, lang, rectangles, mode) {
});
}
async function getScheduler(lang, mode) {
await waitUntil(() => schedulerList[lang + "_" + mode], {
var id = lang + "_" + mode;
await waitUntil(() => schedulerList[id], {
timeout: WAIT_FOREVER,
});
return schedulerList[lang + "_" + mode];
return schedulerList[id];
}

async function loadScheduler(lang, mode) {
if (schedulerList[lang + "_" + mode]) {
return schedulerList[lang + "_" + mode];
var id = lang + "_" + mode;
if (loadingList[id]) {
return;
}
loadingList[id] = true;

var isLocal = lang == "jpn_vert";
var scheduler = Tesseract.createScheduler();
var workerIndexList = mode == "auto" ? [0] : [0, 1];
var workerIndexList = mode == "auto" ? [0] : [0, 1, 2, 3];
var workerPath = chrome.runtime.getURL("/tesseract/worker.min.js");
var corePath = chrome.runtime.getURL(
"/tesseract/tesseract-core-lstm.wasm.js"
);

var tessedit_pageseg_mode =
mode == "auto"
? Tesseract.PSM.AUTO_ONLY
: lang == "jpn_vert"
: lang.includes("vert")
? Tesseract.PSM.SINGLE_BLOCK_VERT_TEXT
: Tesseract.PSM.SINGLE_BLOCK;

await Promise.all(
workerIndexList.map(async (i) => {
var worker = await Tesseract.createWorker({
var worker = await Tesseract.createWorker(lang, 1, {
workerBlobURL: false,
workerPath: chrome.runtime.getURL("/tesseract/worker.min.js"),
corePath: chrome.runtime.getURL("/tesseract/tesseract-core.wasm.js"),
langPath: isLocal
? chrome.runtime.getURL("/traindata")
: "https://raw.githubusercontent.com/naptha/tessdata/gh-pages/4.0.0", //https://github.com/zodiac3539/jpn_vert
gzip: isLocal ? false : true,
// logger: m => console.log(m), // Add logger here
workerPath,
corePath,
// logger: (m) => console.log(m), // Add logger here
});
await worker.loadLanguage(lang);
await worker.initialize(lang);

await worker.setParameters({
user_defined_dpi: "100",
tessedit_pageseg_mode,
Expand All @@ -141,13 +144,11 @@ async function loadScheduler(lang, mode) {
})
);

schedulerList[lang + "_" + mode] = scheduler;
return schedulerList[lang + "_" + mode];
schedulerList[id] = scheduler;
}

function initTesseract(request) {
loadScheduler(request.lang, "auto");
loadScheduler(request.lang, "bbox");
loadScheduler(request.lang, request.mode);

response({
windowPostMessageProxy: request.windowPostMessageProxy,
Expand Down
Loading

0 comments on commit e438cd9

Please sign in to comment.