This commit is contained in:
@@ -38,11 +38,11 @@ class ImageGenerator:
|
||||
2. 画面必须清晰、线条粗壮,适合低分辨率热敏打印机打印。
|
||||
3. 绝对不要有复杂的阴影、渐变、黑白线条描述。
|
||||
4. 背景必须是纯白 (White background)。
|
||||
5. 提示词内容请使用英文描述,因为绘图模型对英文理解更好,但在描述中强调 "black and white line art", "simple lines", "vector style"。
|
||||
5. 提示词内容请使用中文描述,因为绘图模型对中文生成要更准确。
|
||||
6. 尺寸比例遵循宽48mm:高30mm (约 1.6:1)。
|
||||
7. 直接输出优化后的提示词,不要包含任何解释。
|
||||
如果用户要求输入文字,则用```把文字包裹起来,文字是中文
|
||||
"black and white line art, Chinese:```中国人```"
|
||||
"房子的旁边有一个小孩,黑白线稿画作,卡通形象, 文字:```中国人```在下方。"
|
||||
"""
|
||||
|
||||
try:
|
||||
@@ -52,7 +52,7 @@ class ImageGenerator:
|
||||
# Currently using Qwen-Turbo for all providers for prompt optimization
|
||||
# You can also decouple this if needed
|
||||
response = Generation.call(
|
||||
model='qwen-turbo',
|
||||
model='qwen3.5-plus',
|
||||
prompt=f'{system_prompt}\n\n用户语音识别结果:{asr_text}\n\n优化后的提示词:',
|
||||
max_tokens=200,
|
||||
temperature=0.8
|
||||
|
||||
@@ -235,7 +235,17 @@ async def start_async_image_generation(websocket: WebSocket, asr_text: str):
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
# 同步调用图片生成函数
|
||||
image_path = await asyncio.to_thread(generate_image, optimized_prompt, progress_callback)
|
||||
gen_result = await asyncio.to_thread(generate_image, optimized_prompt, progress_callback)
|
||||
|
||||
image_path = None
|
||||
width = 0
|
||||
height = 0
|
||||
|
||||
if gen_result:
|
||||
if isinstance(gen_result, tuple):
|
||||
image_path, width, height = gen_result
|
||||
else:
|
||||
image_path = gen_result
|
||||
|
||||
task.result = image_path
|
||||
|
||||
@@ -244,7 +254,7 @@ async def start_async_image_generation(websocket: WebSocket, asr_text: str):
|
||||
await websocket.send_text("STATUS:COMPLETE:图片生成完成")
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
await send_image_to_client(websocket, image_path)
|
||||
await send_image_to_client(websocket, image_path, width, height)
|
||||
else:
|
||||
task.status = "failed"
|
||||
task.error = "图片生成失败"
|
||||
@@ -267,15 +277,18 @@ async def start_async_image_generation(websocket: WebSocket, asr_text: str):
|
||||
return task
|
||||
|
||||
|
||||
async def send_image_to_client(websocket: WebSocket, image_path: str):
|
||||
async def send_image_to_client(websocket: WebSocket, image_path: str, width: int = 0, height: int = 0):
|
||||
"""发送图片数据到客户端"""
|
||||
with open(image_path, 'rb') as f:
|
||||
image_data = f.read()
|
||||
|
||||
print(f"Sending image to ESP32, size: {len(image_data)} bytes")
|
||||
print(f"Sending image to ESP32, size: {len(image_data)} bytes, dim: {width}x{height}")
|
||||
|
||||
# Send start marker
|
||||
await websocket.send_text(f"IMAGE_START:{len(image_data)}:{THUMB_SIZE}")
|
||||
if width > 0 and height > 0:
|
||||
await websocket.send_text(f"IMAGE_START:{len(image_data)}:{width}:{height}")
|
||||
else:
|
||||
await websocket.send_text(f"IMAGE_START:{len(image_data)}:{THUMB_SIZE}")
|
||||
|
||||
# Send binary data directly
|
||||
chunk_size = 512 # Decreased chunk size for ESP32 memory stability
|
||||
@@ -594,15 +607,16 @@ def generate_image(prompt, progress_callback=None, retry_count=0, max_retries=2)
|
||||
from PIL import Image
|
||||
img = Image.open(GENERATED_IMAGE_FILE)
|
||||
|
||||
# 缩小到THUMB_SIZE x THUMB_SIZE
|
||||
img = img.resize((THUMB_SIZE, THUMB_SIZE), Image.LANCZOS)
|
||||
# 缩小到 fit THUMB_SIZE x THUMB_SIZE (保持比例)
|
||||
img.thumbnail((THUMB_SIZE, THUMB_SIZE), Image.Resampling.LANCZOS)
|
||||
width, height = img.size
|
||||
|
||||
# 转换为RGB565格式的原始数据
|
||||
# 每个像素2字节 (R5 G6 B5)
|
||||
rgb565_data = bytearray()
|
||||
|
||||
for y in range(THUMB_SIZE):
|
||||
for x in range(THUMB_SIZE):
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
r, g, b = img.getpixel((x, y))[:3]
|
||||
|
||||
# 转换为RGB565
|
||||
@@ -619,23 +633,23 @@ def generate_image(prompt, progress_callback=None, retry_count=0, max_retries=2)
|
||||
with open(GENERATED_THUMB_FILE, 'wb') as f:
|
||||
f.write(rgb565_data)
|
||||
|
||||
print(f"Thumbnail saved to {GENERATED_THUMB_FILE}, size: {len(rgb565_data)} bytes")
|
||||
print(f"Thumbnail saved to {GENERATED_THUMB_FILE}, size: {len(rgb565_data)} bytes, dim: {width}x{height}")
|
||||
|
||||
if progress_callback:
|
||||
progress_callback(100, "图片生成完成!")
|
||||
|
||||
return GENERATED_THUMB_FILE
|
||||
return GENERATED_THUMB_FILE, width, height
|
||||
|
||||
except ImportError:
|
||||
print("PIL not available, sending original image")
|
||||
if progress_callback:
|
||||
progress_callback(100, "图片生成完成!(原始格式)")
|
||||
return GENERATED_IMAGE_FILE
|
||||
return GENERATED_IMAGE_FILE, 0, 0
|
||||
except Exception as e:
|
||||
print(f"Error processing image: {e}")
|
||||
if progress_callback:
|
||||
progress_callback(80, f"图片处理出错: {str(e)}")
|
||||
return GENERATED_IMAGE_FILE
|
||||
return GENERATED_IMAGE_FILE, 0, 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in generate_image: {e}")
|
||||
|
||||
Reference in New Issue
Block a user