utilitas 1995.2.4 → 1995.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.
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/manifest.mjs +1 -1
- package/lib/vision.mjs +15 -21
- package/package.json +1 -1
package/lib/manifest.mjs
CHANGED
package/lib/vision.mjs
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
import { getApiKeyCredentials } from './encryption.mjs';
|
|
2
|
+
import get from './shot.mjs';
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
convert, deleteOnCloud, downloadFromCloud, getIdByGs, uploadToCloud,
|
|
5
6
|
} from './storage.mjs';
|
|
6
7
|
|
|
7
8
|
import {
|
|
8
|
-
ensureArray, ignoreErrFunc, log as _log, need, throwError, trim,
|
|
9
|
+
ensureArray, ignoreErrFunc, log as _log, need, throwError, trim,
|
|
9
10
|
} from './utilitas.mjs';
|
|
10
11
|
|
|
11
12
|
const _NEED = ['@google-cloud/vision', 'tesseract.js'];
|
|
12
13
|
const [BASE64, BUFFER, FILE, DEFAULT_LANG] = ['BASE64', 'BUFFER', 'FILE', 'eng'];
|
|
13
14
|
const ceil = num => num.toFixed(4);
|
|
14
15
|
const errorMessage = 'Invalid image data.';
|
|
16
|
+
const getTextFromBatch = bt => bt.responses.map(p => p.fullTextAnnotation.text);
|
|
17
|
+
const DOCUMENT_TEXT_DETECTION = 'DOCUMENT_TEXT_DETECTION';
|
|
18
|
+
const features = [{ type: DOCUMENT_TEXT_DETECTION }];
|
|
19
|
+
const mimeType = 'application/pdf';
|
|
20
|
+
const pages = [1, 2, 3, 4, 5]; // max 5 pages limit for batchAnnotateFiles API
|
|
15
21
|
const log = content => _log(content, import.meta.url);
|
|
16
22
|
|
|
17
23
|
let client;
|
|
@@ -125,16 +131,10 @@ const read = async (image, options) => {
|
|
|
125
131
|
const content = await convert(image, {
|
|
126
132
|
input: options?.input, expected: BASE64, errorMessage,
|
|
127
133
|
});
|
|
128
|
-
|
|
129
|
-
requests: [{
|
|
130
|
-
inputConfig: { mimeType: 'application/pdf', content },
|
|
131
|
-
features: [{ type: 'DOCUMENT_TEXT_DETECTION' }],
|
|
132
|
-
pages: [1, 2, 3, 4, 5], // max 5 pages
|
|
133
|
-
}],
|
|
134
|
+
let result = await client.batchAnnotateFiles({
|
|
135
|
+
requests: [{ inputConfig: { mimeType, content }, features, pages }],
|
|
134
136
|
});
|
|
135
|
-
|
|
136
|
-
options?.raw || (pages = pages.map(x => x.fullTextAnnotation.text));
|
|
137
|
-
return pages;
|
|
137
|
+
return options?.raw ? result : getTextFromBatch(result[0].responses[0]);
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
const readAll = async (image, options) => {
|
|
@@ -149,9 +149,8 @@ const readAll = async (image, options) => {
|
|
|
149
149
|
result.clear = await deleteOnCloud(resultId);
|
|
150
150
|
result.submit = await client.asyncBatchAnnotateFiles({
|
|
151
151
|
requests: [{
|
|
152
|
-
inputConfig: { mimeType
|
|
153
|
-
outputConfig: { gcsDestination: { uri: destination } },
|
|
154
|
-
features: [{ type: 'DOCUMENT_TEXT_DETECTION' }],
|
|
152
|
+
inputConfig: { mimeType, gcsSource: { uri } },
|
|
153
|
+
outputConfig: { gcsDestination: { uri: destination } }, features,
|
|
155
154
|
}],
|
|
156
155
|
});
|
|
157
156
|
result.response = await result.submit[0].promise();
|
|
@@ -159,14 +158,9 @@ const readAll = async (image, options) => {
|
|
|
159
158
|
options?.keep || (result.cleanup = await Promise.all(
|
|
160
159
|
[getIdByGs(uri), resultId].map(deleteOnCloud)
|
|
161
160
|
));
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
result.result[file].responses.map(x => pages.push(
|
|
166
|
-
x.fullTextAnnotation.text
|
|
167
|
-
));
|
|
168
|
-
}
|
|
169
|
-
return pages;
|
|
161
|
+
return options?.raw ? result : Object.keys(result.result).map(
|
|
162
|
+
f => getTextFromBatch(result.result[f])
|
|
163
|
+
).flat();
|
|
170
164
|
};
|
|
171
165
|
|
|
172
166
|
export {
|