File size: 2,447 Bytes
1b756c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ClassicPreset } from 'rete';
import { BaseWorkflowNode } from './base-node';
import { pdfSocket } from '../sockets';
import type { SocketData } from '../types';
import { requirePdfInput, processBatch } from '../types';
import { performOcr } from '../../utils/ocr';

export class OCRNode extends BaseWorkflowNode {
  readonly category = 'Organize & Manage' as const;
  readonly icon = 'ph-barcode';
  readonly description = 'Add searchable text layer via OCR';

  constructor() {
    super('OCR');
    this.addInput('pdf', new ClassicPreset.Input(pdfSocket, 'PDF'));
    this.addOutput(
      'pdf',
      new ClassicPreset.Output(pdfSocket, 'Searchable PDF')
    );
    this.addControl(
      'language',
      new ClassicPreset.InputControl('text', { initial: 'eng' })
    );
    this.addControl(
      'resolution',
      new ClassicPreset.InputControl('text', { initial: '3.0' })
    );
    this.addControl(
      'binarize',
      new ClassicPreset.InputControl('text', { initial: 'false' })
    );
    this.addControl(
      'whitelist',
      new ClassicPreset.InputControl('text', { initial: '' })
    );
  }

  async data(
    inputs: Record<string, SocketData[]>
  ): Promise<Record<string, SocketData>> {
    const pdfInputs = requirePdfInput(inputs, 'OCR');

    const langCtrl = this.controls['language'] as
      | ClassicPreset.InputControl<'text'>
      | undefined;
    const language = langCtrl?.value || 'eng';

    const resCtrl = this.controls['resolution'] as
      | ClassicPreset.InputControl<'text'>
      | undefined;
    const resolution = Math.max(
      1.0,
      Math.min(4.0, parseFloat(resCtrl?.value ?? '3.0'))
    );

    const binarizeCtrl = this.controls['binarize'] as
      | ClassicPreset.InputControl<'text'>
      | undefined;
    const binarize = (binarizeCtrl?.value ?? 'false') === 'true';

    const whitelistCtrl = this.controls['whitelist'] as
      | ClassicPreset.InputControl<'text'>
      | undefined;
    const whitelist = whitelistCtrl?.value || '';

    return {
      pdf: await processBatch(pdfInputs, async (input) => {
        const result = await performOcr(input.bytes, {
          language,
          resolution,
          binarize,
          whitelist,
        });

        return {
          type: 'pdf',
          document: result.pdfDoc,
          bytes: result.pdfBytes,
          filename: input.filename.replace(/\.pdf$/i, '_ocr.pdf'),
        };
      }),
    };
  }
}