captcha_breaker.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import os
  2. import io
  3. import mimetypes
  4. import base64
  5. from typing import Union, Optional, Any
  6. from openai import OpenAI
  7. from configure import CAPTCHA_LLM_CONFIG
  8. class CaptchaBreakerError(Exception):
  9. """验证码识别异常基类"""
  10. pass
  11. def normalize_image_to_data_url(
  12. image_input: Union[str, bytes, io.BytesIO],
  13. default_mime_type: str = "image/png"
  14. ) -> str:
  15. """
  16. 将多种图片输入形式(文件路径、字节数据、BytesIO、Base64字符串、Data URL)
  17. 标准化为可供多模态模型接收的 Data URL (data:<mime-type>;base64,<data>)
  18. """
  19. if isinstance(image_input, (bytes, bytearray)):
  20. b64_str = base64.b64encode(image_input).decode("utf-8")
  21. return f"data:{default_mime_type};base64,{b64_str}"
  22. if isinstance(image_input, io.BytesIO):
  23. b64_str = base64.b64encode(image_input.getvalue()).decode("utf-8")
  24. return f"data:{default_mime_type};base64,{b64_str}"
  25. if isinstance(image_input, str):
  26. # 已经是标准 Data URL
  27. if image_input.startswith("data:image/"):
  28. return image_input
  29. # 如果是本地存在的文件路径
  30. if os.path.isfile(image_input):
  31. mime_type, _ = mimetypes.guess_type(image_input)
  32. mime_type = mime_type or default_mime_type
  33. with open(image_input, "rb") as f:
  34. b64_str = base64.b64encode(f.read()).decode("utf-8")
  35. return f"data:{mime_type};base64,{b64_str}"
  36. # 尝试作为纯 Base64 字符串处理
  37. clean_b64 = image_input.strip()
  38. return f"data:{default_mime_type};base64,{clean_b64}"
  39. raise ValueError(f"Unsupported image input type: {type(image_input)}")
  40. class CaptchaBreaker:
  41. """
  42. 通用大模型验证码识别器
  43. 支持基于 OpenAI 兼容协议的多模态大模型端点(如 Qwen-VL, GPT-4o, DeepSeek-VL, OneAPI 等)
  44. """
  45. DEFAULT_PROMPT = "你是一个精确的OCR机器人。请识别图片中的验证码字符。只输出验证码本身的内容,不要任何多余的汉字或标点符号。"
  46. def __init__(
  47. self,
  48. api_key: Optional[str] = None,
  49. base_url: Optional[str] = None,
  50. model: Optional[str] = None,
  51. timeout: Optional[float] = None,
  52. client: Optional[OpenAI] = None
  53. ):
  54. # 配置读取逻辑:
  55. # 1. 优先使用构造函数传入的参数
  56. # 2. 如果 CAPTCHA_LLM_CONFIG 是字典,从中读取
  57. # 3. 如果 CAPTCHA_LLM_CONFIG 为 None 或未提供,从环境变量读取
  58. cfg = CAPTCHA_LLM_CONFIG if isinstance(CAPTCHA_LLM_CONFIG, dict) else {}
  59. self.api_key = api_key or cfg.get("api_key") or os.getenv("CAPTCHA_LLM_API_KEY")
  60. self.base_url = (
  61. base_url
  62. or cfg.get("base_url")
  63. or os.getenv("CAPTCHA_LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
  64. )
  65. self.model = (
  66. model
  67. or cfg.get("model")
  68. or os.getenv("CAPTCHA_LLM_MODEL", "qwen-vl-max")
  69. )
  70. timeout_val = timeout or cfg.get("timeout") or os.getenv("CAPTCHA_LLM_TIMEOUT")
  71. self.timeout = float(timeout_val) if timeout_val is not None else 30.0
  72. self._client = client
  73. @property
  74. def client(self) -> OpenAI:
  75. """获取或延迟初始化 OpenAI 客户端"""
  76. if self._client is None:
  77. if not self.api_key:
  78. raise CaptchaBreakerError(
  79. "API Key is not configured for CaptchaBreaker. "
  80. "CAPTCHA_LLM_CONFIG is None and CAPTCHA_LLM_API_KEY environment variable is not set."
  81. )
  82. self._client = OpenAI(
  83. api_key=self.api_key,
  84. base_url=self.base_url,
  85. timeout=self.timeout
  86. )
  87. return self._client
  88. def _clean_result(self, text: str) -> str:
  89. """清理大模型返回的验证码文本,去除 markdown 代码块标记与前后符号"""
  90. result = text.strip()
  91. # 移除 markdown 代码块包裹 (如 ``` 或 ```text ... ```)
  92. if result.startswith("```"):
  93. lines = result.splitlines()
  94. if len(lines) >= 2 and lines[-1].strip() == "```":
  95. result = "\n".join(lines[1:-1]).strip()
  96. # 去除首尾常见的引号
  97. if (result.startswith('"') and result.endswith('"')) or (result.startswith("'") and result.endswith("'")):
  98. result = result[1:-1].strip()
  99. return result
  100. def break_captcha(
  101. self,
  102. image: Union[str, bytes, io.BytesIO],
  103. prompt: Optional[str] = None,
  104. temperature: float = 0.0,
  105. **kwargs: Any
  106. ) -> str:
  107. """
  108. 识别验证码
  109. :param image: 图片输入,支持文件路径、二进制字节、BytesIO 或 Base64 / Data URL 字符串
  110. :param prompt: 提示词,默认使用通用字符 OCR 提示词
  111. :param temperature: 采样温度,默认为 0.0 以保证确定性
  112. :param kwargs: 透传给 chat.completions.create 的额外参数
  113. :return: 识别出的验证码文本
  114. :raises CaptchaBreakerError: 调用失败或无法解析时抛出
  115. """
  116. data_url = normalize_image_to_data_url(image)
  117. used_prompt = prompt or self.DEFAULT_PROMPT
  118. try:
  119. response = self.client.chat.completions.create(
  120. model=self.model,
  121. messages=[
  122. {
  123. "role": "user",
  124. "content": [
  125. {"type": "text", "text": used_prompt},
  126. {
  127. "type": "image_url",
  128. "image_url": {
  129. "url": data_url
  130. }
  131. }
  132. ]
  133. }
  134. ],
  135. temperature=temperature,
  136. **kwargs
  137. )
  138. raw_content = response.choices[0].message.content or ""
  139. return self._clean_result(raw_content)
  140. except CaptchaBreakerError:
  141. raise
  142. except Exception as e:
  143. raise CaptchaBreakerError(f"Captcha recognition failed [{self.model}]: {e}") from e
  144. def __call__(
  145. self,
  146. image: Union[str, bytes, io.BytesIO],
  147. prompt: Optional[str] = None,
  148. **kwargs: Any
  149. ) -> str:
  150. """支持将实例作为函数直接调用"""
  151. return self.break_captcha(image, prompt=prompt, **kwargs)
  152. if __name__ == "__main__":
  153. import sys
  154. print("CaptchaBreaker initialized.")
  155. if len(sys.argv) > 1:
  156. test_path = sys.argv[1]
  157. breaker = CaptchaBreaker()
  158. print(f"Testing recognition on: {test_path}")
  159. res = breaker.break_captcha(test_path)
  160. print(f"Result: {res}")