TravelContentCreator/scripts/test_poster_smart_v2.py
jinye_huang 5cc31fc733 feat(poster_v2): 实现智能海报引擎 V2
- 新增 poster_smart_v2.py 引擎
- 双输出: preview_base64 (无底图预览) + fabric_json (前端编辑)
- 5 种布局的 Fabric.js JSON 生成器
- 复用 AI 文案生成和布局渲染
- 测试脚本和输出样例
2025-12-10 15:43:27 +08:00

159 lines
5.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试智能海报生成引擎 V2
输出:
1. preview PNG - 无底图预览
2. fabric JSON - 前端编辑用
"""
import asyncio
import base64
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from domain.aigc.engines.poster_smart_v2 import PosterSmartEngineV2
OUTPUT_DIR = Path(__file__).parent.parent / "result" / "poster_smart_v2_test"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# 所有布局
ALL_LAYOUTS = ["hero_bottom", "overlay_center", "overlay_bottom", "split_vertical", "card_float"]
ALL_THEMES = ["ocean", "sunset", "peach", "mint", "latte"]
async def test_all_layouts():
"""测试所有布局"""
print("=" * 60)
print("测试智能海报引擎 V2 - 双输出")
print("=" * 60)
engine = PosterSmartEngineV2()
# 每个布局使用不同的测试内容
layout_test_data = {
"hero_bottom": {
"category": "景点", "name": "西湖十景",
"description": "杭州最美的风景线,四季皆宜",
"price": "免费", "location": "杭州西湖",
"features": "湖光山色, 历史古迹, 文化底蕴, 四季美景",
"image_url": "https://example.com/xihu.jpg",
},
"overlay_center": {
"category": "活动", "name": "周末露营派对",
"description": "逃离城市,拥抱自然",
"price": "299元", "location": "从化流溪河",
"features": "篝火晚会, 星空观测",
"image_url": "https://example.com/camping.jpg",
},
"overlay_bottom": {
"category": "美食", "name": "探店网红甜品",
"description": "ins风下午茶打卡地",
"price": "人均68元", "location": "深圳万象城",
"features": "颜值超高, 味道在线, 出片率满分, 闺蜜必去",
"image_url": "https://example.com/dessert.jpg",
},
"split_vertical": {
"category": "民宿", "name": "山舍云端民宿",
"description": "藏在莫干山的治愈系民宿,推开窗便是云海与竹林",
"price": "458元/晚", "location": "莫干山",
"features": "独立庭院, 手冲咖啡, 山景露台, 有机早餐, 管家服务",
"image_url": "https://example.com/minsu.jpg",
},
"card_float": {
"category": "酒店", "name": "三亚亚特兰蒂斯",
"description": "住进海底世界的浪漫体验",
"price": "2888元/晚", "location": "三亚海棠湾",
"features": "水族馆景观, 无边泳池, 私人沙滩, 水上乐园",
"image_url": "https://example.com/hotel.jpg",
},
}
for i, layout in enumerate(ALL_LAYOUTS, 1):
theme = ALL_THEMES[i % len(ALL_THEMES)]
print(f"\n[{i}] 布局: {layout}, 主题: {theme}")
test_data = layout_test_data.get(layout, layout_test_data["hero_bottom"])
params = {**test_data, "override_layout": layout, "override_theme": theme, "skip_ai": True}
result = await engine.execute(params)
if result.success:
print(f" ✓ 成功!")
# 保存预览 PNG
preview_path = OUTPUT_DIR / f"{i:02d}_{layout}_preview.png"
with open(preview_path, 'wb') as f:
f.write(base64.b64decode(result.data.get('preview_base64')))
print(f" 预览 PNG: {preview_path.name}")
# 保存 Fabric JSON
json_path = OUTPUT_DIR / f"{i:02d}_{layout}_fabric.json"
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(result.data.get('fabric_json'), f, ensure_ascii=False, indent=2)
print(f" Fabric JSON: {json_path.name}")
# 打印内容摘要
content = result.data.get('content', {})
print(f" 标题: {content.get('title', 'N/A')}")
print(f" 对象数: {len(result.data.get('fabric_json', {}).get('objects', []))}")
else:
print(f" ✗ 失败: {result.error}")
print("\n" + "=" * 60)
print(f"✓ 完成! 输出目录: {OUTPUT_DIR}")
print("=" * 60)
async def test_with_ai():
"""测试 AI 生成"""
print("\n" + "=" * 60)
print("测试 AI 文案生成")
print("=" * 60)
engine = PosterSmartEngineV2()
params = {
"category": "景点",
"name": "正佳极地海洋世界",
"description": "位于广州正佳广场的大型海洋馆,有企鹅、海豚表演",
"price": "199元/人",
"location": "广州天河",
"features": "企鹅馆, 海豚表演, 儿童乐园, 室内恒温",
"target_audience": "亲子家庭",
"image_url": "https://example.com/ocean.jpg",
}
result = await engine.execute(params)
if result.success:
print("✓ AI 生成成功!")
content = result.data.get('content', {})
print(f" 布局: {result.data.get('layout')}")
print(f" 主题: {result.data.get('theme')}")
print(f" 标题: {content.get('title')}")
print(f" 副标题: {content.get('subtitle')}")
print(f" 亮点: {content.get('highlights')}")
# 保存
preview_path = OUTPUT_DIR / "ai_preview.png"
with open(preview_path, 'wb') as f:
f.write(base64.b64decode(result.data.get('preview_base64')))
print(f" 预览: {preview_path.name}")
json_path = OUTPUT_DIR / "ai_fabric.json"
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(result.data.get('fabric_json'), f, ensure_ascii=False, indent=2)
print(f" JSON: {json_path.name}")
else:
print(f"✗ 失败: {result.error}")
if __name__ == "__main__":
asyncio.run(test_all_layouts())
asyncio.run(test_with_ai())