captcha_breaker.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import base64
  2. import os
  3. from openai import OpenAI
  4. def encode_image_to_base64(image_path):
  5. with open(image_path, "rb") as image_file:
  6. return base64.b64encode(image_file.read()).decode('utf-8')
  7. def recognize_captcha_with_qwen(image_path, api_key):
  8. # 利用 OpenAI 的包,调用阿里云的兼容 API 接口
  9. client = OpenAI(
  10. api_key=api_key,
  11. base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
  12. )
  13. base64_image = encode_image_to_base64(image_path)
  14. prompt = "你是一个精确的OCR机器人。请识别图片中的验证码字符。只输出验证码本身的内容,不要任何多余的汉字或标点符号。"
  15. try:
  16. response = client.chat.completions.create(
  17. model="qwen-vl-max", # 也可以用更便宜的 qwen-vl-plus
  18. messages=[
  19. {
  20. "role": "user",
  21. "content":[
  22. {"type": "text", "text": prompt},
  23. {
  24. "type": "image_url",
  25. "image_url": {
  26. "url": f"data:image/png;base64,{base64_image}"
  27. }
  28. }
  29. ]
  30. }
  31. ],
  32. temperature=0.0
  33. )
  34. return response.choices[0].message.content.strip()
  35. except Exception as e:
  36. print(f"Qwen 识别错误: {e}")
  37. return None
  38. if __name__ == "__main__":
  39. # 填入阿里云百炼 (DashScope) 的 API-KEY
  40. API_KEY = "sk-893e895724c6403d81374e515ffaf427"
  41. IMAGE_PATH = "captcha.png"
  42. print(f"Qwen-VL 识别结果: {recognize_captcha_with_qwen(IMAGE_PATH, API_KEY)}")