import os import io import mimetypes import base64 from typing import Union, Optional, Any from openai import OpenAI from configure import CAPTCHA_LLM_CONFIG class CaptchaBreakerError(Exception): """验证码识别异常基类""" pass def normalize_image_to_data_url( image_input: Union[str, bytes, io.BytesIO], default_mime_type: str = "image/png" ) -> str: """ 将多种图片输入形式(文件路径、字节数据、BytesIO、Base64字符串、Data URL) 标准化为可供多模态模型接收的 Data URL (data:;base64,) """ if isinstance(image_input, (bytes, bytearray)): b64_str = base64.b64encode(image_input).decode("utf-8") return f"data:{default_mime_type};base64,{b64_str}" if isinstance(image_input, io.BytesIO): b64_str = base64.b64encode(image_input.getvalue()).decode("utf-8") return f"data:{default_mime_type};base64,{b64_str}" if isinstance(image_input, str): # 已经是标准 Data URL if image_input.startswith("data:image/"): return image_input # 如果是本地存在的文件路径 if os.path.isfile(image_input): mime_type, _ = mimetypes.guess_type(image_input) mime_type = mime_type or default_mime_type with open(image_input, "rb") as f: b64_str = base64.b64encode(f.read()).decode("utf-8") return f"data:{mime_type};base64,{b64_str}" # 尝试作为纯 Base64 字符串处理 clean_b64 = image_input.strip() return f"data:{default_mime_type};base64,{clean_b64}" raise ValueError(f"Unsupported image input type: {type(image_input)}") class CaptchaBreaker: """ 通用大模型验证码识别器 支持基于 OpenAI 兼容协议的多模态大模型端点(如 Qwen-VL, GPT-4o, DeepSeek-VL, OneAPI 等) """ DEFAULT_PROMPT = "你是一个精确的OCR机器人。请识别图片中的验证码字符。只输出验证码本身的内容,不要任何多余的汉字或标点符号。" def __init__( self, api_key: Optional[str] = None, base_url: Optional[str] = None, model: Optional[str] = None, timeout: Optional[float] = None, client: Optional[OpenAI] = None ): # 配置读取逻辑: # 1. 优先使用构造函数传入的参数 # 2. 如果 CAPTCHA_LLM_CONFIG 是字典,从中读取 # 3. 如果 CAPTCHA_LLM_CONFIG 为 None 或未提供,从环境变量读取 cfg = CAPTCHA_LLM_CONFIG if isinstance(CAPTCHA_LLM_CONFIG, dict) else {} self.api_key = api_key or cfg.get("api_key") or os.getenv("CAPTCHA_LLM_API_KEY") self.base_url = ( base_url or cfg.get("base_url") or os.getenv("CAPTCHA_LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1") ) self.model = ( model or cfg.get("model") or os.getenv("CAPTCHA_LLM_MODEL", "qwen-vl-max") ) timeout_val = timeout or cfg.get("timeout") or os.getenv("CAPTCHA_LLM_TIMEOUT") self.timeout = float(timeout_val) if timeout_val is not None else 30.0 self._client = client @property def client(self) -> OpenAI: """获取或延迟初始化 OpenAI 客户端""" if self._client is None: if not self.api_key: raise CaptchaBreakerError( "API Key is not configured for CaptchaBreaker. " "CAPTCHA_LLM_CONFIG is None and CAPTCHA_LLM_API_KEY environment variable is not set." ) self._client = OpenAI( api_key=self.api_key, base_url=self.base_url, timeout=self.timeout ) return self._client def _clean_result(self, text: str) -> str: """清理大模型返回的验证码文本,去除 markdown 代码块标记与前后符号""" result = text.strip() # 移除 markdown 代码块包裹 (如 ``` 或 ```text ... ```) if result.startswith("```"): lines = result.splitlines() if len(lines) >= 2 and lines[-1].strip() == "```": result = "\n".join(lines[1:-1]).strip() # 去除首尾常见的引号 if (result.startswith('"') and result.endswith('"')) or (result.startswith("'") and result.endswith("'")): result = result[1:-1].strip() return result def break_captcha( self, image: Union[str, bytes, io.BytesIO], prompt: Optional[str] = None, temperature: float = 0.0, **kwargs: Any ) -> str: """ 识别验证码 :param image: 图片输入,支持文件路径、二进制字节、BytesIO 或 Base64 / Data URL 字符串 :param prompt: 提示词,默认使用通用字符 OCR 提示词 :param temperature: 采样温度,默认为 0.0 以保证确定性 :param kwargs: 透传给 chat.completions.create 的额外参数 :return: 识别出的验证码文本 :raises CaptchaBreakerError: 调用失败或无法解析时抛出 """ data_url = normalize_image_to_data_url(image) used_prompt = prompt or self.DEFAULT_PROMPT try: response = self.client.chat.completions.create( model=self.model, messages=[ { "role": "user", "content": [ {"type": "text", "text": used_prompt}, { "type": "image_url", "image_url": { "url": data_url } } ] } ], temperature=temperature, **kwargs ) raw_content = response.choices[0].message.content or "" return self._clean_result(raw_content) except CaptchaBreakerError: raise except Exception as e: raise CaptchaBreakerError(f"Captcha recognition failed [{self.model}]: {e}") from e def __call__( self, image: Union[str, bytes, io.BytesIO], prompt: Optional[str] = None, **kwargs: Any ) -> str: """支持将实例作为函数直接调用""" return self.break_captcha(image, prompt=prompt, **kwargs) if __name__ == "__main__": import sys print("CaptchaBreaker initialized.") if len(sys.argv) > 1: test_path = sys.argv[1] breaker = CaptchaBreaker() print(f"Testing recognition on: {test_path}") res = breaker.break_captcha(test_path) print(f"Result: {res}")