Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add OCR for image descriptions using tesseract.js (#1433)
* feat: add OCR for image descriptions using tesseract.js * tweak style of alt editor * remove unnecessary files
- Loading branch information
1 parent
bd2a7ab
commit ca9a32d
Showing
12 changed files
with
272 additions
and
12 deletions.
There are no files selected for viewing
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 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 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { importTesseractWorker } from '../_utils/asyncModules' | ||
|
||
export async function runTesseract (image) { | ||
const worker = await importTesseractWorker() | ||
|
||
// TODO: have to trick tesseract into not creating a blob URL because that would break our CSP | ||
// see https://github.com/naptha/tesseract.js/pull/322 | ||
let promise | ||
const OldBlob = window.Blob | ||
window.Blob = null | ||
try { | ||
promise = worker.recognize(image) | ||
} finally { | ||
window.Blob = OldBlob | ||
} | ||
promise.progress(_ => console.log('progress', _)) | ||
const res = await promise | ||
return res.text | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import workerPath from 'tesseract.js/dist/worker.min.js' | ||
// TODO: we should use .wasm instead of .wasm.js. But currently can't because: | ||
// 1. not supported https://github.com/naptha/tesseract.js/blob/9f1e782/docs/local-installation.md#corepath | ||
// 2. webpack defaultRules issues (fixable with https://github.com/webpack/webpack/issues/8412#issuecomment-445586591) | ||
|
||
// We should explore this at a later date. | ||
import corePath from 'tesseract.js-core/tesseract-core.wasm.js' | ||
import { TesseractWorker } from 'tesseract.js' | ||
|
||
// tesseract has a bug where broken image URLs will silently fail. We could spawn a new worker | ||
// every time to work around the issue, but then it literally spawns a new web worker for each request, | ||
// which seems excessive. So we just live with the bug for now. | ||
// https://github.com/naptha/tesseract.js/issues/325 | ||
const { origin } = location | ||
const tesseractWorker = new TesseractWorker({ | ||
workerPath: `${origin}/${workerPath}`, | ||
langPath: `${origin}/`, | ||
corePath: `${origin}/${corePath}` | ||
}) | ||
|
||
export default tesseractWorker |
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
Binary file not shown.
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 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.