技术博客
印刷体OCR识别:用 need_all_region 获取每行文字坐标与置信度

印刷体OCR识别:用 need_all_region 获取每行文字坐标与置信度

作者: 万维易源
2026-09-01
印刷体OCRneed_all_region坐标置信度
# 印刷体OCR识别:用 need_all_region 获取每行文字坐标与置信度 > 接口 926(接入点 926-1) · 免费 · POST/GET · JSON · 适用人群:需要文字定位/画框的开发者 · 阅读时间:约 6 分钟 ## 核心要点 - 传 `need_all_region=1` 时,返回 `list` 而不是 `str`;`list` 每项含 `text`、`confidence`、`text_region`。 - `text_region` 是 4 个角点 `[x,y]`(左上、右上、右下、左下),基于原图像素坐标。 - `confidence` 为 0~1 置信度,可用于低置信度行的「人工复核」标记。 ## Why:什么时候需要坐标 只想拿到文字,`str` 就够了。但当你要**在图片上把每行文字框出来**、做**校对界面**、或**按区域提取**(如只取表格某一列),就需要每行的坐标。`need_all_region=1` 就是为这个场景准备的。 ## What:list 字段结构 | 字段 | 类型 | 说明 | |------|------|------| | `text` | string | 该行识别文字 | | `confidence` | number | 置信度(0~1) | | `text_region` | number[][] | 4 个角点 `[x,y]`,顺序:左上、右上、右下、左下 | > 不传 `need_all_region`(默认)时返回 `str`(整段),没有坐标与置信度。 ## How:请求并解析 **Python** ```python import requests url = "https://route.showapi.com/926-1" params = {"appKey": "YOUR_APPKEY"} data = {"img_url": "http://showapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com/huangye/img_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png", "need_all_region": "1"} r = requests.post(url, params=params, data=data, timeout=10) rb = r.json()["showapi_res_body"] for it in rb.get("list", []): print(it["text"], round(it["confidence"], 3), it["text_region"]) ``` **cURL** ```bash curl -X POST "https://route.showapi.com/926-1?appKey=YOUR_APPKEY" \ -H "content-type: application/x-www-form-urlencoded" \ -d "img_url=http%3A%2F%2Fshowapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com%2Fhuangye%2Fimg_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png&need_all_region=1" ``` **Node.js(fetch)** ```javascript const res = await fetch("https://route.showapi.com/926-1?appKey=YOUR_APPKEY", { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ img_url: "http://showapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com/huangye/img_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png", need_all_region: "1", }), }); const rb = (await res.json()).showapi_res_body; (rb.list || []).forEach((it) => console.log(it.text, it.confidence, it.text_region)); ``` ## 返回示例与解析 ```json { "showapi_res_body": { "ret_code": 0, "remark": "", "list": [ { "text": "新接口上线一ip查询专业版", "confidence": 0.99603927135468, "text_region": [[373, 40], [683, 40], [683, 65], [373, 65]] } ] } } ``` 画框时取 `text_region` 的最小/最大 x、y 即可得外接矩形:`x0=min(x), y0=min(y), x1=max(x), y1=max(y)`。 ## 前端画框 Demo(HTML + Canvas) 把原图与识别结果叠加,逐行画矩形框并标注置信度: ```html <canvas id="cv"></canvas> <img id="src" src="你的图片URL" style="display:none" onload="draw()" /> <script> async function draw() { const img = document.getElementById("src"); const cv = document.getElementById("cv"); cv.width = img.naturalWidth; cv.height = img.naturalHeight; const ctx = cv.getContext("2d"); ctx.drawImage(img, 0, 0); // 假设 result 是调用接口后得到的 showapi_res_body.list const result = []; // [{text, confidence, text_region:[...]}] ctx.strokeStyle = "red"; ctx.lineWidth = 2; ctx.font = "14px sans-serif"; for (const it of result) { const xs = it.text_region.map(p => p[0]), ys = it.text_region.map(p => p[1]); const x = Math.min(...xs), y = Math.min(...ys); const w = Math.max(...xs) - x, h = Math.max(...ys) - y; ctx.strokeRect(x, y, w, h); ctx.fillStyle = "red"; ctx.fillText(it.text + " " + it.confidence.toFixed(2), x, y - 4); } } </script> ``` ## 进阶 / 边界 - **坐标基于原图像素**:若前端展示图被缩放,画框前需按「展示尺寸 / 原图尺寸」比例换算,否则框会错位。 - **低置信度处理**:`confidence < 0.8`(阈值自行定)的行可标黄、提示人工复核,不要直接当成确定结果。 - 坐标不附带「语义区块」(如「这是标题/这是表格」),如需版面分析需业务侧自行合并相邻行框。 ## FAQ **Q1:不传 need_all_region 能拿到坐标吗?** A1:不能。默认返回 `str`(整段字符串),没有坐标和置信度;必须传 `need_all_region=1` 才有 `list`。 **Q2:text_region 四个点的顺序是固定的吗?** A2:示例顺序为左上、右上、右下、左下;建议代码中用 min/max 容错计算外接框,不依赖固定顺序假设。 **Q3:confidence 能当准确率用吗?** A3:不能。它只是模型对该行识别的置信度估计,不是人工核验的准确率;关键场景仍需人审。 **Q4:坐标支持旋转框吗?** A4:文档给出的是水平四角点矩形,未提供旋转/倾斜框;倾斜文字请以 `text_region` 四点拟合处理。 ## 相关能力 / 下一步阅读 - [印刷体OCR识别返回字段全解:ret_code 与识别结果一文读懂](https://www.showapi.com/guides/printed-ocr-response-codes-926) - [浏览器端实战:HTML+JS 调用印刷体OCR识别(含本地图片预览)](https://www.showapi.com/guides/printed-ocr-web-demo-926) - [客服/审核后台赋能:一键识别用户上传图片中的文字](https://www.showapi.com/guides/printed-ocr-cs-empower-926) - **本系列共 12 篇**:查看[印刷体OCR识别指南总目录](https://www.showapi.com/guides/printed-ocr-guides-926)