优化后端

This commit is contained in:
Miayui
2026-09-11 17:56:56 +08:00
parent 7eed11b774
commit 812e67422d
7299 changed files with 1744959 additions and 112 deletions
@@ -0,0 +1,29 @@
<!DOCTYPE HTML>
<html>
<head>
<script src="/dist/tesseract.min.js"></script>
</head>
<body>
<input type="file" id="uploader" multiple>
<script type="module">
// A worker is created once and used every time a user uploads a new file.
const worker = await Tesseract.createWorker("eng", 1, {
corePath: '../../node_modules/tesseract.js-core',
workerPath: "/dist/worker.min.js",
logger: function(m){console.log(m);}
});
const recognize = async function(evt){
const files = evt.target.files;
for (let i=0; i<files.length; i++) {
const ret = await worker.recognize(files[i]);
console.log(ret.data.text);
}
}
const elm = document.getElementById('uploader');
elm.addEventListener('change', recognize);
</script>
</body>
</html>
@@ -0,0 +1,47 @@
<!DOCTYPE HTML>
<html>
<head>
<script src="/dist/tesseract.min.js"></script>
</head>
<body>
<input type="file" id="uploader" multiple>
<script type="module">
// This example builds on "basic-efficient.html".
// Rather than using a single worker, a scheduler manages a pool of multiple workers.
// While performance is similar for a single file, this parallel processing results in significantly
// faster speeds when used with multiple files.
const scheduler = Tesseract.createScheduler();
// Creates worker and adds to scheduler
const workerGen = async () => {
const worker = await Tesseract.createWorker("eng", 1, {
corePath: '../../node_modules/tesseract.js-core',
workerPath: "/dist/worker.min.js",
logger: function(m){console.log(m);}
});
scheduler.addWorker(worker);
}
const workerN = 4;
(async () => {
const resArr = Array(workerN);
for (let i=0; i<workerN; i++) {
resArr[i] = await workerGen();
}
})();
const recognize = async function(evt){
const files = evt.target.files;
for (let i=0; i<files.length; i++) {
scheduler.addJob('recognize', files[i]).then((x) => console.log(x.data.text))
}
}
const elm = document.getElementById('uploader');
elm.addEventListener('change', recognize);
</script>
</body>
</html>
@@ -0,0 +1,52 @@
<html>
<head>
<script src="/dist/tesseract.min.js"></script>
</head>
<body>
<div>
<input type="file" id="uploader">
<button id="download-pdf" disabled="true">Download PDF</button>
</div>
<textarea id="board" readonly rows="8" cols="80">Upload an image file</textarea>
<script type="module">
const { createWorker } = Tesseract;
const worker = await createWorker("eng", 1, {
corePath: '/node_modules/tesseract.js-core',
workerPath: "/dist/worker.min.js",
logger: m => console.log(m),
});
const uploader = document.getElementById('uploader');
const dlBtn = document.getElementById('download-pdf');
let pdf;
const recognize = async ({ target: { files } }) => {
const res = await worker.recognize(files[0],{pdfTitle: "Example PDF"},{pdf: true});
pdf = res.data.pdf;
const text = res.data.text;
const board = document.getElementById('board');
board.value = text;
dlBtn.disabled = false;
};
const downloadPDF = async () => {
const filename = 'tesseract-ocr-result.pdf';
const blob = new Blob([new Uint8Array(pdf)], { type: 'application/pdf' });
if (navigator.msSaveBlob) {
// IE 10+
navigator.msSaveBlob(blob, filename);
} else {
const link = document.createElement('a');
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
};
uploader.addEventListener('change', recognize);
dlBtn.addEventListener('click', downloadPDF);
</script>
</body>
</html>
@@ -0,0 +1,55 @@
<html>
<head>
<script src="/dist/tesseract.min.js"></script>
<style>
.column {
float: left;
width: 20%;
padding: 5px;
}
</style>
</head>
<body>
<input type="file" id="uploader">
<div class="row">
<div class="column">
<p>Input Image</p>
<img id="imgInput" style="max-width:500px;">
</div>
<div class="column">
<p>Rotated, Original Color</p>
<img id="imgOriginal" style="max-width:500px;">
</div>
<div class="column">
<p>Rotated, Grey</p>
<img id="imgGrey" style="max-width:500px;">
</div>
<div class="column">
<p>Rotated, Binary</p>
<img id="imgBinary" style="max-width:500px;">
</div>
</div>
<script>
const recognize = async ({ target: { files } }) => {
document.getElementById("imgInput").src = URL.createObjectURL(files[0]);
const worker = await Tesseract.createWorker("eng", 1, {
// corePath: '/tesseract-core-simd.wasm.js',
workerPath: "/dist/worker.min.js"
});
const ret = await worker.recognize(files[0], {rotateAuto: true}, {imageColor: true, imageGrey: true, imageBinary: true});
document.getElementById("imgOriginal").src = ret.data.imageColor;
document.getElementById("imgGrey").src = ret.data.imageGrey;
document.getElementById("imgBinary").src = ret.data.imageBinary;
}
const elm = document.getElementById('uploader');
elm.addEventListener('change', recognize);
</script>
</body>
</html>
@@ -0,0 +1,21 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const { createWorker } = require('../..');
const [,, imagePath] = process.argv;
const image = path.resolve(__dirname, (imagePath || '../../tests/assets/images/cosmic.png'));
console.log(`Recognizing ${image}`);
(async () => {
const worker = await createWorker();
const { data: { text, pdf } } = await worker.recognize(image, { pdfTitle: 'Example PDF' }, { pdf: true });
console.log(text);
fs.writeFileSync('tesseract-ocr-result.pdf', Buffer.from(pdf));
console.log('Generate PDF: tesseract-ocr-result.pdf');
await worker.terminate();
})();
@@ -0,0 +1,37 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const { createWorker } = require('../..');
const [,, imagePath] = process.argv;
const image = path.resolve(__dirname, (imagePath || '../data/meditations.jpg'));
console.log(`Recognizing ${image}`);
// Tesseract.js returns images (imageColor, imageGrey, imageBinary) as strings
// to be used as source tags.
// This function converts to Uint8Array data for saving to disk.
const convertImage = (imageSrc) => {
const data = atob(imageSrc.split(',')[1])
.split('')
.map((c) => c.charCodeAt(0));
return new Uint8Array(data);
};
(async () => {
const worker = await createWorker();
const { data: { imageColor, imageGrey, imageBinary } } = await worker.recognize(image,
{ rotateAuto: true }, { imageColor: true, imageGrey: true, imageBinary: true });
console.log('Saving intermediate images: imageColor.png, imageGrey.png, imageBinary.png');
fs.writeFileSync('imageColor.png', convertImage(imageColor));
fs.writeFileSync('imageGrey.png', convertImage(imageGrey));
fs.writeFileSync('imageBinary.png', convertImage(imageBinary));
await worker.terminate();
})();
@@ -0,0 +1,20 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const { createWorker } = require('../..');
const [,, imagePath] = process.argv;
const image = path.resolve(__dirname, (imagePath || '../../tests/assets/images/cosmic.png'));
console.log(`Recognizing ${image}`);
(async () => {
const worker = await createWorker('eng', 1, {
logger: (m) => console.log(m),
});
const { data: { text } } = await worker.recognize(image);
console.log(text);
await worker.terminate();
})();
@@ -0,0 +1,40 @@
'use strict';
const path = require('path');
const { createWorker, createScheduler } = require('../..');
const [,, imagePath] = process.argv;
// Note: This example recognizes the same image 4 times in parallel
// to show how schedulers can be used to speed up bulk jobs.
// In actual use you would (obviously) not want to run multiple identical jobs.
const image = path.resolve(__dirname, (imagePath || '../../tests/assets/images/cosmic.png'));
const imageArr = [image, image, image, image];
const scheduler = createScheduler();
// Creates worker and adds to scheduler
const workerGen = async () => {
const worker = await createWorker('eng', 1, { cachePath: '.' });
scheduler.addWorker(worker);
};
const workerN = 4;
(async () => {
const resArr = Array(workerN);
for (let i = 0; i < workerN; i++) {
resArr[i] = workerGen();
}
await Promise.all(resArr);
const resArr2 = Array(imageArr.length);
for (let i = 0; i < imageArr.length; i++) {
resArr2[i] = scheduler.addJob('recognize', image).then((x) => console.log(x.data.text));
}
await Promise.all(resArr2);
await scheduler.terminate(); // It also terminates all workers.
})();