lfp


Logs | Files | README | README | LICENSE | GitLab


1
commit edf05bc1db0051f282511856fbe0ff2e1b0aeae5
2
Author: Connor Etherington <[email protected]>
3
Date:   Mon Oct 2 08:03:36 2023 +0200
4
5
    Auto-Commit Update 02.10.2023 - 08:03:36
6
---
7
 PKGBUILD                       | 10 ++++++++
8
 usr/share/lfp/lfprc            |  9 ++++---
9
 usr/share/lfp/ocr/ocr.js       | 56 ++++++++++++++++++++++++++++++++++++++++++
10
 usr/share/lfp/ocr/package.json | 19 ++++++++++++++
11
 4 files changed, 91 insertions(+), 3 deletions(-)
12
13
diff --git a/PKGBUILD b/PKGBUILD
14
index 064e02e..00a3d74 100644
15
--- a/PKGBUILD
16
+++ b/PKGBUILD
17
@@ -45,4 +45,14 @@ package() {
18
   install -Dm644 usr/share/doc/${pkgname}/README.md "${pkgdir}/usr/share/doc/${pkgname}/README.md"
19
   install -Dm644 usr/share/man/man1/${pkgname}.1.gz "${pkgdir}/usr/share/man/man1/${pkgname}.1.gz"
20
 
21
+  which nodejs 2>/dev/null && {
22
+    install -Dm755 usr/share/${pkgname}/ocr/ocr.js -t "${pkgdir}/usr/share/${pkgname}/ocr"
23
+    install -Dm644 usr/share/${pkgname}/ocr/package.json -t "${pkgdir}/usr/share/${pkgname}/ocr"
24
+    which yarn 2>/dev/null && {
25
+      cd "${pkgdir}/usr/share/${pkgname}/ocr" && yarn install
26
+    } || {
27
+      cd "${pkgdir}/usr/share/${pkgname}/ocr" && npm install
28
+    } || true
29
+  }
30
+
31
 }
32
diff --git a/usr/share/lfp/lfprc b/usr/share/lfp/lfprc
33
index eac1173..436ce4a 100644
34
--- a/usr/share/lfp/lfprc
35
+++ b/usr/share/lfp/lfprc
36
@@ -476,10 +476,10 @@ cmd ext ${{
37
     [ -f ${ARG} ] && case ${ARG} in
38
       *.bz2)       bunzip2 ${ARG} && SUCC || FAIL ;;
39
       *.rar)       unrar x ${ARG} && SUCC || FAIL ;;
40
-      *.gz)        gunzip  ${ARG} && SUCC || FAIL ;;
41
       *.tar)       tar xpf ${ARG} && SUCC || FAIL ;;
42
       *.tar.*)     tar xpf ${ARG} && SUCC || FAIL ;;
43
       *.tbz2)      tar xjf ${ARG} && SUCC || FAIL ;;
44
+      *.gz)        gunzip  ${ARG} && SUCC || FAIL ;;
45
       *.tgz)       tar xzf ${ARG} && SUCC || FAIL ;;
46
       *.zip)       unzip ${ARG} && SUCC || FAIL ;;
47
       *.Z)         uncompress ${ARG} && SUCC || FAIL ;;
48
@@ -498,6 +498,10 @@ cmd newTernPwd ${{
49
 cmd setbg "$1"
50
 cmd bulkrename $vidir
51
 
52
+cmd ocr ${{
53
+  node /usr/share/lfp/ocr/ocr.js $fx
54
+  notify-send "🔧 OCR Complete" "Text copied to clipboard."
55
+}}
56
 
57
 
58
 
59
@@ -518,8 +522,7 @@ map <delete> delete
60
 map <c-r> reload
61
 map <backspace2> set hidden!
62
 map <enter> shell
63
-map o &mimeopen $f
64
-map O $mimeopen --ask $f
65
+map o ocr
66
 
67
 
68
 # LFP-Custom Functions:
69
diff --git a/usr/share/lfp/ocr/ocr.js b/usr/share/lfp/ocr/ocr.js
70
new file mode 100755
71
index 0000000..db2b655
72
--- /dev/null
73
+++ b/usr/share/lfp/ocr/ocr.js
74
@@ -0,0 +1,56 @@
75
+#!/usr/bin/env node
76
+
77
+const fs = require('fs');
78
+const fsp = require('fs').promises;
79
+const path = require('path');
80
+const tesseract = require('node-tesseract-ocr');
81
+const { spawn } = require('child_process');
82
+
83
+const config = { lang: 'eng', oem: 1, psm: 3 };
84
+
85
+const ocr = async (imagePath) => {
86
+  const image = await fsp.readFile(imagePath);
87
+  return tesseract.recognize(image, config);
88
+}
89
+
90
+const args = process.argv.slice(2);
91
+const writeFile = (file, data) => fs.writeFile(file, data, (err) => { if (err) console.error(err); });
92
+const timestamp = () => new Date().toLocaleDateString('en-GB', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' });
93
+const extractsDir = '/home/nvx1/.local/share/logs/ocrListener/extracts';
94
+
95
+
96
+(async () => {
97
+  const texts = await Promise.all(args.map(async arg => {
98
+    const imagePath = path.resolve(process.cwd(), arg);
99
+    try {
100
+      return await ocr(imagePath);
101
+    } catch (err) {
102
+      console.error(err);
103
+      return '';
104
+    }
105
+  }));
106
+
107
+  const combinedText = texts.join('\n');
108
+
109
+  const echo = spawn('echo', [combinedText]);
110
+  const xclip = spawn('xclip', ['-selection', 'clipboard']);
111
+
112
+  new Promise((resolve, reject) => {
113
+    if (!fs.existsSync(extractsDir)) {
114
+      fs.mkdirSync(extractsDir);
115
+    }
116
+    resolve(writeFile(path.join(extractsDir, `${timestamp().replace(/:/g, '-').replace(/ /g, '_')}.txt`), combinedText));
117
+  }).then(() => {
118
+
119
+    console.log('Extract saved')
120
+
121
+    echo.stdout.pipe(xclip.stdin);
122
+
123
+    echo.on('close', () => {
124
+      console.log(combinedText);
125
+      process.exit(0);
126
+    });
127
+  })
128
+
129
+})();
130
+
131
diff --git a/usr/share/lfp/ocr/package.json b/usr/share/lfp/ocr/package.json
132
new file mode 100644
133
index 0000000..9ce2737
134
--- /dev/null
135
+++ b/usr/share/lfp/ocr/package.json
136
@@ -0,0 +1,19 @@
137
+{
138
+  "name": "lfp-ocr",
139
+  "version": "0.1.0",
140
+  "description": "",
141
+  "main": "ocr.js",
142
+  "scripts": {
143
+    "start": "node ocr.js"
144
+  },
145
+  "bin": {
146
+    "lfp-ocr": "./ocr.js"
147
+  },
148
+  "keywords": [
149
+    "ocr",
150
+    "lfp",
151
+    "lfpreviewer"
152
+  ],
153
+  "author": "Connor Etherington <[email protected]",
154
+  "license": "MIT"
155
+}