lze888lze commited on
Commit
a01b6e6
·
verified ·
1 Parent(s): e9b324a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +326 -28
README.md CHANGED
@@ -9,41 +9,339 @@ app_port: 7860
9
 
10
  # 滑块验证码识别 API
11
 
12
- 基于 FastAPI + ONNX 的滑块验证码识别服务。
13
 
14
- ## 接口
15
 
16
  | 方法 | 路径 | 说明 |
17
- |------|------|------|
18
- | GET | / | 健康检查 |
19
- | GET | /health | 健康检查 |
20
- | POST | /captcha | 文件上传识别 |
21
- | POST | /captcha/base64 | Base64图片识别 |
 
 
 
 
 
 
 
 
22
 
23
- ## 调用示例
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- ### 文件上传
26
  ```bash
27
- curl -X POST https://your-space.hf.space/captcha \
28
- -F "file=@captcha.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  ```
30
 
31
- ### Base64(懒人精灵)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  ```lua
33
- local http = require("http")
34
- local json = require("json")
35
- local base64 = require("base64")
36
-
37
- local f = io.open("/sdcard/captcha.png", "rb")
38
- local img = f:read("*a")
39
- f:close()
40
-
41
- local resp = http.post("https://your-domain.com/captcha/base64", {
42
- headers = {["Content-Type"] = "application/json"},
43
- body = '{"image":"' .. base64.encode(img) .. '"}'
44
- })
45
-
46
- local result = json.decode(resp.body)
47
- -- result.box = [x1, y1, x2, y2]
48
- -- result.confidence = 0.95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ```
 
9
 
10
  # 滑块验证码识别 API
11
 
12
+ 基于 FastAPI + ONNX 的滑块验证码识别服务,支持滑块位置识别、缺口位置识别、滑缺距计算、带框图片可视化上传,以及 IPv4/IPv6 离线归属地查询
13
 
14
+ ## 接口列表
15
 
16
  | 方法 | 路径 | 说明 |
17
+ |---|---|---|
18
+ | `GET` | `/` | 根路径健康检查 |
19
+ | `GET` | `/health` | 部署平台健康检查 |
20
+ | `GET` | `/ip?ip=8.8.8.8` | IP 归属地查询,自动识别 IPv4/IPv6 |
21
+ | `POST` | `/ip` | IP 归属地查询,支持 JSON、`ip=xxx`、纯 IP 文本 |
22
+ | `POST` | `/slide` | 上传图片文件,只返回滑块位置 |
23
+ | `POST` | `/slide-base64` | 上传 base64 图片,只返回滑块位置 |
24
+ | `POST` | `/hole` | 上传图片文件,只返回缺口位置 |
25
+ | `POST` | `/hole-base64` | 上传 base64 图片,只返回缺口位置 |
26
+ | `POST` | `/puzzle` | 上传图片文件,返回滑块、缺口和滑缺距 |
27
+ | `POST` | `/puzzle-base64` | 上传 base64 图片,返回滑块、缺口和滑缺距 |
28
+ | `POST` | `/visualize` | 上传图片文件,返回识别结果和带框图片 URL |
29
+ | `POST` | `/visualize-base64` | 上传 base64 图片,返回识别结果和带框图片 URL |
30
 
31
+ ## 基础地址
32
+
33
+ 推荐通过代理域名访问:
34
+
35
+ ```text
36
+ https://hf-api.lze.cc.cd
37
+ ```
38
+
39
+ 如果你是自行部署,也可以把示例里的域名替换成自己的服务地址。
40
+
41
+ ## 健康检查
42
+
43
+ ### 根路径
44
 
 
45
  ```bash
46
+ curl "https://hf-api.lze.cc.cd/"
47
+ ```
48
+
49
+ 返回示例:
50
+
51
+ ```json
52
+ {
53
+ "Hello": "Captcha",
54
+ "status": "running"
55
+ }
56
+ ```
57
+
58
+ ### health
59
+
60
+ ```bash
61
+ curl "https://hf-api.lze.cc.cd/health"
62
+ ```
63
+
64
+ 返回示例:
65
+
66
+ ```json
67
+ {
68
+ "status": "ok"
69
+ }
70
  ```
71
 
72
+ ## IP 查询
73
+
74
+ 接口会自动识别 IPv4 和 IPv6,并使用项目内的离线库查询:
75
+
76
+ ```text
77
+ data/ip2region_v4.xdb
78
+ data/ip2region_v6.xdb
79
+ ```
80
+
81
+ ### GET 查询
82
+
83
+ ```bash
84
+ curl "https://hf-api.lze.cc.cd/ip?ip=8.8.8.8"
85
+ ```
86
+
87
+ IPv6 示例:
88
+
89
+ ```bash
90
+ curl "https://hf-api.lze.cc.cd/ip?ip=240e:3b7:3272:d8d0:db09:c067:8d59:539e"
91
+ ```
92
+
93
+ ### POST JSON 查询
94
+
95
+ ```bash
96
+ curl -X POST "https://hf-api.lze.cc.cd/ip" \
97
+ -H "Content-Type: application/json" \
98
+ -d '{"ip":"8.8.8.8"}'
99
+ ```
100
+
101
+ ### POST 表单字符串查询
102
+
103
+ ```bash
104
+ curl -X POST "https://hf-api.lze.cc.cd/ip" \
105
+ -d "ip=113.118.113.77"
106
+ ```
107
+
108
+ ### POST 纯文本查询
109
+
110
+ ```bash
111
+ curl -X POST "https://hf-api.lze.cc.cd/ip" \
112
+ -H "Content-Type: text/plain" \
113
+ -d "113.118.113.77"
114
+ ```
115
+
116
+ ### 懒人精灵调用
117
+
118
  ```lua
119
+ local ret, code = httpPost("https://hf-api.lze.cc.cd/ip", "ip=113.118.113.77", 10)
120
+ print("状态码:" .. tostring(code))
121
+ print("返回内容:" .. tostring(ret))
122
+ ```
123
+
124
+ GET 也可以:
125
+
126
+ ```lua
127
+ local ret, code = httpGet("https://hf-api.lze.cc.cd/ip?ip=113.118.113.77", 10)
128
+ print("状态码:" .. tostring(code))
129
+ print("返回内容:" .. tostring(ret))
130
+ ```
131
+
132
+ ### 返回示例
133
+
134
+ ```json
135
+ {
136
+ "ip": "8.8.8.8",
137
+ "版本": "IPv4",
138
+ "归属地": "United States|California|0|Google LLC|US",
139
+ "数据": {
140
+ "国家": "United States",
141
+ "省份/州": "California",
142
+ "城市": null,
143
+ "运营商": "Google LLC",
144
+ "国家代码": "US"
145
+ },
146
+ "消息": null
147
+ }
148
+ ```
149
+
150
+ 非法 IP 返回示例:
151
+
152
+ ```json
153
+ {
154
+ "ip": "not-an-ip",
155
+ "版本": null,
156
+ "归属地": "",
157
+ "数据": {},
158
+ "消息": "无效IP地址"
159
+ }
160
+ ```
161
+
162
+ ## 滑块位置识别
163
+
164
+ ### 上传图片文件
165
+
166
+ ```bash
167
+ curl -X POST "https://hf-api.lze.cc.cd/slide" \
168
+ -F "file=@captcha.png"
169
+ ```
170
+
171
+ 返回示例:
172
+
173
+ ```json
174
+ {
175
+ "滑块": [91, 1045, 248, 1200],
176
+ "相似度": 0.9487,
177
+ "消息": null
178
+ }
179
+ ```
180
+
181
+ ### 上传 base64
182
+
183
+ ```bash
184
+ curl -X POST "https://hf-api.lze.cc.cd/slide-base64" \
185
+ -H "Content-Type: application/json" \
186
+ -d '{"image":"base64字符串"}'
187
+ ```
188
+
189
+ 请求字段:
190
+
191
+ | 字段 | 类型 | 说明 |
192
+ |---|---|---|
193
+ | `image` | `string` | 图片 base64 字符串,支持纯 base64 或 `data:image/png;base64,` 前缀 |
194
+
195
+ ## 缺口位置识别
196
+
197
+ ### 上传图片文件
198
+
199
+ ```bash
200
+ curl -X POST "https://hf-api.lze.cc.cd/hole" \
201
+ -F "file=@captcha.png"
202
+ ```
203
+
204
+ 返回示例:
205
+
206
+ ```json
207
+ {
208
+ "缺口": [803, 1044, 957, 1201],
209
+ "相似度": 0.9474,
210
+ "消息": null
211
+ }
212
+ ```
213
+
214
+ ### 上传 base64
215
+
216
+ ```bash
217
+ curl -X POST "https://hf-api.lze.cc.cd/hole-base64" \
218
+ -H "Content-Type: application/json" \
219
+ -d '{"image":"base64字符串"}'
220
+ ```
221
+
222
+ ## 滑块和缺口组合识别
223
+
224
+ ### 上传图片文件
225
+
226
+ ```bash
227
+ curl -X POST "https://hf-api.lze.cc.cd/puzzle" \
228
+ -F "file=@captcha.png"
229
+ ```
230
+
231
+ 返回示例:
232
+
233
+ ```json
234
+ {
235
+ "滑块": [91, 1045, 248, 1200],
236
+ "滑块相似度": 0.9487,
237
+ "缺口": [803, 1044, 957, 1201],
238
+ "缺口相似度": 0.9474,
239
+ "滑缺距": 712,
240
+ "消息": null
241
+ }
242
+ ```
243
+
244
+ `滑缺距` 的计算方式是:
245
+
246
+ ```text
247
+ 缺口 x1 - 滑块 x1
248
+ ```
249
+
250
+ ### 上传 base64
251
+
252
+ ```bash
253
+ curl -X POST "https://hf-api.lze.cc.cd/puzzle-base64" \
254
+ -H "Content-Type: application/json" \
255
+ -d '{"image":"base64字符串"}'
256
+ ```
257
+
258
+ ## 可视化识别
259
+
260
+ 可视化接口会识别滑块和缺口,在图片上画框,然后把带框图片上传到 Cloudflare R2,最后返回图片 URL。
261
+
262
+ 需要在 Hugging Face Spaces 配置这些环境变量:
263
+
264
+ | 环境变量 | 说明 |
265
+ |---|---|
266
+ | `R2_ENDPOINT` | Cloudflare R2 S3 兼容接口地址 |
267
+ | `R2_ACCESS_KEY_ID` | R2 Access Key ID |
268
+ | `R2_SECRET_ACCESS_KEY` | R2 Secret Access Key |
269
+ | `R2_BUCKET_NAME` | R2 Bucket 名称,默认 `lze` |
270
+ | `R2_PUBLIC_URL` | R2 公开访问域名 |
271
+
272
+ 如果没有配置 R2,接口仍会返回识别坐标,但 `image_url` 会是 `null`。
273
+
274
+ ### 上传图片文件
275
+
276
+ ```bash
277
+ curl -X POST "https://hf-api.lze.cc.cd/visualize" \
278
+ -F "file=@captcha.png"
279
+ ```
280
+
281
+ 返回示例:
282
+
283
+ ```json
284
+ {
285
+ "滑块": [91, 1045, 248, 1200],
286
+ "滑块相似度": 0.9487,
287
+ "缺口": [803, 1044, 957, 1201],
288
+ "缺口相似度": 0.9474,
289
+ "滑缺距": 712,
290
+ "image_url": "https://xxx.r2.dev/captcha_xxx.png",
291
+ "消息": null
292
+ }
293
+ ```
294
+
295
+ ### 上传 base64
296
+
297
+ ```bash
298
+ curl -X POST "https://hf-api.lze.cc.cd/visualize-base64" \
299
+ -H "Content-Type: application/json" \
300
+ -d '{"image":"base64字符串"}'
301
+ ```
302
+
303
+ ## 错误返回
304
+
305
+ 图片格式不支持时:
306
+
307
+ ```json
308
+ {
309
+ "消息": "不支持的图片格式"
310
+ }
311
+ ```
312
+
313
+ base64 参数缺失或解码失败时:
314
+
315
+ ```json
316
+ {
317
+ "消息": "缺少image参数或base64解码失败"
318
+ }
319
+ ```
320
+
321
+ IP 参数缺失时:
322
+
323
+ ```json
324
+ {
325
+ "消息": "缺少ip参数"
326
+ }
327
+ ```
328
+
329
+ ## 部署说明
330
+
331
+ 项目使用 Docker SDK 部署到 Hugging Face Spaces,默认端口是:
332
+
333
+ ```text
334
+ 7860
335
+ ```
336
+
337
+ 运行时依赖在 `requirements.txt` 中声明。新增 IP 查询接口需要:
338
+
339
+ ```text
340
+ py-ip2region
341
+ ```
342
+
343
+ 可视化上传 R2 需要:
344
+
345
+ ```text
346
+ boto3
347
  ```