vision_verify.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. """豆包视觉逐张验收 AI 生成图(通用)
  3. 用法:
  4. FMODE_API_KEY=$FMODE_API_KEY python3 vision_verify.py <图片目录> [<输出json>]
  5. 行为:
  6. - 扫描目录下所有 .png/.jpg/.jpeg
  7. - 逐张缩到 <=1280 宽后 base64,调 doubao-seed-2-0-pro-260215 问 6 项
  8. - 结果写 JSON,重跑跳过已完成页(断点续传)
  9. - 单张失败重试 2 次,退避 3s
  10. """
  11. import os, sys, json, base64, urllib.request, time, io
  12. from PIL import Image
  13. TOKEN = os.environ.get('FMODE_API_KEY', '')
  14. BASE = "https://api.fmode.cn/v1/chat/completions"
  15. MODEL = "doubao-seed-2-0-pro-260215"
  16. CHECK_PROMPT = """请逐项检查这张图,简洁回答:
  17. 1)【】方括号位置标记字符有无残留?
  18. 2)#开头十六进制色号文字有无泄漏到画面?
  19. 3)中文有无错字/缺笔画/乱码/重影?
  20. 4)有无 Hermes/Claude/DeepSeek 等底层真名署名?
  21. 5)风格是否浅色高级感、无3D立体/玻璃发光/卡通插画?
  22. 6)主标题清晰醒目、无文字错位溢出?
  23. 最后一行单独输出结论:通过 或 需重出。"""
  24. def encode(path):
  25. im = Image.open(path).convert('RGB')
  26. if im.width > 1280:
  27. im.thumbnail((1280, 1280))
  28. buf = io.BytesIO()
  29. im.save(buf, 'PNG')
  30. return base64.b64encode(buf.getvalue()).decode()
  31. def ask(b64):
  32. body = {
  33. "model": MODEL,
  34. "messages": [{
  35. "role": "user",
  36. "content": [
  37. {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}},
  38. {"type": "text", "text": CHECK_PROMPT},
  39. ],
  40. }],
  41. }
  42. req = urllib.request.Request(
  43. BASE, data=json.dumps(body).encode(),
  44. headers={"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"},
  45. )
  46. with urllib.request.urlopen(req, timeout=120) as r:
  47. d = json.loads(r.read())
  48. return d["choices"][0]["message"]["content"]
  49. def main():
  50. if not TOKEN:
  51. print("缺 FMODE_API_KEY 环境变量"); sys.exit(2)
  52. d = sys.argv[1] if len(sys.argv) > 1 else '.'
  53. out = sys.argv[2] if len(sys.argv) > 2 else os.path.join(d, 'vision_result.json')
  54. exts = ('.png', '.jpg', '.jpeg')
  55. files = sorted(f for f in os.listdir(d) if f.lower().endswith(exts))
  56. res = json.load(open(out)) if os.path.exists(out) else {}
  57. for f in files:
  58. if f in res and ('通过' in res[f] or '需重出' in res[f]):
  59. continue
  60. last_err = None
  61. for attempt in range(3):
  62. try:
  63. txt = ask(encode(os.path.join(d, f)))
  64. res[f] = txt
  65. print(f"===== {f} ====="); print(txt); print()
  66. json.dump(res, open(out, 'w'), ensure_ascii=False, indent=2)
  67. last_err = None
  68. break
  69. except Exception as e:
  70. last_err = e
  71. time.sleep(3)
  72. if last_err:
  73. res[f] = f"ERR {last_err}"
  74. json.dump(res, open(out, 'w'), ensure_ascii=False, indent=2)
  75. bad = [f for f, t in res.items() if '需重出' in t]
  76. print(f"===== 合计 {len(res)} 张,需重出 {len(bad)} 张 =====")
  77. if bad:
  78. print("需重出页:", ', '.join(bad))
  79. if __name__ == '__main__':
  80. main()