基于豆包视觉大模型,支持多种验证码类型识别
| 依赖 | 版本 |
|---|---|
| Python | >= 3.8 |
| openai | >= 1.0 |
| fastapi | 最新 |
| uvicorn | 最新 |
pip install "openai>=1.0" fastapi uvicorn
python start.py
http://你的服务器IP:2001/docs 可查看 Swagger 交互式文档http://你的服务器IP:2001
| 字段 | 类型 | 说明 |
|---|---|---|
success | boolean | 是否识别成功 |
result | string | 识别结果(成功时返回) |
error | string | 错误信息(失败时返回) |
{
"success": true,
"result": "A3F8",
"error": ""
}
{
"success": false,
"result": "",
"error": "识别失败"
}
通用验证码识别接口,支持图片 URL 和 base64 data URL。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
image | string | 必填 | 图片来源: 图片 URL: https://example.com/captcha.pngbase64 data URL: data:image/png;base64,iVBOR...
|
prompt | string | 可选 | 自定义提示词,描述验证码类型以提高准确率。 不传则使用默认值。 |
# 请求 curl -X POST http://localhost:2001/recognize \ -H "Content-Type: application/json" \ -d '{ "image": "https://example.com/captcha.png", "prompt": "这是一个4位数字字母混合验证码,只返回验证码内容" }' # 响应 { "success": true, "result": "A3F8", "error": "" }
纯 base64 图片识别接口。image 字段可以直接传纯 base64 字符串,无需手动拼接 data:image/png;base64, 前缀。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
image | string | 必填 | 纯 base64 编码的图片字符串(不带 data:image 前缀)也兼容完整 data URL 和 http URL |
prompt | string | 可选 | 自定义提示词,同上 |
# 请求(直接传纯 base64) curl -X POST http://localhost:2001/recognize/base64 \ -H "Content-Type: application/json" \ -d '{ "image": "iVBORw0KGgoAAAANSUhEUgAA...", "prompt": "这是一个6位数字验证码,只返回数字" }' # 响应 { "success": true, "result": "839217", "error": "" }
通过 prompt 字段描述验证码类型,可以显著提高识别准确率:
| 验证码类型 | 推荐 prompt |
|---|---|
| 纯数字验证码 | 这是一个4位纯数字验证码,只返回数字 |
| 数字字母混合 | 这是一个4位数字字母混合验证码,只返回验证码内容 |
| 中文验证码 | 这是一个中文验证码,请识别图中的汉字,只返回汉字内容 |
| 数学运算验证码 | 这是一个数学运算验证码,请计算结果,只返回最终数字 |
| 滑块验证码 | 这是滑块验证码背景图,图中有一个缺口,请返回缺口左边缘的X坐标像素值,只返回整数 |
| 点选验证码 | 请按顺序识别图中需要点击的文字,用逗号分隔 |
import requests resp = requests.post("http://localhost:2001/recognize", json={ "image": "https://example.com/captcha.png", "prompt": "这是一个4位数字验证码,只返回数字" }) data = resp.json() print(data["result"]) # "1234"
const resp = await fetch("http://localhost:2001/recognize", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ image: "https://example.com/captcha.png", prompt: "这是一个4位数字验证码,只返回数字" }) }); const data = await resp.json(); console.log(data.result); // "1234"
├── server.py # API 服务入口 ├── captcha_recognizer.py # 核心识别逻辑 ├── start.py # 启动脚本(端口2001) ├── API_DOC.html # 本文档 └── requirements.txt # 依赖清单
pip install -r requirements.txt python start.py