scribe.js-ocr 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scribe.js +36 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scribe.js-ocr",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "High-quality OCR and text extraction for images and PDFs.",
5
5
  "main": "scribe.js",
6
6
  "directories": {
package/scribe.js CHANGED
@@ -98,10 +98,14 @@ const extractText = async (files, langs = ['eng'], outputFormat = 'txt', options
98
98
  * @public
99
99
  */
100
100
  async function writeDebugImages(ctx, compDebugArrArr, filePath) {
101
- await drawDebugImages({ ctx, compDebugArrArr, context: 'node' });
102
- const buffer0 = ctx.canvas.toBuffer('image/png');
103
- const fs = await import('fs');
104
- fs.writeFileSync(filePath, buffer0);
101
+ if (typeof process === 'undefined') {
102
+ throw new Error('This function is only available in Node.js.');
103
+ } else {
104
+ await drawDebugImages({ ctx, compDebugArrArr, context: 'node' });
105
+ const buffer0 = ctx.canvas.toBuffer('image/png');
106
+ const fs = await import('fs');
107
+ fs.writeFileSync(filePath, buffer0);
108
+ }
105
109
  }
106
110
 
107
111
  /**
@@ -111,39 +115,43 @@ async function writeDebugImages(ctx, compDebugArrArr, filePath) {
111
115
  * @returns
112
116
  */
113
117
  async function dumpDebugImages(dir) {
114
- if (typeof process === 'undefined') throw new Error('This function is only available in Node.js.');
115
-
116
- if (!DebugData.debugImg.Combined || DebugData.debugImg.Combined.length === 0) {
117
- console.log('No debug images to dump.');
118
- return;
119
- }
118
+ if (typeof process === 'undefined') {
119
+ throw new Error('This function is only available in Node.js.');
120
+ } else {
121
+ if (!DebugData.debugImg.Combined || DebugData.debugImg.Combined.length === 0) {
122
+ console.log('No debug images to dump.');
123
+ return;
124
+ }
120
125
 
121
- const { createCanvas } = await import('canvas');
122
- const canvasAlt = createCanvas(200, 200);
123
- const ctxDebug = canvasAlt.getContext('2d');
126
+ const { createCanvas } = await import('canvas');
127
+ const canvasAlt = createCanvas(200, 200);
128
+ const ctxDebug = canvasAlt.getContext('2d');
124
129
 
125
- for (const [name, imgArr] of Object.entries(DebugData.debugImg)) {
126
- if (!imgArr || imgArr.length === 0) continue;
127
- for (let i = 0; i < imgArr.length; i++) {
128
- const filePath = `${dir}/${name}_${i}.png`;
129
- await writeDebugImages(ctxDebug, [imgArr[i]], filePath);
130
+ for (const [name, imgArr] of Object.entries(DebugData.debugImg)) {
131
+ if (!imgArr || imgArr.length === 0) continue;
132
+ for (let i = 0; i < imgArr.length; i++) {
133
+ const filePath = `${dir}/${name}_${i}.png`;
134
+ await writeDebugImages(ctxDebug, [imgArr[i]], filePath);
135
+ }
130
136
  }
131
137
  }
132
138
  }
133
139
 
134
140
  async function dumpHOCR(dir) {
135
- if (typeof process === 'undefined') throw new Error('This function is only available in Node.js.');
136
-
137
- const activeCurrent = ocrAll.active;
141
+ if (typeof process === 'undefined') {
142
+ throw new Error('This function is only available in Node.js.');
143
+ } else {
144
+ const activeCurrent = ocrAll.active;
145
+
146
+ const fs = await import('fs');
147
+ for (const [name, pages] of Object.entries(ocrAll)) {
148
+ ocrAll.active = pages;
149
+ const hocrStr = await exportData('hocr');
150
+ fs.writeFileSync(`${dir}/${name}.hocr`, hocrStr);
151
+ }
138
152
 
139
- const fs = await import('fs');
140
- for (const [name, pages] of Object.entries(ocrAll)) {
141
- ocrAll.active = pages;
142
- const hocrStr = await exportData('hocr');
143
- fs.writeFileSync(`${dir}/${name}.hocr`, hocrStr);
153
+ ocrAll.active = activeCurrent;
144
154
  }
145
-
146
- ocrAll.active = activeCurrent;
147
155
  }
148
156
 
149
157
  class data {