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