|
|
|
|
|
|
|
|
|
|
|
from ten_ai_base.types import LLMChatCompletionContentPartImageParam, LLMToolMetadata, LLMToolResult, LLMToolResultRequery |
|
from ten_ai_base.llm_tool import AsyncLLMToolBaseExtension |
|
from ten import ( |
|
AudioFrame, |
|
VideoFrame, |
|
AsyncTenEnv, |
|
Cmd, |
|
Data, |
|
) |
|
from PIL import Image |
|
from io import BytesIO |
|
from base64 import b64encode |
|
|
|
|
|
def rgb2base64jpeg(rgb_data, width, height): |
|
|
|
pil_image = Image.frombytes("RGBA", (width, height), bytes(rgb_data)) |
|
pil_image = pil_image.convert("RGB") |
|
|
|
|
|
pil_image = resize_image_keep_aspect(pil_image, 512) |
|
|
|
|
|
buffered = BytesIO() |
|
pil_image.save(buffered, format="JPEG") |
|
|
|
|
|
|
|
jpeg_image_data = buffered.getvalue() |
|
|
|
|
|
base64_encoded_image = b64encode(jpeg_image_data).decode("utf-8") |
|
|
|
|
|
mime_type = "image/jpeg" |
|
base64_url = f"data:{mime_type};base64,{base64_encoded_image}" |
|
return base64_url |
|
|
|
|
|
def resize_image_keep_aspect(image, max_size=512): |
|
""" |
|
Resize an image while maintaining its aspect ratio, ensuring the larger dimension is max_size. |
|
If both dimensions are smaller than max_size, the image is not resized. |
|
|
|
:param image: A PIL Image object |
|
:param max_size: The maximum size for the larger dimension (width or height) |
|
:return: A PIL Image object (resized or original) |
|
""" |
|
|
|
width, height = image.size |
|
|
|
|
|
if width <= max_size and height <= max_size: |
|
return image |
|
|
|
|
|
aspect_ratio = width / height |
|
|
|
|
|
if width > height: |
|
new_width = max_size |
|
new_height = int(max_size / aspect_ratio) |
|
else: |
|
new_height = max_size |
|
new_width = int(max_size * aspect_ratio) |
|
|
|
|
|
resized_image = image.resize((new_width, new_height)) |
|
|
|
return resized_image |
|
|
|
|
|
class VisionToolExtension(AsyncLLMToolBaseExtension): |
|
image_data = None |
|
image_width = 0 |
|
image_height = 0 |
|
|
|
async def on_init(self, ten_env: AsyncTenEnv) -> None: |
|
ten_env.log_debug("on_init") |
|
await super().on_init(ten_env) |
|
|
|
async def on_start(self, ten_env: AsyncTenEnv) -> None: |
|
ten_env.log_debug("on_start") |
|
await super().on_start(ten_env) |
|
|
|
async def on_stop(self, ten_env: AsyncTenEnv) -> None: |
|
ten_env.log_debug("on_stop") |
|
|
|
|
|
|
|
await super().on_stop(ten_env) |
|
|
|
async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None: |
|
cmd_name = cmd.get_name() |
|
ten_env.log_debug("on_cmd name {}".format(cmd_name)) |
|
|
|
await super().on_cmd(ten_env, cmd) |
|
|
|
async def on_data(self, ten_env: AsyncTenEnv, data: Data) -> None: |
|
data_name = data.get_name() |
|
ten_env.log_debug("on_data name {}".format(data_name)) |
|
|
|
async def on_audio_frame( |
|
self, ten_env: AsyncTenEnv, audio_frame: AudioFrame |
|
) -> None: |
|
audio_frame_name = audio_frame.get_name() |
|
ten_env.log_debug("on_audio_frame name {}".format(audio_frame_name)) |
|
|
|
async def on_video_frame( |
|
self, ten_env: AsyncTenEnv, video_frame: VideoFrame |
|
) -> None: |
|
video_frame_name = video_frame.get_name() |
|
ten_env.log_debug("on_video_frame name {}".format(video_frame_name)) |
|
|
|
self.image_data = video_frame.get_buf() |
|
self.image_width = video_frame.get_width() |
|
self.image_height = video_frame.get_height() |
|
|
|
def get_tool_metadata(self, ten_env: AsyncTenEnv) -> list[LLMToolMetadata]: |
|
return [ |
|
LLMToolMetadata( |
|
name="get_vision_tool", |
|
description="Get the image from camera. Call this whenever you need to understand the input camera image like you have vision capability, for example when user asks 'What can you see?' or 'Can you see me?'", |
|
parameters=[], |
|
), |
|
] |
|
|
|
async def run_tool( |
|
self, ten_env: AsyncTenEnv, name: str, args: dict |
|
) -> LLMToolResult | None: |
|
if name == "get_vision_tool": |
|
if self.image_data is None: |
|
raise ValueError("No image data available") |
|
|
|
base64_image = rgb2base64jpeg( |
|
self.image_data, self.image_width, self.image_height |
|
) |
|
return LLMToolResultRequery( |
|
type="requery", |
|
content=[ |
|
LLMChatCompletionContentPartImageParam( |
|
type="image_url", |
|
image_url={ |
|
"url": base64_image, |
|
}, |
|
) |
|
], |
|
) |
|
|