-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
780 additions
and
755 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,80 @@ | ||
let ocrWorker: Worker; | ||
// let ocrWorker: Worker; | ||
|
||
// @ts-ignore | ||
import OCRWorker from './ocr-worker?worker&inline'; | ||
// // @ts-ignore | ||
// import OCRWorker from './ocr-worker?worker&inline'; | ||
|
||
export async function loadOCR(model: string): Promise<void> { | ||
return new Promise(async (resolve) => { | ||
if (!ocrWorker) { | ||
ocrWorker = new OCRWorker(); | ||
} | ||
// export async function loadOCR(model: string): Promise<void> { | ||
// return new Promise(async (resolve) => { | ||
// if (!ocrWorker) { | ||
// ocrWorker = new OCRWorker(); | ||
// } | ||
|
||
ocrWorker.onmessage = async (e) => { | ||
if (e.data.type === "loaded") { | ||
resolve(); | ||
} | ||
} | ||
// ocrWorker.onmessage = async (e) => { | ||
// if (e.data.type === "loaded") { | ||
// resolve(); | ||
// } | ||
// } | ||
|
||
// ocrWorker.postMessage({ | ||
// type: "load", | ||
// model | ||
// }); | ||
// }); | ||
// } | ||
|
||
// export function doLocalOCR(blob: Blob) { | ||
// return new Promise((resolve, reject) => { | ||
// try { | ||
// ocrWorker.onmessage = async (e) => { | ||
// if (e.data.type === "ocr") { | ||
// resolve(e.data.text); | ||
// } | ||
// else if (e.data.type === "error") { | ||
// reject(e.data.error); | ||
// } | ||
// } | ||
|
||
// const dataURL = URL.createObjectURL(blob); | ||
|
||
ocrWorker.postMessage({ | ||
type: "load", | ||
model | ||
}); | ||
// ocrWorker.postMessage({ | ||
// type: "ocr", | ||
// blob: dataURL | ||
// }); | ||
// } | ||
// catch (err) { | ||
// reject(err); | ||
// } | ||
// }); | ||
// } | ||
|
||
/* eslint-disable no-async-promise-executor */ | ||
import { pipeline, env } from '@huggingface/transformers'; | ||
|
||
let ocr: any = undefined; | ||
|
||
export async function runOCR(image: Blob, model: string = "Xenova/trocr-small-printed") { | ||
return new Promise(async (resolve) => { | ||
if (!ocr) { | ||
await loadOCR(model); | ||
} | ||
const out = await ocr(image); | ||
resolve(out); | ||
}); | ||
} | ||
|
||
export function doLocalOCR(blob: Blob) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
ocrWorker.onmessage = async (e) => { | ||
if (e.data.type === "ocr") { | ||
resolve(e.data.text); | ||
} | ||
else if (e.data.type === "error") { | ||
reject(e.data.error); | ||
} | ||
} | ||
|
||
const dataURL = URL.createObjectURL(blob); | ||
|
||
ocrWorker.postMessage({ | ||
type: "ocr", | ||
blob: dataURL | ||
async function loadOCR(model: string): Promise<void> { | ||
return new Promise(async (resolve) => { | ||
if (!ocr) { | ||
env.allowLocalModels = false; | ||
env.useBrowserCache = false; | ||
ocr = await pipeline('image-to-text', model || 'Xenova/trocr-small-printed', { | ||
device: (navigator as any).ml ? "webnn" : "webgpu" | ||
}); | ||
console.log("loaded ocr", ocr) | ||
resolve(); | ||
} | ||
catch (err) { | ||
reject(err); | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.