├── README.md ├── api_test.py ├── example.png ├── main.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | 2 | # captcha-api 3 | 4 | 基于FastAPI + [Captcha Recognizer](https://github.com/chenwei-zhao/captcha-recognizer)库 搭建的滑块验证码识别服务 5 | 6 | # 版本要求 7 | 8 | * ``Python`` >= 3.8.0 9 | 10 | # 依赖包 11 | 参考 requirements.txt 12 | 13 | # 使用示例 14 | 15 | ```bash 16 | # 拉取代码 17 | git clone https://github.com/chenwei-zhao/captcha-api 18 | 19 | # 进入项目根目录 20 | cd captcha-api 21 | 22 | # 安装依赖 23 | pip install -r requirements.txt 24 | 25 | # 启动服务 26 | fastapi run 27 | 28 | # 测试接口 29 | python api_test.py 30 | 31 | ``` 32 | 33 | # 接口文档 34 | 35 | 启动服务后,在浏览器中访问 ``http://localhost:8000/docs`` 即可查看接口文档。 36 | 37 | # Python requests 访问服务示例 38 | 39 | ```python3 40 | import requests 41 | 42 | 43 | def test_captcha_api(): 44 | host = 'http://127.0.0.1:8000' 45 | url = f'{host}/captcha' 46 | image_path = 'example.png' 47 | 48 | files = {'file': open(image_path, 'rb')} 49 | 50 | data = {'image_type': 'background'} 51 | 52 | response = requests.post(url, 53 | data=data, 54 | files=files 55 | ) 56 | print(f'状态码: {response.status_code}, 响应内容: {response.json()}') 57 | 58 | 59 | if __name__ == "__main__": 60 | test_captcha_api() 61 | ``` 62 | 63 | # 其他语言访问服务示例 64 | 65 | 其他语言请参考接口文档自行编写 66 | 67 | # 注意事项 68 | 69 | 在生产环境中,建议结合Gunicorn和Uvicorn,高效运行FastAPI应用,以提供足够的并发处理能力和稳定性。 70 | 71 | # 遇到问题 72 | 73 | - Error loading “xxx\Lib\site-packages\torch\lib\fbgemm.dll” or one of its dependencies. 74 | - 参考 [Issues 2](https://github.com/chenwei-zhao/captcha-recognizer/issues/2) 75 | - Model Unsupported model IR version: 9, max supported IR version: 8 76 | - 参考 [Issues 1](https://github.com/chenwei-zhao/captcha-recognizer/issues/1) 77 | 78 | # 免责声明 79 | 80 | 本项目不针对任何一家验证码厂商,项目所有内容仅供学习交流使用,严禁用于非法用途。 81 | 82 | # 许可证 83 | 84 | MIT license 85 | 86 | # 联系方式 87 | 88 | - Gmail: chenwei.zhaozhao@gmail.com 89 | - 163/网易: chenwei_nature@163.com 90 | 91 | # 引用 92 | [1] [Captcha Recognizer](https://github.com/chenwei-zhao/captcha-recognizer) 93 | -------------------------------------------------------------------------------- /api_test.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | 4 | def test_captcha_api(): 5 | host = 'http://127.0.0.1:8000' 6 | url = f'{host}/captcha' 7 | image_path = 'example.png' 8 | 9 | files = {'file': open(image_path, 'rb')} 10 | 11 | data = {'image_type': 'background'} 12 | 13 | response = requests.post(url, 14 | data=data, 15 | files=files 16 | ) 17 | print(f'状态码: {response.status_code}, 响应内容: {response.json()}') 18 | 19 | 20 | if __name__ == "__main__": 21 | test_captcha_api() 22 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenwei-zhao/captcha-api/fffcdf8a7230f9bf34c6433cd5a8681644b2abad/example.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | from io import BytesIO 3 | 4 | from captcha_recognizer.recognizer import Recognizer 5 | from fastapi import FastAPI, File, Form, UploadFile 6 | from PIL import Image 7 | 8 | app = FastAPI() 9 | 10 | recognizer = Recognizer() 11 | 12 | 13 | @app.get("/") 14 | def hello_captcha(): 15 | return {"Hello": "Captcha"} 16 | 17 | 18 | class ImageType(Enum): 19 | Background = "background" 20 | Screenshot = "screenshot" 21 | 22 | 23 | @app.post("/captcha") 24 | async def create_item( 25 | image_type: ImageType = Form(..., description="验证码图片类型,background表示背景图,screenshot表示截屏图"), 26 | file: UploadFile = File(...)): 27 | file_content = await file.read() 28 | image_stream = BytesIO(file_content) 29 | image = Image.open(image_stream) 30 | if image_type == ImageType.Background: 31 | box, confidence = recognizer.identify_gap(source=image, verbose=False) 32 | else: 33 | box, confidence = recognizer.identify_screenshot(source=image, verbose=False) 34 | 35 | return { 36 | 'box': box, 37 | 'confidence': confidence 38 | } 39 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ultralytics>=8.0.0 2 | onnxruntime 3 | onnx 4 | captcha_recognizer 5 | fastapi[standard] 6 | pillow 7 | --------------------------------------------------------------------------------