-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathocr-text-extractor.html
More file actions
602 lines (529 loc) · 18.4 KB
/
Copy pathocr-text-extractor.html
File metadata and controls
602 lines (529 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OCR Text Extractor | One-File-Tools</title>
<meta name="description" content="Extract text from images entirely offline using client-side OCR." />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<!-- Tesseract.js for Offline OCR -->
<script src="https://unpkg.com/tesseract.js@5.0.5/dist/tesseract.min.js"></script>
<style>
:root {
--bg: #030712;
--bg-panel: rgba(17, 24, 39, 0.7);
--panel-border: rgba(255, 255, 255, 0.1);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #6b7280;
--accent: #f59e0b; /* Amber */
--accent-hover: #d97706;
--accent-glow: rgba(245, 158, 11, 0.2);
--error: #ef4444;
--success: #10b981;
--input-bg: rgba(0, 0, 0, 0.5);
--font-sans: "Inter", system-ui, sans-serif;
--font-display: "Space Grotesk", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
color: var(--text);
line-height: 1.6;
min-height: 100vh;
display: flex;
flex-direction: column;
background-image: radial-gradient(circle at 15% 50%, rgba(245, 158, 11, 0.05) 0%, transparent 50%), radial-gradient(circle at 85% 30%, rgba(245, 158, 11, 0.05) 0%, transparent 50%);
}
.container {
max-width: 1000px;
margin: 0 auto;
padding: 3rem 1.5rem;
flex: 1;
width: 100%;
display: flex;
flex-direction: column;
gap: 2rem;
}
header {
text-align: center;
margin-bottom: 1rem;
}
h1 {
font-family: var(--font-display);
font-size: 2.5rem;
font-weight: 700;
letter-spacing: -0.02em;
margin-bottom: 0.5rem;
text-shadow: 0 0 40px rgba(255, 255, 255, 0.1);
}
p.subtitle {
color: var(--text-muted);
font-size: 1.1rem;
}
.main-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1.5rem;
flex: 1;
}
@media (max-width: 768px) {
.main-grid {
grid-template-columns: 1fr;
}
}
.panel {
background: var(--bg-panel);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--panel-border);
border-radius: 16px;
padding: 1.5rem;
display: flex;
flex-direction: column;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
position: relative;
}
.panel-title {
font-family: var(--font-display);
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 1rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
/* Drop Zone */
.drop-zone {
flex: 1;
border: 2px dashed var(--panel-border);
border-radius: 12px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
background: var(--input-bg);
min-height: 250px;
position: relative;
overflow: hidden;
}
.drop-zone:hover,
.drop-zone.dragover {
border-color: var(--accent);
background: rgba(245, 158, 11, 0.05);
}
.drop-zone-icon {
color: var(--text-muted);
margin-bottom: 1rem;
transition: color 0.3s ease;
}
.drop-zone:hover .drop-zone-icon,
.drop-zone.dragover .drop-zone-icon {
color: var(--accent);
}
.drop-zone-text {
color: var(--text-muted);
font-weight: 500;
margin-bottom: 0.5rem;
}
.drop-zone-sub {
color: var(--text-dim);
font-size: 0.85rem;
}
.preview-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
display: none;
background: var(--input-bg);
}
input[type="file"] {
display: none;
}
/* Results Area */
.result-area {
flex: 1;
display: flex;
flex-direction: column;
gap: 1rem;
}
textarea {
flex: 1;
width: 100%;
background: var(--input-bg);
border: 1px solid var(--panel-border);
color: var(--text);
padding: 1rem;
border-radius: 12px;
font-family: var(--font-mono);
font-size: 0.95rem;
resize: none;
outline: none;
transition: border-color 0.2s;
min-height: 250px;
}
textarea:focus {
border-color: var(--accent);
}
textarea::placeholder {
color: var(--text-dim);
font-family: var(--font-sans);
}
/* Buttons & Controls */
.actions {
display: flex;
gap: 1rem;
justify-content: flex-end;
}
.btn {
background: rgba(255, 255, 255, 0.05);
border: 1px solid var(--panel-border);
color: var(--text);
padding: 0.75rem 1.25rem;
border-radius: 8px;
font-family: var(--font-sans);
font-size: 0.95rem;
font-weight: 500;
cursor: pointer;
display: flex;
align-items: center;
gap: 0.5rem;
transition: all 0.2s ease;
}
.btn:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.2);
}
.btn-primary {
background: var(--accent);
border-color: var(--accent);
color: #000;
}
.btn-primary:hover {
background: var(--accent-hover);
border-color: var(--accent-hover);
}
.btn-primary:disabled {
background: var(--text-dim);
border-color: var(--text-dim);
cursor: not-allowed;
opacity: 0.7;
}
/* Progress Bar */
.progress-container {
display: none;
flex-direction: column;
gap: 0.5rem;
margin-top: 1.5rem;
}
.progress-container.active {
display: flex;
}
.progress-header {
display: flex;
justify-content: space-between;
font-size: 0.85rem;
color: var(--text-muted);
font-weight: 500;
}
.progress-track {
height: 6px;
background: var(--input-bg);
border-radius: 3px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: var(--accent);
width: 0%;
border-radius: 3px;
transition: width 0.2s ease;
box-shadow: 0 0 10px var(--accent-glow);
}
/* Toasts */
.toast {
position: fixed;
bottom: 2rem;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: var(--text);
color: var(--bg);
padding: 0.75rem 1.5rem;
border-radius: 8px;
font-weight: 500;
opacity: 0;
transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
z-index: 1000;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>OCR Text Extractor</h1>
<p class="subtitle">Extract text from images entirely offline. No data leaves your browser.</p>
</header>
<div class="main-grid">
<!-- Input Panel -->
<div class="panel">
<div class="panel-title">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
<circle cx="8.5" cy="8.5" r="1.5"></circle>
<polyline points="21 15 16 10 5 21"></polyline>
</svg>
Image Input
</div>
<input type="file" id="fileInput" accept="image/png, image/jpeg, image/webp" />
<div class="drop-zone" id="dropZone">
<div class="drop-zone-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
<polyline points="17 8 12 3 7 8"></polyline>
<line x1="12" y1="3" x2="12" y2="15"></line>
</svg>
</div>
<div class="drop-zone-text">Click or drag an image here</div>
<div class="drop-zone-sub">Supports PNG, JPG, WebP</div>
<img id="previewImage" class="preview-image" alt="Image preview" />
</div>
<div class="progress-container" id="progressContainer">
<div class="progress-header">
<span id="progressLabel">Initializing Engine...</span>
<span id="progressPercent">0%</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
</div>
</div>
<!-- Output Panel -->
<div class="panel">
<div class="panel-title">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
Extracted Text
</div>
<div class="result-area">
<textarea id="outputText" placeholder="Extracted text will appear here..."></textarea>
<div class="actions">
<button class="btn" id="btnClear">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="3 6 5 6 21 6"></polyline>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
</svg>
Clear
</button>
<button class="btn btn-primary" id="btnCopy">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
</svg>
Copy Text
</button>
</div>
</div>
</div>
</div>
</div>
<div class="toast" id="toast">Copied to clipboard!</div>
<script>
const dropZone = document.getElementById("dropZone");
const fileInput = document.getElementById("fileInput");
const previewImage = document.getElementById("previewImage");
const outputText = document.getElementById("outputText");
const btnCopy = document.getElementById("btnCopy");
const btnClear = document.getElementById("btnClear");
const progressContainer = document.getElementById("progressContainer");
const progressLabel = document.getElementById("progressLabel");
const progressPercent = document.getElementById("progressPercent");
const progressFill = document.getElementById("progressFill");
let currentFile = null;
// File Input / Drag & Drop Logic
dropZone.addEventListener("click", () => fileInput.click());
dropZone.addEventListener("dragover", (e) => {
e.preventDefault();
dropZone.classList.add("dragover");
});
dropZone.addEventListener("dragleave", () => {
dropZone.classList.remove("dragover");
});
dropZone.addEventListener("drop", (e) => {
e.preventDefault();
dropZone.classList.remove("dragover");
if (e.dataTransfer.files && e.dataTransfer.files[0]) {
handleFile(e.dataTransfer.files[0]);
}
});
fileInput.addEventListener("change", (e) => {
if (e.target.files && e.target.files[0]) {
handleFile(e.target.files[0]);
}
});
function handleFile(file) {
if (!file.type.startsWith("image/")) {
showToast("Please upload a valid image file.");
return;
}
currentFile = file;
const reader = new FileReader();
reader.onload = async (e) => {
previewImage.src = e.target.result;
previewImage.style.display = "block";
const enhancedImageData = await preprocessImage(e.target.result);
processImage(enhancedImageData);
};
reader.readAsDataURL(file);
}
// Pre-process image to drastically improve OCR accuracy
function preprocessImage(dataUrl) {
return new Promise((resolve) => {
const img = new Image();
img.onerror = () => resolve(dataUrl);
img.onload = () => {
const canvas = document.createElement("canvas");
// Upscale small images (Tesseract prefers larger text, ~20-30px high)
let scale = 1;
const minDimension = Math.min(img.width, img.height);
if (minDimension > 0 && minDimension < 1200) {
scale = 1200 / minDimension;
if (scale > 3) scale = 3; // Cap scale to prevent memory crashes
}
canvas.width = img.width * scale;
canvas.height = img.height * scale;
const ctx = canvas.getContext("2d");
// Apply grayscale and a slight contrast bump natively
ctx.filter = "contrast(120%) grayscale(100%)";
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
resolve(canvas.toDataURL("image/jpeg", 0.9));
};
img.src = dataUrl;
});
}
let activeWorker = null;
// OCR Processing with Tesseract.js
async function processImage(imageData) {
progressContainer.classList.add("active");
outputText.value = "";
outputText.placeholder = "Processing image...";
btnCopy.disabled = true;
try {
if (activeWorker) {
try {
await activeWorker.terminate();
} catch (e) {}
}
const worker = await Tesseract.createWorker("eng", 1, {
logger: (m) => {
// m format: { status: 'recognizing text', progress: 0.5 }
let statusText = m.status;
statusText = statusText.charAt(0).toUpperCase() + statusText.slice(1);
progressLabel.textContent = statusText + "...";
if (m.progress) {
const percent = Math.floor(m.progress * 100);
progressPercent.textContent = `${percent}%`;
progressFill.style.width = `${percent}%`;
}
}
});
activeWorker = worker;
// Begin actual recognition
let text = "";
try {
({
data: { text }
} = await worker.recognize(imageData));
} finally {
// Cleanup worker memory even if recognize throws
activeWorker = null;
await worker.terminate();
}
// Show result
outputText.value = text.trim();
outputText.placeholder = "Extracted text will appear here...";
if (text.trim() === "") {
showToast("No text could be found in the image.");
} else {
showToast("Text extracted successfully!");
}
} catch (error) {
console.error(error);
showToast("An error occurred during text extraction.");
outputText.placeholder = "Error extracting text. Please try again with a clearer image.";
} finally {
btnCopy.disabled = !outputText.value;
// Small delay before hiding progress bar
setTimeout(() => {
progressContainer.classList.remove("active");
progressFill.style.width = "0%";
progressPercent.textContent = "0%";
}, 1000);
}
}
// Action Buttons
btnCopy.addEventListener("click", async () => {
if (!outputText.value) return;
try {
await navigator.clipboard.writeText(outputText.value);
showToast("Copied to clipboard!");
} catch (err) {
showToast("Failed to copy text.");
}
});
btnClear.addEventListener("click", async () => {
if (activeWorker) {
try {
await activeWorker.terminate();
} catch (e) {}
activeWorker = null;
}
currentFile = null;
fileInput.value = "";
previewImage.src = "";
previewImage.style.display = "none";
outputText.value = "";
outputText.placeholder = "Extracted text will appear here...";
btnCopy.disabled = true;
progressContainer.classList.remove("active");
progressFill.style.width = "0%";
progressPercent.textContent = "0%";
});
// Toast functionality
let toastTimeout;
function showToast(message) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimeout);
toastTimeout = setTimeout(() => {
toast.classList.remove("show");
}, 3000);
}
</script>
</body>
</html>