t
All checks were successful
Deploy WebSocket Server / deploy (push) Successful in 20s

This commit is contained in:
jeremygan2021
2026-03-05 21:56:44 +08:00
parent b430051d29
commit 9558ea4b35
4 changed files with 63 additions and 31 deletions

View File

@@ -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}")