中文手写汉字识别器组件
npm install @crispychicken/zh-keyboard-recognizer
bash
使用npm
npm install @crispychicken/zh-keyboard-recognizer
或使用pnpm
pnpm add @crispychicken/zh-keyboard-recognizer
`
基本用法
`typescript
import { ZhkRecognizer } from '@crispychicken/zh-keyboard-recognizer'
// 如果需要使用WebGL后端,需要手动引入
import '@tensorflow/tfjs-backend-webgl'
// 创建识别器实例
const recognizer = new ZhkRecognizer({
modelPath: '/models/handwrite/model.json', // TensorFlow.js模型路径
dictPath: '/models/dict.txt', // 汉字字典路径
backend: 'webgl' // 可选:'webgl'或'cpu',默认为'cpu'
})
// 初始化识别器(加载模型和字典)
await recognizer.initialize()
// 使用识别器识别手写汉字
// strokeData格式:[x1, y1, isEnd1, x2, y2, isEnd2, ...]
// isEnd为0表示笔画继续,为1表示笔画结束
const strokeData = [10, 10, 0, 20, 20, 1,]
const results = await recognizer.recognize(strokeData)
// 识别结果是按置信度排序的汉字数组
console.log('识别结果:', results) // 例如 ['中', '申', '由', ...]
// 使用完毕后释放资源
await recognizer.close()
`
参数
- options: 配置对象
- modelPath: 模型文件路径,指向TensorFlow.js模型的json文件
- dictPath: 字典文件路径,文本文件,每行一个汉字
- backend: 可选,TensorFlow.js后端类型,可选值为'webgl'或'cpu',默认为'cpu'
性能优化
- 在支持WebGL的设备上使用'webgl'后端可以显著提高识别速度
- 注意:使用WebGL后端时,需要手动引入 import '@tensorflow/tfjs-backend-webgl'`