Skip to content

Commit fd0163e

Browse files
committed
fix ocr
1 parent a85f225 commit fd0163e

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

doc/description.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Mouseover Translate Any Language At Once
1919
English, Russian, Japanese, Chinese and so on
2020

2121
# Change Log
22+
- 0.1.194
23+
- Fix Shadow root image ocr not working
24+
- Enhance performance speed for OCR
2225
- 0.1.193
2326
- Fix OCR reprocess conflict
2427
- Portuguese, Brazilian localization (contributed by EdsonBittencourt)

src/contentScript.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ function applyStyleSetting() {
887887
visibility: visible !important;
888888
white-space: pre-line;
889889
}
890-
.tippy-box[data-theme~="custom"], .tippy-content *{
890+
.tippy-box[data-theme~="custom"], .tippy-box[data-theme~="ocr"], .tippy-content *{
891891
font-size: ${setting["tooltipFontSize"]}px !important;
892892
text-align: ${setting["tooltipTextAlign"]} !important;
893893
overflow-wrap: break-word !important;
@@ -907,8 +907,16 @@ function applyStyleSetting() {
907907
box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
908908
opacity: 1.0; /* Adjusted opacity for transparency */
909909
}
910+
.tippy-box[data-theme~="ocr"]{
911+
max-width: $1000px !important;
912+
backdrop-filter: blur(${setting["tooltipBackgroundBlur"]}px) !important;
913+
background-color: ${setting["tooltipBackgroundColor"]} !important;
914+
border: 1px solid ${setting["tooltipBorderColor"]};
915+
box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
916+
opacity: 1.0; /* Adjusted opacity for transparency */
917+
}
910918
.tippy-box[data-theme~="transparent"] {
911-
max-width: 200px !important;
919+
max-width: $1000px !important;
912920
backdrop-filter: blur(${setting["tooltipBackgroundBlur"]}px) !important;
913921
background-color: ${setting["tooltipBackgroundColor"]} !important;
914922
border: 1px solid ${setting["tooltipBorderColor"]};
@@ -921,18 +929,19 @@ function applyStyleSetting() {
921929
visibility: visible !important;
922930
position: absolute !important;
923931
}
924-
.tippy-box[data-theme~='custom'][data-placement^='top'] > .tippy-arrow::before {
932+
.tippy-box[data-theme~='custom'][data-placement^='top'] > .tippy-arrow::before, .tippy-box[data-theme~='ocr'][data-placement^='top'] > .tippy-arrow::before {
925933
border-top-color: ${setting["tooltipBackgroundColor"]} !important;
926934
}
927-
.tippy-box[data-theme~='custom'][data-placement^='bottom'] > .tippy-arrow::before {
935+
.tippy-box[data-theme~='custom'][data-placement^='bottom'] > .tippy-arrow::before, .tippy-box[data-theme~='ocr'][data-placement^='bottom'] > .tippy-arrow::before {
928936
border-bottom-color: ${setting["tooltipBackgroundColor"]} !important;
929937
}
930-
.tippy-box[data-theme~='custom'][data-placement^='left'] > .tippy-arrow::before {
938+
.tippy-box[data-theme~='custom'][data-placement^='left'] > .tippy-arrow::before, .tippy-box[data-theme~='ocr'][data-placement^='left'] > .tippy-arrow::before {
931939
border-left-color: ${setting["tooltipBackgroundColor"]} !important;
932940
}
933-
.tippy-box[data-theme~='custom'][data-placement^='right'] > .tippy-arrow::before {
941+
.tippy-box[data-theme~='custom'][data-placement^='right'] > .tippy-arrow::before, .tippy-box[data-theme~='ocr'][data-placement^='right'] > .tippy-arrow::before {
934942
border-right-color: ${setting["tooltipBackgroundColor"]} !important;
935943
}
944+
936945
.mtt-highlight{
937946
background-color: ${setting["mouseoverTextHighlightColor"]} !important;
938947
position: absolute !important;

src/ocr/ocrView.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ async function showTooltipBoxes(img, textBoxList) {
218218
var { targetText, sourceLang, targetLang } = await handleTranslate(
219219
textBox["text"]
220220
);
221+
// filter large translate text len gap
222+
if(targetText.length >textBox["text"].length*7){
223+
continue;
224+
}
221225
addTooltipBox(img, textBox, targetText, targetLang);
222226
}
223227
}
@@ -289,12 +293,13 @@ function adjustTextBoxBbox(textBox, ratio) {
289293
textBox["bbox"]["y1"] = Math.ceil(textBox["bbox"]["y1"] / ratio);
290294
}
291295

296+
292297
function addTooltipBox(img, textBox, text, targetLang) {
293298
// Create a tooltip element using Tippy.js
294-
const tooltipContent = $("<div/>", {
299+
var tooltipWidth = textBox["bbox"]["x1"] - textBox["bbox"]["x0"];
300+
const tooltipContent = $("<span/>", {
295301
text: text,
296302
css: {
297-
maxWidth: "200px",
298303
wordWrap: "break-word",
299304
zIndex: 1000001, // Ensure tooltip content is in front
300305
pointerEvents: "auto", // Allow pointer interactions with the tooltip content
@@ -322,13 +327,14 @@ function addTooltipBox(img, textBox, text, targetLang) {
322327
const instance = tippy(tooltipTarget[0], {
323328
content: tooltipContent[0],
324329
allowHTML: true,
325-
theme: "custom",
330+
theme: "ocr",
326331
placement: "top",
327332
zIndex: 100000 + textBox["text"].length, // Adjust z-index based on text length
328333
arrow: false,
329334
role: "mtttooltip",
330335
showOnCreate: true, // Ensure the tooltip is always visible
331336
hideOnClick: false, // Prevent hiding on outside click
337+
maxWidth: `${tooltipWidth}px`, // Set tooltip width dynamically
332338
popperOptions: {
333339
modifiers: [
334340
{
@@ -365,10 +371,20 @@ function addTooltipBox(img, textBox, text, targetLang) {
365371
// Restore the tooltip visibility when mouse leaves the tooltip content
366372
tooltipContent.on("mouseleave", () => {
367373
instance.setProps({
368-
theme: "custom", // Restore the original theme
374+
theme: "ocr", // Restore the original theme
369375
});
370376
tooltipContent.css("opacity", 1); // Restore opacity
371377
});
378+
379+
$(window).on("resize", () => {
380+
const { left, top, width, height } = calculateImgSegBoxSize(img, textBox["bbox"]);
381+
tooltipTarget.css({
382+
left: `${left}px`,
383+
top: `${top + height * 0.7}px`,
384+
width: `${width}px`,
385+
height: `1px`,
386+
});
387+
});
372388
}
373389

374390
function getTextBoxList(ocrData) {

src/ocr/opencvHandler.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ function detectText(canvasIn, mode) {
124124
// Convert image to grayscale and ensure single-channel
125125
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY, 0);
126126
// Threshold to get white areas (255, 255, 255)
127+
// cv.bitwise_not(dst, dst);
127128

128129
//get white area as mask
129130
cv.threshold(dst, dst, 230, 255, cv.THRESH_BINARY);
130131

132+
// showImage(dst, mode);
131133
// Combine all masks into one
132134
let combinedFloodMask = new cv.Mat(
133135
dst.rows,
@@ -162,6 +164,7 @@ function detectText(canvasIn, mode) {
162164
combinedFloodMask,
163165
combinedFloodVisited
164166
);
167+
// showImage(combinedFloodMask, mode);
165168

166169
// Remove mask area that exists in combinedFloodMask
167170
cv.bitwise_not(dst, dst);
@@ -172,6 +175,8 @@ function detectText(canvasIn, mode) {
172175
let mask = new cv.Mat();
173176
cv.bitwise_and(src, src, mask, dst);
174177

178+
// showImage(mask, mode);
179+
175180
// make invert white area using floodfill
176181
dst = mask.clone(); // Update dst to the masked image
177182
cv.copyMakeBorder(
@@ -199,12 +204,14 @@ function detectText(canvasIn, mode) {
199204
let slicedResultMask = new cv.Mat();
200205
cv.bitwise_and(src, src, slicedResultMask, slicedBorderMask);
201206

207+
// showImage(slicedResultMask, mode);
202208
// // make white background and combine with slicedResultMask
203209
cv.bitwise_not(slicedBorderMask, slicedBorderMask);
204210
cv.cvtColor(slicedBorderMask, slicedBorderMask, cv.COLOR_GRAY2RGBA, 0);
205211
cv.bitwise_or(slicedResultMask, slicedBorderMask, slicedBorderMask);
206212
// showImage(slicedBorderMask, mode);
207213

214+
208215
// Enhance color saturation
209216
let enhancedImage = new cv.Mat();
210217
cv.cvtColor(slicedBorderMask, enhancedImage, cv.COLOR_RGBA2RGB, 0);

0 commit comments

Comments
 (0)