Compare commits

..
Author SHA1 Message Date
ShareVB 6571963332 fix: deps 2024-07-14 22:54:52 +02:00
ShareVB f74716162b fix: update queryParams 2024-07-14 22:45:57 +02:00
ShareVB 4cd72aeb18 Merge remote-tracking branch 'origin/main' into feat/ocr-image 2024-07-14 22:45:13 +02:00
ShareVB 7a1beb5dd3 feat: add OCRized text statistics 2024-04-28 14:57:17 +02:00
sharevb 7f2bf11bd9 feat: refactor text stats to service 2024-04-28 14:44:05 +02:00
sharevb b7debece4d feat(c-file-upload): paste image
Property paste-image to allow pasting an image directly from clipboard
2024-04-28 14:43:32 +02:00
ShareVB 7e6860719b Merge branch 'feat/ocr-image' of https://github.com/sharevb/it-tools into HEAD 2024-03-18 23:06:56 +01:00
ShareVB 654f7a8b05 Fix build 2024-03-18 23:06:27 +01:00
sharevb 12a8962a6e Merge branch 'main' into feat/ocr-image 2024-03-18 23:00:48 +01:00
sharevb 0d17698bb0 feat(new tool): OCR Image
OCR an image with Tesseract.js
2024-03-18 22:59:12 +01:00
12 changed files with 950 additions and 376 deletions

No files matched your search

+5 -3
View File
@@ -78,7 +78,6 @@ declare module '@vue/runtime-core' {
Encryption: typeof import('./src/tools/encryption/encryption.vue')['default']
EtaCalculator: typeof import('./src/tools/eta-calculator/eta-calculator.vue')['default']
FavoriteButton: typeof import('./src/components/FavoriteButton.vue')['default']
FileHasher: typeof import('./src/tools/file-hasher/file-hasher.vue')['default']
FormatTransformer: typeof import('./src/components/FormatTransformer.vue')['default']
GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default']
'GitMemo.content': typeof import('./src/tools/git-memo/git-memo.content.md')['default']
@@ -128,6 +127,7 @@ declare module '@vue/runtime-core' {
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
NCode: typeof import('naive-ui')['NCode']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
@@ -135,13 +135,15 @@ declare module '@vue/runtime-core' {
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputGroup: typeof import('naive-ui')['NInputGroup']
NInputGroupLabel: typeof import('naive-ui')['NInputGroupLabel']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin']
NStatistic: typeof import('naive-ui')['NStatistic']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OcrImage: typeof import('./src/tools/ocr-image/ocr-image.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
PdfSignatureChecker: typeof import('./src/tools/pdf-signature-checker/pdf-signature-checker.vue')['default']
+5 -2
View File
@@ -41,6 +41,7 @@
"@tiptap/pm": "2.1.6",
"@tiptap/starter-kit": "2.1.6",
"@tiptap/vue-3": "2.0.3",
"@types/pdfjs-dist": "^2.10.378",
"@types/figlet": "^1.5.8",
"@vicons/material": "^0.12.0",
"@vicons/tabler": "^0.12.0",
@@ -61,11 +62,10 @@
"figlet": "^1.7.0",
"figue": "^1.2.0",
"fuse.js": "^6.6.2",
"hash-wasm": "^4.11.0",
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
"js-base64": "^3.7.7",
"js-base64": "^3.7.6",
"json5": "^2.2.3",
"jwt-decode": "^3.1.2",
"libphonenumber-js": "^1.10.28",
@@ -78,11 +78,14 @@
"netmask": "^2.0.2",
"node-forge": "^1.3.1",
"oui-data": "^1.0.10",
"path2d-polyfill": "^3.0.1",
"pdf-signature-reader": "^1.4.2",
"pdfjs-dist": "^4.0.379",
"pinia": "^2.0.34",
"plausible-tracker": "^0.3.8",
"qrcode": "^1.5.1",
"sql-formatter": "^13.0.0",
"tesseract.js": "^5.0.4",
"ua-parser-js": "^1.0.35",
"ulid": "^2.3.0",
"unicode-emoji-json": "^0.4.0",
+467 -88
View File
File diff suppressed because it is too large. Load diff
-226
View File
@@ -1,226 +0,0 @@
<script setup lang="ts">
import {
createAdler32, // (): Promise<IHasher>
createBLAKE2b, // (bits?: number, key?: IDataType): Promise<IHasher> // default is 512 bits
createBLAKE2s, // (bits?: number, key?: IDataType): Promise<IHasher> // default is 256 bits
createBLAKE3, // (bits?: number, key?: IDataType): Promise<IHasher> // default is 256 bits
createCRC32, // (): Promise<IHasher>
createCRC32C, // (): Promise<IHasher>
createKeccak, // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
createMD4, // (): Promise<IHasher>
createMD5, // (): Promise<IHasher>
createRIPEMD160, // (): Promise<IHasher>
createSHA1, // (): Promise<IHasher>
createSHA224, // (): Promise<IHasher>
createSHA256, // (): Promise<IHasher>
createSHA3, // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
createSHA384, // (): Promise<IHasher>
createSHA512, // (): Promise<IHasher>
createSM3, // (): Promise<IHasher>
createWhirlpool, // (): Promise<IHasher>
// createXXHash32, //(seed: number): Promise<IHasher>
// createXXHash64, //(seedLow: number, seedHigh: number): Promise<IHasher>
// createXXHash3, //(seedLow: number, seedHigh: number): Promise<IHasher>
// createXXHash128, //(seedLow: number, seedHigh: number): Promise<IHasher>
} from 'hash-wasm';
import type { lib } from 'crypto-js';
import { enc } from 'crypto-js';
import type { IHasher } from 'hash-wasm/dist/lib/WASMInterface';
import InputCopyable from '../../components/InputCopyable.vue';
import { convertHexToBin } from '../hash-text/hash-text.service';
import { useQueryParamOrStorage } from '@/composable/queryParams';
import { withDefaultOnError } from '@/utils/defaults';
const status = ref<'idle' | 'done' | 'error' | 'processing'>('idle');
const file = ref<File | null>(null);
async function getHashersAsync() {
return {
adler32: await createAdler32(), // (): Promise<IHasher>
BLAKE2b: await createBLAKE2b(), // (bits?: number, key?: IDataType): Promise<IHasher> // default is 512 bits
BLAKE2s: await createBLAKE2s(), // (bits?: number, key?: IDataType): Promise<IHasher> // default is 256 bits
BLAKE3: await createBLAKE3(), // (bits?: number, key?: IDataType): Promise<IHasher> // default is 256 bits
CRC32: await createCRC32(), // (): Promise<IHasher>
CRC32C: await createCRC32C(), // (): Promise<IHasher>
Keccak_224: await createKeccak(224), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
Keccak_256: await createKeccak(256), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
Keccak_384: await createKeccak(384), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
Keccak_512: await createKeccak(512), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
MD4: await createMD4(), // (): Promise<IHasher>
MD5: await createMD5(), // (): Promise<IHasher>
RIPEMD160: await createRIPEMD160(), // (): Promise<IHasher>
SHA1: await createSHA1(), // (): Promise<IHasher>
SHA224: await createSHA224(), // (): Promise<IHasher>
SHA256: await createSHA256(), // (): Promise<IHasher>
SHA3_224: await createSHA3(224), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
SHA3_256: await createSHA3(256), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
SHA3_384: await createSHA3(384), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
SHA3_512: await createSHA3(512), // (bits?: 224 | 256 | 384 | 512): Promise<IHasher> // default is 512 bits
SHA384: await createSHA384(), // (): Promise<IHasher>
SHA512: await createSHA512(), // (): Promise<IHasher>
SM3: await createSM3(), // (): Promise<IHasher>
Whirlpool: await createWhirlpool(), // (): Promise<IHasher>
};
}
const defaultHashWasmValues = {
adler32: '',
BLAKE2b: '',
BLAKE2s: '',
BLAKE3: '',
CRC32: '',
CRC32C: '',
Keccak_224: '',
Keccak_256: '',
Keccak_384: '',
Keccak_512: '',
MD4: '',
MD5: '',
RIPEMD160: '',
SHA1: '',
SHA224: '',
SHA256: '',
SHA3_224: '',
SHA3_256: '',
SHA3_384: '',
SHA3_512: '',
SHA384: '',
SHA512: '',
SM3: '',
Whirlpool: '',
};
const chunkSize = 64 * 1024 * 1024;
const fileReader = new FileReader();
async function hashChunkAsync(chunk: Blob, hashers: IHasher[]) {
return new Promise<void>((resolve, _reject) => {
fileReader.onload = async (e) => {
const view = new Uint8Array((e.target?.result as ArrayBuffer)!);
for (const hasher of hashers) {
hasher.update(view);
}
resolve();
};
fileReader.readAsArrayBuffer(chunk);
});
}
type AlgoWasmNames = keyof typeof defaultHashWasmValues;
const algoWasmNames = Object.keys(defaultHashWasmValues) as AlgoWasmNames[];
async function hashFileAsync(file: File) {
const chunkNumber = Math.floor(file.size / chunkSize);
const hashers = await getHashersAsync();
for (let i = 0; i <= chunkNumber; i++) {
const chunk = file.slice(
chunkSize * i,
Math.min(chunkSize * (i + 1), file.size),
);
await hashChunkAsync(chunk, Object.values(hashers));
}
const ret = { ...defaultHashWasmValues };
for (const algo of algoWasmNames) {
ret[algo as AlgoWasmNames] = hashers[algo as AlgoWasmNames].digest();
}
return Promise.resolve(ret);
}
const hashes = ref(defaultHashWasmValues);
async function onUpload(uploadedFile: File) {
file.value = uploadedFile;
status.value = 'processing';
try {
hashes.value = await hashFileAsync(uploadedFile);
status.value = 'done';
}
catch (e) {
status.value = 'error';
}
}
type Encoding = keyof typeof enc | 'Bin';
const encoding = useQueryParamOrStorage<Encoding>({ defaultValue: 'Hex', storageName: 'hash-text:encoding', name: 'encoding' });
function formatWithEncoding(words: lib.WordArray, encoding: Encoding) {
if (encoding === 'Bin') {
return convertHexToBin(words.toString(enc.Hex));
}
return words.toString(enc[encoding]);
}
const hashWasmValues = computed(() => withDefaultOnError(() => {
const encodingValue = encoding.value;
const hashesValue = hashes.value;
const ret = defaultHashWasmValues;
for (const algo of algoWasmNames) {
ret[algo] = formatWithEncoding(enc.Hex.parse(hashesValue[algo]), encodingValue);
}
return ret;
}, defaultHashWasmValues));
</script>
<template>
<div>
<c-card>
<c-file-upload
title="Drag and drop a file here, or click to select a file"
@file-upload="onUpload"
/>
<n-divider />
<c-select
v-model:value="encoding"
mb-4
label="Digest encoding"
:options="[
{
label: 'Binary (base 2)',
value: 'Bin',
},
{
label: 'Hexadecimal (base 16)',
value: 'Hex',
},
{
label: 'Base64 (base 64)',
value: 'Base64',
},
{
label: 'Base64url (base 64 with url safe chars)',
value: 'Base64url',
},
]"
/>
</c-card>
<div mt-3 flex justify-center>
<c-alert v-if="status === 'error'" type="error">
An error occured hashing file '{{ file?.name }}'.
</c-alert>
<n-spin
v-if="status === 'processing'"
size="small"
/>
</div>
<c-card v-if="status === 'done'" :title="`Hashes of ${file?.name}`">
<div v-for="algo in algoWasmNames" :key="algo" style="margin: 5px 0">
<n-input-group>
<n-input-group-label style="flex: 0 0 120px">
{{ algo.toUpperCase() }}
</n-input-group-label>
<InputCopyable :value="hashWasmValues[algo]" readonly />
</n-input-group>
</div>
</c-card>
</div>
</template>
-38
View File
@@ -1,38 +0,0 @@
import { EyeOff } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'File Hasher',
path: '/file-hasher',
description: 'Compute Hash of files',
keywords: ['file', 'hash',
'digest',
'crypto',
'security',
'text',
'MD5',
'SHA1',
'SHA256',
'SHA224',
'SHA512',
'SHA384',
'SHA3',
'RIPEMD160',
'ADLER32',
'BLAKE2B',
'BLAKE2S',
'BLAKE3',
'CRC32',
'CRC32C',
'KECCAK',
'PBKDF2',
'SM3',
'WHIRLPOOL',
'XXHASH128',
'XXHASH3',
'XXHASH32',
'XXHASH64'],
component: () => import('./file-hasher.vue'),
icon: EyeOff,
createdAt: new Date('2024-05-11'),
});
+9 -16
View File
@@ -1,11 +1,11 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as fileHasher } from './file-hasher';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
import { tool as textToUnicode } from './text-to-unicode';
import { tool as ocrImage } from './ocr-image';
import { tool as safelinkDecoder } from './safelink-decoder';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
@@ -86,20 +86,7 @@ import { tool as yamlViewer } from './yaml-viewer';
export const toolsByCategory: ToolCategory[] = [
{
name: 'Crypto',
components: [
tokenGenerator,
hashText,
fileHasher,
bcrypt,
uuidGenerator,
ulidGenerator,
cypher,
bip39,
hmacGenerator,
rsaKeyPairGenerator,
passwordStrengthAnalyser,
pdfSignatureChecker,
],
components: [tokenGenerator, hashText, bcrypt, uuidGenerator, ulidGenerator, cypher, bip39, hmacGenerator, rsaKeyPairGenerator, passwordStrengthAnalyser, pdfSignatureChecker],
},
{
name: 'Converter',
@@ -146,7 +133,13 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Images and videos',
components: [qrCodeGenerator, wifiQrCodeGenerator, svgPlaceholderGenerator, cameraRecorder],
components: [
qrCodeGenerator,
wifiQrCodeGenerator,
svgPlaceholderGenerator,
cameraRecorder,
ocrImage,
],
},
{
name: 'Development',
+12
View File
@@ -0,0 +1,12 @@
import { Scan } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'OCRize image and PDF',
path: '/ocr-image',
description: 'Perform OCR (Tesseract) on an image or PDF',
keywords: ['ocr', 'image', 'tesseract', 'pdf'],
component: () => import('./ocr-image.vue'),
icon: Scan,
createdAt: new Date('2024-03-09'),
});
+251
View File
@@ -0,0 +1,251 @@
<script setup lang="ts">
import type { Ref } from 'vue';
import { createWorker } from 'tesseract.js';
import { getDocument } from 'pdfjs-dist';
import * as pdfJS from 'pdfjs-dist';
import pdfJSWorkerURL from 'pdfjs-dist/build/pdf.worker?url';
import { textStatistics } from '../text-statistics/text-statistics.service';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const languages = [
{ name: 'English', code: 'eng' },
{ name: 'Portuguese', code: 'por' },
{ name: 'Afrikaans', code: 'afr' },
{ name: 'Albanian', code: 'sqi' },
{ name: 'Amharic', code: 'amh' },
{ name: 'Arabic', code: 'ara' },
{ name: 'Assamese', code: 'asm' },
{ name: 'Azerbaijani', code: 'aze' },
{ name: 'Azerbaijani - Cyrillic', code: 'aze_cyrl' },
{ name: 'Basque', code: 'eus' },
{ name: 'Belarusian', code: 'bel' },
{ name: 'Bengali', code: 'ben' },
{ name: 'Bosnian', code: 'bos' },
{ name: 'Bulgarian', code: 'bul' },
{ name: 'Burmese', code: 'mya' },
{ name: 'Catalan; Valencian', code: 'cat' },
{ name: 'Cebuano', code: 'ceb' },
{ name: 'Central Khmer', code: 'khm' },
{ name: 'Cherokee', code: 'chr' },
{ name: 'Chinese - Simplified', code: 'chi_sim' },
{ name: 'Chinese - Traditional', code: 'chi_tra' },
{ name: 'Croatian', code: 'hrv' },
{ name: 'Czech', code: 'ces' },
{ name: 'Danish', code: 'dan' },
{ name: 'Dutch; Flemish', code: 'nld' },
{ name: 'Dzongkha', code: 'dzo' },
{ name: 'English, Middle (1100-1500)', code: 'enm' },
{ name: 'Esperanto', code: 'epo' },
{ name: 'Estonian', code: 'est' },
{ name: 'Finnish', code: 'fin' },
{ name: 'French', code: 'fra' },
{ name: 'French, Middle (ca. 1400-1600)', code: 'frm' },
{ name: 'Galician', code: 'glg' },
{ name: 'Georgian', code: 'kat' },
{ name: 'German', code: 'deu' },
{ name: 'German Fraktur', code: 'frk' },
{ name: 'Greek, Modern (1453-)', code: 'ell' },
{ name: 'Greek, Ancient (-1453)', code: 'grc' },
{ name: 'Gujarati', code: 'guj' },
{ name: 'Haitian; Haitian Creole', code: 'hat' },
{ name: 'Hebrew', code: 'heb' },
{ name: 'Hindi', code: 'hin' },
{ name: 'Hungarian', code: 'hun' },
{ name: 'Icelandic', code: 'isl' },
{ name: 'Indonesian', code: 'ind' },
{ name: 'Inuktitut', code: 'iku' },
{ name: 'Irish', code: 'gle' },
{ name: 'Italian', code: 'ita' },
{ name: 'Japanese', code: 'jpn' },
{ name: 'Javanese', code: 'jav' },
{ name: 'Kannada', code: 'kan' },
{ name: 'Kazakh', code: 'kaz' },
{ name: 'Kirghiz; Kyrgyz', code: 'kir' },
{ name: 'Korean', code: 'kor' },
{ name: 'Kurdish', code: 'kur' },
{ name: 'Lao', code: 'lao' },
{ name: 'Latin', code: 'lat' },
{ name: 'Latvian', code: 'lav' },
{ name: 'Lithuanian', code: 'lit' },
{ name: 'Macedonian', code: 'mkd' },
{ name: 'Malay', code: 'msa' },
{ name: 'Malayalam', code: 'mal' },
{ name: 'Maltese', code: 'mlt' },
{ name: 'Marathi', code: 'mar' },
{ name: 'Nepali', code: 'nep' },
{ name: 'Norwegian', code: 'nor' },
{ name: 'Oriya', code: 'ori' },
{ name: 'Panjabi; Punjabi', code: 'pan' },
{ name: 'Persian', code: 'fas' },
{ name: 'Polish', code: 'pol' },
{ name: 'Pushto; Pashto', code: 'pus' },
{ name: 'Romanian; Moldavian; Moldovan', code: 'ron' },
{ name: 'Russian', code: 'rus' },
{ name: 'Sanskrit', code: 'san' },
{ name: 'Serbian', code: 'srp' },
{ name: 'Serbian - Latin', code: 'srp_latn' },
{ name: 'Sinhala; Sinhalese', code: 'sin' },
{ name: 'Slovak', code: 'slk' },
{ name: 'Slovenian', code: 'slv' },
{ name: 'Spanish; Castilian', code: 'spa' },
{ name: 'Swahili', code: 'swa' },
{ name: 'Swedish', code: 'swe' },
{ name: 'Syriac', code: 'syr' },
{ name: 'Tagalog', code: 'tgl' },
{ name: 'Tajik', code: 'tgk' },
{ name: 'Tamil', code: 'tam' },
{ name: 'Telugu', code: 'tel' },
{ name: 'Thai', code: 'tha' },
{ name: 'Tibetan', code: 'bod' },
{ name: 'Tigrinya', code: 'tir' },
{ name: 'Turkish', code: 'tur' },
{ name: 'Uighur; Uyghur', code: 'uig' },
{ name: 'Ukrainian', code: 'ukr' },
{ name: 'Urdu', code: 'urd' },
{ name: 'Uzbek', code: 'uzb' },
{ name: 'Uzbek - Cyrillic', code: 'uzb_cyrl' },
{ name: 'Vietnamese', code: 'vie' },
{ name: 'Welsh', code: 'cym' },
{ name: 'Yiddish', code: 'yid' },
];
const languagesOptions = Array.from(languages.map(l => ({
label: l.name,
value: l.code,
})));
const language = useQueryParamOrStorage({ name: 'lang', storageName: 'ocr-image:lang', defaultValue: 'eng' });
const pageSeparator = '\n=============\n';
const ocrInProgress = ref(false);
const fileInput = ref() as Ref<File>;
const ocrText = computedAsync(async () => {
try {
return (await ocr(fileInput.value, language.value));
}
catch (e: any) {
return e.toString();
}
});
const stats = computed(() => textStatistics(ocrText.value?.replace(new RegExp(pageSeparator, 'g'), ' ') || ''));
const pageCount = computed(() => ocrText.value?.split(new RegExp(pageSeparator, 'g')).length || 0);
async function onUpload(file: File) {
if (file) {
fileInput.value = file;
}
}
async function loadPdfFile(file: File) {
const arrBuffer = await file.arrayBuffer();
const byteArray = new Uint8Array(arrBuffer);
return getDocument({ data: byteArray }).promise;
}
async function convertPdfToImage(file: File) {
const pdf = await loadPdfFile(file);
const container = document.getElementById('container');
const images = [];
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) {
const page = await pdf.getPage(pageNumber);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.createElement('canvas');
canvas.height = viewport.height;
canvas.width = viewport.width;
container?.appendChild(canvas);
await page.render({
canvasContext: canvas.getContext('2d') as CanvasRenderingContext2D,
viewport,
}).promise;
images.push(canvas.toDataURL('image/png'));
}
return images;
};
async function ocr(file: File, language: string) {
if (!file) {
return '';
}
ocrInProgress.value = true;
const worker = await createWorker();
await worker.reinitialize(language);
const allTexts = [];
if (file.type.match('^image/')) {
const ret = await worker.recognize(file);
allTexts.push(ret.data.text);
}
else {
pdfJS.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;
for (const image of (await convertPdfToImage(file))) {
const ret = await worker.recognize(image);
allTexts.push(ret.data.text);
}
}
await worker.terminate();
ocrInProgress.value = false;
return allTexts.join(pageSeparator);
};
</script>
<template>
<div style="max-width: 600px;">
<c-select
v-model:value="language"
label="Language"
:options="languagesOptions"
searchable mb-2
/>
<c-file-upload
title="Drag and drop a Image or PDF here, or click to select a file"
:paste-image="true"
@file-upload="onUpload"
/>
<n-divider />
<div id="container" style="display: none;" />
<div>
<h3>OCR</h3>
<TextareaCopyable
v-if="!ocrInProgress"
v-model:value="ocrText"
:word-wrap="true"
/>
<n-spin
v-if="ocrInProgress"
size="small"
/>
</div>
<c-card v-if="!ocrInProgress && stats" title="Statistics">
<n-space mt-3>
<n-statistic label="Character count" :value="stats.chars" />
<n-statistic label="Word count" :value="stats.words" />
<n-statistic label="Line count" :value="stats.lines" />
<n-statistic label="Pages count" :value="pageCount" />
<n-statistic label="Sentences count" :value="stats.sentences" />
</n-space>
<n-divider />
<n-space>
<n-statistic label="Chars (no spaces)" :value="stats.chars_no_spaces" />
<n-statistic label="Uppercase chars" :value="stats.chars_upper" />
<n-statistic label="Lowercase chars" :value="stats.chars_lower" />
<n-statistic label="Digit chars" :value="stats.chars_digits" />
<n-statistic label="Punctuations" :value="stats.chars_puncts" />
<n-statistic label="Spaces chars" :value="stats.chars_spaces" />
<n-statistic label="Word count (no punct)" :value="stats.words_no_puncs" />
</n-space>
</c-card>
</div>
</template>
<style lang="less" scoped>
::v-deep(.n-upload-trigger) {
width: 100%;
}
</style>
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { getStringSizeInBytes } from './text-statistics.service';
import { getStringSizeInBytes, textStatistics } from './text-statistics.service';
describe('text-statistics', () => {
describe('getStringSizeInBytes', () => {
@@ -11,4 +11,114 @@ describe('text-statistics', () => {
expect(getStringSizeInBytes('aaaaaaaaaa')).toEqual(10);
});
});
describe('textStatistics', () => {
it('should return text statistics', () => {
expect(textStatistics('a')).toEqual({
chars: 1,
chars_digits: 0,
chars_lower: 1,
chars_no_spaces: 1,
chars_puncts: 0,
chars_spaces: 0,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 1,
words_no_puncs: 1,
});
expect(textStatistics('A')).toEqual({
chars: 1,
chars_digits: 0,
chars_lower: 0,
chars_no_spaces: 1,
chars_puncts: 0,
chars_spaces: 0,
chars_upper: 1,
lines: 1,
sentences: 1,
words: 1,
words_no_puncs: 1,
});
expect(textStatistics('a a')).toEqual({
chars: 3,
chars_digits: 0,
chars_lower: 2,
chars_no_spaces: 2,
chars_puncts: 0,
chars_spaces: 1,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 2,
words_no_puncs: 2,
});
expect(textStatistics('A a ; 1')).toEqual({
chars: 7,
chars_digits: 1,
chars_lower: 1,
chars_no_spaces: 4,
chars_puncts: 1,
chars_spaces: 3,
chars_upper: 1,
lines: 1,
sentences: 1,
words: 4,
words_no_puncs: 3,
});
expect(textStatistics('Some sentence! Une autre phrase ? « et avec des chiffres 1234 ! »')).toEqual({
chars: 65,
chars_digits: 4,
chars_lower: 41,
chars_no_spaces: 52,
chars_puncts: 5,
chars_spaces: 13,
chars_upper: 2,
lines: 1,
sentences: 3,
words: 14,
words_no_puncs: 10,
});
expect(textStatistics(`Some sentence! Une autre phrase ?
« et avec des chiffres 1234 ! »`)).toEqual({
chars: 72,
chars_digits: 4,
chars_lower: 41,
chars_no_spaces: 52,
chars_puncts: 5,
chars_spaces: 20,
chars_upper: 2,
lines: 2,
sentences: 3,
words: 14,
words_no_puncs: 10,
});
expect(textStatistics('12 35')).toEqual({
chars: 5,
chars_digits: 4,
chars_lower: 0,
chars_no_spaces: 4,
chars_puncts: 0,
chars_spaces: 1,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 2,
words_no_puncs: 2,
});
expect(textStatistics(' 1 2 3. Other ')).toEqual({
chars: 14,
chars_digits: 3,
chars_lower: 4,
chars_no_spaces: 9,
chars_puncts: 1,
chars_spaces: 5,
chars_upper: 1,
lines: 1,
sentences: 2,
words: 4,
words_no_puncs: 4,
});
});
});
});
@@ -1,3 +1,19 @@
export function getStringSizeInBytes(text: string) {
return new TextEncoder().encode(text).buffer.byteLength;
}
export function textStatistics(text: string) {
return {
chars: text.length,
chars_no_spaces: text.replace(/\s+/ug, '').length,
chars_upper: text.replace(/[^\p{Lu}]/ug, '').length,
chars_lower: text.replace(/[^\p{Ll}]/ug, '').length,
chars_digits: text.replace(/\D+/ug, '').length,
chars_puncts: text.replace(/[^\p{P}]/ug, '').length,
chars_spaces: text.replace(/\S/ug, '').length,
words: text.trim().split(/\s+/).length,
words_no_puncs: text.replace(/\p{P}/ug, '').trim().split(/\s+/).length,
sentences: (`${text} `).split(/\w\s*[\.!\?][\s\p{P}]*\s/u).filter(s => s && s?.length > 0).length,
lines: text.split(/\r\n|\r|\n/).length,
};
}
+66 -2
View File
@@ -5,10 +5,12 @@ const props = withDefaults(defineProps<{
multiple?: boolean
accept?: string
title?: string
pasteImage?: boolean
}>(), {
multiple: false,
accept: undefined,
title: 'Drag and drop files here, or click to select files',
pasteImage: false,
});
const emit = defineEmits<{
@@ -16,11 +18,31 @@ const emit = defineEmits<{
(event: 'fileUpload', file: File): void
}>();
const { multiple } = toRefs(props);
const { multiple, pasteImage } = toRefs(props);
const isOverDropZone = ref(false);
function toBase64(file: File) {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result?.toString() ?? '');
reader.onerror = error => reject(error);
});
}
const fileInput = ref<HTMLInputElement | null>(null);
const imgPreview = ref<HTMLImageElement | null>(null);
async function handlePreview(image: File) {
if (imgPreview.value) {
imgPreview.value.src = await toBase64(image);
}
}
function clearPreview() {
if (imgPreview.value) {
imgPreview.value.src = '';
}
}
function triggerFileInput() {
fileInput.value?.click();
@@ -39,7 +61,30 @@ function handleDrop(event: DragEvent) {
handleUpload(files);
}
function handleUpload(files: FileList | null | undefined) {
async function onPasteImage(evt: ClipboardEvent) {
if (!pasteImage.value) {
return false;
}
const items = evt.clipboardData?.items;
if (!items) {
return false;
}
for (let i = 0; i < items.length; i++) {
if (items[i].type.includes('image')) {
const imageFile = items[i].getAsFile();
if (imageFile) {
await handlePreview(imageFile);
emit('fileUpload', imageFile);
}
}
}
return true;
}
async function handleUpload(files: FileList | null | undefined) {
clearPreview();
if (_.isNil(files) || _.isEmpty(files)) {
return;
}
@@ -49,6 +94,7 @@ function handleUpload(files: FileList | null | undefined) {
return;
}
await handlePreview(files[0]);
emit('fileUpload', files[0]);
}
</script>
@@ -60,6 +106,7 @@ function handleUpload(files: FileList | null | undefined) {
'border-primary border-opacity-100': isOverDropZone,
}"
@click="triggerFileInput"
@paste.prevent="onPasteImage"
@drop.prevent="handleDrop"
@dragover.prevent
@dragenter="isOverDropZone = true"
@@ -73,6 +120,7 @@ function handleUpload(files: FileList | null | undefined) {
:accept="accept"
@change="handleFileInput"
>
<slot>
<span op-70>
{{ title }}
@@ -90,6 +138,22 @@ function handleUpload(files: FileList | null | undefined) {
<c-button>
Browse files
</c-button>
<div v-if="pasteImage">
<!-- separator -->
<div my-4 w-full flex items-center justify-center op-70>
<div class="h-1px max-w-100px flex-1 bg-gray-300 op-50" />
<div class="mx-2 text-gray-400">
or
</div>
<div class="h-1px max-w-100px flex-1 bg-gray-300 op-50" />
</div>
<p>Paste an image from clipboard</p>
</div>
</slot>
<div mt-2>
<img ref="imgPreview" width="150">
</div>
</div>
</template>
+8
View File
@@ -113,4 +113,12 @@ export default defineConfig({
build: {
target: 'esnext',
},
optimizeDeps: {
include: ['pdfjs-dist'], // optionally specify dependency name
esbuildOptions: {
supported: {
'top-level-await': true,
},
},
},
});