使用 Tesseract.js(OCR)从图像中提取文字,支持多语言(包括中文、英文)识别、区域识别、白名单字符等...
Extract text content from images based on Tesseract.js (the WebAssembly port of the Tesseract OCR engine).
Use when users need "image to text," "OCR recognition," "extract text from images," "screenshot character recognition," "scan to text," or "image text orientation detection."
['eng', 'chi_sim'])--rectangle), character whitelists ( --whitelist)--detect)--psm) and OCR engine modes ( --oem)text (default), hocr, blocks (JSON), tsvshell
node -v && npm ls tesseract.js 2>/dev/null || echo "tesseract.js not installed"
If not installed:
shell
cd /root/.openclaw/workspace/skills/tesseract-ocr && npm init -y > /dev/null 2>&1 && npm install tesseract.js
Basic command:
shell
node scripts/ocr.js <image-path-or-url> [--options]
Parameter Descriptions:
ParameterTypeDefaultDescription<image>Required—Local path or HTTPS URL--langstringengLanguage code(s), multiple joined with +, e.g., eng+chi_sim``--psmnumber—Page segmentation mode (see PSM table below)--oemnumber—OCR engine mode (see OEM table below)--whiteliststring—Character whitelist, e.g., 0123456789 to recognize only digits--rectanglestring—Recognition region, format top,left,width,height``--outputstringtextOutput format: text / hocr / blocks / tsv``--detectflag—Detect text orientation and script (does not perform OCR)--dpinumber—Manually specify image DPI
Common Examples:
shell
# Basic mixed Chinese-English recognition
node scripts/ocr.js photo.jpg --lang chi_sim+eng
# Recognize digits only (license plates, CAPTCHAs, etc.)
node scripts/ocr.js captcha.png --whitelist 0123456789
# Column-based recognition (suitable for vertical Chinese text)
node scripts/ocr.js scroll.jpg --lang chi_sim --psm 4
# Specify a region for recognition
node scripts/ocr.js receipt.png --rectangle 50,100,400,200
# Detect image text orientation
node scripts/ocr.js rotated.jpg --detect
# Output structured data
node scripts/ocr.js doc.png --output blocks
# Manually specify DPI (avoids "Invalid resolution 0 dpi" warning)
node scripts/ocr.js scan.png --dpi 300
To reuse a Worker, the AI should write an inline script:
javascript
const { createWorker } = require('tesseract.js');
(async () => {
const worker = await createWorker('eng');
for (const img of ['a.png', 'b.png', 'c.png']) {
const { data: { text } } = await worker.recognize(img);
console.log(img, '→', text);
}
await worker.terminate();
})();
The --psm parameter controls how Tesseract analyzes page layout:
PSMNameDescription0OSD_ONLYOrientation and script detection only1AUTO_OSDAutomatic page segmentation + orientation detection2AUTO_ONLYAutomatic page segmentation, no orientation detection3AUTOFully automatic page segmentation (default)4SINGLE_COLUMNSingle column of variable size text5SINGLE_BLOCK_VERT_TEXTSingle block of vertical text6SINGLE_BLOCKSingle block of text7SINGLE_LINESingle line of text8SINGLE_WORDSingle word9CIRCLE_WORDSingle word in a circular arrangement10SINGLE_CHARSingle character11SPARSE_TEXTSparse text (find as much as possible)12SPARSE_TEXT_OSDSparse text + orientation detection13RAW_LINERaw line (treated as a single line)
OEMDescription0Legacy engine1LSTM neural network engine (default)2Legacy + LSTM3Default (automatically selected based on current configuration)
LanguageCodeEnglishengSimplified Chinesechi_simTraditional Chinesechi_traJapanesejpnKoreankorFrenchfraGermandeuSpanishspaRussianrusArabicaraHindihin
Full list: tesseract_lang_list.md
The following scenarios require the AI to write inline scripts directly rather than using scripts/ocr.js:
worker.reinitialize(langs, oem)worker.setParameters({ tessedit_pageseg_mode: ... })worker.detect(image) after createWorker('eng', 0, { legacyCore: true, legacyLang: true })createScheduler() + multiple WorkersFor complete API reference, see references/api.md.