esearch-ocr
Advanced tools
Comparing version 5.1.1 to 5.1.2
{ | ||
"name": "esearch-ocr", | ||
"version": "5.1.1", | ||
"version": "5.1.2", | ||
"description": "paddleocr models run on onnx", | ||
@@ -5,0 +5,0 @@ "main": "./dist/esearch-ocr.umd.cjs", |
@@ -50,2 +50,4 @@ // biome-ignore lint/suspicious/noImplicitAnyLet: 可自定义cv | ||
let onProgress = (type: "det" | "rec", total: number, count: number) => {}; | ||
async function init(op: { | ||
@@ -69,2 +71,4 @@ detPath?: string; | ||
cv?; | ||
onProgress?: (type: "det" | "rec", total: number, count: number) => void; | ||
}) { | ||
@@ -96,2 +100,3 @@ ort = op.ort; | ||
else if (typeof require !== "undefined") cv = require("opencv.js"); | ||
if (op.onProgress) onProgress = op.onProgress; | ||
return { ocr: x, det: Det, rec: Rec }; | ||
@@ -109,2 +114,3 @@ } | ||
const box = await Det(img); | ||
onProgress("det", 1, 1); | ||
@@ -114,3 +120,3 @@ const mainLine = await Rec(box); | ||
const newMainLine = afAfRec(mainLine); | ||
console.log(mainLine, newMainLine); | ||
log(mainLine, newMainLine); | ||
task.l("end"); | ||
@@ -151,5 +157,8 @@ return { src: mainLine, ...newMainLine }; | ||
const recL = beforeRec(box); | ||
let runCount = 0; | ||
const recPromises = recL.map(async (item) => { | ||
const { b, imgH, imgW } = item; | ||
const recResults = await runRec(b, imgH, imgW, rec); | ||
runCount++; | ||
onProgress("rec", recL.length, runCount); | ||
return afterRec(recResults, dic); | ||
@@ -214,3 +223,3 @@ }); | ||
const transposedData = toPaddleInput(image, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]); | ||
console.log(image); | ||
log(image); | ||
if (dev) { | ||
@@ -288,3 +297,3 @@ const srcCanvas = data2canvas(image); | ||
console.log(edgeRect); | ||
log(edgeRect); | ||
@@ -627,2 +636,54 @@ src.delete(); | ||
// 短轴扩散,合并为段 | ||
const newL_ = structuredClone(l).sort((a, b) => a.box[0][1] - b.box[0][1]) as resultType[0][]; | ||
const newLZ: resultType[0][][] = []; | ||
// 合并行 | ||
for (const j of newL_) { | ||
const last = newLZ.at(-1)?.at(-1); | ||
if (!last) { | ||
newLZ.push([j]); | ||
continue; | ||
} | ||
const thisCy = (j.box[2][1] + j.box[0][1]) / 2; | ||
const lastCy = (last.box[2][1] + last.box[0][1]) / 2; | ||
if (Math.abs(thisCy - lastCy) < 0.5 * (j.box[2][1] - j.box[0][1])) { | ||
const lLast = newLZ.at(-1); | ||
if (!lLast) { | ||
newLZ.push([j]); | ||
} else { | ||
lLast.push(j); | ||
} | ||
} else { | ||
newLZ.push([j]); | ||
} | ||
} | ||
// 根据距离,合并或保持拆分 | ||
const newL: (resultType[0] | null)[] = []; | ||
for (const l of newLZ) { | ||
if (l.length === 1) { | ||
newL.push(l.at(0) as resultType[0]); | ||
continue; | ||
} | ||
const em = average(l.map((i) => i.box[2][1] - i.box[0][1])); | ||
l.sort((a, b) => a.box[0][0] - b.box[0][0]); | ||
let last = l.at(0) as resultType[0]; | ||
for (const this_ of l.slice(1)) { | ||
const lastBoxRightX = last.box[1][0] ?? Number.NEGATIVE_INFINITY; | ||
const thisLeftX = this_.box[0][0]; | ||
if (thisLeftX - lastBoxRightX > em) { | ||
newL.push(last); | ||
last = this_; | ||
} else { | ||
last.text += this_.text; | ||
last.mean = (last.mean + this_.mean) / 2; | ||
last.box = outerRect([last.box, this_.box]); | ||
} | ||
} | ||
newL.push(last); | ||
} | ||
// todo 分割线为边界 | ||
@@ -633,3 +694,2 @@ // 分栏 | ||
const newL = structuredClone(l).sort((a, b) => a.box[0][1] - b.box[0][1]) as (resultType[0] | null)[]; | ||
const maxY = newL.reduce((a, b) => Math.max(a, b?.box[2][1] ?? 0), 0); | ||
@@ -688,3 +748,3 @@ for (let i = 0; i <= maxY; i++) { | ||
if (!last) continue; | ||
const jl = r(centerPoint(b.box), centerPoint(last.box)); | ||
const jl = centerPoint(b.box)[1] - centerPoint(last.box)[1]; | ||
if (jl < _jl) { | ||
@@ -777,2 +837,3 @@ nearest = Number(i); | ||
const c = v.src; | ||
// gap feq | ||
const gs: Record<number, number> = {}; | ||
@@ -794,6 +855,6 @@ for (let i = 1; i < c.length; i++) { | ||
const maxN = Math.max(...Object.values(gs)); | ||
const minX = Math.min(...Object.keys(gs).map(Number)); | ||
const gapsL = Object.keys(gs) | ||
.sort() | ||
.map(Number) | ||
.filter((g) => g > 0) | ||
.map((i) => Number(i) - minX) | ||
.filter((g) => gs[g] !== maxN); // 去掉一个最大值 | ||
@@ -830,2 +891,15 @@ for (let i = 1; i < gapsL.length; i++) { | ||
if (dev) { | ||
const color: string[] = []; | ||
for (let h = 10; h < 360; h += Math.floor(360 / p.length)) { | ||
color.push(`hsl(${h}, 100%, 50%)`); | ||
} | ||
for (const i in p) { | ||
for (const b of p[i].parragraphs) { | ||
drawBox(b.parse.box, b.parse.text, color[i]); | ||
} | ||
} | ||
} | ||
const pss = p.flatMap((v) => v.parragraphs.map((p) => p.parse)); | ||
@@ -868,3 +942,4 @@ | ||
ctx.stroke(); | ||
ctx.strokeStyle = "black"; | ||
ctx.strokeText(id, box[0][0], box[0][1]); | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
320730
4855