# # This file is part of TEN Framework, an open source project. # Licensed under the Apache License, Version 2.0. # See the LICENSE file for more information. # import json from ten import ( AudioFrame, VideoFrame, AsyncTenEnv, Cmd, Data, ) from PIL import Image from io import BytesIO from base64 import b64encode from ten_ai_base.const import CMD_CHAT_COMPLETION_CALL from ten_ai_base.types import ( LLMChatCompletionUserMessageParam, LLMToolMetadata, LLMToolMetadataParameter, LLMToolResult, LLMToolResultLLMResult, ) from ten_ai_base.llm_tool import AsyncLLMToolBaseExtension def rgb2base64jpeg(rgb_data, width, height): # Convert the RGB image to a PIL Image pil_image = Image.frombytes("RGBA", (width, height), bytes(rgb_data)) pil_image = pil_image.convert("RGB") # Resize the image while maintaining its aspect ratio pil_image = resize_image_keep_aspect(pil_image, 512) # Save the image to a BytesIO object in JPEG format buffered = BytesIO() pil_image.save(buffered, format="JPEG") # pil_image.save("test.jpg", format="JPEG") # Get the byte data of the JPEG image jpeg_image_data = buffered.getvalue() # Convert the JPEG byte data to a Base64 encoded string base64_encoded_image = b64encode(jpeg_image_data).decode("utf-8") # Create the data URL 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) """ # Get current width and height width, height = image.size # If both dimensions are already smaller than max_size, return the original image if width <= max_size and height <= max_size: return image # Calculate the aspect ratio aspect_ratio = width / height # Determine the new dimensions 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) # Resize the image with the new dimensions resized_image = image.resize((new_width, new_height)) return resized_image class VisionAnalyzeToolExtension(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") 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") # TODO: clean up resources async def on_deinit(self, ten_env: AsyncTenEnv) -> None: ten_env.log_debug("on_deinit") 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_chat_completion", description="Get the image analyze result 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 in my camera?' or 'Can you see me?'", parameters=[ LLMToolMetadataParameter( name="query", type="string", description="The vision completion query.", required=True, ), ], ), ] async def run_tool( self, ten_env: AsyncTenEnv, name: str, args: dict ) -> LLMToolResult | None: if name == "get_vision_chat_completion": if self.image_data is None: raise ValueError("No image data available") if "query" not in args: raise ValueError("Failed to get property") query = args["query"] base64_image = rgb2base64jpeg( self.image_data, self.image_width, self.image_height ) # return LLMToolResult(message=LLMCompletionArgsMessage(role="user", content=[result])) cmd: Cmd = Cmd.create(CMD_CHAT_COMPLETION_CALL) message: LLMChatCompletionUserMessageParam = ( LLMChatCompletionUserMessageParam( role="user", content=[ {"type": "text", "text": query}, {"type": "image_url", "image_url": {"url": base64_image}}, ], ) ) cmd.set_property_from_json("arguments", json.dumps({"messages": [message]})) ten_env.log_info("send_cmd {}".format(message)) [cmd_result, _] = await ten_env.send_cmd(cmd) result = cmd_result.get_property_to_json("response") return LLMToolResultLLMResult( type="llmresult", content=json.dumps(result), )