import base64 import os from openai import OpenAI QWEN_API_KEY = "sk-893e895724c6403d81374e515ffaf427" def encode_image_to_base64(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8') def recognize_captcha_with_qwen(image_path, api_key): # 利用 OpenAI 的包,调用阿里云的兼容 API 接口 client = OpenAI( api_key=api_key, base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" ) base64_image = encode_image_to_base64(image_path) prompt = "你是一个精确的OCR机器人。请识别图片中的验证码字符。只输出验证码本身的内容,不要任何多余的汉字或标点符号。" try: response = client.chat.completions.create( model="qwen-vl-max", # 也可以用更便宜的 qwen-vl-plus messages=[ { "role": "user", "content":[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": { "url": f"data:image/png;base64,{base64_image}" } } ] } ], temperature=0.0 ) return response.choices[0].message.content.strip() except Exception as e: print(f"Qwen 识别错误: {e}") return None def recognize_vfs_captcha_with_qwen(base64_image): # 利用 OpenAI 的包,调用阿里云的兼容 API 接口 client = OpenAI( api_key=QWEN_API_KEY, base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" ) prompt = "图片中有多组验证码,其中OTP箭头指向的验证码为真,其余为假。只输出真实验证码的内容,不要任何多余的汉字或标点符号。" try: response = client.chat.completions.create( model="qwen-vl-max", # 也可以用更便宜的 qwen-vl-plus messages=[ { "role": "user", "content":[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": { "url": base64_image } } ] } ], temperature=0.0 ) return response.choices[0].message.content.strip() except Exception as e: print(f"Qwen 识别错误: {e}") return None if __name__ == "__main__": # 填入阿里云百炼 (DashScope) 的 API-KEY IMAGE_PATH = "/home/jerry/workspace/coordinator/data/275c31f009e612b65eb92cb52bb1d98.png" print(f"Qwen-VL 识别结果: {recognize_vfs_captcha_with_qwen(IMAGE_PATH, QWEN_API_KEY)}")