如何将SVG格式的数字矢量图形转换为对应数字?
识别SVG矢量数字的两种可行方案
方案一:SVG转位图后用OCR识别
Tesseract确实没法直接解析SVG,但把SVG转成普通的位图(PNG/JPG)后,就能用Tesseract这类OCR工具正常识别了。
浏览器端实现示例
利用Canvas把SVG渲染成位图,再用Tesseract.js识别:
// 假设你的SVG代码存在svgString变量中 async function recognizeSvgNumber(svgString) { // 创建SVG Blob对象 const svgBlob = new Blob([svgString], {type: 'image/svg+xml'}); const svgUrl = URL.createObjectURL(svgBlob); // 创建img元素加载SVG const img = new Image(); await new Promise(resolve => { img.onload = resolve; img.src = svgUrl; }); // 绘制到Canvas const canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); // 用Tesseract识别,限定只识别数字提高准确率 const { data: { text } } = await Tesseract.recognize( canvas, 'eng', { tessedit_char_whitelist: '0123456789' } ); URL.revokeObjectURL(svgUrl); return text.trim(); } // 测试你提供的SVG const yourSvg = `<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1066.6667 800" height="800" width="1066.6667" xml:space="preserve" id="svg2" version="1.1"><metadata id="metadata8"><rdf:RDF><cc:Work rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /></cc:Work></rdf:RDF></metadata><defs id="defs6" /><g transform="matrix(1.3333333,0,0,-1.3333333,0,800)" id="g10"><path id="path20" style="fill:none;stroke:#000000;stroke-width:0.074;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" d="m 294.09,426.05 -0.46,-1.38 -0.92,-0.92 -1.38,-0.46 h -0.46 l -1.37,0.46 -0.92,0.92 -0.46,1.38 v 0.46 l 0.46,1.37 0.92,0.92 1.37,0.46 h 0.46 l 1.38,-0.46 0.92,-0.92 0.46,-1.83 v -2.3 l -0.46,-2.29 -0.92,-1.38 -1.38,-0.46 h -0.91 l -1.38,0.46 -0.46,0.92" /></g></svg>`; recognizeSvgNumber(yourSvg).then(result => { console.log(result); // 输出应为"9" });
注:使用前需引入Tesseract.js的CDN或npm包。
方案二:直接解析SVG路径特征
如果你的SVG数字样式固定(比如统一手写体、线条粗细一致),可以直接解析<path>的d属性,提取路径特征(节点数量、方向变化、闭合段等),和预设的数字特征库匹配。
针对你提供的数字9的简单示例
function parsePathCommands(d) { // 拆分路径命令,提取移动方向 const commands = d.split(/(?=[mlhvcsqtaMLHVCSQTA])/); const movements = []; for (const cmd of commands) { const type = cmd[0]; const coords = cmd.slice(1).trim().split(/\s+/).map(Number); if (type === 'm' || type === 'l') { // 记录相对移动的坐标变化 for (let i = 0; i < coords.length; i += 2) { const dx = coords[i]; const dy = coords[i+1]; movements.push({ dx, dy }); } } } return movements; } function recognizeByPath(movements) { // 统计各方向的移动次数,匹配数字9的特征 let downCount = 0; let upCount = 0; let leftCount = 0; let rightCount = 0; for (const m of movements) { if (m.dy > 0) downCount++; if (m.dy < 0) upCount++; if (m.dx < 0) leftCount++; if (m.dx > 0) rightCount++; } // 根据你的SVG中9的路径特征设置判断条件 if (downCount > 3 && upCount > 2 && leftCount > 4 && rightCount > 2) { return '9'; } return 'unknown'; } // 测试 const pathD = document.querySelector('#path20').getAttribute('d'); const movements = parsePathCommands(pathD); console.log(recognizeByPath(movements)); // 输出"9"
这个方法无需依赖OCR库,速度快,但通用性差,仅适用于固定样式的数字。
内容的提问来源于stack exchange,提问作者Buzz Su
相关产品推荐
相关产品推荐

