@areb0s/ocr-common 1.0.3 → 1.0.4

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/build/Ocr.d.ts CHANGED
@@ -7,6 +7,10 @@ export declare class Ocr {
7
7
  detection: Detection;
8
8
  recognition: Recognition;
9
9
  });
10
+ /**
11
+ * Detect text from image
12
+ * Note: Calls are automatically serialized to prevent "Session already started" error
13
+ */
10
14
  detect(image: string | ImageRawData | BrowserImageInput, options?: {}): Promise<{
11
15
  texts: import("./types/index.js").Line[];
12
16
  resizedImageWidth: any;
package/build/Ocr.js CHANGED
@@ -7,11 +7,34 @@ export class Ocr {
7
7
  }
8
8
  #detection;
9
9
  #recognition;
10
+ #mutex = Promise.resolve();
10
11
  constructor({ detection, recognition, }) {
11
12
  this.#detection = detection;
12
13
  this.#recognition = recognition;
13
14
  }
15
+ /**
16
+ * Detect text from image
17
+ * Note: Calls are automatically serialized to prevent "Session already started" error
18
+ */
14
19
  async detect(image, options = {}) {
20
+ // Queue this call behind any pending operations
21
+ const currentMutex = this.#mutex;
22
+ let releaseMutex;
23
+ this.#mutex = new Promise((resolve) => {
24
+ releaseMutex = resolve;
25
+ });
26
+ try {
27
+ // Wait for previous operation to complete
28
+ await currentMutex;
29
+ // Execute detection
30
+ return await this.#detectInternal(image, options);
31
+ }
32
+ finally {
33
+ // Release mutex for next operation
34
+ releaseMutex();
35
+ }
36
+ }
37
+ async #detectInternal(image, options = {}) {
15
38
  const { lineImages, resizedImageWidth, resizedImageHeight } = await this.#detection.run(image, options);
16
39
  const texts = await this.#recognition.run(lineImages, options);
17
40
  return {
package/build/Ocr.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Ocr.js","sourceRoot":"","sources":["../src/Ocr.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAEjD,MAAM,OAAO,GAAG;IACd,MAAM,CAAC,KAAK,CAAC,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C,CAAC;IAED,UAAU,CAAW;IACrB,YAAY,CAAa;IACzB,MAAM,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAA;IAEzC,YAAY,EACV,SAAS,EACT,WAAW,GAIZ;QACC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAA;QAC3B,IAAI,CAAC,YAAY,GAAG,WAAW,CAAA;IACjC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,KAAgD,EAAE,OAAO,GAAG,EAAE;QACzE,gDAAgD;QAChD,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAA;QAChC,IAAI,YAAwB,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACpC,YAAY,GAAG,OAAO,CAAA;QACxB,CAAC,CAAC,CAAA;QAEF,IAAI,CAAC;YACH,0CAA0C;YAC1C,MAAM,YAAY,CAAA;YAClB,oBAAoB;YACpB,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACnD,CAAC;gBAAS,CAAC;YACT,mCAAmC;YACnC,YAAa,EAAE,CAAA;QACjB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,KAAgD,EAAE,OAAO,GAAG,EAAE;QAClF,MAAM,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACvG,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;QAC9D,OAAO;YACL,KAAK;YACL,iBAAiB;YACjB,kBAAkB;SACnB,CAAA;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@areb0s/ocr-common",
3
3
  "description": "Fork of @gutenye/ocr-common with ImageBitmap support. Shared OCR library for Browser/Node based on PaddleOCR and ONNX runtime.",
4
- "version": "1.0.3",
4
+ "version": "1.0.4",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {
package/src/Ocr.ts CHANGED
@@ -1,34 +1,58 @@
1
- import type { ImageRawData, BrowserImageInput, ModelCreateOptions } from '#common/types'
2
- import { Detection, Recognition } from './models'
3
-
4
- export class Ocr {
5
- static async create(options: ModelCreateOptions = {}) {
6
- const detection = await Detection.create(options)
7
- const recognition = await Recognition.create(options)
8
- return new Ocr({ detection, recognition })
9
- }
10
-
11
- #detection: Detection
12
- #recognition: Recognition
13
-
14
- constructor({
15
- detection,
16
- recognition,
17
- }: {
18
- detection: Detection
19
- recognition: Recognition
20
- }) {
21
- this.#detection = detection
22
- this.#recognition = recognition
23
- }
24
-
25
- async detect(image: string | ImageRawData | BrowserImageInput, options = {}) {
26
- const { lineImages, resizedImageWidth, resizedImageHeight } = await this.#detection.run(image, options)
27
- const texts = await this.#recognition.run(lineImages, options)
28
- return {
29
- texts,
30
- resizedImageWidth,
31
- resizedImageHeight,
32
- }
33
- }
34
- }
1
+ import type { ImageRawData, BrowserImageInput, ModelCreateOptions } from '#common/types'
2
+ import { Detection, Recognition } from './models'
3
+
4
+ export class Ocr {
5
+ static async create(options: ModelCreateOptions = {}) {
6
+ const detection = await Detection.create(options)
7
+ const recognition = await Recognition.create(options)
8
+ return new Ocr({ detection, recognition })
9
+ }
10
+
11
+ #detection: Detection
12
+ #recognition: Recognition
13
+ #mutex: Promise<void> = Promise.resolve()
14
+
15
+ constructor({
16
+ detection,
17
+ recognition,
18
+ }: {
19
+ detection: Detection
20
+ recognition: Recognition
21
+ }) {
22
+ this.#detection = detection
23
+ this.#recognition = recognition
24
+ }
25
+
26
+ /**
27
+ * Detect text from image
28
+ * Note: Calls are automatically serialized to prevent "Session already started" error
29
+ */
30
+ async detect(image: string | ImageRawData | BrowserImageInput, options = {}) {
31
+ // Queue this call behind any pending operations
32
+ const currentMutex = this.#mutex
33
+ let releaseMutex: () => void
34
+ this.#mutex = new Promise((resolve) => {
35
+ releaseMutex = resolve
36
+ })
37
+
38
+ try {
39
+ // Wait for previous operation to complete
40
+ await currentMutex
41
+ // Execute detection
42
+ return await this.#detectInternal(image, options)
43
+ } finally {
44
+ // Release mutex for next operation
45
+ releaseMutex!()
46
+ }
47
+ }
48
+
49
+ async #detectInternal(image: string | ImageRawData | BrowserImageInput, options = {}) {
50
+ const { lineImages, resizedImageWidth, resizedImageHeight } = await this.#detection.run(image, options)
51
+ const texts = await this.#recognition.run(lineImages, options)
52
+ return {
53
+ texts,
54
+ resizedImageWidth,
55
+ resizedImageHeight,
56
+ }
57
+ }
58
+ }