@tdh-keyboard/recognizer
v1.0.10
Published
中文手写汉字识别器组件
Readme
@tdh-keyboard/recognizer | 中文手写识别模块
@tdh-keyboard/recognizer 是中文虚拟键盘组件库的手写识别模块,提供可直接注册到键盘组件中的 TdhRecognizer 实现。
功能特点
- ✏️ 实时手写汉字识别
- 🔧 可自定义识别模型和字典
- 🚀 支持 CPU 模式,可按需启用 WebGL 加速
- 📚 内置支持 GB2312 一级汉字识别模型接入
安装
npm install @tdh-keyboard/recognizer导出内容
TdhRecognizer:默认手写识别器实现RecognizerOptions:构造参数类型
基本用法
import { TdhRecognizer } from '@tdh-keyboard/recognizer'
import '@tensorflow/tfjs-backend-webgl'
const recognizer = new TdhRecognizer({
modelPath: '/models/handwrite/model.json',
dictPath: '/models/dict.txt',
backend: 'webgl',
})
await recognizer.initialize()
const strokeData = [10, 10, 0, 20, 20, 1]
const results = await recognizer.recognize(strokeData)
console.log(results)
await recognizer.close()参数
RecognizerOptions 支持以下字段:
modelPath:TensorFlow.js 模型路径dictPath:汉字字典路径,文本文件,每行一个汉字backend:可选,'webgl' | 'cpu',默认'cpu'
初始化
initialize(options?) 支持来自 @tdh-keyboard/core 的初始化参数:
onProgress(progress):模型加载进度回调,范围为0到1
识别输入格式
recognize(strokeData) 需要接收如下格式的笔迹数组:
[x1, y1, c1, x2, y2, c2, ...]其中:
x、y是坐标c表示该点是否是当前笔画最后一点0表示笔画继续1表示笔画结束
与键盘组件配合使用
import { registerHandwritingRecognizer } from '@tdh-keyboard/vue'
import { TdhRecognizer } from '@tdh-keyboard/recognizer'
registerHandwritingRecognizer(new TdhRecognizer({
modelPath: '/models/handwrite/model.json',
dictPath: '/models/dict.txt',
}))性能建议
- 在支持 WebGL 的设备上,可通过
backend: 'webgl'提高识别速度 - 使用 WebGL 后端时,需要手动引入
@tensorflow/tfjs-backend-webgl - 初始化阶段会加载模型和字典,建议在应用启动或键盘首次使用前完成
